BlueGit

ict rusty-teams

Files

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

general.rs

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