tauri_plugin_autostart/
lib.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Automatically launch your application at startup. Supports Windows, Mac (via AppleScript or Launch Agent), and Linux.
6
7#![doc(
8    html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
9    html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
10)]
11#![cfg(not(any(target_os = "android", target_os = "ios")))]
12
13use auto_launch::{AutoLaunch, AutoLaunchBuilder};
14use serde::{ser::Serializer, Serialize};
15use tauri::{
16    command,
17    plugin::{Builder as PluginBuilder, TauriPlugin},
18    Manager, Runtime, State,
19};
20
21use std::env::current_exe;
22
23type Result<T> = std::result::Result<T, Error>;
24
25#[derive(Debug, Default, Copy, Clone)]
26pub enum MacosLauncher {
27    #[default]
28    LaunchAgent,
29    AppleScript,
30}
31
32#[derive(Debug, thiserror::Error)]
33pub enum Error {
34    #[error(transparent)]
35    Io(#[from] std::io::Error),
36    #[error("{0}")]
37    Anyhow(String),
38}
39
40impl Serialize for Error {
41    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
42    where
43        S: Serializer,
44    {
45        serializer.serialize_str(self.to_string().as_ref())
46    }
47}
48
49pub struct AutoLaunchManager(AutoLaunch);
50
51impl AutoLaunchManager {
52    pub fn enable(&self) -> Result<()> {
53        self.0
54            .enable()
55            .map_err(|e| e.to_string())
56            .map_err(Error::Anyhow)
57    }
58
59    pub fn disable(&self) -> Result<()> {
60        self.0
61            .disable()
62            .map_err(|e| e.to_string())
63            .map_err(Error::Anyhow)
64    }
65
66    pub fn is_enabled(&self) -> Result<bool> {
67        self.0
68            .is_enabled()
69            .map_err(|e| e.to_string())
70            .map_err(Error::Anyhow)
71    }
72}
73
74pub trait ManagerExt<R: Runtime> {
75    /// TODO: Rename these to `autostart` or `auto_start` in v3
76    fn autolaunch(&self) -> State<'_, AutoLaunchManager>;
77}
78
79impl<R: Runtime, T: Manager<R>> ManagerExt<R> for T {
80    /// TODO: Rename these to `autostart` or `auto_start` in v3
81    fn autolaunch(&self) -> State<'_, AutoLaunchManager> {
82        self.state::<AutoLaunchManager>()
83    }
84}
85
86#[command]
87async fn enable(manager: State<'_, AutoLaunchManager>) -> Result<()> {
88    manager.enable()
89}
90
91#[command]
92async fn disable(manager: State<'_, AutoLaunchManager>) -> Result<()> {
93    manager.disable()
94}
95
96#[command]
97async fn is_enabled(manager: State<'_, AutoLaunchManager>) -> Result<bool> {
98    manager.is_enabled()
99}
100
101#[derive(Default)]
102pub struct Builder {
103    #[cfg(target_os = "macos")]
104    macos_launcher: MacosLauncher,
105    args: Vec<String>,
106    app_name: Option<String>,
107}
108
109impl Builder {
110    /// Create a new auto start builder with default settings
111    pub fn new() -> Self {
112        Self::default()
113    }
114
115    /// Adds an argument to pass to your app on startup.
116    ///
117    /// ## Examples
118    ///
119    /// ```no_run
120    /// Builder::new()
121    ///     .arg("--from-autostart")
122    ///     .arg("--hey")
123    ///     .build();
124    /// ```
125    pub fn arg<S: Into<String>>(mut self, arg: S) -> Self {
126        self.args.push(arg.into());
127        self
128    }
129
130    /// Adds multiple arguments to pass to your app on startup.
131    ///
132    /// ## Examples
133    ///
134    /// ```no_run
135    /// Builder::new()
136    ///     .args(["--from-autostart", "--hey"])
137    ///     .build();
138    /// ```
139    pub fn args<I, S>(mut self, args: I) -> Self
140    where
141        I: IntoIterator<Item = S>,
142        S: Into<String>,
143    {
144        for arg in args {
145            self = self.arg(arg);
146        }
147        self
148    }
149
150    /// Sets whether to use launch agent or apple script to be used to enable auto start,
151    /// the builder's default is [`MacosLauncher::LaunchAgent`]
152    #[cfg(target_os = "macos")]
153    pub fn macos_launcher(mut self, macos_launcher: MacosLauncher) -> Self {
154        self.macos_launcher = macos_launcher;
155        self
156    }
157
158    /// Sets the app name to be used for the auto start entry.
159    ///
160    /// ## Examples
161    ///
162    /// ```no_run
163    /// Builder::new()
164    ///     .app_name("My Custom Name"))
165    ///     .build();
166    /// ```
167    pub fn app_name<S: Into<String>>(mut self, app_name: S) -> Self {
168        self.app_name = Some(app_name.into());
169        self
170    }
171
172    pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
173        PluginBuilder::new("autostart")
174            .invoke_handler(tauri::generate_handler![enable, disable, is_enabled])
175            .setup(move |app, _api| {
176                let mut builder = AutoLaunchBuilder::new();
177
178                let app_name = self
179                    .app_name
180                    .as_ref()
181                    .unwrap_or_else(|| &app.package_info().name);
182                builder.set_app_name(app_name);
183
184                builder.set_args(&self.args);
185
186                let current_exe = current_exe()?;
187
188                #[cfg(windows)]
189                builder.set_app_path(&current_exe.display().to_string());
190
191                #[cfg(target_os = "macos")]
192                {
193                    builder.set_use_launch_agent(matches!(
194                        self.macos_launcher,
195                        MacosLauncher::LaunchAgent
196                    ));
197                    // on macOS, current_exe gives path to /Applications/Example.app/MacOS/Example
198                    // but this results in seeing a Unix Executable in macOS login items
199                    // It must be: /Applications/Example.app
200                    // If it didn't find exactly a single occurance of .app, it will default to
201                    // exe path to not break it.
202                    let exe_path = current_exe.canonicalize()?.display().to_string();
203                    let parts: Vec<&str> = exe_path.split(".app/").collect();
204                    let app_path = if parts.len() == 2
205                        && matches!(self.macos_launcher, MacosLauncher::AppleScript)
206                    {
207                        format!("{}.app", parts.first().unwrap())
208                    } else {
209                        exe_path
210                    };
211                    builder.set_app_path(&app_path);
212                }
213
214                #[cfg(target_os = "linux")]
215                if let Some(appimage) = app
216                    .env()
217                    .appimage
218                    .and_then(|p| p.to_str().map(|s| s.to_string()))
219                {
220                    builder.set_app_path(&appimage);
221                } else {
222                    builder.set_app_path(&current_exe.display().to_string());
223                }
224
225                app.manage(AutoLaunchManager(
226                    builder.build().map_err(|e| e.to_string())?,
227                ));
228                Ok(())
229            })
230            .build()
231    }
232}
233
234/// Initializes the plugin.
235///
236/// `args` - are passed to your app on startup.
237pub fn init<R: Runtime>(
238    #[allow(unused)] macos_launcher: MacosLauncher,
239    args: Option<Vec<&'static str>>,
240) -> TauriPlugin<R> {
241    let mut builder = Builder::new();
242    if let Some(args) = args {
243        builder = builder.args(args)
244    }
245    #[cfg(target_os = "macos")]
246    {
247        builder = builder.macos_launcher(macos_launcher);
248    }
249    builder.build()
250}