use egui::{Color32, CornerRadius, Response, RichText, Role, Sense, Stroke, Ui, Vec2, WidgetInfo};
use crate::Icons;
use crate::close_glyph;
use crate::components::style::{ButtonVariant, IconButtonSize, Size};
use crate::components::{Component, IconComponent};
#[derive(Clone, Copy, Debug)]
pub struct Button<'a> {
label: &'a str,
variant: ButtonVariant,
size: Size,
enabled: bool,
selected: bool,
width: Option<f32>,
tooltip: Option<&'a str>,
fill: Option<Color32>,
text_color: Option<Color32>,
stroke: Option<Stroke>,
radius: Option<u8>,
}
impl<'a> Button<'a> {
pub fn new(label: &'a str) -> Self {
Self {
label,
variant: ButtonVariant::Primary,
size: Size::Md,
enabled: true,
selected: false,
width: None,
tooltip: None,
fill: None,
text_color: None,
stroke: None,
radius: None,
}
}
pub fn variant(mut self, variant: ButtonVariant) -> Self {
self.variant = variant;
self
}
pub fn size(mut self, size: Size) -> Self {
self.size = size;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}
pub fn tooltip(mut self, tooltip: &'a str) -> Self {
self.tooltip = Some(tooltip);
self
}
pub fn fill(mut self, fill: Color32) -> Self {
self.fill = Some(fill);
self
}
pub fn text_color(mut self, color: Color32) -> Self {
self.text_color = Some(color);
self
}
pub fn stroke(mut self, stroke: Stroke) -> Self {
self.stroke = Some(stroke);
self
}
pub fn radius(mut self, radius: u8) -> Self {
self.radius = Some(radius);
self
}
pub fn label(&self) -> &'a str {
self.label
}
}
impl Component for Button<'_> {
type Output = Response;
fn show(self, ui: &mut Ui) -> Response {
let visuals = ui.visuals().clone();
let (fill, stroke, text_color) = match self.variant {
ButtonVariant::Primary => (visuals.selection.bg_fill, Stroke::NONE, visuals.selection.stroke.color),
ButtonVariant::Secondary => (
visuals.widgets.inactive.weak_bg_fill,
Stroke::new(1.0, visuals.widgets.inactive.bg_stroke.color),
visuals.text_color(),
),
ButtonVariant::Outline => (
Color32::TRANSPARENT,
Stroke::new(1.0, visuals.widgets.inactive.bg_stroke.color),
visuals.text_color(),
),
ButtonVariant::Ghost => (Color32::TRANSPARENT, Stroke::NONE, visuals.text_color()),
ButtonVariant::Destructive => {
let fill = visuals.error_fg_color;
(fill, Stroke::NONE, crate::theme::readable_on(fill, visuals.panel_fill))
}
ButtonVariant::Link => (Color32::TRANSPARENT, Stroke::NONE, visuals.hyperlink_color),
};
let (fill, text_color) = if self.selected {
(visuals.selection.bg_fill, visuals.selection.stroke.color)
} else {
(fill, text_color)
};
let style = crate::style::get(ui.ctx());
let fill = self.fill.unwrap_or(fill);
let text_color = self.text_color.unwrap_or(text_color);
let border = if self.variant == ButtonVariant::Outline {
style.border_width.max(1.0)
} else {
style.border_width
};
let stroke = self.stroke.unwrap_or(if stroke.width > 0.0 {
Stroke::new(border, stroke.color)
} else {
stroke
});
let radius = self.radius.map_or_else(|| style.control(), CornerRadius::same);
let width = self.width.unwrap_or_else(|| self.size.min_width());
let rich = RichText::new(self.label).size(style.text(self.size)).color(text_color);
if self.variant == ButtonVariant::Link {
let mut response = ui.add_enabled(self.enabled, egui::Link::new(rich));
if let Some(tooltip) = self.tooltip {
response = response.on_hover_text(tooltip);
}
return response;
}
let padding = style.size_padding(self.size);
let height = style.size_height(self.size);
let mut button = egui::Button::new(rich)
.wrap_mode(egui::TextWrapMode::Extend)
.stroke(stroke)
.corner_radius(radius)
.min_size(Vec2::new(width, height));
if self.variant == ButtonVariant::Ghost && !self.selected && self.fill.is_none() {
button = button.frame_when_inactive(false);
} else {
button = button.fill(fill);
}
let text_width = ui
.painter()
.layout_no_wrap(self.label.to_owned(), style.font(self.size.text()), text_color)
.size()
.x;
let size = Vec2::new(
(text_width + 2.0 * (padding.x + stroke.width)).ceil().max(width),
height,
);
let (rect, _) = ui.allocate_exact_size(size, egui::Sense::hover());
let previous = ui.spacing().button_padding;
ui.spacing_mut().button_padding = padding;
let enabled = self.enabled;
let mut response = ui.put(rect, |ui: &mut Ui| ui.add_enabled(enabled, button));
ui.spacing_mut().button_padding = previous;
if self.selected {
response.widget_info(|| WidgetInfo::selected(Role::Button, self.enabled, true, self.label));
}
if let Some(tooltip) = self.tooltip {
response = response.on_hover_text(tooltip);
}
response
}
}
#[derive(Clone, Copy, Debug)]
pub struct IconButton<'a> {
icon: &'a str,
tooltip: &'a str,
size: IconButtonSize,
active: bool,
open: bool,
enabled: bool,
color: Option<Color32>,
radius: Option<u8>,
}
impl<'a> IconButton<'a> {
pub fn new(icon: &'a str, tooltip: &'a str) -> Self {
Self {
icon,
tooltip,
size: IconButtonSize::Action,
active: false,
open: false,
enabled: true,
color: None,
radius: None,
}
}
pub fn size(mut self, size: IconButtonSize) -> Self {
self.size = size;
self
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn open(mut self, open: bool) -> Self {
self.open = open;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn color(mut self, color: Color32) -> Self {
self.color = Some(color);
self
}
pub fn radius(mut self, radius: u8) -> Self {
self.radius = Some(radius);
self
}
}
impl IconComponent for IconButton<'_> {
type Output = Response;
fn show(self, ui: &mut Ui, icons: &mut Icons) -> Response {
let side = self.size.side();
let sense = if self.enabled { Sense::click() } else { Sense::hover() };
let (rect, response) = ui.allocate_exact_size(Vec2::splat(side), sense);
response
.widget_info(|| WidgetInfo::selected(Role::Button, self.enabled, self.active || self.open, self.tooltip));
let lit = self.active || self.open;
let fill = if !self.enabled {
Color32::TRANSPARENT
} else if response.is_pointer_button_down_on() {
ui.visuals().widgets.active.weak_bg_fill
} else if lit || response.hovered() {
ui.visuals().widgets.hovered.weak_bg_fill
} else {
Color32::TRANSPARENT
};
let item = crate::style::get(ui.ctx()).item_radius;
let radius = self.radius.unwrap_or(match self.size {
IconButtonSize::Small => item.saturating_sub(1),
IconButtonSize::Action | IconButtonSize::Tool => item,
});
ui.painter().rect_filled(rect, CornerRadius::same(radius), fill);
if self.active && self.size == IconButtonSize::Action {
let underline = egui::Rect::from_min_max(
egui::pos2(rect.left() + 7.0, rect.bottom() - 2.0),
egui::pos2(rect.right() - 7.0, rect.bottom()),
);
ui.painter().rect_filled(
underline,
crate::style::get(ui.ctx()).mark(),
ui.visuals().selection.bg_fill,
);
}
let color = if self.enabled {
self.color.unwrap_or_else(|| ui.visuals().text_color())
} else {
ui.visuals().weak_text_color().gamma_multiply(0.45)
};
icons.paint(ui, self.icon, rect.center(), self.size.glyph(), color);
response.on_hover_text(self.tooltip)
}
}
pub struct CloseButton<'a> {
tooltip: &'a str,
size: f32,
}
impl<'a> CloseButton<'a> {
pub fn new(tooltip: &'a str) -> Self {
Self { tooltip, size: 14.0 }
}
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
}
impl Component for CloseButton<'_> {
type Output = bool;
fn show(self, ui: &mut Ui) -> bool {
close_glyph(ui, self.size, self.tooltip)
}
}