From 264ebe10e52675c687236a8008c99f38ab26aef2 Mon Sep 17 00:00:00 2001 From: Colin Lienard Date: Sat, 2 Sep 2023 14:21:44 +0200 Subject: [PATCH] toggle-able progress --- src-tauri/src/commands.rs | 31 +++++++++ src-tauri/src/main.rs | 132 ++++++++++++++++++-------------------- src-tauri/src/tray.rs | 20 ++++++ src-tauri/tauri.conf.json | 5 +- 4 files changed, 115 insertions(+), 73 deletions(-) create mode 100644 src-tauri/src/commands.rs create mode 100644 src-tauri/src/tray.rs diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs new file mode 100644 index 00000000..9d43a751 --- /dev/null +++ b/src-tauri/src/commands.rs @@ -0,0 +1,31 @@ +#[tauri::command] +pub fn update_tray( + app_handle: tauri::AppHandle, + title: Option, + description: Option, + new_icon: Option, +) { + let tray_handle = app_handle.tray_handle(); + #[cfg(target_os = "macos")] + if let Some(title) = title { + tray_handle.set_title(&title).unwrap(); + } + if let Some(description) = description { + tray_handle.get_item("text").set_title(description).unwrap(); + } + if let Some(new_icon) = new_icon { + if new_icon { + tray_handle + .set_icon(tauri::Icon::Raw( + include_bytes!("../icons/tray-new.png").to_vec(), + )) + .unwrap(); + } else { + tray_handle + .set_icon(tauri::Icon::Raw( + include_bytes!("../icons/tray-base.png").to_vec(), + )) + .unwrap(); + } + } +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index feec345c..eebacc61 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,62 +3,38 @@ windows_subsystem = "windows" )] +use std::sync::{Arc, RwLock}; use tauri::{ - CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, ActivationPolicy, Size, LogicalSize, + CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, }; use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_positioner::{Position, WindowExt}; - -#[tauri::command] -fn update_tray( - app_handle: tauri::AppHandle, - title: Option, - description: Option, - new_icon: Option, -) { - let tray_handle = app_handle.tray_handle(); - #[cfg(target_os = "macos")] - if let Some(title) = title { - tray_handle.set_title(&title).unwrap(); - } - if let Some(description) = description { - tray_handle.get_item("text").set_title(description).unwrap(); - } - if let Some(new_icon) = new_icon { - if new_icon { - tray_handle - .set_icon(tauri::Icon::Raw( - include_bytes!("../icons/tray-new.png").to_vec(), - )) - .unwrap(); - } else { - tray_handle - .set_icon(tauri::Icon::Raw( - include_bytes!("../icons/tray-base.png").to_vec(), - )) - .unwrap(); - } - } -} +mod commands; +mod tray; fn main() { - tauri_plugin_deep_link::prepare("de.fabianlars.deep-link-test"); + tauri_plugin_deep_link::prepare("app.gitlight"); let title = CustomMenuItem::new("title".to_string(), "GitHub notifications").disabled(); let text = CustomMenuItem::new("text".to_string(), "1 pinned • 2 unread"); let focus = CustomMenuItem::new("focus".to_string(), "Dashboard"); + let toggle = CustomMenuItem::new("toggle".to_string(), "Toggle"); let quit = CustomMenuItem::new("quit".to_string(), "Quit"); let tray_menu = SystemTrayMenu::new() .add_item(title) .add_item(text) .add_native_item(SystemTrayMenuItem::Separator) .add_item(focus) + .add_item(toggle) .add_native_item(SystemTrayMenuItem::Separator) .add_item(quit); let tray = SystemTray::new().with_menu(tray_menu); + let is_tray_app = Arc::new(RwLock::new(false)); + let is_tray_app_clone = is_tray_app.clone(); + tauri::Builder::default() - .setup(|app| { + .setup(move |app| { let handle = app.handle(); tauri_plugin_deep_link::register("gitlight", move |request| { handle.emit_all("scheme-request", request).unwrap(); @@ -70,7 +46,13 @@ fn main() { app.emit_all("scheme-request", url).unwrap(); } - app.set_activation_policy(ActivationPolicy::Accessory); + if *is_tray_app.read().unwrap() { + tray::activate_tray_mode(&app.handle()); + let window = app.get_window("main").unwrap(); + window.move_window(Position::TrayCenter).unwrap(); + } else { + tray::deactivate_tray_mode(&app.handle()); + } Ok(()) }) @@ -81,7 +63,7 @@ fn main() { )) .plugin(tauri_plugin_positioner::init()) .system_tray(tray) - .on_system_tray_event(|app, event| { + .on_system_tray_event(move |app, event| { tauri_plugin_positioner::on_tray_event(app, &event); match event { SystemTrayEvent::LeftClick { @@ -89,52 +71,60 @@ fn main() { size: _, .. } => { - let window = app.get_window("main").unwrap(); - // use TrayCenter as initial window position - let _ = window.move_window(Position::TrayCenter); - if window.is_visible().unwrap() { - window.hide().unwrap(); - } else { - window.show().unwrap(); - window.set_focus().unwrap(); + if *is_tray_app_clone.read().unwrap() { + let window = app.get_window("main").unwrap(); + window.move_window(Position::TrayCenter).unwrap(); + if window.is_visible().unwrap() { + window.hide().unwrap(); + } else { + window.show().unwrap(); + window.set_focus().unwrap(); + } } - - window.set_decorations(false).unwrap(); - window.set_size(Size::Logical(LogicalSize { width: 400.0, height: 600.0 })).unwrap(); } + SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() { + "text" | "focus" => { + let window = app.get_window("main").unwrap(); + window.set_focus().unwrap(); + } + "toggle" => { + let is_tray_app_value = *is_tray_app_clone.read().unwrap(); + let mut is_tray_app_lock = is_tray_app_clone.write().unwrap(); + *is_tray_app_lock = !is_tray_app_value; + if is_tray_app_value { + tray::deactivate_tray_mode(app); + } else { + tray::activate_tray_mode(app); + let window = app.get_window("main").unwrap(); + window.move_window(Position::TrayCenter).unwrap(); + } + } + "quit" => { + std::process::exit(0); + } + _ => {} + }, _ => {} } - // if let SystemTrayEvent::MenuItemClick { id, .. } = event { - // match id.as_str() { - // "text" | "focus" => { - // let window = app.get_window("main").unwrap(); - // window.set_focus().unwrap(); - // } - // "quit" => { - // std::process::exit(0); - // } - // _ => {} - // } - // } }) .on_window_event(|event| match event.event() { + tauri::WindowEvent::CloseRequested { api, .. } => { + #[cfg(not(target_os = "macos"))] + event.window().hide().unwrap(); + + #[cfg(target_os = "macos")] + tauri::AppHandle::hide(&event.window().app_handle()).unwrap(); + + api.prevent_close(); + } tauri::WindowEvent::Focused(is_focused) => { - // detect click outside of the focused window and hide the app if !is_focused { event.window().hide().unwrap(); } } - _ => {} // if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() { - // #[cfg(not(target_os = "macos"))] - // event.window().hide().unwrap(); - - // #[cfg(target_os = "macos")] - // tauri::AppHandle::hide(&event.window().app_handle()).unwrap(); - - // api.prevent_close(); - // } + _ => {} }) - .invoke_handler(tauri::generate_handler![update_tray]) + .invoke_handler(tauri::generate_handler![commands::update_tray]) .build(tauri::generate_context!()) .expect("error while running tauri application") .run(|_app_handle, event| { diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs new file mode 100644 index 00000000..fc0c9e7d --- /dev/null +++ b/src-tauri/src/tray.rs @@ -0,0 +1,20 @@ +use tauri::{LogicalSize, Manager, Size}; +use tauri_plugin_window_state::{AppHandleExt, StateFlags, WindowExt}; + +pub fn activate_tray_mode(app: &tauri::AppHandle) { + app.save_window_state(StateFlags::all()).unwrap(); + let window = app.get_window("main").unwrap(); + window.set_decorations(false).unwrap(); + window + .set_size(Size::Logical(LogicalSize { + width: 400.0, + height: 600.0, + })) + .unwrap(); +} + +pub fn deactivate_tray_mode(app: &tauri::AppHandle) { + let window = app.get_window("main").unwrap(); + window.restore_state(StateFlags::all()).unwrap(); + window.set_decorations(true).unwrap(); +} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 6306c56c..2bb586d5 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -74,13 +74,14 @@ "resizable": true, "title": "GitLight", "width": 1400, - "titleBarStyle": "Overlay", + "titleBarStyle": "Transparent", "hiddenTitle": true, "url": "/dashboard" } ], "systemTray": { - "iconPath": "icons/tray-base.png" + "iconPath": "icons/tray-base.png", + "menuOnLeftClick": false } } }