use crate::{
AnimationManager,
Background,
Constraints,
EventCtx,
EventStatus,
InputEvent,
Interaction,
IntoThemed,
LayoutBox,
Length,
MeasureContext,
MeasureResult,
PaintContext,
RectCommand,
Style,
StyleBuilder,
TextCommand,
Widget,
WidgetContent,
WidgetId,
properties::{ DEFAULT_CURSOR_ICON, DEFAULT_FONT_SIZE, DEFAULT_POINTER_CURSOR_ICON },
};
use smol_str::SmolStr;
use std::cell::Cell;
pub struct Button {
key: Option<SmolStr>,
dirty: bool,
style: Style,
inherited_style: Style,
computed_style: Style,
hover_style: Option<Style>,
pressed_style: Option<Style>,
disabled_style: Option<Style>,
focus_style: Option<Style>,
interaction: Interaction,
content: SmolStr,
layout_box: LayoutBox,
content_size: Cell<(f32, f32)>,
anim_id: WidgetId,
}
impl Button {
pub fn new() -> Self {
let mut interaction = Interaction::new();
interaction.focusable = true;
interaction.hover_cursor = Some(DEFAULT_POINTER_CURSOR_ICON);
let mut button = Self {
key: None,
dirty: true,
style: Style::default(),
inherited_style: Style::default(),
computed_style: Style::default(),
hover_style: None,
pressed_style: None,
disabled_style: None,
focus_style: None,
interaction,
content: SmolStr::new(""),
layout_box: LayoutBox::default(),
content_size: Cell::new((0.0, 0.0)),
anim_id: WidgetId::new_unique(),
};
button.recompute_style();
button
}
pub fn key(mut self, key: impl Into<SmolStr>) -> Self {
self.key = Some(key.into());
self
}
pub fn label(mut self, content: impl Into<SmolStr>) -> Self {
self.content = content.into();
self.mark_dirty();
self
}
pub fn font(mut self, font: impl Into<SmolStr>) -> Self {
self.style.font = Some(font.into());
self.mark_dirty();
self
}
pub fn hover_background<M>(mut self, background: impl IntoThemed<Background, M>) -> Self {
self.hover_style.get_or_insert_with(Style::default).background = Some(
background.resolve_themed()
);
self.mark_dirty();
self
}
pub fn pressed_background<M>(mut self, background: impl IntoThemed<Background, M>) -> Self {
self.pressed_style.get_or_insert_with(Style::default).background = Some(
background.resolve_themed()
);
self.mark_dirty();
self
}
pub fn disabled_background<M>(mut self, background: impl IntoThemed<Background, M>) -> Self {
self.disabled_style.get_or_insert_with(Style::default).background = Some(
background.resolve_themed()
);
self.mark_dirty();
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.interaction.set_enabled(enabled);
self.mark_dirty();
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.interaction.set_enabled(!disabled);
self.mark_dirty();
self
}
fn recompute_style(&mut self) {
let patch = if !self.interaction.enabled {
self.disabled_style.as_ref()
} else if self.interaction.pressed {
self.pressed_style.as_ref().or(self.hover_style.as_ref())
} else if self.interaction.focused {
self.focus_style.as_ref()
} else if self.interaction.hovered {
self.hover_style.as_ref()
} else {
None
};
let base = self.inherited_style.inherit_style(&self.style);
self.computed_style = match patch {
Some(patch) => base.overlay(patch),
None => base,
};
self.interaction.hover_cursor = self.computed_style.cursor
.map(crate::Cursor::to_winit)
.or(
Some(
if self.interaction.enabled {
DEFAULT_POINTER_CURSOR_ICON
} else {
DEFAULT_CURSOR_ICON
}
)
);
}
}
impl Default for Button {
fn default() -> Self {
Self::new()
}
}
impl StyleBuilder for Button {
fn style_mut(&mut self) -> &mut Style {
&mut self.style
}
fn mark_dirty(&mut self) {
self.dirty = true;
self.recompute_style();
}
}
impl WidgetContent for Button {
fn with_content(self, content: impl Into<SmolStr>) -> Self {
self.label(content)
}
}
crate::impl_interaction_builders!(Button);
crate::impl_themed_style_builders!(Button; hover_style => hover_style, pressed_style => pressed_style, disabled_style => disabled_style, focus_style => focus_style);
impl Widget for Button {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn debug_name(&self) -> &'static str {
"Widget#Button"
}
fn get_key(&self) -> Option<&SmolStr> {
self.key.as_ref()
}
fn is_dirty(&self) -> bool {
self.dirty
}
fn set_dirty(&mut self, dirty: bool) {
self.dirty = dirty;
}
fn style(&self) -> &Style {
&self.style
}
fn style_mut(&mut self) -> &mut Style {
&mut self.style
}
fn computed_style(&self) -> &Style {
&self.computed_style
}
fn children(&self) -> &[Box<dyn Widget>] {
&[]
}
fn interaction(&self) -> Option<&Interaction> {
Some(&self.interaction)
}
fn interaction_mut(&mut self) -> Option<&mut Interaction> {
Some(&mut self.interaction)
}
fn layout(&mut self, rect: LayoutBox) {
self.layout_box = rect;
}
fn layout_box(&self) -> &LayoutBox {
&self.layout_box
}
fn measure(&self, ctx: &mut MeasureContext, constraints: Constraints) -> MeasureResult {
let scale_factor = ctx.scale_factor;
let style = &self.computed_style;
let font_size = style.font_size
.map(|s| s.to_physical(scale_factor))
.unwrap_or(DEFAULT_FONT_SIZE.to_physical(scale_factor));
let letter_spacing = style.letter_spacing
.map(|ls| ls.value().to_physical(scale_factor))
.unwrap_or(0.0);
let line_height = style.line_height
.map(|lh| lh.value().to_physical(scale_factor))
.unwrap_or(0.0);
let result = ctx.text.measure(
&self.content,
style.font.as_deref(),
font_size,
style.font_weight.unwrap_or_default(),
style.font_style.unwrap_or_default(),
letter_spacing,
line_height,
constraints.max_width
);
self.content_size.set((result.width, result.height));
let padding = style.padding.unwrap_or_default();
let width = result.width + padding.left.value() + padding.right.value();
let height = result.height + padding.top.value() + padding.bottom.value();
let (width, height) = constraints.constrain_size(width, height);
MeasureResult::new(width, height)
}
fn paint(&self, ctx: &mut PaintContext) {
log::trace!(
"paint -> '{}' x={} y={} dirty={:?}",
self.content,
self.layout_box.x,
self.layout_box.y,
self.is_dirty()
);
let style = &self.computed_style;
let scale = style.scale.unwrap_or(1.0);
let background_box = crate::scaled_layout_box(self.layout_box, scale);
if style.background.is_some() || style.border.is_some() {
let border = style.border.as_ref();
ctx.draw_rect(RectCommand {
position: (background_box.x, background_box.y),
size: (background_box.width, background_box.height),
background: style.background.clone(),
border_radius: border.map(|b| b.radius),
border_color: border.map(|b| b.color),
border_width: border.map(|b| b.width),
clip_rect: None,
});
}
self.paint_outline(ctx);
let (content_w, content_h) = self.content_size.get();
let padding = style.padding.unwrap_or_default();
let available_w = self.layout_box.width - padding.left.value() - padding.right.value();
let draw_max_width = available_w.max(content_w);
let text_x =
self.layout_box.x + padding.left.value() + (available_w - content_w).max(0.0) * 0.5;
let text_y =
self.layout_box.y +
padding.top.value() +
(self.layout_box.height - padding.top.value() - padding.bottom.value() - content_h).max(
0.0
) *
0.5;
let content_scale = style.content_scale.unwrap_or(scale);
let content_box = crate::scaled_layout_box(
LayoutBox { x: text_x, y: text_y, width: content_w, height: content_h },
content_scale
);
let mut text_style = style.clone();
let base_font_size = text_style.font_size
.map(|f| f.value())
.unwrap_or(DEFAULT_FONT_SIZE.value());
text_style.font_size = Some(Length::px(base_font_size * content_scale));
ctx.draw_text(TextCommand {
text: self.content.clone(),
position: (content_box.x, content_box.y),
style: text_style,
max_width: Some(draw_max_width),
clip_rect: None,
});
}
fn event(&mut self, event: &InputEvent, ctx: &mut EventCtx) -> EventStatus {
if !self.interaction.is_active() {
return EventStatus::Ignored;
}
let status = self.interaction.handle(event, ctx);
if matches!(status, EventStatus::Handled) {
self.recompute_style();
self.dirty = true;
}
status
}
fn content_eq(&self, other: &dyn Widget) -> bool {
let Some(other) = other.as_any().downcast_ref::<Button>() else {
return false;
};
self.content == other.content &&
self.style == other.style &&
self.hover_style == other.hover_style &&
self.pressed_style == other.pressed_style &&
self.disabled_style == other.disabled_style &&
self.focus_style == other.focus_style
}
fn cascade_style(&mut self, parent: &Style, anim: &mut AnimationManager) {
self.inherited_style = parent.clone();
self.recompute_style();
if crate::animate_computed_style(self.anim_id, &mut self.computed_style, anim) {
self.dirty = true;
}
}
fn after_interaction_transfer(&mut self) {
self.recompute_style();
}
fn transfer_measured_state(&mut self, old: &dyn Widget) {
if let Some(old) = old.as_any().downcast_ref::<Button>() {
self.content_size.set(old.content_size.get());
self.anim_id = old.anim_id;
}
}
fn anim_id(&self) -> WidgetId {
self.anim_id
}
}