BlueGit

ict rusty-teams

Files

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

general.rs

general.rs 14.2 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 control_row(
238 tr("settings.general.external_browser"),
239 tr("settings.general.external_browser_detail"),
240 select("settings-external-browser", current_label, false, choices),
241 )
242 .into_any_element(),
243 ]
244 }
245}
246
247fn translation_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {
248 let state = app.read(cx);
249 if state.translation.settings.is_none() {
250 return vec![
251 control_row(
252 tr("settings.general.translation_title"),
253 tr("settings.general.translation_not_loaded"),
254 div(),
255 )
256 .into_any_element(),
257 ];
258 }
259 let translation = &state.translation;
260 let target = translation.target();
261 let behavior = translation.behavior();
262 let targets = translation
263 .languages
264 .iter()
265 .map(|language| {
266 let (app, code) = (app.clone(), language.code.clone());
267 Choice::new(language.name.clone(), language.code == target, move |cx| {
268 app.update(cx, |state, cx| state.set_translation_target(&code, cx))
269 })
270 })
271 .collect();
272 let behavior_options = [
273 (
274 tr("settings.general.behavior_ask"),
275 TranslationBehavior::Ask,
276 ),
277 (
278 tr("settings.general.behavior_auto"),
279 TranslationBehavior::Auto,
280 ),
281 (
282 tr("settings.general.behavior_never"),
283 TranslationBehavior::Never,
284 ),
285 ];
286 let behavior_label = behavior_options
287 .iter()
288 .find(|(_, choice)| *choice == behavior)
289 .map_or(String::new(), |(label, _)| label.clone());
290 let behaviors = behavior_options
291 .into_iter()
292 .map(|(label, choice)| {
293 let app = app.clone();
294 Choice::new(label, behavior == choice, move |cx| {
295 app.update(cx, |state, cx| state.set_translation_behavior(choice, cx))
296 })
297 })
298 .collect();
299 vec![
300 control_row(
301 tr("settings.general.translate_into"),
302 "Read from your Teams account",
303 select(
304 "settings-translation-target",
305 translation.language_name(&target),
306 false,
307 targets,
308 ),
309 )
310 .into_any_element(),
311 control_row(
312 tr("settings.general.behavior_title"),
313 "",
314 select(
315 "settings-translation-behavior",
316 behavior_label,
317 false,
318 behaviors,
319 ),
320 )
321 .into_any_element(),
322 control_row(
323 tr("settings.general.never_translate"),
324 "",
325 never_chips(app, &target, cx),
326 )
327 .into_any_element(),
328 ]
329}
330
331fn never_chips(app: &Entity<AppState>, target: &str, cx: &App) -> Div {
332 let translation = &app.read(cx).translation;
333 let never: Vec<String> = translation
334 .display_codes()
335 .into_iter()
336 .filter(|code| code != target)
337 .collect();
338 let chips = never.iter().map(|code| {
339 let (remove_app, remove_code) = (app.clone(), code.clone());
340 h_flex()
341 .h(px(CHIP_HEIGHT))
342 .pl(px(8.))
343 .gap(px(4.))
344 .items_center()
345 .rounded(px(6.))
346 .bg(theme::surface_raised())
347 .text_size(px(12.))
348 .text_color(theme::text_strong())
349 .child(translation.language_name(code))
350 .child(
351 div()
352 .id(SharedString::from(format!("settings-never-remove-{code}")))
353 .w(px(16.))
354 .h_full()
355 .flex()
356 .items_center()
357 .justify_center()
358 .cursor_pointer()
359 .child(icon(IconName::X, 10., theme::text_faint()))
360 .on_click(move |_, _, cx| {
361 remove_app.update(cx, |state, cx| {
362 state.allow_translating_language(&remove_code, cx)
363 })
364 }),
365 )
366 });
367 let addable = translation
368 .languages
369 .iter()
370 .filter(|language| language.code != target && !never.contains(&language.code))
371 .map(|language| {
372 let (app, code) = (app.clone(), language.code.clone());
373 Choice::new(language.name.clone(), false, move |cx| {
374 app.update(cx, |state, cx| state.never_translate_language(&code, cx))
375 })
376 })
377 .collect();
378 h_flex()
379 .flex_wrap()
380 .justify_end()
381 .gap(px(6.))
382 .items_center()
383 .children(chips)
384 .child(add_language_button(addable))
385}
386
387fn add_language_button(choices: Vec<Choice>) -> impl IntoElement {
388 Button::new("settings-never-add")
389 .outline()
390 .h(px(CHIP_HEIGHT))
391 .px(px(8.))
392 .rounded(px(6.))
393 .text_size(px(12.))
394 .text_color(theme::accent_text())
395 .label(format!("+ {}", tr("common.add")))
396 .dropdown_menu(move |menu, _, _| {
397 choices.iter().fold(
398 menu.max_h(px(LANGUAGE_MENU_HEIGHT)).scrollable(true),
399 |menu, choice| {
400 let choose = choice.choose.clone();
401 menu.item(
402 PopupMenuItem::new(choice.label.clone())
403 .on_click(move |_, _, cx| choose(cx)),
404 )
405 },
406 )
407 })
408}