Skip to content
113 linesCodeBlameRaw

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 work1//! 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 work6
Checking for updates in the desktop app answers in its settings window, with a restart beside a downloaded version.7use std::sync::Mutex;
preparing for cloud work8
9use tauri::menu::{Menu, MenuItem, PredefinedMenuItem};
10use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};
11use tauri::{AppHandle, Manager, Wry};
12
The desktop app has settings of its own and a menu, and it is for using g1t, not browsing it.13use crate::{WINDOW, config, updates};
preparing for cloud work14
15pub 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.
19static RESTART: Mutex<Vec<MenuItem<Wry>>> = Mutex::new(Vec::new());
preparing for cloud work20
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.
22pub 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 work28pub 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 work33 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 work44 &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 work66pub 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 work68 let _ = item.set_text(format!("Restart to update to {version}"));
69 let _ = item.set_enabled(true);
70 }
71}
72
73pub 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
The desktop app has settings of its own and a menu, and it is for using g1t, not browsing it.81/// The window was closed: it waits in the tray, or g1t quits, as settings say.
82pub fn close(app: &AppHandle) {
83 if config::settings(app).close_to_tray {
84 hide(app);
85 } else {
86 updates::quit(app);
87 }
88}
89
preparing for cloud work90pub 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.
97pub 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.
108pub 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}