use std::rc::Rc;
use gpui::{App, ElementId, IntoElement, RenderOnce, SharedString, Window};
use crate::action::{Action, InvokeAction};
use super::{
appearance::button_appearance,
appearance_builders, common_builders, slot_builders,
slots::{Slots, loading_overlay},
types::{ButtonVariant, Common},
};
#[derive(IntoElement)]
pub struct Button {
common: Common,
pub(super) variant: ButtonVariant,
slots: Slots,
on_activate: Option<InvokeAction>,
scale: f32,
}
impl Button {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
common: Common::new(id, label),
variant: ButtonVariant::default(),
slots: Slots::default(),
on_activate: None,
scale: 1.,
}
}
pub fn filled(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self::new(id, label).variant(ButtonVariant::Filled)
}
pub fn tonal(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self::new(id, label).variant(ButtonVariant::FilledTonal)
}
pub fn elevated(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self::new(id, label).variant(ButtonVariant::Elevated)
}
pub fn outlined(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self::new(id, label).variant(ButtonVariant::Outlined)
}
pub fn text(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self::new(id, label).variant(ButtonVariant::Text)
}
common_builders!();
appearance_builders!();
slot_builders!();
pub fn variant(mut self, variant: ButtonVariant) -> Self {
self.variant = variant;
self
}
pub fn loading(mut self, loading: bool) -> Self {
self.common.loading = loading;
self
}
#[cfg(feature = "feedback")]
pub(crate) fn scale(mut self, scale: f32) -> Self {
self.scale = scale;
self
}
pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_activate = Some(InvokeAction::Full(Rc::new(handler)));
self
}
pub fn on_accessible_activate(
mut self,
handler: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
self.on_activate = Some(InvokeAction::AccessibleOnly(Rc::new(handler)));
self
}
}
impl RenderOnce for Button {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let metrics = self.common.size.metrics();
let appearance = button_appearance(self.variant, None, self.common.tone);
let style = appearance.style(
metrics,
self.common.shape.corner(metrics.square, false),
58.,
);
let tint = self
.common
.appearance
.colors
.content(self.common.disabled, false)
.unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
let resting_content = self.slots.render(
self.common.label.clone(),
metrics,
self.scale,
window,
tint,
cx,
);
let content = if self.common.loading {
loading_overlay(
&self.common.id,
resting_content,
metrics.icon * self.scale,
tint,
window,
cx,
)
} else {
resting_content
};
self.common.render_scaled(
content,
style,
Action::Invoke(self.on_activate),
self.scale,
window,
cx,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn button_activation_modes_replace_each_other() {
let button = Button::new("b", "Open").on_accessible_activate(|_, _| {});
assert!(matches!(
button.on_activate,
Some(InvokeAction::AccessibleOnly(_))
));
let button = button.on_activate(|_, _| {});
assert!(matches!(button.on_activate, Some(InvokeAction::Full(_))));
let button = button.on_accessible_activate(|_, _| {});
assert!(matches!(
button.on_activate,
Some(InvokeAction::AccessibleOnly(_))
));
}
}