| 1 | //! The tray icon and its menu, and showing and hiding the window. Closing |
| 2 | //! the window hides it, unless settings say closing quits; g1t keeps |
| 3 | //! running for notifications, and the tray's Open, or the icon, brings it |
| 4 | //! back. Quit is here too. The menu's rows are handled with the app |
| 5 | //! menu's (menu.rs). |
| 6 | |
| 7 | use std::sync::Mutex; |
| 8 | |
| 9 | use tauri::menu::{Menu, MenuItem, PredefinedMenuItem}; |
| 10 | use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}; |
| 11 | use tauri::{AppHandle, Manager, Wry}; |
| 12 | |
| 13 | use crate::{WINDOW, config, updates}; |
| 14 | |
| 15 | pub const TRAY: &str = "g1t"; |
| 16 | |
| 17 | /// The menu rows that restart into a downloaded update, the tray's and |
| 18 | /// the Help menu's: off until one is. |
| 19 | static RESTART: Mutex<Vec<MenuItem<Wry>>> = Mutex::new(Vec::new()); |
| 20 | |
| 21 | /// A row that restarts into the update, kept so `update_ready` can turn it on. |
| 22 | pub fn restart_item(app: &AppHandle) -> tauri::Result<MenuItem<Wry>> { |
| 23 | let item = MenuItem::with_id(app, "restart", "Restart to finish updating", false, None::<&str>)?; |
| 24 | RESTART.lock().unwrap().push(item.clone()); |
| 25 | Ok(item) |
| 26 | } |
| 27 | |
| 28 | pub fn install(app: &AppHandle) -> tauri::Result<()> { |
| 29 | let open = MenuItem::with_id(app, "open", "Open g1t", true, None::<&str>)?; |
| 30 | let check = MenuItem::with_id(app, "check", "Check for updates…", true, None::<&str>)?; |
| 31 | let restart = restart_item(app)?; |
| 32 | let settings = MenuItem::with_id(app, "settings", "Settings…", true, None::<&str>)?; |
| 33 | let server = MenuItem::with_id(app, "server", "Use another g1t…", true, None::<&str>)?; |
| 34 | let quit = MenuItem::with_id(app, "quit", "Quit g1t", true, None::<&str>)?; |
| 35 | let menu = Menu::with_items( |
| 36 | app, |
| 37 | &[ |
| 38 | &open, |
| 39 | &PredefinedMenuItem::separator(app)?, |
| 40 | &check, |
| 41 | &restart, |
| 42 | &PredefinedMenuItem::separator(app)?, |
| 43 | &settings, |
| 44 | &server, |
| 45 | &PredefinedMenuItem::separator(app)?, |
| 46 | &quit, |
| 47 | ], |
| 48 | )?; |
| 49 | let mut tray = TrayIconBuilder::with_id(TRAY) |
| 50 | .menu(&menu) |
| 51 | .show_menu_on_left_click(false) |
| 52 | .tooltip("g1t") |
| 53 | .on_tray_icon_event(|tray, event| { |
| 54 | if let TrayIconEvent::Click { button: MouseButton::Left, button_state: MouseButtonState::Up, .. } = event { |
| 55 | show(tray.app_handle()); |
| 56 | } |
| 57 | }); |
| 58 | if let Some(icon) = app.default_window_icon() { |
| 59 | tray = tray.icon(icon.clone()); |
| 60 | } |
| 61 | tray.build(app)?; |
| 62 | Ok(()) |
| 63 | } |
| 64 | |
| 65 | /// The update is downloaded: the menus say so, and offer the restart. |
| 66 | pub fn update_ready(version: &str) { |
| 67 | for item in RESTART.lock().unwrap().iter() { |
| 68 | let _ = item.set_text(format!("Restart to update to {version}")); |
| 69 | let _ = item.set_enabled(true); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn show(app: &AppHandle) { |
| 74 | if let Some(window) = app.get_webview_window(WINDOW) { |
| 75 | let _ = window.show(); |
| 76 | let _ = window.unminimize(); |
| 77 | let _ = window.set_focus(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// The window was closed: it waits in the tray, or g1t quits, as settings say. |
| 82 | pub fn close(app: &AppHandle) { |
| 83 | if config::settings(app).close_to_tray { |
| 84 | hide(app); |
| 85 | } else { |
| 86 | updates::quit(app); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub fn hide(app: &AppHandle) { |
| 91 | if let Some(window) = app.get_webview_window(WINDOW) { |
| 92 | let _ = window.hide(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// The address of the launcher page bundled with the app. |
| 97 | pub fn launcher_url(query: &str) -> url::Url { |
| 98 | let base = if cfg!(windows) { "http://tauri.localhost/" } else { "tauri://localhost/" }; |
| 99 | let mut url = url::Url::parse(base).expect("the launcher's address is a URL"); |
| 100 | url.set_path("/index.html"); |
| 101 | if !query.is_empty() { |
| 102 | url.set_query(Some(query)); |
| 103 | } |
| 104 | url |
| 105 | } |
| 106 | |
| 107 | /// Takes the window to the launcher: `change` to choose another server. |
| 108 | pub fn launcher(app: &AppHandle, query: &str) { |
| 109 | if let Some(window) = app.get_webview_window(WINDOW) { |
| 110 | let _ = window.navigate(launcher_url(query)); |
| 111 | show(app); |
| 112 | } |
| 113 | } |