BlueGit

ict rusty-teams

Files

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

rows.rs

rows.rs 5.3 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
119pub 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
134pub 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
146pub 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
165pub fn select(
166 id: impl Into<ElementId>,
167 label: impl Into<SharedString>,
168 disabled: bool,
169 choices: Vec<Choice>,
170) -> impl IntoElement {
171 select_with_width(id, label, SELECT_WIDTH, disabled, choices)
172}
173
174pub fn select_with_width(
175 id: impl Into<ElementId>,
176 label: impl Into<SharedString>,
177 width: f32,
178 disabled: bool,
179 choices: Vec<Choice>,
180) -> impl IntoElement {
181 let scrollable = choices.len() > SCROLL_MENU_AFTER;
182 Button::new(id)
183 .outline()
184 .w(px(width))
185 .flex_none()
186 .disabled(disabled)
187 .child(
188 h_flex()
189 .w_full()
190 .min_w_0()
191 .gap(px(8.))
192 .items_center()
193 .justify_between()
194 .child(div().flex_1().min_w_0().truncate().child(label.into()))
195 .child(icon(IconName::ChevronDown, 12., theme::text_muted())),
196 )
197 .dropdown_menu(move |menu, _, _| {
198 let menu = if scrollable {
199 menu.max_h(px(MENU_MAX_HEIGHT)).scrollable(true)
200 } else {
201 menu
202 };
203 choices.iter().fold(menu, |menu, choice| {
204 let choose = choice.choose.clone();
205 menu.item(
206 PopupMenuItem::new(choice.label.clone())
207 .checked(choice.checked)
208 .on_click(move |_, _, cx| choose(cx)),
209 )
210 })
211 })
212}