From e8f5c4ce24a5744155f8abfba7db7617b003ef78 Mon Sep 17 00:00:00 2001 From: Krishan <33421343+kfiven@users.noreply.github.com> Date: Thu, 25 Jul 2024 14:15:59 +1000 Subject: [PATCH] Undo system tray and single instance (#312) * Revert 5872ab9 * Undo 095b8fc --- src-tauri/Cargo.toml | 3 +- src-tauri/src/main.rs | 35 +--------------- src-tauri/src/tray.rs | 87 --------------------------------------- src-tauri/tauri.conf.json | 5 --- 4 files changed, 2 insertions(+), 128 deletions(-) delete mode 100644 src-tauri/src/tray.rs diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 32a63be..1b576da 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -17,8 +17,7 @@ tauri-build = { version = "1.5.3", features = [] } [dependencies] serde_json = "1.0.109" serde = { version = "1.0.193", features = ["derive"] } -tauri = { version = "1.6.8", features = ["api-all", "devtools", "system-tray", "updater"] } -tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } +tauri = { version = "1.6.8", features = ["api-all", "devtools", "updater"] } [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 4bf2f75..56a1ab7 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,10 +3,8 @@ windows_subsystem = "windows" )] -use tauri::Manager; #[cfg(target_os = "macos")] mod menu; -mod tray; fn main() { let builder = tauri::Builder::default(); @@ -14,38 +12,7 @@ fn main() { #[cfg(target_os = "macos")] let builder = builder.menu(menu::menu()); - let builder = builder - .system_tray(tray::system_tray()) - .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!()) + .run(tauri::generate_context!()) .expect("error while building tauri application") - .run(run_event_handler) -} - -fn run_event_handler(app: &tauri::AppHandle, event: tauri::RunEvent) { - match event { - tauri::RunEvent::WindowEvent { label, event, .. } => { - tray::window_event_handler(app, &label, &event); - } - _ => {} - } } diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs deleted file mode 100644 index cb45e13..0000000 --- a/src-tauri/src/tray.rs +++ /dev/null @@ -1,87 +0,0 @@ -use tauri::{ - CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, - SystemTrayMenuItem, WindowEvent, SystemTrayHandle, Window, -}; - -pub const TRAY_LABEL: &'static str = "main-tray"; - -pub fn window_event_handler( - app: &tauri::AppHandle, - label: &str, - event: &WindowEvent, -) { - match event { - // Prevent Cinny from closing, instead hide it and let it be - // reopened through the tray. - WindowEvent::CloseRequested { api, .. } => { - api.prevent_close(); - app.get_window(&label).unwrap().hide().unwrap(); - app.tray_handle_by_id(TRAY_LABEL) - .unwrap() - .get_item("toggle") - .set_title("Show Cinny") - .unwrap(); - } - _ => {} - } -} - -/// Build the system tray object -pub fn system_tray() -> SystemTray { - let toggle = CustomMenuItem::new("toggle".to_owned(), "Hide Cinny"); - let quit = CustomMenuItem::new("quit".to_owned(), "Quit"); - let menu = SystemTrayMenu::new() - .add_item(toggle) - .add_native_item(SystemTrayMenuItem::Separator) - .add_item(quit); - - tauri::SystemTray::new() - .with_menu(menu) - .with_id(TRAY_LABEL.to_owned()) -} - -pub fn toggle_window_state(window: Window, tray_handle: SystemTrayHandle) { - // Hide the window if it's visible, show it if not - // `is_visible` returns true for minimized state for whatever reason - if window.is_visible().unwrap() { - window.hide().unwrap(); - tray_handle - .get_item("toggle") - .set_title("Show Cinny") - .unwrap(); - } else { - window.unminimize().unwrap(); - window.show().unwrap(); - window.set_focus().unwrap(); - tray_handle - .get_item("toggle") - .set_title("Hide Cinny") - .unwrap(); - }; -} - -pub fn system_tray_handler(app: &tauri::AppHandle, event: SystemTrayEvent) { - let tray_handle = match app.tray_handle_by_id(TRAY_LABEL) { - Some(h) => h, - None => return, - }; - let window = app.get_window("main").unwrap(); - - match event { - SystemTrayEvent::LeftClick { .. } => { - toggle_window_state(window, tray_handle); - } - SystemTrayEvent::MenuItemClick { id, .. } => { - match id.as_str() { - "quit" => { - app.exit(0); - } - "toggle" => { - toggle_window_state(window, tray_handle) - } - _ => {} - } - } - _ => {} - } -} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 7d59f66..3cb524c 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -72,11 +72,6 @@ ], "security": { "csp": "script-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'" - }, - "systemTray": { - "iconPath": "icons/32x32.png", - "iconAsTemplate": true, - "menuOnLeftClick": false } } }