use crate::{
BackgroundSizing, Brush, ColorTransition, CommonState, CommonStates, ControlBorder,
ControlStates, FocusVisual, ThemeResources, control_text_style,
};
use std::{fmt, rc::Rc};
use waft_embedder::{Color, FontWeight};
use waft_foundation::{App, Listener};
use waft_widgets::*;
pub type ButtonTemplate = Rc<dyn Fn(&mut App, BuildContext, ControlStates, WidgetRef) -> WidgetRef>;
pub const BUTTON_BORDER_THICKNESS: f64 = 1.0;
pub const BUTTON_FOCUS_VISUAL_MARGIN: [f64; 4] = [-3.0; 4];
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ButtonStyle {
#[default]
Default,
Accent,
Subtle,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ButtonBrushes {
pub background: Color,
pub border: Brush,
pub foreground: Color,
}
impl ButtonStyle {
pub fn brushes(self, resources: &ThemeResources, state: CommonState) -> ButtonBrushes {
let r = resources;
match (self, state) {
(ButtonStyle::Default, CommonState::Normal) => ButtonBrushes {
background: r.button_background,
border: Brush::ControlElevation(r.button_border_brush),
foreground: r.button_foreground,
},
(ButtonStyle::Default, CommonState::PointerOver) => ButtonBrushes {
background: r.button_background_pointer_over,
border: Brush::ControlElevation(r.button_border_brush_pointer_over),
foreground: r.button_foreground_pointer_over,
},
(ButtonStyle::Default, CommonState::Pressed) => ButtonBrushes {
background: r.button_background_pressed,
border: Brush::Solid(r.button_border_brush_pressed),
foreground: r.button_foreground_pressed,
},
(ButtonStyle::Default, CommonState::Disabled) => ButtonBrushes {
background: r.button_background_disabled,
border: Brush::Solid(r.button_border_brush_disabled),
foreground: r.button_foreground_disabled,
},
(ButtonStyle::Accent, CommonState::Normal) => ButtonBrushes {
background: r.accent_button_background,
border: Brush::ControlElevation(r.accent_button_border_brush),
foreground: r.accent_button_foreground,
},
(ButtonStyle::Accent, CommonState::PointerOver) => ButtonBrushes {
background: r.accent_button_background_pointer_over,
border: Brush::ControlElevation(r.accent_button_border_brush_pointer_over),
foreground: r.accent_button_foreground_pointer_over,
},
(ButtonStyle::Accent, CommonState::Pressed) => ButtonBrushes {
background: r.accent_button_background_pressed,
border: Brush::Solid(r.accent_button_border_brush_pressed),
foreground: r.accent_button_foreground_pressed,
},
(ButtonStyle::Accent, CommonState::Disabled) => ButtonBrushes {
background: r.accent_button_background_disabled,
border: Brush::Solid(r.accent_button_border_brush_disabled),
foreground: r.accent_button_foreground_disabled,
},
(ButtonStyle::Subtle, CommonState::Normal) => ButtonBrushes {
background: r.subtle_button_background,
border: Brush::Solid(r.subtle_button_border_brush),
foreground: r.subtle_button_foreground,
},
(ButtonStyle::Subtle, CommonState::PointerOver) => ButtonBrushes {
background: r.subtle_button_background_pointer_over,
border: Brush::Solid(r.subtle_button_border_brush_pointer_over),
foreground: r.subtle_button_foreground_pointer_over,
},
(ButtonStyle::Subtle, CommonState::Pressed) => ButtonBrushes {
background: r.subtle_button_background_pressed,
border: Brush::Solid(r.subtle_button_border_brush_pressed),
foreground: r.subtle_button_foreground_pressed,
},
(ButtonStyle::Subtle, CommonState::Disabled) => ButtonBrushes {
background: r.subtle_button_background_disabled,
border: Brush::Solid(r.subtle_button_border_brush_disabled),
foreground: r.subtle_button_foreground_disabled,
},
}
}
pub fn background_sizing(self) -> BackgroundSizing {
match self {
ButtonStyle::Accent => BackgroundSizing::OuterBorderEdge,
ButtonStyle::Default | ButtonStyle::Subtle => BackgroundSizing::InnerBorderEdge,
}
}
}
#[derive(Clone)]
pub struct Button {
pub content: WidgetRef,
pub click: Listener,
pub style: ButtonStyle,
pub padding: Option<[f64; 4]>,
pub is_enabled: bool,
pub is_tab_stop: bool,
pub focus_node: Option<FocusNodeRef>,
pub template: Option<ButtonTemplate>,
pub key: Option<KeyRef>,
}
impl Button {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(content: impl IntoWidget<K>, click: Listener) -> Button {
Button {
content: content.into_widget(),
click,
style: ButtonStyle::Default,
padding: None,
is_enabled: true,
is_tab_stop: true,
focus_node: None,
template: None,
key: None,
}
}
pub fn text(text: impl Into<String>, click: Listener) -> Button {
Button::new(Text::new(text), click)
}
pub fn style(mut self, style: ButtonStyle) -> Button {
self.style = style;
self
}
pub fn padding(mut self, padding: [f64; 4]) -> Button {
self.padding = Some(padding);
self
}
pub fn is_enabled(mut self, enabled: bool) -> Button {
self.is_enabled = enabled;
self
}
pub fn is_tab_stop(mut self, value: bool) -> Self {
self.is_tab_stop = value;
self
}
pub fn focus_node(mut self, node: FocusNodeRef) -> Self {
self.focus_node = Some(node);
self
}
pub fn template(
mut self,
builder: impl Fn(&mut App, BuildContext, ControlStates, WidgetRef) -> WidgetRef + 'static,
) -> Self {
self.template = Some(Rc::new(builder));
self
}
pub fn key(mut self, key: KeyRef) -> Button {
self.key = Some(key);
self
}
}
impl fmt::Debug for Button {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Button")
.field("style", &self.style)
.field("is_enabled", &self.is_enabled)
.finish_non_exhaustive()
}
}
impl StatelessWidget for Button {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
let (content, style, padding) = (self.content.clone(), self.style, self.padding);
let custom_template = self.template.clone();
let mut states = CommonStates::new(self.click.clone(), move |app, context, states| {
if let Some(builder) = &custom_template {
builder(app, context, states, content.clone())
} else {
template(app, context, style, padding, content.clone(), states)
}
})
.is_enabled(self.is_enabled)
.is_tab_stop(self.is_tab_stop);
if let Some(node) = self.focus_node.clone() {
states = states.focus_node(node);
}
states.into_widget()
}
}
fn template(
app: &mut App,
context: BuildContext,
style: ButtonStyle,
padding: Option<[f64; 4]>,
content: WidgetRef,
states: ControlStates,
) -> WidgetRef {
let r = ThemeResources::of(app, context);
let brushes = style.brushes(&r, states.common);
let padding = padding.unwrap_or(r.button_padding);
let corner_radius = r.control_corner_radius[0];
let text = control_text_style(
&r,
r.control_content_theme_font_size,
FontWeight::W400,
brushes.foreground,
);
let label = Center::new()
.width_factor(1.0)
.height_factor(1.0)
.child(DefaultTextStyle::new(text, content))
.into_widget();
let presenter = ColorTransition::new(
brushes.background,
r.control_faster_animation_duration,
move |_app, background| {
ControlBorder::new(Brush::Solid(background), brushes.border)
.border_thickness(BUTTON_BORDER_THICKNESS)
.corner_radius(corner_radius)
.background_sizing(style.background_sizing())
.padding(padding)
.child(label.clone())
.into_widget()
},
);
FocusVisual::new(presenter, r.theme)
.visible(states.focused)
.margin(BUTTON_FOCUS_VISUAL_MARGIN)
.corner_radius(corner_radius)
.into_widget()
}