use super::{paint, Interact, Show};
use crate::theme::{radius, Text, Theme, RING};
use eframe::egui::{self, Rect, Vec2};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BarSide {
#[default]
Bottom,
Left,
}
#[must_use = "an icon button does nothing until it is added to a Ui"]
pub struct IconButton<'a> {
glyph: &'a str,
name: &'a str,
description: Option<&'a str>,
shortcut: Option<&'a str>,
size: f32,
active: bool,
bar: BarSide,
enabled: bool,
show: Show,
}
impl<'a> IconButton<'a> {
pub fn new(glyph: &'a str, name: &'a str) -> Self {
Self { glyph, name, description: None, shortcut: None, size: 28.0, active: false, bar: BarSide::Bottom, enabled: true, show: Show::Live }
}
pub fn ribbon(mut self) -> Self {
self.size = 32.0;
self
}
pub fn description(mut self, text: &'a str) -> Self {
self.description = Some(text);
self
}
pub fn shortcut(mut self, keys: &'a str) -> Self {
self.shortcut = Some(keys);
self
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn bar(mut self, side: BarSide) -> Self {
self.bar = side;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn show(mut self, show: Show) -> Self {
self.show = show;
self
}
}
pub fn tooltip_card(ui: &mut egui::Ui, name: &str, description: Option<&str>, shortcut: Option<&str>) {
let theme = Theme::of(ui);
ui.horizontal(|ui| {
ui.label(egui::RichText::new(name).font(theme.font(Text::Label)).color(theme.colors.text_primary));
if let Some(keys) = shortcut {
let g = paint::galley(ui, keys, theme.font(Text::Caption));
let (rect, _) = ui.allocate_exact_size(g.size() + Vec2::new(8.0, 4.0), egui::Sense::hover());
paint::key_chip(ui, rect.min, keys, &theme);
}
});
if let Some(text) = description {
ui.label(egui::RichText::new(text).font(theme.font(Text::Caption)).color(theme.colors.text_secondary));
}
}
impl egui::Widget for IconButton<'_> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
let theme = Theme::of(ui);
let c = &theme.colors;
let sense = if self.enabled { egui::Sense::click() } else { egui::Sense::hover() };
let (rect, response) = ui.allocate_exact_size(Vec2::splat(self.size), sense);
response.widget_info(|| {
let mut info = egui::WidgetInfo::labeled(egui::WidgetType::Button, self.enabled, self.name);
info.selected = Some(self.active);
info
});
if ui.is_rect_visible(rect) {
let state = Interact::of(&response, self.show, self.enabled);
let hover = ui.ctx().animate_bool_with_time(response.id.with("hover"), state.hovered, theme.motion_fast());
let on = ui.ctx().animate_bool_with_time(response.id.with("on"), self.active, theme.motion_base());
let pad = rect.shrink(1.0);
if on > 0.0 {
ui.painter().rect_filled(pad, radius::BUTTON, paint::lerp_color(egui::Color32::TRANSPARENT, c.accent_muted, on));
}
let overlay = if state.pressed { c.pressed } else { paint::lerp_color(egui::Color32::TRANSPARENT, c.hover, hover) };
ui.painter().rect_filled(pad, radius::BUTTON, overlay);
if on > 0.0 {
let bar = match self.bar {
BarSide::Bottom => Rect::from_min_max(egui::pos2(rect.left() + 6.0, rect.bottom() - RING - 1.0), egui::pos2(rect.right() - 6.0, rect.bottom() - 1.0)),
BarSide::Left => Rect::from_min_max(egui::pos2(rect.left() + 1.0, rect.top() + 6.0), egui::pos2(rect.left() + 1.0 + RING, rect.bottom() - 6.0)),
};
ui.painter().rect_filled(bar, 1.0, paint::lerp_color(egui::Color32::TRANSPARENT, c.accent_ring, on));
}
if state.focused {
paint::focus_ring(ui, pad, radius::BUTTON, &theme);
}
let ink = if self.enabled { c.text_primary } else { c.text_disabled };
let art = (self.size * 0.56).round();
let nudge = Vec2::new(0.0, if state.pressed { 1.0 } else { 0.0 });
let slot = Rect::from_center_size(rect.center() + nudge, Vec2::splat(art));
if !paint::icon(ui, self.glyph, slot, ink) {
let g = paint::galley(ui, self.glyph, theme.font(Text::Body));
ui.painter().galley(slot.center() - g.size() / 2.0, g, ink);
}
}
let (name, description, shortcut) = (self.name, self.description, self.shortcut);
let response = if self.enabled { response.on_hover_cursor(egui::CursorIcon::PointingHand) } else { response };
response.on_hover_ui(|ui| tooltip_card(ui, name, description, shortcut))
}
}
pub fn ribbon_group<R>(ui: &mut egui::Ui, label: &str, contents: impl FnOnce(&mut egui::Ui) -> R) -> R {
let theme = Theme::of(ui);
ui.vertical(|ui| {
ui.spacing_mut().item_spacing = Vec2::new(2.0, 2.0);
let row = ui.horizontal(contents);
let g = paint::galley(ui, label, theme.font(Text::Caption));
let (rect, _) = ui.allocate_exact_size(Vec2::new(row.response.rect.width().max(g.size().x), g.size().y), egui::Sense::hover());
ui.painter().galley(egui::pos2(rect.center().x - g.size().x / 2.0, rect.top()), g, theme.colors.text_secondary);
row.inner
})
.inner
}
pub fn group_separator(ui: &mut egui::Ui, height: f32) {
let theme = Theme::of(ui);
let (rect, _) = ui.allocate_exact_size(Vec2::new(9.0, height), egui::Sense::hover());
ui.painter().vline(rect.center().x, rect.y_range(), egui::Stroke::new(1.0, theme.colors.border_subtle));
}