Add single-instance tauri plugin (#261)

Co-authored-by: lukas <lukas.walter@aceart.de>
This commit is contained in:
aceArt-GmbH 2024-06-12 14:44:43 +02:00 committed by GitHub
parent c5ddfa91a2
commit 5872ab9ea4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 1 deletions

15
src-tauri/Cargo.lock generated
View file

@ -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"

View file

@ -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

View file

@ -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)

View file

@ -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<R: tauri::Runtime>(
app: &tauri::AppHandle<R>,