use super::{paint, Interact, Show};
use crate::theme::{radius, space, Text, Theme, BORDER};
use eframe::egui::{self, Color32, Stroke, Vec2};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Variant {
Primary,
#[default]
Secondary,
Ghost,
Danger,
DangerFilled,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Size {
#[default]
Auto,
Sm,
Md,
}
#[must_use = "a button does nothing until it is added to a Ui"]
pub struct Button<'a> {
text: &'a str,
variant: Variant,
size: Size,
icon: Option<&'a str>,
shortcut: Option<&'a str>,
busy: bool,
enabled: bool,
min_width: f32,
show: Show,
}
impl<'a> Button<'a> {
pub fn new(text: &'a str) -> Self {
Self { text, variant: Variant::default(), size: Size::default(), icon: None, shortcut: None, busy: false, enabled: true, min_width: 0.0, show: Show::Live }
}
pub fn primary(text: &'a str) -> Self {
Self::new(text).variant(Variant::Primary)
}
pub fn ghost(text: &'a str) -> Self {
Self::new(text).variant(Variant::Ghost)
}
pub fn danger(text: &'a str) -> Self {
Self::new(text).variant(Variant::Danger)
}
pub fn variant(mut self, variant: Variant) -> Self {
self.variant = variant;
self
}
pub fn size(mut self, size: Size) -> Self {
self.size = size;
self
}
pub fn icon(mut self, glyph: &'a str) -> Self {
self.icon = Some(glyph);
self
}
pub fn shortcut(mut self, shortcut: &'a str) -> Self {
self.shortcut = Some(shortcut);
self
}
pub fn busy(mut self, busy: bool) -> Self {
self.busy = busy;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn min_width(mut self, width: f32) -> Self {
self.min_width = width;
self
}
pub fn show(mut self, show: Show) -> Self {
self.show = show;
self
}
}
pub(crate) fn rest_colors(theme: &Theme, variant: Variant) -> (Color32, Color32, Color32) {
let c = &theme.colors;
match variant {
Variant::Primary => (c.accent_fill, c.accent_fill, c.on_accent),
Variant::Secondary => (c.bg_raised, c.border_strong, c.text_primary),
Variant::Ghost => (Color32::TRANSPARENT, Color32::TRANSPARENT, c.text_primary),
Variant::Danger => (c.bg_raised, c.border_strong, c.danger),
Variant::DangerFilled => (c.danger_fill, c.danger_fill, c.on_accent),
}
}
const ICON: f32 = 16.0;
impl egui::Widget for Button<'_> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
let theme = Theme::of(ui);
let c = &theme.colors;
let height = match self.size {
Size::Auto => theme.control_height(),
Size::Sm => theme.control_height_sm(),
Size::Md => 28.0,
};
let pad = theme.control_padding() + 2.0;
let text = paint::galley(ui, self.text, theme.font(Text::Body));
let shortcut = self.shortcut.map(|s| paint::galley(ui, s, theme.font(Text::Caption)));
let lead = if self.icon.is_some() { ICON + space::SM } else { 0.0 };
let trail = shortcut.as_ref().map_or(0.0, |g| g.size().x + space::LG);
let width = (pad * 2.0 + lead + text.size().x + trail).max(self.min_width).max(height);
let active = self.enabled && !self.busy;
let sense = if active { egui::Sense::click() } else { egui::Sense::hover() };
let (rect, response) = ui.allocate_exact_size(Vec2::new(width, height), sense);
response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, self.enabled, self.text));
if !ui.is_rect_visible(rect) {
return response;
}
let state = Interact::of(&response, self.show, active);
let hover = ui.ctx().animate_bool_with_time(response.id.with("hover"), state.hovered, theme.motion_fast());
let press = ui.ctx().animate_bool_with_time(response.id.with("press"), state.pressed, theme.motion_fast());
let (mut fill, mut border, mut ink) = rest_colors(&theme, self.variant);
if !self.enabled {
ink = c.text_disabled;
match self.variant {
Variant::Ghost => {}
_ => {
fill = crate::theme::over(c.bg_panel, c.hover);
border = c.border_subtle;
}
}
} else {
let overlay = paint::lerp_color(paint::lerp_color(Color32::TRANSPARENT, c.hover, hover), c.pressed, press);
fill = match self.variant {
Variant::Ghost => overlay,
Variant::Primary | Variant::DangerFilled => {
let hovered = paint::lerp_color(fill, crate::theme::Palette::fill_hover(fill), hover);
paint::lerp_color(hovered, crate::theme::Palette::fill_pressed(fill), press)
}
_ => crate::theme::over(fill, overlay),
};
if matches!(self.variant, Variant::Primary | Variant::DangerFilled) {
border = fill;
}
}
let radius = if height <= 24.0 { radius::INPUT } else { radius::BUTTON };
ui.painter().rect(rect, radius, fill, Stroke::new(BORDER, border), egui::StrokeKind::Inside);
if state.focused {
paint::focus_ring(ui, rect, radius, &theme);
}
let nudge = Vec2::new(0.0, press.round());
let content = lead + text.size().x + trail;
let mut x = if shortcut.is_some() { rect.left() + pad } else { rect.center().x - content / 2.0 };
let cy = rect.center().y;
let slot = egui::Rect::from_center_size(egui::pos2(x + ICON / 2.0, cy), Vec2::splat(ICON)).translate(nudge);
if self.busy {
if self.icon.is_some() {
paint::spinner(ui, slot.center(), 6.0, ink, &theme);
} else {
paint::spinner(ui, rect.center() + nudge, 6.0, ink, &theme);
}
} else if let Some(glyph) = self.icon {
paint::icon(ui, glyph, slot, ink);
}
x += lead;
if !(self.busy && self.icon.is_none()) {
ui.painter().galley(egui::pos2(x, cy - text.size().y / 2.0) + nudge, text, ink);
}
if let Some(g) = shortcut {
let ink = if self.enabled && !matches!(self.variant, Variant::Primary | Variant::DangerFilled) { c.text_secondary } else { ink };
ui.painter().galley(egui::pos2(rect.right() - pad - g.size().x, cy - g.size().y / 2.0) + nudge, g, ink);
}
if active {
response.on_hover_cursor(egui::CursorIcon::PointingHand)
} else {
response
}
}
}