use gpui::prelude::FluentBuilder as _; use gpui::{ div, relative, AnyElement, App, ClickEvent, Div, ElementId, Hsla, InteractiveElement, IntoElement, MouseButton, ParentElement, RenderOnce, SharedString, Stateful, StatefulInteractiveElement as _, StyleRefinement, Styled, Window, }; use theme::ActiveTheme; use crate::indicator::Indicator; use crate::tooltip::Tooltip; use crate::{h_flex, Disableable, Icon, Selectable, Sizable, Size, StyledExt}; #[derive(Clone, Copy, PartialEq, Eq)] pub struct ButtonCustomVariant { color: Hsla, foreground: Hsla, hover: Hsla, active: Hsla, } pub trait ButtonVariants: Sized { fn with_variant(self, variant: ButtonVariant) -> Self; /// With the primary style for the Button. fn primary(self) -> Self { self.with_variant(ButtonVariant::Primary) } /// With the secondary style for the Button. fn secondary(self) -> Self { self.with_variant(ButtonVariant::Secondary) } /// With the danger style for the Button. fn danger(self) -> Self { self.with_variant(ButtonVariant::Danger) } /// With the warning style for the Button. fn warning(self) -> Self { self.with_variant(ButtonVariant::Warning) } /// With the ghost style for the Button. fn ghost(self) -> Self { self.with_variant(ButtonVariant::Ghost { alt: false }) } /// With the ghost style for the Button. fn ghost_alt(self) -> Self { self.with_variant(ButtonVariant::Ghost { alt: true }) } /// With the transparent style for the Button. fn transparent(self) -> Self { self.with_variant(ButtonVariant::Transparent) } /// With the custom style for the Button. fn custom(self, style: ButtonCustomVariant) -> Self { self.with_variant(ButtonVariant::Custom(style)) } } impl ButtonCustomVariant { pub fn new(_window: &Window, cx: &App) -> Self { Self { color: cx.theme().element_background, foreground: cx.theme().element_foreground, hover: cx.theme().element_hover, active: cx.theme().element_active, } } pub fn color(mut self, color: Hsla) -> Self { self.color = color; self } pub fn foreground(mut self, color: Hsla) -> Self { self.foreground = color; self } pub fn hover(mut self, color: Hsla) -> Self { self.hover = color; self } pub fn active(mut self, color: Hsla) -> Self { self.active = color; self } } /// The variant of the Button. #[derive(Clone, Copy, PartialEq, Eq)] pub enum ButtonVariant { Primary, Secondary, Danger, Warning, Ghost { alt: bool }, Transparent, Custom(ButtonCustomVariant), } impl Default for ButtonVariant { fn default() -> Self { Self::Primary } } type OnClick = Option>; /// A Button element. #[derive(IntoElement)] pub struct Button { base: Stateful
, style: StyleRefinement, icon: Option, label: Option, tooltip: Option, children: Vec, variant: ButtonVariant, rounded: bool, size: Size, disabled: bool, reverse: bool, bold: bool, cta: bool, loading: bool, loading_icon: Option, on_click: OnClick, pub(crate) selected: bool, pub(crate) stop_propagation: bool, } impl From