pytauri_core/plugins/autostart/
mod.rs1use std::{
2 error::Error,
3 fmt::{Debug, Display, Formatter},
4};
5
6use pyo3::{exceptions::PyRuntimeError, prelude::*};
7use tauri_plugin_autostart::{self as plugin};
8
9use crate::{ext_mod::plugin::Plugin, tauri_runtime::Runtime};
10
11#[derive(Debug)]
12struct PluginError(plugin::Error);
13
14impl Display for PluginError {
15 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16 Display::fmt(&self.0, f)
17 }
18}
19
20impl Error for PluginError {}
21
22impl From<PluginError> for PyErr {
23 fn from(value: PluginError) -> Self {
24 match value.0 {
25 plugin::Error::Io(e) => e.into(),
26 e @ plugin::Error::Anyhow(_) => PyRuntimeError::new_err(e.to_string()),
27 }
28 }
29}
30
31impl From<plugin::Error> for PluginError {
32 fn from(value: plugin::Error) -> Self {
33 Self(value)
34 }
35}
36
37macro_rules! macos_launcher_impl {
38 ($ident:ident => : $($variant:ident),*) => {
39 #[pyclass(frozen, eq, eq_int)]
41 #[derive(PartialEq, Clone, Copy)]
42 pub enum $ident {
43 $($variant,)*
44 }
45
46 impl From<$ident> for tauri_plugin_autostart::MacosLauncher {
47 fn from(val: $ident) -> Self {
48 match val {
49 $($ident::$variant => tauri_plugin_autostart::MacosLauncher::$variant,)*
50 }
51 }
52 }
53
54 impl From<tauri_plugin_autostart::MacosLauncher> for $ident {
55 fn from(val: tauri_plugin_autostart::MacosLauncher) -> Self {
56 match val {
57 $(tauri_plugin_autostart::MacosLauncher::$variant => $ident::$variant,)*
58 }
59 }
60 }
61
62 };
63}
64
65macos_launcher_impl! (
66 MacosLauncher => :
67 LaunchAgent,
68 AppleScript
69);
70
71#[pyfunction]
73#[pyo3(signature = (macos_launcher = MacosLauncher::LaunchAgent, args = None))]
74pub fn init(
75 #[cfg_attr(not(target_os = "macos"), expect(unused_variables))] macos_launcher: MacosLauncher,
76 args: Option<Vec<String>>,
77) -> Plugin {
78 let mut builder = plugin::Builder::new();
82
83 if let Some(args) = args {
84 builder = builder.args(args);
85 }
86 #[cfg(target_os = "macos")]
87 {
88 builder = builder.macos_launcher(macos_launcher.into());
89 }
90
91 Plugin::new(Box::new(move || Box::new(builder.build::<Runtime>())))
92}
93
94#[pymodule(submodule, gil_used = false)]
96pub mod autostart {
97 #[pymodule_export]
98 pub use super::{init, MacosLauncher};
99}