BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / general.rs

general.rs

general.rs 17.8 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::{
3 button::Button,
4 h_flex,
5 menu::{DropdownMenu as _, PopupMenuItem},
6 v_flex,
7};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10use teams_core::{ChatSettingChange, TranslationBehavior};
11
12use super::account::{UP_ARROW, account_select};
13use super::rows::{Choice, caption, control_row, select, toggle_row};
14use crate::app_state::AppState;
15use crate::i18n::tr;
16use crate::notify::NotificationCenter;
17use crate::notify::rules::Settings;
18use crate::theme;
19use crate::views::widgets::icon;
20
21const CHIP_HEIGHT: f32 = 24.;
22const DISABLED_OPACITY: f32 = 0.4;
23const LANGUAGE_MENU_HEIGHT: f32 = 320.;
24
25fn startup_rows(center: &Entity<NotificationCenter>, cx: &App) -> [Stateful<Div>; 2] {
26 let startup = center.read(cx).startup();
27 let (auto_start_center, background_center) = (center.clone(), center.clone());
28 [
29 toggle_row(
30 "settings-auto-start",
31 tr("settings.general.auto_start"),
32 "",
33 theme::text_muted(),
34 startup.auto_start,
35 )
36 .on_click(move |_, _, cx| {
37 auto_start_center.update(cx, |center, cx| {
38 center.set_auto_start(!startup.auto_start, cx)
39 })
40 }),
41 toggle_row(
42 "settings-open-in-background",
43 tr("settings.general.open_in_background"),
44 "",
45 theme::text_muted(),
46 startup.open_in_background,
47 )
48 .when(!startup.auto_start, |row| row.opacity(DISABLED_OPACITY))
49 .when(startup.auto_start, |row| {
50 row.on_click(move |_, _, cx| {
51 background_center.update(cx, |center, cx| {
52 center.set_open_in_background(!startup.open_in_background, cx)
53 })
54 })
55 }),
56 ]
57}
58
59pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &App) -> Div {
60 let settings = center.read(cx).settings();
61 v_flex()
62 .w_full()
63 .child(caption(tr("settings.caption.system")))
64 .children(startup_rows(center, cx))
65 .child(settings_toggle(
66 center,
67 (
68 "settings-show-in-tray",
69 tr("settings.general.show_in_tray"),
70 tr("settings.general.show_in_tray_detail"),
71 ),
72 settings.show_in_tray,
73 |settings| settings.show_in_tray = !settings.show_in_tray,
74 ))
75 .when(settings.show_in_tray, |rows| {
76 rows.child(settings_toggle(
77 center,
78 (
79 "settings-close-to-tray",
80 tr("settings.general.close_to_tray"),
81 tr("settings.general.close_to_tray_detail"),
82 ),
83 settings.close_to_tray,
84 |settings| settings.close_to_tray = !settings.close_to_tray,
85 ))
86 })
87 .children(external_browser_rows(app, cx))
88 .children(install_rows(cx))
89 .children(update_rows(cx))
90 .child(caption(tr("settings.caption.compose_box")))
91 .child(account_select(
92 app,
93 (
94 "settings-up-arrow",
95 tr("settings.general.up_arrow"),
96 tr("settings.general.up_arrow_detail"),
97 ),
98 &UP_ARROW,
99 app.read(cx).account_chat_settings().up_arrow,
100 ChatSettingChange::UpArrow,
101 ))
102 .child(caption(tr("settings.caption.translation")))
103 .children(translation_rows(app, cx))
104}
105
106fn settings_toggle(
107 center: &Entity<NotificationCenter>,
108 (id, title, detail): (&'static str, String, String),
109 enabled: bool,
110 change: fn(&mut Settings),
111) -> Stateful<Div> {
112 let center = center.clone();
113 toggle_row(id, title, detail, theme::text_muted(), enabled).on_click(move |_, _, cx| {
114 center.update(cx, |center, cx| center.update_settings(cx, change))
115 })
116}
117
118/// Linux only: the application menu install and the install question.
119fn install_rows(cx: &App) -> Vec<AnyElement> {
120 #[cfg(not(target_os = "linux"))]
121 {
122 let _ = cx;
123 Vec::new()
124 }
125 #[cfg(target_os = "linux")]
126 {
127 use std::collections::HashMap;
128
129 use super::rows::{control_row, text_button};
130 use crate::i18n::tr_args;
131 use crate::linux_install::{self, Installation};
132
133 let with = |key: &str, name: &str, value: String| {
134 let mut args = HashMap::new();
135 args.insert(name.to_owned(), value);
136 tr_args(key, &args)
137 };
138 let folder = linux_install::install_folder();
139 let installation = linux_install::installation();
140 let detail = match installation {
141 Installation::Running => with("install.installed_detail", "folder", folder),
142 Installation::OtherCopy => with("install.other_copy_detail", "folder", folder),
143 Installation::NotInstalled => tr("install.not_installed_detail"),
144 };
145 let mut buttons = h_flex().gap(px(8.)).flex_none();
146 if installation != Installation::Running {
147 let label = match installation {
148 Installation::OtherCopy => tr("install.replace"),
149 _ => tr("install.move"),
150 };
151 buttons = buttons.child(
152 text_button("settings-install", label)
153 .on_click(|_, _, cx| linux_install::install_now(cx)),
154 );
155 }
156 if installation != Installation::NotInstalled {
157 buttons = buttons.child(
158 text_button("settings-install-remove", tr("install.remove"))
159 .on_click(|_, _, cx| linux_install::remove(cx)),
160 );
161 }
162 let mut rows =
163 vec![control_row(tr("install.menu_title"), detail, buttons).into_any_element()];
164 if installation != Installation::Running {
165 let asks = linux_install::asks_on_start(cx);
166 rows.push(
167 toggle_row(
168 "settings-install-ask",
169 tr("install.ask_on_start"),
170 "",
171 theme::text_muted(),
172 asks,
173 )
174 .on_click(move |_, _, cx| linux_install::set_asks_on_start(!asks, cx))
175 .into_any_element(),
176 );
177 }
178 rows
179 }
180}
181
182/// Same release check on every OS. Flatpak hides it; the package owns updates.
183fn update_rows(cx: &App) -> Vec<AnyElement> {
184 if !crate::linux_update::checks_releases() {
185 return Vec::new();
186 }
187 use std::collections::HashMap;
188
189 use super::rows::{control_row, text_button};
190 use crate::i18n::tr_args;
191
192 let version = app_base::version::release_tag()
193 .unwrap_or(concat!("v", env!("CARGO_PKG_VERSION")))
194 .to_owned();
195 let mut version_args = HashMap::new();
196 version_args.insert("version".to_owned(), version);
197 let detail_key = if app_base::version::release_tag().is_some() {
198 "install.updates_detail"
199 } else {
200 "install.updates_detail_dev"
201 };
202 let automatic = crate::linux_update::enabled(cx);
203 vec![
204 toggle_row(
205 "settings-auto-updates",
206 tr("install.auto_updates"),
207 tr("install.auto_updates_detail"),
208 theme::text_muted(),
209 automatic,
210 )
211 .on_click(move |_, _, cx| crate::linux_update::set_enabled(!automatic, cx))
212 .into_any_element(),
213 control_row(
214 tr("install.updates_title"),
215 tr_args(detail_key, &version_args),
216 text_button("settings-check-updates", tr("install.check_now"))
217 .on_click(|_, _, cx| crate::linux_update::check(true, cx)),
218 )
219 .into_any_element(),
220 ]
221}
222
223fn external_browser_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {
224 #[cfg(not(any(target_os = "linux", target_os = "macos")))]
225 {
226 let _ = (app, cx);
227 return Vec::new();
228 }
229 #[cfg(any(target_os = "linux", target_os = "macos"))]
230 {
231 use crate::external_browser::{self, ExternalBrowser};
232
233 let selected = external_browser::load(&app.read(cx).store);
234 let current_label = external_browser::available_choices()
235 .iter()
236 .find(|choice| choice.id == selected.id())
237 .map(|choice| {
238 if external_browser::is_system_id(&choice.id) {
239 tr("settings.general.external_browser_system")
240 } else {
241 choice.label.clone()
242 }
243 })
244 .unwrap_or_else(|| tr("settings.general.external_browser_system"));
245 let choices: Vec<Choice> = external_browser::available_choices()
246 .iter()
247 .map(|choice| {
248 let label = if external_browser::is_system_id(&choice.id) {
249 tr("settings.general.external_browser_system")
250 } else {
251 choice.label.clone()
252 };
253 let is_selected = selected.id() == choice.id;
254 let (app, id) = (app.clone(), choice.id.clone());
255 Choice::new(label, is_selected, move |cx| {
256 app.update(cx, |state, cx| {
257 let browser = if external_browser::is_system_id(&id) {
258 ExternalBrowser::System
259 } else {
260 ExternalBrowser::Program(id.clone())
261 };
262 external_browser::save(&state.store, &browser);
263 cx.notify();
264 });
265 })
266 })
267 .collect();
268 vec![
269 caption(tr("settings.caption.browser")).into_any_element(),
270 sign_in_browser_row(),
271 control_row(
272 tr("settings.general.external_browser"),
273 tr("settings.general.external_browser_detail"),
274 select("settings-external-browser", current_label, false, choices),
275 )
276 .into_any_element(),
277 ]
278 }
279}
280
281/// The browser the Teams session runs in. Changing it restarts the app, which stops the old
282/// browser on the way out and starts the new one.
283#[cfg(any(target_os = "linux", target_os = "macos"))]
284fn sign_in_browser_row() -> AnyElement {
285 use std::sync::LazyLock;
286
287 use browser::{InstalledBrowser, SignInBrowser};
288
289 static INSTALLED: LazyLock<Vec<InstalledBrowser>> = LazyLock::new(browser::installed_browsers);
290 static AUTOMATIC: LazyLock<Option<String>> = LazyLock::new(|| {
291 browser::automatic_browser(&INSTALLED).map(|browser| browser.name.clone())
292 });
293
294 let saved = SignInBrowser::load();
295 let automatic_label = match AUTOMATIC.as_deref() {
296 Some(name) => format!(
297 "{} ({name})",
298 tr("settings.general.sign_in_browser_automatic")
299 ),
300 None => tr("settings.general.sign_in_browser_automatic"),
301 };
302 let options = std::iter::once((SignInBrowser::Automatic, automatic_label)).chain(
303 INSTALLED.iter().map(|browser| {
304 (
305 SignInBrowser::Program(browser.path.clone()),
306 browser.name.clone(),
307 )
308 }),
309 );
310 let mut current_label = None;
311 let choices: Vec<Choice> = options
312 .map(|(option, label)| {
313 let is_selected = option == saved;
314 if is_selected {
315 current_label = Some(label.clone());
316 }
317 Choice::new(label, is_selected, move |cx| {
318 if is_selected {
319 return;
320 }
321 match option.save() {
322 Ok(()) => restart_app(cx),
323 Err(error) => {
324 app_base::diagnostics::record(format!("sign-in browser not saved: {error}"))
325 }
326 }
327 })
328 })
329 .collect();
330 let current_label =
331 current_label.unwrap_or_else(|| tr("settings.general.sign_in_browser_automatic"));
332 control_row(
333 tr("settings.general.sign_in_browser"),
334 tr("settings.general.sign_in_browser_detail"),
335 select("settings-sign-in-browser", current_label, false, choices),
336 )
337 .into_any_element()
338}
339
340#[cfg(any(target_os = "linux", target_os = "macos"))]
341fn restart_app(cx: &mut App) {
342 let spawned = std::env::current_exe().and_then(|exe| {
343 std::process::Command::new(exe)
344 .args(std::env::args_os().skip(1))
345 .env(
346 app_base::updater::RELAUNCH_ENV,
347 std::process::id().to_string(),
348 )
349 .spawn()
350 });
351 match spawned {
352 Ok(_) => cx.quit(),
353 Err(error) => app_base::diagnostics::record(format!(
354 "restart for the sign-in browser failed: {error}"
355 )),
356 }
357}
358
359fn translation_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {
360 let state = app.read(cx);
361 if state.translation.settings.is_none() {
362 return vec![
363 control_row(
364 tr("settings.general.translation_title"),
365 tr("settings.general.translation_not_loaded"),
366 div(),
367 )
368 .into_any_element(),
369 ];
370 }
371 let translation = &state.translation;
372 let target = translation.target();
373 let behavior = translation.behavior();
374 let targets = translation
375 .languages
376 .iter()
377 .map(|language| {
378 let (app, code) = (app.clone(), language.code.clone());
379 Choice::new(language.name.clone(), language.code == target, move |cx| {
380 app.update(cx, |state, cx| state.set_translation_target(&code, cx))
381 })
382 })
383 .collect();
384 let behavior_options = [
385 (
386 tr("settings.general.behavior_ask"),
387 TranslationBehavior::Ask,
388 ),
389 (
390 tr("settings.general.behavior_auto"),
391 TranslationBehavior::Auto,
392 ),
393 (
394 tr("settings.general.behavior_never"),
395 TranslationBehavior::Never,
396 ),
397 ];
398 let behavior_label = behavior_options
399 .iter()
400 .find(|(_, choice)| *choice == behavior)
401 .map_or(String::new(), |(label, _)| label.clone());
402 let behaviors = behavior_options
403 .into_iter()
404 .map(|(label, choice)| {
405 let app = app.clone();
406 Choice::new(label, behavior == choice, move |cx| {
407 app.update(cx, |state, cx| state.set_translation_behavior(choice, cx))
408 })
409 })
410 .collect();
411 vec![
412 control_row(
413 tr("settings.general.translate_into"),
414 "Read from your Teams account",
415 select(
416 "settings-translation-target",
417 translation.language_name(&target),
418 false,
419 targets,
420 ),
421 )
422 .into_any_element(),
423 control_row(
424 tr("settings.general.behavior_title"),
425 "",
426 select(
427 "settings-translation-behavior",
428 behavior_label,
429 false,
430 behaviors,
431 ),
432 )
433 .into_any_element(),
434 control_row(
435 tr("settings.general.never_translate"),
436 "",
437 never_chips(app, &target, cx),
438 )
439 .into_any_element(),
440 ]
441}
442
443fn never_chips(app: &Entity<AppState>, target: &str, cx: &App) -> Div {
444 let translation = &app.read(cx).translation;
445 let never: Vec<String> = translation
446 .display_codes()
447 .into_iter()
448 .filter(|code| code != target)
449 .collect();
450 let chips = never.iter().map(|code| {
451 let (remove_app, remove_code) = (app.clone(), code.clone());
452 h_flex()
453 .h(px(CHIP_HEIGHT))
454 .pl(px(8.))
455 .gap(px(4.))
456 .items_center()
457 .rounded(px(6.))
458 .bg(theme::surface_raised())
459 .text_size(px(12.))
460 .text_color(theme::text_strong())
461 .child(translation.language_name(code))
462 .child(
463 div()
464 .id(SharedString::from(format!("settings-never-remove-{code}")))
465 .w(px(16.))
466 .h_full()
467 .flex()
468 .items_center()
469 .justify_center()
470 .cursor_pointer()
471 .child(icon(IconName::X, 10., theme::text_faint()))
472 .on_click(move |_, _, cx| {
473 remove_app.update(cx, |state, cx| {
474 state.allow_translating_language(&remove_code, cx)
475 })
476 }),
477 )
478 });
479 let addable = translation
480 .languages
481 .iter()
482 .filter(|language| language.code != target && !never.contains(&language.code))
483 .map(|language| {
484 let (app, code) = (app.clone(), language.code.clone());
485 Choice::new(language.name.clone(), false, move |cx| {
486 app.update(cx, |state, cx| state.never_translate_language(&code, cx))
487 })
488 })
489 .collect();
490 h_flex()
491 .flex_wrap()
492 .justify_end()
493 .gap(px(6.))
494 .items_center()
495 .children(chips)
496 .child(add_language_button(addable))
497}
498
499fn add_language_button(choices: Vec<Choice>) -> impl IntoElement {
500 Button::new("settings-never-add")
501 .outline()
502 .h(px(CHIP_HEIGHT))
503 .px(px(8.))
504 .rounded(px(6.))
505 .text_size(px(12.))
506 .text_color(theme::accent_text())
507 .label(format!("+ {}", tr("common.add")))
508 .dropdown_menu(move |menu, _, _| {
509 choices.iter().fold(
510 menu.max_h(px(LANGUAGE_MENU_HEIGHT)).scrollable(true),
511 |menu, choice| {
512 let choose = choice.choose.clone();
513 menu.item(
514 PopupMenuItem::new(choice.label.clone())
515 .on_click(move |_, _, cx| choose(cx)),
516 )
517 },
518 )
519 })
520}