| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui_kit::assets::IconName; |
| 4 | use gpui_kit::base::Disableable as _; |
| 5 | use gpui_kit::component::{ |
| 6 | button::Button, |
| 7 | h_flex, |
| 8 | menu::{DropdownMenu as _, PopupMenuItem}, |
| 9 | v_flex, |
| 10 | }; |
| 11 | use gpui_kit::prelude::FluentBuilder as _; |
| 12 | use gpui_kit::*; |
| 13 | |
| 14 | use crate::theme; |
| 15 | use crate::views::widgets::icon; |
| 16 | |
| 17 | pub const ROW_INSET: f32 = 12.; |
| 18 | pub const SELECT_WIDTH: f32 = 216.; |
| 19 | const ROW_PADDING: f32 = 12.; |
| 20 | const ROW_GAP: f32 = 12.; |
| 21 | const ROW_RADIUS: f32 = 8.; |
| 22 | const BUTTON_HEIGHT: f32 = 32.; |
| 23 | const BUTTON_RADIUS: f32 = 8.; |
| 24 | const SCROLL_MENU_AFTER: usize = 10; |
| 25 | const MENU_MAX_HEIGHT: f32 = 320.; |
| 26 | |
| 27 | pub struct Choice { |
| 28 | pub label: SharedString, |
| 29 | pub checked: bool, |
| 30 | pub choose: Rc<dyn Fn(&mut App)>, |
| 31 | } |
| 32 | |
| 33 | impl Choice { |
| 34 | pub fn new( |
| 35 | label: impl Into<SharedString>, |
| 36 | checked: bool, |
| 37 | choose: impl Fn(&mut App) + 'static, |
| 38 | ) -> Self { |
| 39 | Choice { |
| 40 | label: label.into(), |
| 41 | checked, |
| 42 | choose: Rc::new(choose), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub fn switch(on: bool) -> Div { |
| 48 | div() |
| 49 | .w(px(36.)) |
| 50 | .h(px(20.)) |
| 51 | .flex_none() |
| 52 | .rounded_full() |
| 53 | .relative() |
| 54 | .bg(if on { |
| 55 | theme::accent() |
| 56 | } else { |
| 57 | theme::border_strong() |
| 58 | }) |
| 59 | .child( |
| 60 | div() |
| 61 | .absolute() |
| 62 | .top(px(2.)) |
| 63 | .left(px(if on { 18. } else { 2. })) |
| 64 | .size(px(16.)) |
| 65 | .rounded_full() |
| 66 | .bg(theme::text()), |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | pub fn titled( |
| 71 | title: impl Into<SharedString>, |
| 72 | detail: impl Into<SharedString>, |
| 73 | detail_color: Hsla, |
| 74 | ) -> Div { |
| 75 | let detail = detail.into(); |
| 76 | v_flex() |
| 77 | .flex_1() |
| 78 | .min_w(px(0.)) |
| 79 | .gap(px(2.)) |
| 80 | .child( |
| 81 | div() |
| 82 | .text_size(px(14.)) |
| 83 | .line_height(px(20.)) |
| 84 | .font_weight(FontWeight::SEMIBOLD) |
| 85 | .text_color(theme::text()) |
| 86 | .child(title.into()), |
| 87 | ) |
| 88 | .when(!detail.is_empty(), |column| { |
| 89 | column.child( |
| 90 | div() |
| 91 | .text_size(px(12.)) |
| 92 | .line_height(px(16.)) |
| 93 | .text_color(detail_color) |
| 94 | .child(detail), |
| 95 | ) |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | pub fn row() -> Div { |
| 100 | h_flex() |
| 101 | .w_full() |
| 102 | .px(px(ROW_INSET)) |
| 103 | .py(px(ROW_PADDING)) |
| 104 | .gap(px(ROW_GAP)) |
| 105 | .items_center() |
| 106 | .rounded(px(ROW_RADIUS)) |
| 107 | } |
| 108 | |
| 109 | pub fn control_row( |
| 110 | title: impl Into<SharedString>, |
| 111 | detail: impl Into<SharedString>, |
| 112 | control: impl IntoElement, |
| 113 | ) -> Div { |
| 114 | row() |
| 115 | .child(titled(title, detail, theme::text_muted())) |
| 116 | .child(control) |
| 117 | } |
| 118 | |
| 119 | pub fn toggle_row( |
| 120 | id: impl Into<ElementId>, |
| 121 | title: impl Into<SharedString>, |
| 122 | detail: impl Into<SharedString>, |
| 123 | detail_color: Hsla, |
| 124 | on: bool, |
| 125 | ) -> Stateful<Div> { |
| 126 | row() |
| 127 | .id(id) |
| 128 | .cursor_pointer() |
| 129 | .hover(|row| row.bg(theme::popup_hover())) |
| 130 | .child(titled(title, detail, detail_color)) |
| 131 | .child(switch(on)) |
| 132 | } |
| 133 | |
| 134 | pub fn caption(label: impl Into<SharedString>) -> Div { |
| 135 | div() |
| 136 | .w_full() |
| 137 | .px(px(ROW_INSET)) |
| 138 | .pt(px(18.)) |
| 139 | .pb(px(6.)) |
| 140 | .text_size(px(12.)) |
| 141 | .font_weight(FontWeight::SEMIBOLD) |
| 142 | .text_color(theme::text_muted()) |
| 143 | .child(label.into()) |
| 144 | } |
| 145 | |
| 146 | pub fn text_button(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Stateful<Div> { |
| 147 | div() |
| 148 | .id(id) |
| 149 | .h(px(BUTTON_HEIGHT)) |
| 150 | .px(px(14.)) |
| 151 | .flex() |
| 152 | .flex_none() |
| 153 | .items_center() |
| 154 | .rounded(px(BUTTON_RADIUS)) |
| 155 | .border_1() |
| 156 | .border_color(theme::border_strong()) |
| 157 | .cursor_pointer() |
| 158 | .text_size(px(13.)) |
| 159 | .font_weight(FontWeight::SEMIBOLD) |
| 160 | .text_color(theme::text()) |
| 161 | .hover(|button| button.bg(theme::popup_hover())) |
| 162 | .child(label.into()) |
| 163 | } |
| 164 | |
| 165 | pub fn select_selector(id: &str) -> String { |
| 166 | format!("settings-select/{id}") |
| 167 | } |
| 168 | |
| 169 | pub fn select( |
| 170 | id: impl Into<ElementId>, |
| 171 | label: impl Into<SharedString>, |
| 172 | disabled: bool, |
| 173 | choices: Vec<Choice>, |
| 174 | ) -> impl IntoElement { |
| 175 | select_with_width(id, label, SELECT_WIDTH, disabled, choices) |
| 176 | } |
| 177 | |
| 178 | pub fn select_with_width( |
| 179 | id: impl Into<ElementId>, |
| 180 | label: impl Into<SharedString>, |
| 181 | width: f32, |
| 182 | disabled: bool, |
| 183 | choices: Vec<Choice>, |
| 184 | ) -> impl IntoElement { |
| 185 | let scrollable = choices.len() > SCROLL_MENU_AFTER; |
| 186 | let id: ElementId = id.into(); |
| 187 | Button::new(id.clone()) |
| 188 | .debug_selector(|| select_selector(&id.to_string())) |
| 189 | .outline() |
| 190 | .w(px(width)) |
| 191 | .flex_none() |
| 192 | .disabled(disabled) |
| 193 | .child( |
| 194 | h_flex() |
| 195 | .w_full() |
| 196 | .min_w_0() |
| 197 | .gap(px(8.)) |
| 198 | .items_center() |
| 199 | .justify_between() |
| 200 | .child(div().flex_1().min_w_0().truncate().child(label.into())) |
| 201 | .child(icon(IconName::ChevronDown, 12., theme::text_muted())), |
| 202 | ) |
| 203 | .dropdown_menu(move |menu, _, _| { |
| 204 | let menu = if scrollable { |
| 205 | menu.max_h(px(MENU_MAX_HEIGHT)).scrollable(true) |
| 206 | } else { |
| 207 | menu |
| 208 | }; |
| 209 | choices.iter().fold(menu, |menu, choice| { |
| 210 | let choose = choice.choose.clone(); |
| 211 | menu.item( |
| 212 | PopupMenuItem::new(choice.label.clone()) |
| 213 | .checked(choice.checked) |
| 214 | .on_click(move |_, _, cx| choose(cx)), |
| 215 | ) |
| 216 | }) |
| 217 | }) |
| 218 | .listbox() |
| 219 | } |