Skip to content

feat: add the ability to check if the app is autostart #2650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/is-autostart-ability.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fs: minor
fs-js: minor
---

Add the ability to check if the app is autostart
2 changes: 1 addition & 1 deletion plugins/autostart/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

const COMMANDS: &[&str] = &["enable", "disable", "is_enabled"];
const COMMANDS: &[&str] = &["enable", "disable", "is_enabled", "is_autostart"];

fn main() {
tauri_plugin::Builder::new(COMMANDS)
Expand Down
4 changes: 4 additions & 0 deletions plugins/autostart/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ export async function enable(): Promise<void> {
export async function disable(): Promise<void> {
await invoke('plugin:autostart|disable')
}

export async function isAutostart(): Promise<boolean> {
await invoke('plugin:autostart|is_autostart')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!

"$schema" = "../../schemas/schema.json"

[[permission]]
identifier = "allow-is-autostart"
description = "Enables the is_autostart command without any pre-configured scope."
commands.allow = ["is_autostart"]

[[permission]]
identifier = "deny-is-autostart"
description = "Denies the is_autostart command without any pre-configured scope."
commands.deny = ["is_autostart"]
27 changes: 27 additions & 0 deletions plugins/autostart/permissions/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ disable the automatic start on boot.
- `allow-enable`
- `allow-disable`
- `allow-is-enabled`
- `allow-is-autostart`

## Permission Table

Expand Down Expand Up @@ -81,6 +82,32 @@ Denies the enable command without any pre-configured scope.
<tr>
<td>

`autostart:allow-is-autostart`

</td>
<td>

Enables the is_autostart command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`autostart:deny-is-autostart`

</td>
<td>

Denies the is_autostart command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`autostart:allow-is-enabled`

</td>
Expand Down
2 changes: 1 addition & 1 deletion plugins/autostart/permissions/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ disable the automatic start on boot.

"""

permissions = ["allow-enable", "allow-disable", "allow-is-enabled"]
permissions = ["allow-enable", "allow-disable", "allow-is-enabled", "allow-is-autostart"]
16 changes: 14 additions & 2 deletions plugins/autostart/permissions/schemas/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@
"const": "deny-enable",
"markdownDescription": "Denies the enable command without any pre-configured scope."
},
{
"description": "Enables the is_autostart command without any pre-configured scope.",
"type": "string",
"const": "allow-is-autostart",
"markdownDescription": "Enables the is_autostart command without any pre-configured scope."
},
{
"description": "Denies the is_autostart command without any pre-configured scope.",
"type": "string",
"const": "deny-is-autostart",
"markdownDescription": "Denies the is_autostart command without any pre-configured scope."
},
{
"description": "Enables the is_enabled command without any pre-configured scope.",
"type": "string",
Expand All @@ -331,10 +343,10 @@
"markdownDescription": "Denies the is_enabled command without any pre-configured scope."
},
{
"description": "This permission set configures if your\napplication can enable or disable auto\nstarting the application on boot.\n\n#### Granted Permissions\n\nIt allows all to check, enable and\ndisable the automatic start on boot.\n\n\n#### This default permission set includes:\n\n- `allow-enable`\n- `allow-disable`\n- `allow-is-enabled`",
"description": "This permission set configures if your\napplication can enable or disable auto\nstarting the application on boot.\n\n#### Granted Permissions\n\nIt allows all to check, enable and\ndisable the automatic start on boot.\n\n\n#### This default permission set includes:\n\n- `allow-enable`\n- `allow-disable`\n- `allow-is-enabled`\n- `allow-is-autostart`",
"type": "string",
"const": "default",
"markdownDescription": "This permission set configures if your\napplication can enable or disable auto\nstarting the application on boot.\n\n#### Granted Permissions\n\nIt allows all to check, enable and\ndisable the automatic start on boot.\n\n\n#### This default permission set includes:\n\n- `allow-enable`\n- `allow-disable`\n- `allow-is-enabled`"
"markdownDescription": "This permission set configures if your\napplication can enable or disable auto\nstarting the application on boot.\n\n#### Granted Permissions\n\nIt allows all to check, enable and\ndisable the automatic start on boot.\n\n\n#### This default permission set includes:\n\n- `allow-enable`\n- `allow-disable`\n- `allow-is-enabled`\n- `allow-is-autostart`"
}
]
}
Expand Down
18 changes: 17 additions & 1 deletion plugins/autostart/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ async fn is_enabled(manager: State<'_, AutoLaunchManager>) -> Result<bool> {
manager.is_enabled()
}

#[command]
async fn is_autostart(manager: State<'_, AutoLaunchManager>) -> Result<bool> {
let process_args: Vec<String> = std::env::args().collect();

let passed_args = manager.0.get_args();

let is_autostart = passed_args.iter().all(|arg| process_args.contains(arg));

Ok(is_autostart)
}
Comment on lines +102 to +110
Copy link
Contributor

@Legend-Master Legend-Master Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just checking if all the args used to launch the program match the ones we passed into the plugin builder, this wouldn't work if we didn't pass in anything or if we changed the args

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be used to determine whether the app was launched automatically or manually during its first startup. In the case of a manual launch, these parameters are usually not present.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you can pass in nothing in the builder, and this wouldn't work in that case, also when you relaunch/restart your app with app.restart, it will include all the args you previously set and that's going to be another false positive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I really didn't think about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other approaches to determine whether an application is set to launch automatically at startup?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to use command line args for this purpose, maybe a function in the builder that adds in a special cli arg to the args and we can detect if that cli arg is present or not, about the restart, that one would probably be a bit more involved since we don't have a way to change the restart cli args right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's an opt-in, I'm fine with that 🙃, or we could provide some docs for people to do it in their own apps for now


#[derive(Default)]
pub struct Builder {
#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -156,7 +167,12 @@ impl Builder {

pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
PluginBuilder::new("autostart")
.invoke_handler(tauri::generate_handler![enable, disable, is_enabled])
.invoke_handler(tauri::generate_handler![
enable,
disable,
is_enabled,
is_autostart
])
.setup(move |app, _api| {
let mut builder = AutoLaunchBuilder::new();
builder.set_app_name(&app.package_info().name);
Expand Down
Loading