Skip to content
143 linesCodeBlameRaw
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. The runner's state is a row of its own, which opens the runner's
5//! menu under the title bar's dot, with Start or Stop under it
6//! (runner/mod.rs). Quit is here too. The menu's rows are handled with the
7//! app menu's (menu.rs).
8
9use std::sync::Mutex;
10
11use tauri::menu::{Menu, MenuItem, PredefinedMenuItem};
12use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};
13use tauri::{AppHandle, Wry};
14
15use crate::{config, updates};
16
17pub const TRAY: &str = "g1t";
18
19/// The menu rows that restart into a downloaded update, the tray's and
20/// the Help menu's: off until one is.
21static RESTART: Mutex<Vec<MenuItem<Wry>>> = Mutex::new(Vec::new());
22
23/// The runner's rows: what it is doing, and Start or Stop.
24static RUNNER: Mutex<Option<(MenuItem<Wry>, MenuItem<Wry>)>> = Mutex::new(None);
25
26/// A row that restarts into the update, kept so `update_ready` can turn it on.
27pub fn restart_item(app: &AppHandle) -> tauri::Result<MenuItem<Wry>> {
28 let item = MenuItem::with_id(app, "restart", "Restart to finish updating", false, None::<&str>)?;
29 RESTART.lock().unwrap().push(item.clone());
30 Ok(item)
31}
32
33pub fn install(app: &AppHandle) -> tauri::Result<()> {
34 let open = MenuItem::with_id(app, "open", "Open g1t", true, None::<&str>)?;
35 let check = MenuItem::with_id(app, "check", "Check for updates…", true, None::<&str>)?;
36 let restart = restart_item(app)?;
37 let runner_view = crate::runner::view(app);
38 let runner = MenuItem::with_id(app, "runner-status", crate::runner::headline(&runner_view), true, None::<&str>)?;
39 let runner_toggle = MenuItem::with_id(app, "runner-toggle", toggle_text(&runner_view), true, None::<&str>)?;
40 *RUNNER.lock().unwrap() = Some((runner.clone(), runner_toggle.clone()));
41 let settings = MenuItem::with_id(app, "settings", "Settings…", true, None::<&str>)?;
42 let server = MenuItem::with_id(app, "server", "Use another g1t…", true, None::<&str>)?;
43 let quit = MenuItem::with_id(app, "quit", "Quit g1t", true, None::<&str>)?;
44 let menu = Menu::with_items(
45 app,
46 &[
47 &open,
48 &PredefinedMenuItem::separator(app)?,
49 &runner,
50 &runner_toggle,
51 &PredefinedMenuItem::separator(app)?,
52 &check,
53 &restart,
54 &PredefinedMenuItem::separator(app)?,
55 &settings,
56 &server,
57 &PredefinedMenuItem::separator(app)?,
58 &quit,
59 ],
60 )?;
61 let mut tray = TrayIconBuilder::with_id(TRAY)
62 .menu(&menu)
63 .show_menu_on_left_click(false)
64 .tooltip("g1t")
65 .on_tray_icon_event(|tray, event| {
66 if let TrayIconEvent::Click { button: MouseButton::Left, button_state: MouseButtonState::Up, .. } = event {
67 show(tray.app_handle());
68 }
69 });
70 if let Some(icon) = app.default_window_icon() {
71 tray = tray.icon(icon.clone());
72 }
73 tray.build(app)?;
74 Ok(())
75}
76
77fn toggle_text(view: &crate::runner::View) -> &'static str {
78 if view.enabled && !view.paused { "Stop running jobs" } else { "Start running jobs" }
79}
80
81/// The runner changed: its rows, and the icon's tooltip, say what it is doing now.
82pub fn runner_changed(app: &AppHandle, view: &crate::runner::View) {
83 if let Some((status, toggle)) = RUNNER.lock().unwrap().as_ref() {
84 let _ = status.set_text(crate::runner::headline(view));
85 let _ = toggle.set_text(toggle_text(view));
86 }
87 if let Some(tray) = app.tray_by_id(TRAY) {
88 let _ = tray.set_tooltip(Some(format!("g1t · {}", crate::runner::headline(view))));
89 }
90}
91
92/// The update is downloaded: the menus say so, and offer the restart.
93pub fn update_ready(version: &str) {
94 for item in RESTART.lock().unwrap().iter() {
95 let _ = item.set_text(format!("Restart to update to {version}"));
96 let _ = item.set_enabled(true);
97 }
98}
99
100pub fn show(app: &AppHandle) {
101 if let Some(window) = crate::frame::window(app) {
102 let _ = window.show();
103 let _ = window.unminimize();
104 let _ = window.set_focus();
105 }
106 if let Some(site) = crate::frame::site(app) {
107 let _ = site.set_focus();
108 }
109}
110
111/// The window was closed: it waits in the tray, or g1t quits, as settings say.
112pub fn close(app: &AppHandle) {
113 if config::settings(app).close_to_tray {
114 hide(app);
115 } else {
116 updates::quit(app);
117 }
118}
119
120pub fn hide(app: &AppHandle) {
121 if let Some(window) = crate::frame::window(app) {
122 let _ = window.hide();
123 }
124}
125
126/// The address of the launcher page bundled with the app.
127pub fn launcher_url(query: &str) -> url::Url {
128 let base = if cfg!(windows) { "http://tauri.localhost/" } else { "tauri://localhost/" };
129 let mut url = config::dev_server().cloned().unwrap_or_else(|| url::Url::parse(base).expect("the launcher's address is a URL"));
130 url.set_path("/index.html");
131 if !query.is_empty() {
132 url.set_query(Some(query));
133 }
134 url
135}
136
137/// Takes the window to the launcher: `change` to choose another server.
138pub fn launcher(app: &AppHandle, query: &str) {
139 if let Some(site) = crate::frame::site(app) {
140 let _ = site.navigate(launcher_url(query));
141 show(app);
142 }
143}