Skip to content
199 linesCodeBlameRaw
1//! The app's own settings: how it starts, what closing the window does,
2//! how it tells the person something arrived, its title bar, zoom and
3//! updates. They live in their own window (`launcher/settings.html`),
4//! opened from the menu, the tray, or the site's account menu, and are
5//! kept with the server in `desktop.json` (config.rs). Whether g1t opens
6//! when someone signs in to their computer is kept by the system itself,
7//! through the autostart plugin, and read back from it.
8
9use serde::{Deserialize, Serialize};
10use tauri::{AppHandle, Emitter, Manager, WebviewUrl, WebviewWindowBuilder};
11use tauri_plugin_autostart::ManagerExt;
12
13use crate::config;
14
15/// The settings window's label.
16pub const SETTINGS: &str = "settings";
17
18/// The argument the system starts g1t with at sign-in, so it can tell a
19/// start of its own from someone opening it.
20pub const AT_LOGIN: &str = "--at-login";
21
22/// The zoom levels the View menu steps through.
23pub const ZOOMS: &[f64] = &[0.67, 0.75, 0.8, 0.9, 1.0, 1.1, 1.25, 1.5, 1.75, 2.0];
24
25#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
26#[serde(default, rename_all = "camelCase")]
27pub struct Settings {
28 /// Opened at sign-in, g1t waits in the tray instead of opening its window.
29 pub start_hidden: bool,
30 /// Closing the window leaves g1t running in the tray, for notifications;
31 /// otherwise closing it quits.
32 pub close_to_tray: bool,
33 /// Notifications from the system, for what the site says needs the person.
34 pub notifications: bool,
35 /// The taskbar icon flashes, or the dock icon bounces, with a notification.
36 pub flash: bool,
37 /// What needs the person, counted on the icon.
38 pub badge: bool,
39 /// The system's title bar, and on Windows and Linux its menu bar, in
40 /// place of g1t's own (frame.rs). Takes effect when g1t restarts.
41 pub system_title_bar: bool,
42 /// The site's zoom, 1 for as it is.
43 pub zoom: f64,
44 /// Updates download by themselves; a person can still check from the menu.
45 pub auto_update: bool,
46}
47
48impl Default for Settings {
49 fn default() -> Self {
50 Self { start_hidden: true, close_to_tray: true, notifications: true, flash: true, badge: true, system_title_bar: false, zoom: 1.0, auto_update: true }
51 }
52}
53
54impl Settings {
55 /// A zoom the app offers, nearest to the one asked for.
56 pub fn clamp_zoom(zoom: f64) -> f64 {
57 ZOOMS.iter().copied().min_by(|a, b| (a - zoom).abs().total_cmp(&(b - zoom).abs())).unwrap_or(1.0)
58 }
59}
60
61/// What the settings window shows: the settings, and what the app and the
62/// system know.
63#[derive(Serialize)]
64#[serde(rename_all = "camelCase")]
65pub struct View {
66 #[serde(flatten)]
67 pub settings: Settings,
68 pub open_at_login: bool,
69 pub version: String,
70 pub platform: String,
71 pub server: String,
72 pub update_ready: Option<String>,
73 /// When the app last asked for updates and heard back, in milliseconds since 1970.
74 pub checked_at: Option<u64>,
75 /// The title bar setting differs from the one the windows have: a restart applies it.
76 pub title_bar_changed: bool,
77 /// Whether this window draws g1t's own title bar, which the page then draws (settings.html).
78 pub custom_frame: bool,
79}
80
81pub fn view(app: &AppHandle) -> View {
82 View {
83 settings: config::settings(app),
84 open_at_login: app.autolaunch().is_enabled().unwrap_or(false),
85 version: env!("CARGO_PKG_VERSION").into(),
86 platform: std::env::consts::OS.into(),
87 server: config::server(app).to_string(),
88 update_ready: crate::updates::ready(app),
89 checked_at: crate::updates::checked_at(app),
90 title_bar_changed: crate::frame::started(app) != crate::frame::custom(app),
91 custom_frame: crate::frame::started(app),
92 }
93}
94
95/// Keeps new settings and puts them to work at once.
96pub fn apply(app: &AppHandle, mut settings: Settings) -> Result<(), String> {
97 settings.zoom = Settings::clamp_zoom(settings.zoom);
98 let before = config::settings(app);
99 config::set_settings(app, settings.clone())?;
100 if let Some(site) = crate::frame::site(app).filter(|_| before.zoom != settings.zoom) {
101 let _ = site.set_zoom(settings.zoom);
102 }
103 if before.badge != settings.badge {
104 crate::badge::refresh(app);
105 }
106 Ok(())
107}
108
109/// Turns opening at sign-in on or off, with the system.
110pub fn open_at_login(app: &AppHandle, on: bool) -> Result<(), String> {
111 let autolaunch = app.autolaunch();
112 let result = if on { autolaunch.enable() } else { autolaunch.disable() };
113 result.map_err(|error| error.to_string())
114}
115
116/// Steps the zoom in (1), out (-1), or back to as it is (0).
117pub fn zoom(app: &AppHandle, step: i32) {
118 let mut settings = config::settings(app);
119 let at = ZOOMS.iter().position(|zoom| *zoom == Settings::clamp_zoom(settings.zoom)).unwrap_or(4);
120 settings.zoom = match step {
121 0 => 1.0,
122 1 => ZOOMS[(at + 1).min(ZOOMS.len() - 1)],
123 _ => ZOOMS[at.saturating_sub(1)],
124 };
125 let _ = apply(app, settings);
126}
127
128/// Whether this launch is the system's, at sign-in, and g1t should wait in the tray.
129pub fn starts_hidden(app: &AppHandle) -> bool {
130 std::env::args().any(|arg| arg == AT_LOGIN) && config::settings(app).start_hidden
131}
132
133/// What the settings window listens for: check for updates now.
134pub const CHECK_EVENT: &str = "g1t-check-updates";
135/// What it listens for: an update is downloaded, with its version.
136pub const READY_EVENT: &str = "g1t-update-ready";
137
138/// Opens the settings window, or brings it forward.
139pub fn open(app: &AppHandle) {
140 show(app, false);
141}
142
143/// "Check for updates…" in a menu: the settings window opens and checks,
144/// and says what it found, the download included, in its Updates section.
145pub fn open_and_check(app: &AppHandle) {
146 show(app, true);
147}
148
149fn show(app: &AppHandle, check: bool) {
150 if let Some(window) = app.get_webview_window(SETTINGS) {
151 let _ = window.unminimize();
152 let _ = window.show();
153 let _ = window.set_focus();
154 if check {
155 let _ = app.emit_to(SETTINGS, CHECK_EVENT, ());
156 }
157 return;
158 }
159 // A window still loading has no listener yet: the address asks instead.
160 let page = if check { "settings.html?check" } else { "settings.html" };
161 let custom = crate::frame::started(app);
162 let builder = WebviewWindowBuilder::new(app, SETTINGS, WebviewUrl::App(page.into()))
163 .title("g1t settings");
164 let built = crate::frame::dialog(builder, custom)
165 .inner_size(560.0, 680.0)
166 .min_inner_size(440.0, 420.0)
167 .maximizable(false)
168 .minimizable(false)
169 .center()
170 // The page is bundled with the app; its links go to the browser (settings.js).
171 .on_navigation(config::is_launcher)
172 .build();
173 match built {
174 Ok(window) => {
175 let _ = window.set_focus();
176 }
177 Err(error) => eprintln!("g1t: the settings window could not open: {error}"),
178 }
179}
180
181#[cfg(test)]
182mod tests {
183 use super::Settings;
184
185 #[test]
186 fn a_missing_setting_is_its_default() {
187 let settings: Settings = serde_json::from_str(r#"{"closeToTray":false}"#).unwrap();
188 assert!(!settings.close_to_tray);
189 assert!(settings.notifications);
190 assert_eq!(settings.zoom, 1.0);
191 }
192
193 #[test]
194 fn zoom_is_one_the_app_offers() {
195 assert_eq!(Settings::clamp_zoom(1.07), 1.1);
196 assert_eq!(Settings::clamp_zoom(9.0), 2.0);
197 assert_eq!(Settings::clamp_zoom(0.1), 0.67);
198 }
199}