| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::{ |
| 3 | button::{Button, ButtonVariants as _}, |
| 4 | h_flex, |
| 5 | menu::{DropdownMenu as _, PopupMenu, PopupMenuItem}, |
| 6 | }; |
| 7 | use gpui_kit::prelude::FluentBuilder as _; |
| 8 | use gpui_kit::*; |
| 9 | |
| 10 | use super::settings_controls::Choice; |
| 11 | use crate::theme; |
| 12 | use crate::views::widgets::icon; |
| 13 | |
| 14 | pub const LABEL_WIDTH: f32 = 92.; |
| 15 | const SELECT_HEIGHT: f32 = 32.; |
| 16 | const MENU_HEIGHT: f32 = 320.; |
| 17 | const MENU_WIDTH: f32 = 380.; |
| 18 | const ROW_WIDTH: f32 = MENU_WIDTH - 56.; |
| 19 | const DOT_SIZE: f32 = 6.; |
| 20 | |
| 21 | #[derive(Clone)] |
| 22 | pub struct MenuRow { |
| 23 | pub label: String, |
| 24 | pub meta: Option<String>, |
| 25 | pub running: bool, |
| 26 | pub selected: bool, |
| 27 | pub choose: Choice, |
| 28 | } |
| 29 | |
| 30 | #[derive(Clone)] |
| 31 | pub enum MenuPart { |
| 32 | Header(String), |
| 33 | Separator, |
| 34 | Row(MenuRow), |
| 35 | } |
| 36 | |
| 37 | pub fn picker_row(label: String, control: impl IntoElement) -> Div { |
| 38 | h_flex() |
| 39 | .w_full() |
| 40 | .items_center() |
| 41 | .gap(px(12.)) |
| 42 | .child( |
| 43 | div() |
| 44 | .w(px(LABEL_WIDTH)) |
| 45 | .flex_none() |
| 46 | .text_size(px(11.)) |
| 47 | .font_weight(FontWeight::SEMIBOLD) |
| 48 | .text_color(theme::text_faint()) |
| 49 | .child(label), |
| 50 | ) |
| 51 | .child(div().flex_1().min_w(px(0.)).child(control)) |
| 52 | } |
| 53 | |
| 54 | fn meta_text(meta: String, running: bool) -> Div { |
| 55 | h_flex() |
| 56 | .flex_none() |
| 57 | .items_center() |
| 58 | .gap(px(4.)) |
| 59 | .text_size(px(11.)) |
| 60 | .text_color(theme::text_faint()) |
| 61 | .when(running, |meta_row| { |
| 62 | meta_row.child( |
| 63 | div() |
| 64 | .size(px(DOT_SIZE)) |
| 65 | .rounded_full() |
| 66 | .bg(theme::accent_text()), |
| 67 | ) |
| 68 | }) |
| 69 | .child(meta) |
| 70 | } |
| 71 | |
| 72 | fn select_body(value: String, meta: Option<String>, running: bool, chevron: bool) -> Div { |
| 73 | h_flex() |
| 74 | .w_full() |
| 75 | .items_center() |
| 76 | .gap(px(8.)) |
| 77 | .child( |
| 78 | div() |
| 79 | .flex_1() |
| 80 | .min_w(px(0.)) |
| 81 | .truncate() |
| 82 | .text_size(px(13.)) |
| 83 | .child(value), |
| 84 | ) |
| 85 | .children(meta.map(|meta| meta_text(meta, running))) |
| 86 | .when(chevron, |body| { |
| 87 | body.child(icon(IconName::ChevronDown, 14., theme::text_muted())) |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | fn menu_from(menu: PopupMenu, parts: &[MenuPart]) -> PopupMenu { |
| 92 | parts.iter().fold(menu, |menu, part| match part { |
| 93 | MenuPart::Header(text) => menu.item(PopupMenuItem::label(text.clone())), |
| 94 | MenuPart::Separator => menu.item(PopupMenuItem::separator()), |
| 95 | MenuPart::Row(row) => { |
| 96 | let choose = row.choose.clone(); |
| 97 | let (label, meta, running) = (row.label.clone(), row.meta.clone(), row.running); |
| 98 | menu.item( |
| 99 | PopupMenuItem::element(move |_, _| { |
| 100 | select_body(label.clone(), meta.clone(), running, false).w(px(ROW_WIDTH)) |
| 101 | }) |
| 102 | .checked(row.selected) |
| 103 | .on_click(move |_, window, cx| choose(window, cx)), |
| 104 | ) |
| 105 | } |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | pub fn select( |
| 110 | id: &'static str, |
| 111 | value: String, |
| 112 | meta: Option<String>, |
| 113 | running: bool, |
| 114 | parts: Vec<MenuPart>, |
| 115 | ) -> impl IntoElement { |
| 116 | Button::new(id) |
| 117 | .ghost() |
| 118 | .w_full() |
| 119 | .h(px(SELECT_HEIGHT)) |
| 120 | .px(px(12.)) |
| 121 | .rounded(px(8.)) |
| 122 | .bg(theme::surface_raised()) |
| 123 | .child(select_body(value, meta, running, true)) |
| 124 | .dropdown_menu(move |menu, _, _| { |
| 125 | menu_from( |
| 126 | menu.min_w(px(MENU_WIDTH)) |
| 127 | .max_w(px(MENU_WIDTH)) |
| 128 | .max_h(px(MENU_HEIGHT)) |
| 129 | .scrollable(true), |
| 130 | &parts, |
| 131 | ) |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | pub fn locked_select(value: String, meta: &'static str) -> Div { |
| 136 | h_flex() |
| 137 | .w_full() |
| 138 | .h(px(SELECT_HEIGHT)) |
| 139 | .px(px(12.)) |
| 140 | .items_center() |
| 141 | .rounded(px(8.)) |
| 142 | .bg(theme::surface_raised()) |
| 143 | .text_color(theme::text_faint()) |
| 144 | .child(select_body(value, Some(meta.to_owned()), false, false)) |
| 145 | } |