| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::{ |
| 3 | button::Button, |
| 4 | h_flex, |
| 5 | menu::{DropdownMenu as _, PopupMenuItem}, |
| 6 | v_flex, |
| 7 | }; |
| 8 | use gpui_kit::prelude::FluentBuilder as _; |
| 9 | use gpui_kit::*; |
| 10 | use teams_core::{ChatSettingChange, TranslationBehavior}; |
| 11 | |
| 12 | use super::account::{UP_ARROW, account_select}; |
| 13 | use super::rows::{Choice, caption, control_row, select, toggle_row}; |
| 14 | use crate::app_state::AppState; |
| 15 | use crate::i18n::tr; |
| 16 | use crate::notify::NotificationCenter; |
| 17 | use crate::theme; |
| 18 | use crate::views::widgets::icon; |
| 19 | |
| 20 | const CHIP_HEIGHT: f32 = 24.; |
| 21 | const DISABLED_OPACITY: f32 = 0.4; |
| 22 | const LANGUAGE_MENU_HEIGHT: f32 = 320.; |
| 23 | |
| 24 | fn 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 | |
| 58 | pub 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 | |
| 97 | fn 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 | ( |
| 124 | tr("settings.general.behavior_ask"), |
| 125 | TranslationBehavior::Ask, |
| 126 | ), |
| 127 | ( |
| 128 | tr("settings.general.behavior_auto"), |
| 129 | TranslationBehavior::Auto, |
| 130 | ), |
| 131 | ( |
| 132 | tr("settings.general.behavior_never"), |
| 133 | TranslationBehavior::Never, |
| 134 | ), |
| 135 | ]; |
| 136 | let behavior_label = behavior_options |
| 137 | .iter() |
| 138 | .find(|(_, choice)| *choice == behavior) |
| 139 | .map_or(String::new(), |(label, _)| label.clone()); |
| 140 | let behaviors = behavior_options |
| 141 | .into_iter() |
| 142 | .map(|(label, choice)| { |
| 143 | let app = app.clone(); |
| 144 | Choice::new(label, behavior == choice, move |cx| { |
| 145 | app.update(cx, |state, cx| state.set_translation_behavior(choice, cx)) |
| 146 | }) |
| 147 | }) |
| 148 | .collect(); |
| 149 | vec![ |
| 150 | control_row( |
| 151 | tr("settings.general.translate_into"), |
| 152 | "Read from your Teams account", |
| 153 | select( |
| 154 | "settings-translation-target", |
| 155 | translation.language_name(&target), |
| 156 | false, |
| 157 | targets, |
| 158 | ), |
| 159 | ) |
| 160 | .into_any_element(), |
| 161 | control_row( |
| 162 | tr("settings.general.behavior_title"), |
| 163 | "", |
| 164 | select( |
| 165 | "settings-translation-behavior", |
| 166 | behavior_label, |
| 167 | false, |
| 168 | behaviors, |
| 169 | ), |
| 170 | ) |
| 171 | .into_any_element(), |
| 172 | control_row( |
| 173 | tr("settings.general.never_translate"), |
| 174 | "", |
| 175 | never_chips(app, &target, cx), |
| 176 | ) |
| 177 | .into_any_element(), |
| 178 | ] |
| 179 | } |
| 180 | |
| 181 | fn never_chips(app: &Entity<AppState>, target: &str, cx: &App) -> Div { |
| 182 | let translation = &app.read(cx).translation; |
| 183 | let never: Vec<String> = translation |
| 184 | .display_codes() |
| 185 | .into_iter() |
| 186 | .filter(|code| code != target) |
| 187 | .collect(); |
| 188 | let chips = never.iter().map(|code| { |
| 189 | let (remove_app, remove_code) = (app.clone(), code.clone()); |
| 190 | h_flex() |
| 191 | .h(px(CHIP_HEIGHT)) |
| 192 | .pl(px(8.)) |
| 193 | .gap(px(4.)) |
| 194 | .items_center() |
| 195 | .rounded(px(6.)) |
| 196 | .bg(theme::surface_raised()) |
| 197 | .text_size(px(12.)) |
| 198 | .text_color(theme::text_strong()) |
| 199 | .child(translation.language_name(code)) |
| 200 | .child( |
| 201 | div() |
| 202 | .id(SharedString::from(format!("settings-never-remove-{code}"))) |
| 203 | .w(px(16.)) |
| 204 | .h_full() |
| 205 | .flex() |
| 206 | .items_center() |
| 207 | .justify_center() |
| 208 | .cursor_pointer() |
| 209 | .child(icon(IconName::X, 10., theme::text_faint())) |
| 210 | .on_click(move |_, _, cx| { |
| 211 | remove_app.update(cx, |state, cx| { |
| 212 | state.allow_translating_language(&remove_code, cx) |
| 213 | }) |
| 214 | }), |
| 215 | ) |
| 216 | }); |
| 217 | let addable = translation |
| 218 | .languages |
| 219 | .iter() |
| 220 | .filter(|language| language.code != target && !never.contains(&language.code)) |
| 221 | .map(|language| { |
| 222 | let (app, code) = (app.clone(), language.code.clone()); |
| 223 | Choice::new(language.name.clone(), false, move |cx| { |
| 224 | app.update(cx, |state, cx| state.never_translate_language(&code, cx)) |
| 225 | }) |
| 226 | }) |
| 227 | .collect(); |
| 228 | h_flex() |
| 229 | .flex_wrap() |
| 230 | .justify_end() |
| 231 | .gap(px(6.)) |
| 232 | .items_center() |
| 233 | .children(chips) |
| 234 | .child(add_language_button(addable)) |
| 235 | } |
| 236 | |
| 237 | fn add_language_button(choices: Vec<Choice>) -> impl IntoElement { |
| 238 | Button::new("settings-never-add") |
| 239 | .outline() |
| 240 | .h(px(CHIP_HEIGHT)) |
| 241 | .px(px(8.)) |
| 242 | .rounded(px(6.)) |
| 243 | .text_size(px(12.)) |
| 244 | .text_color(theme::accent_text()) |
| 245 | .label(format!("+ {}", tr("common.add"))) |
| 246 | .dropdown_menu(move |menu, _, _| { |
| 247 | choices.iter().fold( |
| 248 | menu.max_h(px(LANGUAGE_MENU_HEIGHT)).scrollable(true), |
| 249 | |menu, choice| { |
| 250 | let choose = choice.choose.clone(); |
| 251 | menu.item( |
| 252 | PopupMenuItem::new(choice.label.clone()) |
| 253 | .on_click(move |_, _, cx| choose(cx)), |
| 254 | ) |
| 255 | }, |
| 256 | ) |
| 257 | }) |
| 258 | } |