| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui_kit::assets::IconName; |
| 4 | use gpui_kit::component::{ |
| 5 | ActiveTheme as _, |
| 6 | button::{Button, ButtonVariants as _}, |
| 7 | h_flex, |
| 8 | input::{Input, InputState}, |
| 9 | menu::{DropdownMenu as _, PopupMenuItem}, |
| 10 | v_flex, |
| 11 | }; |
| 12 | use gpui_kit::prelude::FluentBuilder as _; |
| 13 | use gpui_kit::*; |
| 14 | |
| 15 | use app_base::theme; |
| 16 | |
| 17 | pub const PAGE_PADDING: f32 = 20.; |
| 18 | const FIELD_HEIGHT: f32 = 32.; |
| 19 | const MONO_SIZE: f32 = 11.5; |
| 20 | |
| 21 | pub type Choice = Rc<dyn Fn(&mut Window, &mut App)>; |
| 22 | |
| 23 | pub struct MenuEntry { |
| 24 | pub label: String, |
| 25 | pub selected: bool, |
| 26 | pub choose: Choice, |
| 27 | } |
| 28 | |
| 29 | pub fn field( |
| 30 | label: &'static str, |
| 31 | control: impl IntoElement, |
| 32 | hint: Option<AnyElement>, |
| 33 | problem: Option<String>, |
| 34 | ) -> Div { |
| 35 | v_flex() |
| 36 | .w_full() |
| 37 | .gap(px(6.)) |
| 38 | .child( |
| 39 | div() |
| 40 | .text_size(px(12.)) |
| 41 | .font_weight(FontWeight::SEMIBOLD) |
| 42 | .text_color(theme::text_soft()) |
| 43 | .child(label), |
| 44 | ) |
| 45 | .child(control) |
| 46 | .children(hint) |
| 47 | .children(problem.map(|problem| { |
| 48 | div() |
| 49 | .text_size(px(12.)) |
| 50 | .text_color(theme::red_soft()) |
| 51 | .child(problem) |
| 52 | })) |
| 53 | } |
| 54 | |
| 55 | pub fn hint(text: &'static str) -> AnyElement { |
| 56 | div() |
| 57 | .text_size(px(11.)) |
| 58 | .text_color(theme::text_faint()) |
| 59 | .child(text) |
| 60 | .into_any_element() |
| 61 | } |
| 62 | |
| 63 | pub fn text_field(state: &Entity<InputState>, mono: bool, invalid: bool, cx: &App) -> Div { |
| 64 | div() |
| 65 | .w_full() |
| 66 | .h(px(FIELD_HEIGHT)) |
| 67 | .px(px(12.)) |
| 68 | .flex() |
| 69 | .items_center() |
| 70 | .overflow_hidden() |
| 71 | .rounded(px(8.)) |
| 72 | .bg(theme::surface_raised()) |
| 73 | .border_1() |
| 74 | .border_color(if invalid { |
| 75 | theme::red_soft() |
| 76 | } else { |
| 77 | theme::surface_raised() |
| 78 | }) |
| 79 | .when(mono, |field| { |
| 80 | field |
| 81 | .font_family(cx.theme().mono_font_family.clone()) |
| 82 | .text_size(px(MONO_SIZE)) |
| 83 | }) |
| 84 | .child(Input::new(state).appearance(false).bordered(false)) |
| 85 | } |
| 86 | |
| 87 | pub fn boxed(content: impl IntoElement, invalid: bool, cx: &App) -> Div { |
| 88 | div() |
| 89 | .w_full() |
| 90 | .px(px(12.)) |
| 91 | .py(px(8.)) |
| 92 | .rounded(px(8.)) |
| 93 | .bg(theme::surface_raised()) |
| 94 | .border_1() |
| 95 | .border_color(if invalid { |
| 96 | theme::red_soft() |
| 97 | } else { |
| 98 | theme::surface_raised() |
| 99 | }) |
| 100 | .font_family(cx.theme().mono_font_family.clone()) |
| 101 | .text_size(px(MONO_SIZE)) |
| 102 | .child(content) |
| 103 | } |
| 104 | |
| 105 | pub fn raised_button(id: &'static str, label: &'static str) -> Stateful<Div> { |
| 106 | h_flex() |
| 107 | .id(id) |
| 108 | .h(px(FIELD_HEIGHT)) |
| 109 | .px(px(12.)) |
| 110 | .items_center() |
| 111 | .rounded(px(8.)) |
| 112 | .cursor_pointer() |
| 113 | .bg(theme::surface_raised()) |
| 114 | .hover(|button| button.bg(theme::popup_hover())) |
| 115 | .text_size(px(13.)) |
| 116 | .child(label) |
| 117 | } |
| 118 | |
| 119 | pub fn dropdown(id: &'static str, label: String, entries: Vec<MenuEntry>) -> impl IntoElement { |
| 120 | Button::new(id) |
| 121 | .outline() |
| 122 | .w_full() |
| 123 | .label(label) |
| 124 | .icon(IconName::ChevronDown) |
| 125 | .dropdown_menu(move |menu, _, _| { |
| 126 | entries.iter().fold(menu, |menu, entry| { |
| 127 | let choose = entry.choose.clone(); |
| 128 | menu.item( |
| 129 | PopupMenuItem::new(entry.label.clone()) |
| 130 | .checked(entry.selected) |
| 131 | .on_click(move |_, window, cx| choose(window, cx)), |
| 132 | ) |
| 133 | }) |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | pub fn link_dropdown( |
| 138 | id: &'static str, |
| 139 | label: &'static str, |
| 140 | entries: Vec<MenuEntry>, |
| 141 | ) -> impl IntoElement { |
| 142 | Button::new(id) |
| 143 | .link() |
| 144 | .label(label) |
| 145 | .dropdown_menu(move |menu, _, _| { |
| 146 | entries.iter().fold(menu, |menu, entry| { |
| 147 | let choose = entry.choose.clone(); |
| 148 | menu.item( |
| 149 | PopupMenuItem::new(entry.label.clone()) |
| 150 | .on_click(move |_, window, cx| choose(window, cx)), |
| 151 | ) |
| 152 | }) |
| 153 | }) |
| 154 | } |
| 155 | |
| 156 | pub fn section_header(title: &'static str, trailing: Option<AnyElement>) -> Div { |
| 157 | h_flex() |
| 158 | .w_full() |
| 159 | .items_center() |
| 160 | .justify_between() |
| 161 | .px(px(PAGE_PADDING)) |
| 162 | .pt(px(16.)) |
| 163 | .pb(px(6.)) |
| 164 | .text_size(px(11.)) |
| 165 | .font_weight(FontWeight::SEMIBOLD) |
| 166 | .text_color(theme::text_faint()) |
| 167 | .child(title) |
| 168 | .children(trailing) |
| 169 | } |