forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.07.2 MiB4b67e80Tahinli1 hour ago400 commits

ui.rs309 lines, 7.6 KiBHistoryRawDownload
1use crate::gui::style::{size, space, text};
2use crate::gui::{
3 Palette, Tooltip as _,
4 parts::{self, Scale, Tone},
5 text,
6};
7use crate::lang::w;
8use gpui_kit::{
9 AnyElement, App, Div, FontWeight, Hsla, IntoElement, ParentElement, Stateful, Styled, Window,
10 assets::IconName, div, prelude::FluentBuilder as _, px,
11};
12use jiff::{civil::Date, tz::TimeZone};
13
14const LABEL_WIDTH: f32 = 88.;
15
16pub(super) fn to_bytes(text: &str, unit: u64) -> Option<u64> {
17 text.trim().parse::<u64>().ok()?.checked_mul(unit)
18}
19
20pub(super) fn to_units(bytes: u64, unit: u64) -> String {
21 (bytes / unit).to_string()
22}
23
24pub(super) fn confirmed<T: PartialEq>(pending: &mut Option<T>, id: T) -> bool {
25 if pending.as_ref() == Some(&id) {
26 *pending = None;
27 true
28 } else {
29 *pending = Some(id);
30 false
31 }
32}
33
34pub(super) fn fixed(width: f32, control: impl IntoElement) -> AnyElement {
35 div()
36 .w(px(width))
37 .flex_shrink_0()
38 .child(control)
39 .into_any_element()
40}
41
42pub(super) fn number(text: &str) -> Option<u32> {
43 text.trim().parse().ok()
44}
45
46pub(super) fn icon_button(
47 id: String,
48 icon: IconName,
49 tip: &'static str,
50 color: Hsla,
51 palette: Palette,
52 action: impl Fn(&mut Window, &mut App) + 'static,
53) -> Stateful<Div> {
54 parts::icon_button(
55 id,
56 icon,
57 tip,
58 Tone::Idle,
59 Scale::Small,
60 palette.tokens,
61 action,
62 )
63 .text_color(color)
64}
65
66pub(super) fn delete_icon_button(
67 id: String,
68 pending: bool,
69 palette: Palette,
70 action: impl Fn(&mut Window, &mut App) + 'static,
71) -> Stateful<Div> {
72 icon_button(
73 format!("{id}_{pending}"),
74 IconName::Trash,
75 if pending {
76 w().builder_ui.confirm
77 } else {
78 w().builder_ui.delete
79 },
80 palette.danger,
81 palette,
82 action,
83 )
84 .when(pending, |this| {
85 this.bg(palette.danger).text_color(palette.on_accent)
86 })
87}
88
89pub(super) fn action_button(
90 id: String,
91 icon: IconName,
92 label: &'static str,
93 tip: &'static str,
94 color: Hsla,
95 palette: Palette,
96 action: impl Fn(&mut Window, &mut App) + 'static,
97) -> Stateful<Div> {
98 parts::press(id, palette.tokens, action)
99 .tip(tip)
100 .px_0()
101 .min_w(px(size::THUMB))
102 .text_size(text(text::CAPTION))
103 .font_weight(FontWeight::MEDIUM)
104 .text_color(color)
105 .child(crate::gui::parts::small_icon(icon))
106 .child(label)
107}
108
109pub(super) fn confirm_button(
110 id: String,
111 icon: IconName,
112 label: &'static str,
113 tip: &'static str,
114 pending: bool,
115 palette: Palette,
116 action: impl Fn(&mut Window, &mut App) + 'static,
117) -> Stateful<Div> {
118 action_button(
119 format!("{id}_{pending}"),
120 icon,
121 if pending {
122 w().builder_ui.confirm
123 } else {
124 label
125 },
126 if pending {
127 w().builder_ui.confirm_tip
128 } else {
129 tip
130 },
131 palette.danger,
132 palette,
133 action,
134 )
135 .when(pending, |this| this.font_weight(FontWeight::SEMIBOLD))
136}
137
138pub(super) fn action_group(
139 title: &'static str,
140 palette: Palette,
141 children: Vec<AnyElement>,
142) -> Option<Div> {
143 if children.is_empty() {
144 return None;
145 }
146
147 Some(
148 div()
149 .flex()
150 .items_start()
151 .gap(px(space::MD))
152 .child(
153 div()
154 .w(px(LABEL_WIDTH))
155 .h(px(size::CONTROL_SM))
156 .flex_shrink_0()
157 .flex()
158 .items_center()
159 .text_size(text(text::CAPTION))
160 .font_weight(FontWeight::MEDIUM)
161 .text_color(palette.muted)
162 .child(title),
163 )
164 .child(
165 div()
166 .flex_1()
167 .min_w_0()
168 .flex()
169 .flex_col()
170 .gap(px(space::XS))
171 .child(
172 div()
173 .flex()
174 .flex_wrap()
175 .gap(px(space::XS))
176 .children(children),
177 ),
178 ),
179 )
180}
181
182fn epoch_ms(date: Date) -> Option<u64> {
183 let millis = date
184 .to_zoned_datetime(TimeZone::system())
185 .ok()?
186 .timestamp()
187 .as_millisecond();
188 u64::try_from(millis).ok()
189}
190
191pub(super) fn start_of_day_ms(text: &str) -> Option<u64> {
192 let date: Date = text.parse().ok()?;
193 epoch_ms(date)
194}
195
196pub(super) fn end_of_day_ms(text: &str) -> Option<u64> {
197 let date: Date = text.parse().ok()?;
198 epoch_ms(date.tomorrow().ok()?)?.checked_sub(1)
199}
200
201pub(super) fn rows_body(rows: Vec<Div>, none: &str, palette: Palette) -> Div {
202 if rows.is_empty() {
203 return crate::gui::parts::empty(none, palette.tokens);
204 }
205 div().flex().flex_col().children(rows)
206}
207
208pub(super) fn card(_palette: Palette) -> Div {
209 div().flex().flex_col()
210}
211
212pub(super) fn card_header(title: &str, actions: Vec<AnyElement>, palette: Palette) -> Div {
213 div()
214 .pb(px(space::XS))
215 .flex()
216 .items_center()
217 .justify_between()
218 .gap(px(space::SM))
219 .child(crate::gui::parts::section(
220 title.to_string(),
221 palette.tokens,
222 ))
223 .children(actions)
224}
225
226pub(super) fn list_card(title: Option<&str>, rows: Vec<Div>, none: &str, palette: Palette) -> Div {
227 let body = if rows.is_empty() {
228 crate::gui::parts::empty(none, palette.tokens)
229 } else {
230 div().flex().flex_col().children(rows)
231 };
232
233 card(palette)
234 .children(title.map(|title| card_header(title, Vec::new(), palette)))
235 .child(body)
236}
237
238pub(super) fn list_row() -> Div {
239 div()
240 .min_h(px(size::ROW))
241 .py(px(space::SM))
242 .flex()
243 .items_center()
244 .gap(px(space::MD))
245}
246
247pub(super) fn toolbar(items: Vec<AnyElement>) -> Div {
248 div()
249 .flex()
250 .flex_wrap()
251 .items_center()
252 .gap(px(space::SM))
253 .children(items)
254}
255
256pub(super) fn stat_tile(label: &str, value: &str, palette: Palette) -> Div {
257 card(palette)
258 .gap(px(space::XXS))
259 .child(
260 div()
261 .text_size(text(text::DISPLAY))
262 .text_color(palette.text)
263 .child(value.to_string()),
264 )
265 .child(
266 div()
267 .text_size(text(text::CAPTION))
268 .text_color(palette.muted)
269 .child(label.to_string()),
270 )
271}
272
273pub(super) fn field_row(
274 label: &str,
275 hint: Option<&str>,
276 control: impl IntoElement,
277 palette: Palette,
278) -> Div {
279 list_row()
280 .justify_between()
281 .child(
282 div()
283 .flex_1()
284 .min_w_0()
285 .flex()
286 .flex_col()
287 .child(
288 div()
289 .text_size(text(text::LABEL))
290 .text_color(palette.text)
291 .child(label.to_string()),
292 )
293 .children(hint.map(|hint| {
294 div()
295 .text_size(text(text::CAPTION))
296 .text_color(palette.muted)
297 .child(hint.to_string())
298 })),
299 )
300 .child(div().w(px(size::SIDEBAR)).flex_shrink_0().child(control))
301}
302
303pub(super) fn pill(label: &str, color: Hsla, palette: Palette) -> Div {
304 let _ = palette;
305 div()
306 .text_size(text(text::CAPTION))
307 .text_color(color)
308 .child(label.to_string())
309}