Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| preparing for cloud work | 1 | //! The tray icon and its menu, and showing and hiding the window. Closing |
| The desktop app has settings of its own and a menu, and it is for using g1t, not browsing it. | 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). | |
| preparing for cloud work | 6 | |
| Checking for updates in the desktop app answers in its settings window, with a restart beside a downloaded version. | 7 | use std::sync::Mutex; |
| preparing for cloud work | 8 | |
| 9 | use tauri::menu::{Menu, MenuItem, PredefinedMenuItem}; | |
| 10 | use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}; | |
| Merge branch 'main' of github.com:flagon-io/g1t | 11 | use tauri::{AppHandle, Wry}; |
| preparing for cloud work | 12 | |
| Merge branch 'main' of github.com:flagon-io/g1t | 13 | use crate::{config, updates}; |
| preparing for cloud work | 14 | |
| 15 | pub const TRAY: &str = "g1t"; | |
| 16 | ||
| Checking for updates in the desktop app answers in its settings window, with a restart beside a downloaded version. | 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()); | |
| preparing for cloud work | 20 | |
| Checking for updates in the desktop app answers in its settings window, with a restart beside a downloaded version. | 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 | ||
| preparing for cloud work | 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>)?; | |
| Checking for updates in the desktop app answers in its settings window, with a restart beside a downloaded version. | 31 | let restart = restart_item(app)?; |
| The desktop app has settings of its own and a menu, and it is for using g1t, not browsing it. | 32 | let settings = MenuItem::with_id(app, "settings", "Settings…", true, None::<&str>)?; |
| preparing for cloud work | 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)?, | |
| The desktop app has settings of its own and a menu, and it is for using g1t, not browsing it. | 43 | &settings, |
| preparing for cloud work | 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 | ||
| Checking for updates in the desktop app answers in its settings window, with a restart beside a downloaded version. | 65 | /// The update is downloaded: the menus say so, and offer the restart. |
| preparing for cloud work | 66 | pub fn update_ready(version: &str) { |
| Checking for updates in the desktop app answers in its settings window, with a restart beside a downloaded version. | 67 | for item in RESTART.lock().unwrap().iter() { |
| preparing for cloud work | 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) { | |
| Merge branch 'main' of github.com:flagon-io/g1t | 74 | if let Some(window) = crate::frame::window(app) { |
| preparing for cloud work | 75 | let _ = window.show(); |
| 76 | let _ = window.unminimize(); | |
| 77 | let _ = window.set_focus(); | |
| 78 | } | |
| Merge branch 'main' of github.com:flagon-io/g1t | 79 | if let Some(site) = crate::frame::site(app) { |
| 80 | let _ = site.set_focus(); | |
| 81 | } | |
| preparing for cloud work | 82 | } |
| 83 | ||
| The desktop app has settings of its own and a menu, and it is for using g1t, not browsing it. | 84 | /// The window was closed: it waits in the tray, or g1t quits, as settings say. |
| 85 | pub fn close(app: &AppHandle) { | |
| 86 | if config::settings(app).close_to_tray { | |
| 87 | hide(app); | |
| 88 | } else { | |
| 89 | updates::quit(app); | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| preparing for cloud work | 93 | pub fn hide(app: &AppHandle) { |
| Merge branch 'main' of github.com:flagon-io/g1t | 94 | if let Some(window) = crate::frame::window(app) { |
| preparing for cloud work | 95 | let _ = window.hide(); |
| 96 | } | |
| 97 | } | |
| 98 | ||
| 99 | /// The address of the launcher page bundled with the app. | |
| 100 | pub fn launcher_url(query: &str) -> url::Url { | |
| 101 | let base = if cfg!(windows) { "http://tauri.localhost/" } else { "tauri://localhost/" }; | |
| Merge branch 'main' into worktree-agent-a0d19db53ce9b29d8 | 102 | let mut url = config::dev_server().cloned().unwrap_or_else(|| url::Url::parse(base).expect("the launcher's address is a URL")); |
| preparing for cloud work | 103 | url.set_path("/index.html"); |
| 104 | if !query.is_empty() { | |
| 105 | url.set_query(Some(query)); | |
| 106 | } | |
| 107 | url | |
| 108 | } | |
| 109 | ||
| 110 | /// Takes the window to the launcher: `change` to choose another server. | |
| 111 | pub fn launcher(app: &AppHandle, query: &str) { | |
| Merge branch 'main' of github.com:flagon-io/g1t | 112 | if let Some(site) = crate::frame::site(app) { |
| 113 | let _ = site.navigate(launcher_url(query)); | |
| preparing for cloud work | 114 | show(app); |
| 115 | } | |
| 116 | } |
This file's history is long; its oldest lines are credited to the oldest commit read.