BlueGit

ict rusty-teams

Files

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

general.rs

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