BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-ui-kit / src / settings_controls.rs

settings_controls.rs

settings_controls.rs 4.4 KiB Download Raw
1use std::rc::Rc;
2
3use gpui_kit::assets::IconName;
4use 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};
12use gpui_kit::prelude::FluentBuilder as _;
13use gpui_kit::*;
14
15use app_base::theme;
16
17pub const PAGE_PADDING: f32 = 20.;
18const FIELD_HEIGHT: f32 = 32.;
19const MONO_SIZE: f32 = 11.5;
20
21pub type Choice = Rc<dyn Fn(&mut Window, &mut App)>;
22
23pub struct MenuEntry {
24 pub label: String,
25 pub selected: bool,
26 pub choose: Choice,
27}
28
29pub 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
55pub 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
63pub 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
87pub 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
105pub 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
119pub 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 .listbox()
136}
137
138pub fn link_dropdown(
139 id: &'static str,
140 label: &'static str,
141 entries: Vec<MenuEntry>,
142) -> impl IntoElement {
143 Button::new(id)
144 .link()
145 .label(label)
146 .dropdown_menu(move |menu, _, _| {
147 entries.iter().fold(menu, |menu, entry| {
148 let choose = entry.choose.clone();
149 menu.item(
150 PopupMenuItem::new(entry.label.clone())
151 .on_click(move |_, window, cx| choose(window, cx)),
152 )
153 })
154 })
155}
156
157pub fn section_header(title: &'static str, trailing: Option<AnyElement>) -> Div {
158 h_flex()
159 .w_full()
160 .items_center()
161 .justify_between()
162 .px(px(PAGE_PADDING))
163 .pt(px(16.))
164 .pb(px(6.))
165 .text_size(px(11.))
166 .font_weight(FontWeight::SEMIBOLD)
167 .text_color(theme::text_faint())
168 .child(title)
169 .children(trailing)
170}