use crate::{
density,
icons::{CarbonIcon, Icon, IconSize},
theme::{Selection, color},
tokens::{self, ColorRole},
u0::primitives::parts,
};
use gpui_kit::{
App, BoxShadow, ClickEvent, CursorStyle, ElementId, FontWeight, Hsla, IntoElement,
ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, Window, div,
prelude::FluentBuilder as _, px, rems,
};
use std::rc::Rc;
pub fn colors(current: bool, selected: bool, hovered: bool) -> (Hsla, Hsla) {
let sel = Selection::of(current, selected);
let mut bg = color(ColorRole::Layer01);
let mut fg = color(ColorRole::TextSecondary);
if hovered {
bg = color(ColorRole::LayerHover01);
fg = color(ColorRole::TextPrimary);
}
if let Some(b) = sel.background {
bg = b;
fg = color(ColorRole::TextPrimary);
}
(bg, fg)
}
type ClickHandler = Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct StateRow {
id: ElementId,
label: SharedString,
icon: CarbonIcon,
current: bool,
selected: bool,
focus_ring: bool,
on_click: Option<ClickHandler>,
}
impl StateRow {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
icon: CarbonIcon::Chat,
current: false,
selected: false,
focus_ring: false,
on_click: None,
}
}
pub fn icon(mut self, icon: CarbonIcon) -> Self {
self.icon = icon;
self
}
pub fn current(mut self, current: bool) -> Self {
self.current = current;
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn focus_ring(mut self, on: bool) -> Self {
self.focus_ring = on;
self
}
pub fn on_click(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl RenderOnce for StateRow {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let focus = parts::keyed_focus((id.clone(), "focus"), window, cx);
let hover = window.use_keyed_state((id.clone(), "hover"), cx, |_, _| false);
let (bg, fg) = colors(self.current, self.selected, *hover.read(cx));
let sel = Selection::of(self.current, self.selected);
let ring = self.focus_ring || parts::focus_visible(&focus, window);
gpui_kit::base::Button::new(id)
.track_focus(&focus)
.accessibility_label(self.label.clone())
.selected(self.selected)
.relative()
.flex()
.items_center()
.justify_start()
.gap(tokens::SPACING_03)
.w_full()
.h(density::current().row())
.px(tokens::SPACING_04)
.bg(bg)
.text_color(fg)
.text_size(rems(0.875))
.line_height(rems(1.125))
.when(sel.semibold, |b| b.font_weight(FontWeight::SEMIBOLD))
.when_some(sel.indicator, |b, c| {
b.shadow(vec![BoxShadow {
color: c,
offset: gpui_kit::point(px(3.), px(0.)),
blur_radius: px(0.),
spread_radius: px(0.),
inset: true,
}])
})
.cursor(CursorStyle::PointingHand)
.on_hover(move |h, _, cx| {
hover.update(cx, |v, cx| {
*v = *h;
cx.notify();
})
})
.child(Icon::carbon(self.icon).size(IconSize::S16).color(fg))
.child(
div()
.min_w_0()
.overflow_hidden()
.text_ellipsis()
.child(self.label),
)
.when(ring, |b| {
b.child(crate::components::form::inset_outline(2., sel.focus))
})
.when_some(self.on_click, |b, f| {
b.on_click(move |e, w, cx| f(e, w, cx))
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CarbonTheme, theme::color_in};
#[test]
fn states_stack_as_in_ghost_css() {
crate::theme::tests_set(CarbonTheme::White);
let c = |r| color_in(CarbonTheme::White, r);
assert_eq!(
colors(false, false, false),
(c(ColorRole::Layer01), c(ColorRole::TextSecondary))
);
assert_eq!(colors(false, false, true).0, c(ColorRole::LayerHover01));
assert_eq!(
colors(true, false, true),
(
c(ColorRole::StateCurrentBackground),
c(ColorRole::TextPrimary)
)
);
assert_eq!(
colors(true, true, false).0,
c(ColorRole::StateSelectedBackground)
);
let both = Selection::of(true, true);
assert_eq!(both.indicator, Some(c(ColorRole::StateCurrentIndicator)));
assert!(both.semibold);
assert_eq!(Selection::of(false, true).indicator, None);
assert_eq!(both.focus, c(ColorRole::StateFocus));
}
}