From 5872ab9ea4b19f6e00874e616d046d85cceb37ee Mon Sep 17 00:00:00 2001 From: aceArt-GmbH <33117017+aceArt-GmbH@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:44:43 +0200 Subject: [PATCH] Add single-instance tauri plugin (#261) Co-authored-by: lukas --- src-tauri/Cargo.lock | 15 +++++++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/main.rs | 19 +++++++++++++++++++ src-tauri/src/tray.rs | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 779e291..7db5fc0 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -450,6 +450,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tauri-plugin-single-instance", ] [[package]] @@ -3631,6 +3632,20 @@ dependencies = [ "tauri-utils", ] +[[package]] +name = "tauri-plugin-single-instance" +version = "0.0.0" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v1#562425644ef7c11a7b301a30c07a8c8b545621a8" +dependencies = [ + "log", + "serde", + "serde_json", + "tauri", + "thiserror", + "windows-sys 0.52.0", + "zbus", +] + [[package]] name = "tauri-runtime" version = "0.14.3" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 62b790a..d8c29a8 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -18,6 +18,7 @@ tauri-build = { version = "1.5.2", features = [] } serde_json = "1.0.109" serde = { version = "1.0.193", features = ["derive"] } tauri = { version = "1.6.7", features = ["api-all", "devtools", "system-tray", "updater"] } +tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 4a2f602..4bf2f75 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,6 +2,8 @@ all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows" )] + +use tauri::Manager; #[cfg(target_os = "macos")] mod menu; mod tray; @@ -17,6 +19,23 @@ fn main() { .on_system_tray_event(tray::system_tray_handler); builder + .plugin(tauri_plugin_single_instance::init(|app, _, _| { + let tray_handle = match app.tray_handle_by_id(crate::tray::TRAY_LABEL) { + Some(h) => h, + None => return, + }; + let window = app.get_window("main").unwrap(); + + if !window.is_visible().unwrap() || window.is_minimized().unwrap() { + window.unminimize().unwrap(); + window.show().unwrap(); + window.set_focus().unwrap(); + tray_handle + .get_item("toggle") + .set_title("Hide Cinny") + .unwrap(); + } + })) .build(tauri::generate_context!()) .expect("error while building tauri application") .run(run_event_handler) diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index 2d43c8c..cb45e13 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -3,7 +3,7 @@ use tauri::{ SystemTrayMenuItem, WindowEvent, SystemTrayHandle, Window, }; -const TRAY_LABEL: &'static str = "main-tray"; +pub const TRAY_LABEL: &'static str = "main-tray"; pub fn window_event_handler( app: &tauri::AppHandle,