From 08186c0fc105eb4f8872d165fe1bc6f59ea7afad Mon Sep 17 00:00:00 2001 From: 4yn Date: Thu, 27 Jan 2022 21:13:34 +0800 Subject: [PATCH] system tray --- src-tauri/Cargo.lock | 24 ++++++++++++++++++ src-tauri/Cargo.toml | 2 +- src-tauri/src/main.rs | 53 +++++++++++++++++++++++++++++++++++++-- src-tauri/tauri.conf.json | 6 ++++- 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 322267f..05941f5 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2866,6 +2866,7 @@ dependencies = [ "raw-window-handle 0.3.4", "scopeguard", "serde", + "tauri-libappindicator", "unicode-segmentation", "winapi", "x11-dl", @@ -2964,6 +2965,29 @@ dependencies = [ "zstd", ] +[[package]] +name = "tauri-libappindicator" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af6057bf1b03141122da159ad4136d8a474240db59d81af1840d7fce3b819ef2" +dependencies = [ + "glib", + "gtk", + "gtk-sys", + "log", + "tauri-libappindicator-sys", +] + +[[package]] +name = "tauri-libappindicator-sys" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c40a817171cf25d69dd31e3fa9360fd4367e54948e30f3356962f0b1d9f23dad" +dependencies = [ + "gtk-sys", + "pkg-config", +] + [[package]] name = "tauri-macros" version = "1.0.0-beta.5" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index e460700..f8a45b2 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -17,7 +17,7 @@ tauri-build = { version = "1.0.0-beta.4" } [dependencies] serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } -tauri = { version = "1.0.0-beta.8", features = ["api-all"] } +tauri = { version = "1.0.0-beta.8", features = ["api-all", "system-tray"] } [features] default = [ "custom-protocol" ] diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e994ea4..241cca0 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,8 +3,57 @@ windows_subsystem = "windows" )] +use tauri::{ + CustomMenuItem, Event, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, +}; + fn main() { tauri::Builder::default() - .run(tauri::generate_context!()) - .expect("error while running tauri application"); + .system_tray( + SystemTray::new().with_menu( + SystemTrayMenu::new() + .add_item(CustomMenuItem::new("slidershim".to_string(), "slidershim").disabled()) + .add_item(CustomMenuItem::new("show".to_string(), "Show")) + .add_item(CustomMenuItem::new("quit".to_string(), "Quit")), + ), + ) + .on_system_tray_event(|app, event| match event { + SystemTrayEvent::LeftClick { + position: _, + size: _, + .. + } => { + app + .get_window("main") + .unwrap() + .show().ok(); + } + SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() { + "show" => { + app + .get_window("main") + .unwrap() + .show().ok(); + } + "quit" => { + std::process::exit(0); + } + _ => { + panic!("Unexpected menu item click {}", id.as_str()); + } + }, + _ => {} + }) + .build(tauri::generate_context!()) + .expect("error while running tauri application") + .run(|app_handle, event| match event { + Event::CloseRequested { label, api, .. } if label.as_str() == "main" => { + api.prevent_close(); + app_handle + .get_window("main") + .unwrap() + .hide().ok(); + } + _ => {} + }); } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 2fe4377..6afa19d 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -56,12 +56,16 @@ "title": "slidershim", "width": 800, "height": 600, - "resizable": true, + "resizable": false, "fullscreen": false } ], "security": { "csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'" + }, + "systemTray": { + "iconPath": "icons/icon.png", + "iconAsTemplate": true } } } \ No newline at end of file