use crate::icon_button::IconButton;
use crate::tokens::ButtonRole;
use gpui_kit::component::button::{Button, ButtonVariants as _};
use gpui_kit::component::{Disableable as _, Sizable as _, h_flex};
use gpui_kit::{App, ElementId, IntoElement, ParentElement as _, SharedString, Styled as _, px};
use subspace_lucide::SvgIcon;
const HEADER_BUTTON_HEIGHT: f32 = 28.;
const HEADER_BUTTON_PAD_X: f32 = 6.;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ActionState {
#[default]
Idle,
Busy,
Success,
Failure,
}
impl ActionState {
pub fn label(self, labels: [&str; 4]) -> &str {
labels[match self {
Self::Idle => 0,
Self::Busy => 1,
Self::Success => 2,
Self::Failure => 3,
}]
}
pub fn apply(self, button: IconButton) -> IconButton {
match self {
Self::Idle => button,
Self::Busy => button.loading(true).disabled(true),
Self::Success => button.success().icon(subspace_lucide::IconName::Check),
Self::Failure => button
.danger()
.icon(subspace_lucide::IconName::TriangleAlert),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Intent {
Accept,
Reject,
Caution,
Primary,
Neutral,
Quiet,
}
fn menu_button_intent(active: bool) -> Intent {
if active {
Intent::Primary
} else {
Intent::Neutral
}
}
pub fn action_button(
id: impl Into<ElementId>,
intent: Intent,
icon: Option<SvgIcon>,
label: impl Into<SharedString>,
) -> IconButton {
labelled(id, ButtonRole::Action, intent, icon, label)
}
pub fn dialog_button(
id: impl Into<ElementId>,
intent: Intent,
icon: Option<SvgIcon>,
label: impl Into<SharedString>,
) -> IconButton {
labelled(id, ButtonRole::Dialog, intent, icon, label)
}
fn labelled(
id: impl Into<ElementId>,
role: ButtonRole,
intent: Intent,
icon: Option<SvgIcon>,
label: impl Into<SharedString>,
) -> IconButton {
let label = label.into();
let button = styled(IconButton::from_button(Button::new(id)), intent)
.role(role)
.xsmall()
.accessibility_label(label.clone())
.label(label);
match icon {
Some(icon) => button.icon(icon),
None => button,
}
}
pub fn menu_button(
id: impl Into<ElementId>,
icon: SvgIcon,
label: impl Into<SharedString>,
active: bool,
) -> IconButton {
let label = label.into();
action_button(id, menu_button_intent(active), Some(icon), label.clone())
.role(ButtonRole::Toolbar)
.dropdown_caret(true)
.accessibility_label(label)
}
pub fn header_button(
id: impl Into<ElementId>,
intent: Intent,
icon: Option<SvgIcon>,
label: impl Into<SharedString>,
) -> IconButton {
action_button(id, intent, icon, label)
.h(px(HEADER_BUTTON_HEIGHT))
.px(px(HEADER_BUTTON_PAD_X))
}
pub fn icon_button(
id: impl Into<ElementId>,
intent: Intent,
icon: SvgIcon,
name: impl Into<SharedString>,
) -> IconButton {
let name = name.into();
styled(IconButton::from_button(Button::new(id)), intent)
.xsmall()
.icon(icon)
.tooltip(name.clone())
.accessibility_label(name)
}
pub fn row_button(
id: impl Into<ElementId>,
name: impl Into<SharedString>,
content: impl IntoElement,
) -> IconButton {
styled(IconButton::from_button(Button::new(id)), Intent::Quiet)
.small()
.accessibility_label(name)
.w_full()
.child(h_flex().flex_1().min_w_0().gap_2().child(content))
}
pub fn card_button(
id: impl Into<ElementId>,
name: impl Into<SharedString>,
content: impl IntoElement,
) -> Button {
Button::new(id)
.outline()
.accessibility_label(name)
.w_full()
.h_auto()
.px_3()
.py_2()
.child(content)
}
pub fn toolbar_button(
id: impl Into<ElementId>,
icon: SvgIcon,
name: impl Into<SharedString>,
presentation: (Intent, bool),
cx: &App,
) -> IconButton {
let (intent, show_label) = presentation;
let name = name.into();
let tokens = crate::Tokens::current(cx);
let button = IconButton::new(id)
.ghost()
.xsmall()
.icon(icon)
.tooltip(name.clone())
.accessibility_label(name.clone());
let button = match intent {
Intent::Accept => button.text_color(tokens.green),
Intent::Reject => button.text_color(tokens.red),
Intent::Caution => button.text_color(tokens.orange),
Intent::Primary => button.text_color(tokens.accent),
Intent::Neutral | Intent::Quiet => button,
};
if show_label {
button.label(name)
} else {
button
}
}
fn styled(button: IconButton, intent: Intent) -> IconButton {
match intent {
Intent::Accept => button.success(),
Intent::Reject => button.danger(),
Intent::Caution => button.warning(),
Intent::Primary => button.primary(),
Intent::Neutral => button.outline(),
Intent::Quiet => button.ghost(),
}
}
#[cfg(test)]
mod tests {
use super::{ActionState, Intent, menu_button_intent};
#[test]
fn action_results_have_distinct_text_even_without_colour() {
let labels = ["Validate", "Validating…", "Valid", "Invalid"];
assert_eq!(ActionState::default().label(labels), "Validate");
assert_eq!(ActionState::Busy.label(labels), "Validating…");
assert_eq!(ActionState::Success.label(labels), "Valid");
assert_eq!(ActionState::Failure.label(labels), "Invalid");
}
#[test]
fn menu_buttons_are_primary_only_for_a_non_default_choice() {
assert_eq!(menu_button_intent(false), Intent::Neutral);
assert_eq!(menu_button_intent(true), Intent::Primary);
}
}