BlueGit

ict rusty-teams

Files

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

general.rs

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