Skip to content

Commit

Permalink
Add single-instance tauri plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas authored and aceArt-GmbH committed Feb 8, 2024
1 parent 6f83708 commit 49b6f59
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
81 changes: 81 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tauri-build = { version = "1.5.0", features = [] }
serde_json = "1.0.108"
serde = { version = "1.0.190", features = ["derive"] }
tauri = { version = "1.5.2", 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
Expand Down
19 changes: 19 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down

0 comments on commit 49b6f59

Please sign in to comment.