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