use crate::components::button::{ButtonKind, ButtonState, Look};
use crate::components::floating::Align;
use crate::t3::press::{Handler, inset_frame, rem};
use crate::{
components::Tooltip,
icons::{CarbonIcon, Icon},
motion::{self, Motion},
theme::color,
tokens::{ColorRole, FontFamilyKey},
typography::font,
};
use gpui_kit::{
AnyElement, App, CursorStyle, ElementId, Hsla, InteractiveElement as _, IntoElement,
MouseButton, ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled,
Window, div, prelude::FluentBuilder as _, px,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ActionKind {
Carbon(ButtonKind),
NeutralOutline,
}
pub(crate) fn action_look(
kind: ActionKind,
state: ButtonState,
) -> (Hsla, Hsla, Option<Hsla>, Option<Hsla>, Option<Hsla>) {
match kind {
ActionKind::Carbon(kind) => {
let look = Look::of(kind, state, false);
(
look.background,
look.text,
None,
look.focus_ring,
look.focus_inset,
)
}
ActionKind::NeutralOutline => {
let base = Look::of(ButtonKind::Secondary, state, false);
(
if state.hovered && !state.disabled {
color(ColorRole::LayerHover01)
} else {
gpui_kit::transparent_black()
},
color(if state.disabled {
ColorRole::TextDisabled
} else {
ColorRole::TextPrimary
}),
Some(color(ColorRole::BorderStrong01)),
base.focus_ring,
base.focus_inset,
)
}
}
}
pub(crate) enum Part {
Icon(CarbonIcon),
Text(SharedString),
Key(SharedString, KeyCap),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum KeyCap {
Framed,
Bare,
}
#[derive(IntoElement)]
pub(crate) struct ActionButton {
id: ElementId,
label: SharedString,
kind: ActionKind,
height: f32,
padding: (f32, f32),
gap: f32,
parts: Vec<Part>,
disabled: bool,
tooltip: Option<SharedString>,
on_click: Option<Handler>,
square: bool,
ink: Option<Hsla>,
tooltip_align: Align,
}
impl ActionButton {
pub(crate) fn new(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
kind: ActionKind,
) -> Self {
Self {
id: id.into(),
label: label.into(),
kind,
height: 32.,
padding: (15., 16.),
gap: 0.,
parts: Vec::new(),
disabled: false,
tooltip: None,
on_click: None,
square: false,
ink: None,
tooltip_align: Align::Top,
}
}
pub(crate) fn height(mut self, height: f32) -> Self {
self.height = height;
self
}
pub(crate) fn padding(mut self, start: f32, end: f32) -> Self {
self.padding = (start, end);
self
}
pub(crate) fn gap(mut self, gap: f32) -> Self {
self.gap = gap;
self
}
pub(crate) fn part(mut self, part: Part) -> Self {
self.parts.push(part);
self
}
pub(crate) fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub(crate) fn tooltip(mut self, text: impl Into<SharedString>) -> Self {
self.tooltip = Some(text.into());
self
}
pub(crate) fn on_click(mut self, f: Option<Handler>) -> Self {
self.on_click = f;
self
}
pub(crate) fn ink(mut self, ink: Hsla) -> Self {
self.ink = Some(ink);
self
}
pub(crate) fn tooltip_align(mut self, align: Align) -> Self {
self.tooltip_align = align;
self
}
pub(crate) fn square(mut self, square: bool) -> Self {
self.square = square;
self
}
}
#[derive(Default)]
struct PointerState {
hovered: bool,
pressed: bool,
}
impl RenderOnce for ActionButton {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let pointer =
window.use_keyed_state((id.clone(), "pointer"), cx, |_, _| PointerState::default());
let focus = window
.use_keyed_state((id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let state = {
let p = pointer.read(cx);
ButtonState {
hovered: p.hovered && !self.disabled,
pressed: p.pressed && !self.disabled,
focused: focus.is_focused(window),
disabled: self.disabled,
}
};
let (bg, ink, mut frame, ring, inset) = action_look(self.kind, state);
let ink = if self.disabled {
ink
} else {
self.ink.unwrap_or(ink)
};
let bg = motion::animate_color((id.clone(), "bg"), bg, Motion::HOVER, window, cx);
let parts: Vec<AnyElement> = self
.parts
.into_iter()
.map(|part| match part {
Part::Icon(icon) => Icon::carbon(icon)
.size_length(rem(16.))
.color(ink)
.into_any_element(),
Part::Text(text) => div().child(text).into_any_element(),
Part::Key(label, KeyCap::Framed) => div()
.flex()
.flex_none()
.items_center()
.ml(rem(8.))
.h(rem(18.))
.px(rem(4.))
.rounded(px(4.))
.border_1()
.border_color(ink.opacity(0.7))
.text_color(ink.opacity(0.7))
.font(font(FontFamilyKey::Sans, 400))
.text_size(rem(11.))
.child(label)
.into_any_element(),
Part::Key(label, KeyCap::Bare) => div()
.flex_none()
.ml(rem(8.))
.text_color(ink.opacity(0.75))
.font(font(FontFamilyKey::Mono, 400))
.text_size(rem(12.))
.child(label)
.into_any_element(),
})
.collect();
let outline = match self.kind {
ActionKind::NeutralOutline => frame.take().map(|c| (1., c)),
ActionKind::Carbon(_) => None,
};
let focus_layers: Vec<AnyElement> =
[outline, ring.map(|c| (0., c)), inset.map(|c| (1., c))]
.into_iter()
.flatten()
.map(|(at, c)| {
div()
.absolute()
.top(px(at))
.left(px(at))
.right(px(at))
.bottom(px(at))
.border_1()
.border_color(c)
.into_any_element()
})
.collect();
let on_click = self.on_click.clone();
let button = gpui_kit::base::Button::new(id.clone())
.track_focus(&focus)
.disabled(self.disabled)
.accessibility_label(self.label.clone())
.relative()
.flex()
.flex_row()
.flex_none()
.items_center()
.gap(rem(self.gap))
.h(rem(self.height))
.pl(rem(self.padding.0))
.pr(rem(self.padding.1))
.map(|this| {
if self.square {
this.w(rem(self.height)).justify_center()
} else {
this.justify_start()
}
})
.bg(bg)
.when_some(frame, |this, frame| this.shadow(inset_frame(frame)))
.text_color(ink)
.text_size(rem(14.))
.line_height(rem(18.))
.whitespace_nowrap()
.cursor(if self.disabled {
CursorStyle::OperationNotAllowed
} else {
CursorStyle::PointingHand
})
.on_hover({
let pointer = pointer.clone();
move |over, _, cx| {
pointer.update(cx, |p, cx| {
if p.hovered != *over {
p.hovered = *over;
if !over {
p.pressed = false;
}
cx.notify();
}
})
}
})
.on_mouse_down(MouseButton::Left, {
let pointer = pointer.clone();
move |_, _, cx| {
pointer.update(cx, |p, cx| {
p.pressed = true;
cx.notify();
})
}
})
.on_mouse_up(MouseButton::Left, {
let pointer = pointer.clone();
move |_, _, cx| {
pointer.update(cx, |p, cx| {
p.pressed = false;
cx.notify();
})
}
})
.when_some(on_click, |this, f| {
this.on_click(move |_, window, cx| f(window, cx))
})
.children(parts)
.children(focus_layers);
match self.tooltip {
Some(text) => Tooltip::new((id, "tooltip"), button)
.content(text)
.align(self.tooltip_align)
.into_any_element(),
None => button.into_any_element(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn neutral_outline_is_a_strong_hairline_on_nothing() {
crate::theme::tests_set(CarbonTheme::White);
let (bg, ink, frame, ..) = action_look(ActionKind::NeutralOutline, ButtonState::default());
assert_eq!(bg.a, 0.);
assert_eq!(ink, color(ColorRole::TextPrimary));
assert_eq!(frame, Some(color(ColorRole::BorderStrong01)));
let hover = ButtonState {
hovered: true,
..Default::default()
};
assert_eq!(
action_look(ActionKind::NeutralOutline, hover).0,
color(ColorRole::LayerHover01)
);
}
}