Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
157 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.
82///
83/// The change is handed to the main thread and not waited for: off the
84/// main thread `set_text` waits for the main thread to make it, so the
85/// runner's thread would stall on a busy window, and holding `RUNNER`
86/// across it while the main thread came here too (the site's
87/// `runner_offer`) froze the app, the window marked Not Responding.
88pub fn runner_changed(app: &AppHandle, view: &crate::runner::View) {
89 let headline = crate::runner::headline(view);
90 let toggle_line = toggle_text(view);
91 let handle = app.clone();
92 let _ = app.run_on_main_thread(move || {
93 let rows = RUNNER.lock().unwrap().clone();
94 if let Some((status, toggle)) = rows {
95 let _ = status.set_text(&headline);
96 let _ = toggle.set_text(toggle_line);
97 }
98 if let Some(tray) = handle.tray_by_id(TRAY) {
99 let _ = tray.set_tooltip(Some(format!("g1t · {headline}")));
100 }
101 });
102}
103
104/// The update is downloaded: the menus say so, and offer the restart.
105pub fn update_ready(version: &str) {
106 // Out of the lock first, as in runner_changed: this runs off the main thread.
107 let items = RESTART.lock().unwrap().clone();
108 for item in &items {
109 let _ = item.set_text(format!("Restart to update to {version}"));
110 let _ = item.set_enabled(true);
111 }
112}
113
114pub fn show(app: &AppHandle) {
115 if let Some(window) = crate::frame::window(app) {
116 let _ = window.show();
117 let _ = window.unminimize();
118 let _ = window.set_focus();
119 }
120 if let Some(site) = crate::frame::site(app) {
121 let _ = site.set_focus();
122 }
123}
124
125/// The window was closed: it waits in the tray, or g1t quits, as settings say.
126pub fn close(app: &AppHandle) {
127 if config::settings(app).close_to_tray {
128 hide(app);
129 } else {
130 updates::quit(app);
131 }
132}
133
134pub fn hide(app: &AppHandle) {
135 if let Some(window) = crate::frame::window(app) {
136 let _ = window.hide();
137 }
138}
139
140/// The address of the launcher page bundled with the app.
141pub fn launcher_url(query: &str) -> url::Url {
142 let base = if cfg!(windows) { "http://tauri.localhost/" } else { "tauri://localhost/" };
143 let mut url = config::dev_server().cloned().unwrap_or_else(|| url::Url::parse(base).expect("the launcher's address is a URL"));
144 url.set_path("/index.html");
145 if !query.is_empty() {
146 url.set_query(Some(query));
147 }
148 url
149}
150
151/// Takes the window to the launcher: `change` to choose another server.
152pub fn launcher(app: &AppHandle, query: &str) {
153 if let Some(site) = crate::frame::site(app) {
154 let _ = site.navigate(launcher_url(query));
155 show(app);
156 }
157}