BlueGit

ict rusty-teams

Files

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

general.rs

general.rs 8.1 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::notify::NotificationCenter;
16use crate::theme;
17use crate::views::widgets::icon;
18
19const BEHAVIORS: [(&str, TranslationBehavior); 3] = [
20 ("Ask me before translating", TranslationBehavior::Ask),
21 ("Auto-translate all messages", TranslationBehavior::Auto),
22 ("Never translate", TranslationBehavior::Never),
23];
24const CHIP_HEIGHT: f32 = 24.;
25const DISABLED_OPACITY: f32 = 0.4;
26const LANGUAGE_MENU_HEIGHT: f32 = 320.;
27
28fn startup_rows(center: &Entity<NotificationCenter>, cx: &App) -> [Stateful<Div>; 2] {
29 let startup = center.read(cx).startup();
30 let (auto_start_center, background_center) = (center.clone(), center.clone());
31 [
32 toggle_row(
33 "settings-auto-start",
34 "Auto-start Teams",
35 "",
36 theme::text_muted(),
37 startup.auto_start,
38 )
39 .on_click(move |_, _, cx| {
40 auto_start_center.update(cx, |center, cx| {
41 center.set_auto_start(!startup.auto_start, cx)
42 })
43 }),
44 toggle_row(
45 "settings-open-in-background",
46 "Open application in background",
47 "",
48 theme::text_muted(),
49 startup.open_in_background,
50 )
51 .when(!startup.auto_start, |row| row.opacity(DISABLED_OPACITY))
52 .when(startup.auto_start, |row| {
53 row.on_click(move |_, _, cx| {
54 background_center.update(cx, |center, cx| {
55 center.set_open_in_background(!startup.open_in_background, cx)
56 })
57 })
58 }),
59 ]
60}
61
62pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &App) -> Div {
63 let close_to_tray = center.read(cx).settings().close_to_tray;
64 let toggle_center = center.clone();
65 v_flex()
66 .w_full()
67 .child(caption("SYSTEM"))
68 .children(startup_rows(center, cx))
69 .child(
70 toggle_row(
71 "settings-close-to-tray",
72 "On close, keep the application running",
73 "The title bar X hides the window to the tray",
74 theme::text_muted(),
75 close_to_tray,
76 )
77 .on_click(move |_, _, cx| {
78 toggle_center.update(cx, |center, cx| {
79 center.update_settings(cx, |settings| {
80 settings.close_to_tray = !settings.close_to_tray
81 })
82 })
83 }),
84 )
85 .child(caption("COMPOSE BOX"))
86 .child(account_select(
87 app,
88 (
89 "settings-up-arrow",
90 "Up arrow key behaviour in compose box",
91 "Choose what happens when you press Up arrow at the beginning of the compose box.",
92 ),
93 &UP_ARROW,
94 app.read(cx).account_chat_settings().up_arrow,
95 ChatSettingChange::UpArrow,
96 ))
97 .child(caption("TRANSLATION"))
98 .children(translation_rows(app, cx))
99}
100
101fn translation_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> {
102 let state = app.read(cx);
103 if state.translation.settings.is_none() {
104 return vec![
105 control_row("Translation", "Translation settings not loaded yet", div())
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 behaviors = BEHAVIORS
123 .into_iter()
124 .map(|(label, choice)| {
125 let app = app.clone();
126 Choice::new(label, behavior == choice, move |cx| {
127 app.update(cx, |state, cx| state.set_translation_behavior(choice, cx))
128 })
129 })
130 .collect();
131 let behavior_label = BEHAVIORS
132 .iter()
133 .find(|(_, choice)| *choice == behavior)
134 .map_or("", |(label, _)| label);
135 vec![
136 control_row(
137 "Translate messages into this language",
138 "Read from your Teams account",
139 select(
140 "settings-translation-target",
141 translation.language_name(&target),
142 false,
143 targets,
144 ),
145 )
146 .into_any_element(),
147 control_row(
148 "How to handle messages in other languages",
149 "",
150 select(
151 "settings-translation-behavior",
152 behavior_label,
153 false,
154 behaviors,
155 ),
156 )
157 .into_any_element(),
158 control_row(
159 "Never translate messages in these languages",
160 "",
161 never_chips(app, &target, cx),
162 )
163 .into_any_element(),
164 ]
165}
166
167fn never_chips(app: &Entity<AppState>, target: &str, cx: &App) -> Div {
168 let translation = &app.read(cx).translation;
169 let never: Vec<String> = translation
170 .display_codes()
171 .into_iter()
172 .filter(|code| code != target)
173 .collect();
174 let chips = never.iter().map(|code| {
175 let (remove_app, remove_code) = (app.clone(), code.clone());
176 h_flex()
177 .h(px(CHIP_HEIGHT))
178 .pl(px(8.))
179 .gap(px(4.))
180 .items_center()
181 .rounded(px(6.))
182 .bg(theme::surface_raised())
183 .text_size(px(12.))
184 .text_color(theme::text_strong())
185 .child(translation.language_name(code))
186 .child(
187 div()
188 .id(SharedString::from(format!("settings-never-remove-{code}")))
189 .w(px(16.))
190 .h_full()
191 .flex()
192 .items_center()
193 .justify_center()
194 .cursor_pointer()
195 .child(icon(IconName::X, 10., theme::text_faint()))
196 .on_click(move |_, _, cx| {
197 remove_app.update(cx, |state, cx| {
198 state.allow_translating_language(&remove_code, cx)
199 })
200 }),
201 )
202 });
203 let addable = translation
204 .languages
205 .iter()
206 .filter(|language| language.code != target && !never.contains(&language.code))
207 .map(|language| {
208 let (app, code) = (app.clone(), language.code.clone());
209 Choice::new(language.name.clone(), false, move |cx| {
210 app.update(cx, |state, cx| state.never_translate_language(&code, cx))
211 })
212 })
213 .collect();
214 h_flex()
215 .flex_wrap()
216 .justify_end()
217 .gap(px(6.))
218 .items_center()
219 .children(chips)
220 .child(add_language_button(addable))
221}
222
223fn add_language_button(choices: Vec<Choice>) -> impl IntoElement {
224 Button::new("settings-never-add")
225 .outline()
226 .h(px(CHIP_HEIGHT))
227 .px(px(8.))
228 .rounded(px(6.))
229 .text_size(px(12.))
230 .text_color(theme::accent_text())
231 .label("+ Add language")
232 .dropdown_menu(move |menu, _, _| {
233 choices.iter().fold(
234 menu.max_h(px(LANGUAGE_MENU_HEIGHT)).scrollable(true),
235 |menu, choice| {
236 let choose = choice.choose.clone();
237 menu.item(
238 PopupMenuItem::new(choice.label.clone())
239 .on_click(move |_, _, cx| choose(cx)),
240 )
241 },
242 )
243 })
244}