BlueGit

ict rusty-teams

Files

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

general.rs

general.rs 8.3 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 .child(caption(tr("settings.caption.compose_box")))
82 .child(account_select(
83 app,
84 (
85 "settings-up-arrow",
86 tr("settings.general.up_arrow"),
87 tr("settings.general.up_arrow_detail"),
88 ),
89 &UP_ARROW,
90 app.read(cx).account_chat_settings().up_arrow,
91 ChatSettingChange::UpArrow,
92 ))
93 .child(caption(tr("settings.caption.translation")))
94 .children(translation_rows(app, cx))
95}
96
97fn translation_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {
98 let state = app.read(cx);
99 if state.translation.settings.is_none() {
100 return vec![
101 control_row(
102 tr("settings.general.translation_title"),
103 tr("settings.general.translation_not_loaded"),
104 div(),
105 )
106 .into_any_element(),
107 ];
108 }
109 let translation = &state.translation;
110 let target = translation.target();
111 let behavior = translation.behavior();
112 let targets = translation
113 .languages
114 .iter()
115 .map(|language| {
116 let (app, code) = (app.clone(), language.code.clone());
117 Choice::new(language.name.clone(), language.code == target, move |cx| {
118 app.update(cx, |state, cx| state.set_translation_target(&code, cx))
119 })
120 })
121 .collect();
122 let behavior_options = [
123 (tr("settings.general.behavior_ask"), TranslationBehavior::Ask),
124 (tr("settings.general.behavior_auto"), TranslationBehavior::Auto),
125 (
126 tr("settings.general.behavior_never"),
127 TranslationBehavior::Never,
128 ),
129 ];
130 let behavior_label = behavior_options
131 .iter()
132 .find(|(_, choice)| *choice == behavior)
133 .map_or(String::new(), |(label, _)| label.clone());
134 let behaviors = behavior_options
135 .into_iter()
136 .map(|(label, choice)| {
137 let app = app.clone();
138 Choice::new(label, behavior == choice, move |cx| {
139 app.update(cx, |state, cx| state.set_translation_behavior(choice, cx))
140 })
141 })
142 .collect();
143 vec![
144 control_row(
145 tr("settings.general.translate_into"),
146 "Read from your Teams account",
147 select(
148 "settings-translation-target",
149 translation.language_name(&target),
150 false,
151 targets,
152 ),
153 )
154 .into_any_element(),
155 control_row(
156 tr("settings.general.behavior_title"),
157 "",
158 select(
159 "settings-translation-behavior",
160 behavior_label,
161 false,
162 behaviors,
163 ),
164 )
165 .into_any_element(),
166 control_row(
167 tr("settings.general.never_translate"),
168 "",
169 never_chips(app, &target, cx),
170 )
171 .into_any_element(),
172 ]
173}
174
175fn never_chips(app: &Entity<AppState>, target: &str, cx: &App) -> Div {
176 let translation = &app.read(cx).translation;
177 let never: Vec<String> = translation
178 .display_codes()
179 .into_iter()
180 .filter(|code| code != target)
181 .collect();
182 let chips = never.iter().map(|code| {
183 let (remove_app, remove_code) = (app.clone(), code.clone());
184 h_flex()
185 .h(px(CHIP_HEIGHT))
186 .pl(px(8.))
187 .gap(px(4.))
188 .items_center()
189 .rounded(px(6.))
190 .bg(theme::surface_raised())
191 .text_size(px(12.))
192 .text_color(theme::text_strong())
193 .child(translation.language_name(code))
194 .child(
195 div()
196 .id(SharedString::from(format!("settings-never-remove-{code}")))
197 .w(px(16.))
198 .h_full()
199 .flex()
200 .items_center()
201 .justify_center()
202 .cursor_pointer()
203 .child(icon(IconName::X, 10., theme::text_faint()))
204 .on_click(move |_, _, cx| {
205 remove_app.update(cx, |state, cx| {
206 state.allow_translating_language(&remove_code, cx)
207 })
208 }),
209 )
210 });
211 let addable = translation
212 .languages
213 .iter()
214 .filter(|language| language.code != target && !never.contains(&language.code))
215 .map(|language| {
216 let (app, code) = (app.clone(), language.code.clone());
217 Choice::new(language.name.clone(), false, move |cx| {
218 app.update(cx, |state, cx| state.never_translate_language(&code, cx))
219 })
220 })
221 .collect();
222 h_flex()
223 .flex_wrap()
224 .justify_end()
225 .gap(px(6.))
226 .items_center()
227 .children(chips)
228 .child(add_language_button(addable))
229}
230
231fn add_language_button(choices: Vec<Choice>) -> impl IntoElement {
232 Button::new("settings-never-add")
233 .outline()
234 .h(px(CHIP_HEIGHT))
235 .px(px(8.))
236 .rounded(px(6.))
237 .text_size(px(12.))
238 .text_color(theme::accent_text())
239 .label(format!("+ {}", tr("common.add")))
240 .dropdown_menu(move |menu, _, _| {
241 choices.iter().fold(
242 menu.max_h(px(LANGUAGE_MENU_HEIGHT)).scrollable(true),
243 |menu, choice| {
244 let choose = choice.choose.clone();
245 menu.item(
246 PopupMenuItem::new(choice.label.clone())
247 .on_click(move |_, _, cx| choose(cx)),
248 )
249 },
250 )
251 })
252}