tauri_plugin_single_window/
lib.rs1use std::sync::Arc;
2#[cfg(desktop)]
3use tauri::Manager;
4use tauri::{
5 plugin::{Builder, TauriPlugin},
6 AppHandle, Runtime,
7};
8
9mod config;
10pub use config::Config;
11mod models;
12pub use models::ActivationPayload;
13
14#[cfg(desktop)]
15mod desktop;
16
17mod error;
18
19pub use error::{Error, Result};
20
21type ActivationCallback<R> = Arc<dyn Fn(AppHandle<R>, ActivationPayload) + Send + Sync>;
22
23pub fn init<R: Runtime>() -> TauriPlugin<R> {
25 init_with(Config::default())
26}
27
28pub fn init_with<R: Runtime>(config: Config) -> TauriPlugin<R> {
30 let callback = config
31 .activation_callback
32 .as_ref()
33 .and_then(|callback| callback.downcast_ref::<ActivationCallback<R>>())
34 .cloned();
35
36 let builder = Builder::new("single-window").setup(move |app, api| {
37 #[cfg(desktop)]
38 {
39 let single_instance = desktop::init(app, api, &config, callback.clone())?;
40 app.manage(single_instance);
41 }
42
43 #[cfg(mobile)]
44 {
45 let _ = (app, api, &config, &callback);
46 }
47
48 Ok(())
49 });
50
51 #[cfg(target_os = "linux")]
52 let builder = {
53 let mut ready_seen = false;
54
55 builder.on_event(move |app, event| {
56 if matches!(event, tauri::RunEvent::Ready) {
57 if ready_seen {
58 if let Some(single_instance) = app.try_state::<desktop::SingleInstance<R>>() {
59 single_instance.handle_remote_activation();
60 }
61 } else {
62 ready_seen = true;
63 }
64 }
65 })
66 };
67
68 builder.build()
69}