BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / rows.rs

rows.rs

rows.rs 6.5 KiB Download Raw
1use std::rc::Rc;
2
3use gpui_kit::assets::IconName;
4use gpui_kit::base::Disableable as _;
5use gpui_kit::component::{
6 button::Button,
7 h_flex,
8 menu::{DropdownMenu as _, PopupMenuItem},
9 v_flex,
10};
11use gpui_kit::prelude::FluentBuilder as _;
12use gpui_kit::*;
13
14use crate::theme;
15use crate::views::widgets::icon;
16
17pub const ROW_INSET: f32 = 12.;
18pub const SELECT_WIDTH: f32 = 216.;
19const ROW_PADDING: f32 = 12.;
20const ROW_GAP: f32 = 12.;
21const ROW_RADIUS: f32 = 8.;
22const BUTTON_HEIGHT: f32 = 32.;
23const BUTTON_RADIUS: f32 = 8.;
24const SCROLL_MENU_AFTER: usize = 10;
25const MENU_MAX_HEIGHT: f32 = 320.;
26
27pub struct Choice {
28 pub label: SharedString,
29 pub checked: bool,
30 pub choose: Rc<dyn Fn(&mut App)>,
31}
32
33impl 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
47pub 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
70pub 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
99pub 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
109pub 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/// Label above a full-width control. Use for text inputs so `Input::size_full`
120/// does not squeeze a horizontal title to zero width.
121pub fn stacked_field(
122 title: impl Into<SharedString>,
123 detail: impl Into<SharedString>,
124 control: impl IntoElement,
125) -> Div {
126 let detail = detail.into();
127 v_flex()
128 .w_full()
129 .px(px(ROW_INSET))
130 .py(px(ROW_PADDING))
131 .gap(px(6.))
132 .rounded(px(ROW_RADIUS))
133 .child(
134 div()
135 .text_size(px(14.))
136 .line_height(px(20.))
137 .font_weight(FontWeight::SEMIBOLD)
138 .text_color(theme::text())
139 .child(title.into()),
140 )
141 .when(!detail.is_empty(), |column| {
142 column.child(
143 div()
144 .text_size(px(12.))
145 .line_height(px(16.))
146 .text_color(theme::text_muted())
147 .child(detail),
148 )
149 })
150 .child(div().w_full().child(control))
151}
152
153pub fn toggle_row(
154 id: impl Into<ElementId>,
155 title: impl Into<SharedString>,
156 detail: impl Into<SharedString>,
157 detail_color: Hsla,
158 on: bool,
159) -> Stateful<Div> {
160 row()
161 .id(id)
162 .cursor_pointer()
163 .hover(|row| row.bg(theme::popup_hover()))
164 .child(titled(title, detail, detail_color))
165 .child(switch(on))
166}
167
168pub fn caption(label: impl Into<SharedString>) -> Div {
169 div()
170 .w_full()
171 .px(px(ROW_INSET))
172 .pt(px(18.))
173 .pb(px(6.))
174 .text_size(px(12.))
175 .font_weight(FontWeight::SEMIBOLD)
176 .text_color(theme::text_muted())
177 .child(label.into())
178}
179
180pub fn text_button(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Stateful<Div> {
181 div()
182 .id(id)
183 .h(px(BUTTON_HEIGHT))
184 .px(px(14.))
185 .flex()
186 .flex_none()
187 .items_center()
188 .rounded(px(BUTTON_RADIUS))
189 .border_1()
190 .border_color(theme::border_strong())
191 .cursor_pointer()
192 .text_size(px(13.))
193 .font_weight(FontWeight::SEMIBOLD)
194 .text_color(theme::text())
195 .hover(|button| button.bg(theme::popup_hover()))
196 .child(label.into())
197}
198
199pub fn select_selector(id: &str) -> String {
200 format!("settings-select/{id}")
201}
202
203pub fn select(
204 id: impl Into<ElementId>,
205 label: impl Into<SharedString>,
206 disabled: bool,
207 choices: Vec<Choice>,
208) -> impl IntoElement {
209 select_with_width(id, label, SELECT_WIDTH, disabled, choices)
210}
211
212pub fn select_with_width(
213 id: impl Into<ElementId>,
214 label: impl Into<SharedString>,
215 width: f32,
216 disabled: bool,
217 choices: Vec<Choice>,
218) -> impl IntoElement {
219 let scrollable = choices.len() > SCROLL_MENU_AFTER;
220 let id: ElementId = id.into();
221 Button::new(id.clone())
222 .debug_selector(|| select_selector(&id.to_string()))
223 .outline()
224 .w(px(width))
225 .flex_none()
226 .disabled(disabled)
227 .child(
228 h_flex()
229 .w_full()
230 .min_w_0()
231 .gap(px(8.))
232 .items_center()
233 .justify_between()
234 .child(div().flex_1().min_w_0().truncate().child(label.into()))
235 .child(icon(IconName::ChevronDown, 12., theme::text_muted())),
236 )
237 .dropdown_menu(move |menu, _, _| {
238 let menu = if scrollable {
239 menu.max_h(px(MENU_MAX_HEIGHT)).scrollable(true)
240 } else {
241 menu
242 };
243 choices.iter().fold(menu, |menu, choice| {
244 let choose = choice.choose.clone();
245 menu.item(
246 PopupMenuItem::new(choice.label.clone())
247 .checked(choice.checked)
248 .on_click(move |_, _, cx| choose(cx)),
249 )
250 })
251 })
252 .listbox()
253}