use crate::gui::parts::{self, Fill, Scale, Tone}; use crate::gui::style::{Tokens, size, space, text}; use crate::gui::{Tooltip as _, text}; use crate::lang::w; use gpui_kit::{ AnyElement, App, Div, FontWeight, Hsla, IntoElement, ParentElement, Stateful, Styled, Window, assets::IconName, div, prelude::FluentBuilder as _, px, }; use jiff::{civil::Date, tz::TimeZone}; const LABEL_WIDTH: f32 = 88.; const METER: f32 = 140.; pub(super) fn to_bytes(text: &str, unit: u64) -> Option { text.trim().parse::().ok()?.checked_mul(unit) } pub(super) fn to_units(bytes: u64, unit: u64) -> String { (bytes / unit).to_string() } pub(super) fn confirmed(pending: &mut Option, id: T) -> bool { if pending.as_ref() == Some(&id) { *pending = None; true } else { *pending = Some(id); false } } pub(super) fn fixed(width: f32, control: impl IntoElement) -> AnyElement { div() .w(px(width)) .flex_shrink_0() .child(control) .into_any_element() } pub(super) fn number(text: &str) -> Option { text.trim().parse().ok() } pub(super) fn caption_text(value: impl Into, color: Hsla) -> Div { div() .text_size(text(text::CAPTION)) .text_color(color) .child(value.into()) } pub(super) fn meter(share: f32, label: String, t: Tokens) -> Div { div() .w(px(METER)) .flex() .items_center() .gap(px(space::SM)) .child(parts::track(Fill::Part(share), false, t)) .child(caption_text(label, t.dim())) } pub(super) fn icon_button( id: String, icon: IconName, tip: &'static str, color: Hsla, t: Tokens, action: impl Fn(&mut Window, &mut App) + 'static, ) -> Stateful
{ parts::icon_button(id, icon, tip, Tone::Idle, Scale::Small, t, action).text_color(color) } pub(super) fn delete_icon_button( id: String, pending: bool, t: Tokens, action: impl Fn(&mut Window, &mut App) + 'static, ) -> Stateful
{ icon_button( format!("{id}_{pending}"), IconName::Trash, if pending { w().builder_ui.confirm } else { w().builder_ui.delete }, t.bad(), t, action, ) .when(pending, |this| this.bg(t.bad()).text_color(t.ground())) } pub(super) fn action_button( id: String, icon: IconName, label: &'static str, tip: &'static str, color: Hsla, t: Tokens, action: impl Fn(&mut Window, &mut App) + 'static, ) -> Stateful
{ parts::press(id, t, action) .tip(tip) .px_0() .min_w(px(size::THUMB)) .text_size(text(text::CAPTION)) .font_weight(FontWeight::MEDIUM) .text_color(color) .child(parts::small_icon(icon)) .child(label) } pub(super) fn confirm_button( id: String, icon: IconName, label: &'static str, tip: &'static str, pending: bool, t: Tokens, action: impl Fn(&mut Window, &mut App) + 'static, ) -> Stateful
{ action_button( format!("{id}_{pending}"), icon, if pending { w().builder_ui.confirm } else { label }, if pending { w().builder_ui.confirm_tip } else { tip }, t.bad(), t, action, ) .when(pending, |this| this.font_weight(FontWeight::SEMIBOLD)) } pub(super) fn action_group( title: &'static str, t: Tokens, children: Vec, ) -> Option
{ if children.is_empty() { return None; } Some( div() .flex() .items_start() .gap(px(space::MD)) .child( div() .w(px(LABEL_WIDTH)) .h(px(size::CONTROL_SM)) .flex_shrink_0() .flex() .items_center() .text_size(text(text::CAPTION)) .font_weight(FontWeight::MEDIUM) .text_color(t.dim()) .child(title), ) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .gap(px(space::XS)) .child( div() .flex() .flex_wrap() .gap(px(space::XS)) .children(children), ), ), ) } fn epoch_ms(date: Date) -> Option { let millis = date .to_zoned_datetime(TimeZone::system()) .ok()? .timestamp() .as_millisecond(); u64::try_from(millis).ok() } pub(super) fn start_of_day_ms(text: &str) -> Option { let date: Date = text.parse().ok()?; epoch_ms(date) } pub(super) fn end_of_day_ms(text: &str) -> Option { let date: Date = text.parse().ok()?; epoch_ms(date.tomorrow().ok()?)?.checked_sub(1) } pub(super) fn rows_body(rows: Vec
, none: &str, t: Tokens) -> Div { if rows.is_empty() { return parts::empty(none, t); } div().flex().flex_col().children(rows) } pub(super) fn card() -> Div { div().flex().flex_col() } pub(super) fn card_header(title: &str, actions: Vec, t: Tokens) -> Div { div() .pb(px(space::XS)) .flex() .items_center() .justify_between() .gap(px(space::SM)) .child(parts::section(title.to_string(), t)) .children(actions) } pub(super) fn list_card(title: Option<&str>, rows: Vec
, none: &str, t: Tokens) -> Div { card() .children(title.map(|title| card_header(title, Vec::new(), t))) .child(rows_body(rows, none, t)) } pub(super) fn list_row() -> Div { div() .min_h(px(size::ROW)) .py(px(space::SM)) .flex() .items_center() .gap(px(space::MD)) } pub(super) fn toolbar(items: Vec) -> Div { div() .flex() .flex_wrap() .items_center() .gap(px(space::SM)) .children(items) } pub(super) fn stat_tile(label: &str, value: &str, t: Tokens) -> Div { card() .gap(px(space::XXS)) .child( div() .text_size(text(text::DISPLAY)) .text_color(t.paper()) .child(value.to_string()), ) .child(caption_text(label.to_string(), t.dim())) } pub(super) fn field_row( label: &str, hint: Option<&str>, control: impl IntoElement, t: Tokens, ) -> Div { list_row() .justify_between() .child( div() .flex_1() .min_w_0() .flex() .flex_col() .child( div() .text_size(text(text::LABEL)) .text_color(t.paper()) .child(label.to_string()), ) .children(hint.map(|hint| caption_text(hint.to_string(), t.dim()))), ) .child(div().w(px(size::SIDEBAR)).flex_shrink_0().child(control)) } pub(super) fn pill(label: &str, color: Hsla) -> Div { caption_text(label.to_string(), color) }