BlueGit

ict rusty-teams

Files

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

general.rs

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