| 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 | .children(external_browser_rows(app, cx)) |
| 82 | .child(caption(tr("settings.caption.compose_box"))) |
| 83 | .child(account_select( |
| 84 | app, |
| 85 | ( |
| 86 | "settings-up-arrow", |
| 87 | tr("settings.general.up_arrow"), |
| 88 | tr("settings.general.up_arrow_detail"), |
| 89 | ), |
| 90 | &UP_ARROW, |
| 91 | app.read(cx).account_chat_settings().up_arrow, |
| 92 | ChatSettingChange::UpArrow, |
| 93 | )) |
| 94 | .child(caption(tr("settings.caption.translation"))) |
| 95 | .children(translation_rows(app, cx)) |
| 96 | } |
| 97 | |
| 98 | fn external_browser_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> { |
| 99 | #[cfg(not(any(target_os = "linux", target_os = "macos")))] |
| 100 | { |
| 101 | let _ = (app, cx); |
| 102 | return Vec::new(); |
| 103 | } |
| 104 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 105 | { |
| 106 | use crate::external_browser::{self, ExternalBrowser}; |
| 107 | |
| 108 | let selected = external_browser::load(&app.read(cx).store); |
| 109 | let current_label = external_browser::available_choices() |
| 110 | .iter() |
| 111 | .find(|choice| choice.id == selected.id()) |
| 112 | .map(|choice| { |
| 113 | if external_browser::is_system_id(&choice.id) { |
| 114 | tr("settings.general.external_browser_system") |
| 115 | } else { |
| 116 | choice.label.clone() |
| 117 | } |
| 118 | }) |
| 119 | .unwrap_or_else(|| tr("settings.general.external_browser_system")); |
| 120 | let choices: Vec<Choice> = external_browser::available_choices() |
| 121 | .iter() |
| 122 | .map(|choice| { |
| 123 | let label = if external_browser::is_system_id(&choice.id) { |
| 124 | tr("settings.general.external_browser_system") |
| 125 | } else { |
| 126 | choice.label.clone() |
| 127 | }; |
| 128 | let is_selected = selected.id() == choice.id; |
| 129 | let (app, id) = (app.clone(), choice.id.clone()); |
| 130 | Choice::new(label, is_selected, move |cx| { |
| 131 | app.update(cx, |state, cx| { |
| 132 | let browser = if external_browser::is_system_id(&id) { |
| 133 | ExternalBrowser::System |
| 134 | } else { |
| 135 | ExternalBrowser::Program(id.clone()) |
| 136 | }; |
| 137 | external_browser::save(&state.store, &browser); |
| 138 | cx.notify(); |
| 139 | }); |
| 140 | }) |
| 141 | }) |
| 142 | .collect(); |
| 143 | vec![ |
| 144 | caption(tr("settings.caption.browser")).into_any_element(), |
| 145 | control_row( |
| 146 | tr("settings.general.external_browser"), |
| 147 | tr("settings.general.external_browser_detail"), |
| 148 | select("settings-external-browser", current_label, false, choices), |
| 149 | ) |
| 150 | .into_any_element(), |
| 151 | ] |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | fn translation_rows(app: &Entity<AppState>, cx: &App) -> Vec<AnyElement> { |
| 156 | let state = app.read(cx); |
| 157 | if state.translation.settings.is_none() { |
| 158 | return vec![ |
| 159 | control_row( |
| 160 | tr("settings.general.translation_title"), |
| 161 | tr("settings.general.translation_not_loaded"), |
| 162 | div(), |
| 163 | ) |
| 164 | .into_any_element(), |
| 165 | ]; |
| 166 | } |
| 167 | let translation = &state.translation; |
| 168 | let target = translation.target(); |
| 169 | let behavior = translation.behavior(); |
| 170 | let targets = translation |
| 171 | .languages |
| 172 | .iter() |
| 173 | .map(|language| { |
| 174 | let (app, code) = (app.clone(), language.code.clone()); |
| 175 | Choice::new(language.name.clone(), language.code == target, move |cx| { |
| 176 | app.update(cx, |state, cx| state.set_translation_target(&code, cx)) |
| 177 | }) |
| 178 | }) |
| 179 | .collect(); |
| 180 | let behavior_options = [ |
| 181 | ( |
| 182 | tr("settings.general.behavior_ask"), |
| 183 | TranslationBehavior::Ask, |
| 184 | ), |
| 185 | ( |
| 186 | tr("settings.general.behavior_auto"), |
| 187 | TranslationBehavior::Auto, |
| 188 | ), |
| 189 | ( |
| 190 | tr("settings.general.behavior_never"), |
| 191 | TranslationBehavior::Never, |
| 192 | ), |
| 193 | ]; |
| 194 | let behavior_label = behavior_options |
| 195 | .iter() |
| 196 | .find(|(_, choice)| *choice == behavior) |
| 197 | .map_or(String::new(), |(label, _)| label.clone()); |
| 198 | let behaviors = behavior_options |
| 199 | .into_iter() |
| 200 | .map(|(label, choice)| { |
| 201 | let app = app.clone(); |
| 202 | Choice::new(label, behavior == choice, move |cx| { |
| 203 | app.update(cx, |state, cx| state.set_translation_behavior(choice, cx)) |
| 204 | }) |
| 205 | }) |
| 206 | .collect(); |
| 207 | vec![ |
| 208 | control_row( |
| 209 | tr("settings.general.translate_into"), |
| 210 | "Read from your Teams account", |
| 211 | select( |
| 212 | "settings-translation-target", |
| 213 | translation.language_name(&target), |
| 214 | false, |
| 215 | targets, |
| 216 | ), |
| 217 | ) |
| 218 | .into_any_element(), |
| 219 | control_row( |
| 220 | tr("settings.general.behavior_title"), |
| 221 | "", |
| 222 | select( |
| 223 | "settings-translation-behavior", |
| 224 | behavior_label, |
| 225 | false, |
| 226 | behaviors, |
| 227 | ), |
| 228 | ) |
| 229 | .into_any_element(), |
| 230 | control_row( |
| 231 | tr("settings.general.never_translate"), |
| 232 | "", |
| 233 | never_chips(app, &target, cx), |
| 234 | ) |
| 235 | .into_any_element(), |
| 236 | ] |
| 237 | } |
| 238 | |
| 239 | fn never_chips(app: &Entity<AppState>, target: &str, cx: &App) -> Div { |
| 240 | let translation = &app.read(cx).translation; |
| 241 | let never: Vec<String> = translation |
| 242 | .display_codes() |
| 243 | .into_iter() |
| 244 | .filter(|code| code != target) |
| 245 | .collect(); |
| 246 | let chips = never.iter().map(|code| { |
| 247 | let (remove_app, remove_code) = (app.clone(), code.clone()); |
| 248 | h_flex() |
| 249 | .h(px(CHIP_HEIGHT)) |
| 250 | .pl(px(8.)) |
| 251 | .gap(px(4.)) |
| 252 | .items_center() |
| 253 | .rounded(px(6.)) |
| 254 | .bg(theme::surface_raised()) |
| 255 | .text_size(px(12.)) |
| 256 | .text_color(theme::text_strong()) |
| 257 | .child(translation.language_name(code)) |
| 258 | .child( |
| 259 | div() |
| 260 | .id(SharedString::from(format!("settings-never-remove-{code}"))) |
| 261 | .w(px(16.)) |
| 262 | .h_full() |
| 263 | .flex() |
| 264 | .items_center() |
| 265 | .justify_center() |
| 266 | .cursor_pointer() |
| 267 | .child(icon(IconName::X, 10., theme::text_faint())) |
| 268 | .on_click(move |_, _, cx| { |
| 269 | remove_app.update(cx, |state, cx| { |
| 270 | state.allow_translating_language(&remove_code, cx) |
| 271 | }) |
| 272 | }), |
| 273 | ) |
| 274 | }); |
| 275 | let addable = translation |
| 276 | .languages |
| 277 | .iter() |
| 278 | .filter(|language| language.code != target && !never.contains(&language.code)) |
| 279 | .map(|language| { |
| 280 | let (app, code) = (app.clone(), language.code.clone()); |
| 281 | Choice::new(language.name.clone(), false, move |cx| { |
| 282 | app.update(cx, |state, cx| state.never_translate_language(&code, cx)) |
| 283 | }) |
| 284 | }) |
| 285 | .collect(); |
| 286 | h_flex() |
| 287 | .flex_wrap() |
| 288 | .justify_end() |
| 289 | .gap(px(6.)) |
| 290 | .items_center() |
| 291 | .children(chips) |
| 292 | .child(add_language_button(addable)) |
| 293 | } |
| 294 | |
| 295 | fn add_language_button(choices: Vec<Choice>) -> impl IntoElement { |
| 296 | Button::new("settings-never-add") |
| 297 | .outline() |
| 298 | .h(px(CHIP_HEIGHT)) |
| 299 | .px(px(8.)) |
| 300 | .rounded(px(6.)) |
| 301 | .text_size(px(12.)) |
| 302 | .text_color(theme::accent_text()) |
| 303 | .label(format!("+ {}", tr("common.add"))) |
| 304 | .dropdown_menu(move |menu, _, _| { |
| 305 | choices.iter().fold( |
| 306 | menu.max_h(px(LANGUAGE_MENU_HEIGHT)).scrollable(true), |
| 307 | |menu, choice| { |
| 308 | let choose = choice.choose.clone(); |
| 309 | menu.item( |
| 310 | PopupMenuItem::new(choice.label.clone()) |
| 311 | .on_click(move |_, _, cx| choose(cx)), |
| 312 | ) |
| 313 | }, |
| 314 | ) |
| 315 | }) |
| 316 | } |