use super::surface;
use crate::theme::{radius, space, Text, Theme, BORDER, RING};
use eframe::egui::{self, Rect, Stroke, Vec2};
pub fn overlay_card(theme: &Theme) -> egui::Frame {
surface::popup_frame(theme).inner_margin(egui::Margin::same(space::MD as i8))
}
pub fn title(text: impl Into<String>, theme: &Theme) -> egui::RichText {
egui::RichText::new(text).font(theme.font(Text::Label)).color(theme.colors.text_primary)
}
pub fn caption(text: impl Into<String>, theme: &Theme) -> egui::RichText {
egui::RichText::new(text).font(theme.font(Text::Caption)).color(theme.colors.text_secondary)
}
pub fn muted(text: impl Into<String>, theme: &Theme) -> egui::RichText {
egui::RichText::new(text).font(theme.font(Text::Body)).color(theme.colors.text_secondary)
}
pub fn button_width(ui: &egui::Ui, text: &str, icon: bool) -> f32 {
let theme = Theme::of(ui);
let pad = theme.control_padding() + 2.0;
let galley = super::paint::galley(ui, text, theme.font(Text::Body));
let lead = if icon { BUTTON_ICON + space::SM } else { 0.0 };
(pad * 2.0 + lead + galley.size().x).max(theme.control_height())
}
const BUTTON_ICON: f32 = 16.0;
pub fn dialog_frame(theme: &Theme) -> egui::Frame {
theme.raised_frame().shadow(theme.shadow_medium()).inner_margin(egui::Margin::same(space::LG as i8))
}
pub fn menu_entry(ui: &mut egui::Ui, label: &str) -> egui::Response {
let height = Theme::of(ui).row_height();
super::chrome_toolbar::ToolRow::new(super::chrome_toolbar::RowIcon::None, label, Vec2::new(ui.available_width(), height)).show(ui).0
}
pub fn removable_chip(ui: &mut egui::Ui, name: &str) -> egui::Response {
let theme = Theme::of(ui);
let c = &theme.colors;
let height = theme.control_height_sm();
let g = super::paint::galley(ui, name, theme.font(Text::Body));
let width = space::MD + g.size().x + space::SM + CHIP_CROSS + space::SM;
let (rect, response) = ui.allocate_exact_size(Vec2::new(width, height), egui::Sense::click());
response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, format!("Remove {name}")));
if ui.is_rect_visible(rect) {
let fill = if response.is_pointer_button_down_on() {
crate::theme::over(c.bg_raised, c.pressed)
} else if response.hovered() {
crate::theme::over(c.bg_raised, c.hover)
} else {
c.bg_raised
};
ui.painter().rect(rect, height / 2.0, fill, Stroke::new(BORDER, c.border_strong), egui::StrokeKind::Inside);
if response.has_focus() {
super::paint::focus_ring(ui, rect, height / 2.0, &theme);
}
ui.painter().galley(egui::pos2(rect.left() + space::MD, rect.center().y - g.size().y / 2.0), g, c.text_primary);
let cross = Rect::from_center_size(egui::pos2(rect.right() - space::SM - CHIP_CROSS / 2.0, rect.center().y), Vec2::splat(CHIP_CROSS));
let ink = if response.hovered() { c.text_primary } else { c.text_secondary };
super::paint::cross(ui.painter(), cross.center(), 3.0, Stroke::new(1.3, ink));
}
response.on_hover_cursor(egui::CursorIcon::PointingHand)
}
const CHIP_CROSS: f32 = 14.0;
pub fn select_row(ui: &mut egui::Ui, size: Vec2, text: &str, selected: bool) -> egui::Response {
let theme = Theme::of(ui);
let c = &theme.colors;
let (rect, response) = ui.allocate_exact_size(size, egui::Sense::click());
response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, selected, text));
if ui.is_rect_visible(rect) {
if selected {
ui.painter().rect_filled(rect, radius::INPUT, c.accent_muted);
let edge = Rect::from_min_max(rect.left_top() + Vec2::new(0.0, space::XS), egui::pos2(rect.left() + RING, rect.bottom() - space::XS));
ui.painter().rect_filled(edge, 1.0, c.accent_ring);
}
if response.hovered() {
let overlay = if response.is_pointer_button_down_on() { c.pressed } else { c.hover };
ui.painter().rect_filled(rect, radius::INPUT, overlay);
}
let mut job = egui::text::LayoutJob::simple_singleline(text.to_owned(), theme.font(Text::Body), c.text_primary);
job.wrap = egui::text::TextWrapping::truncate_at_width((rect.width() - space::MD * 2.0).max(0.0));
let g = ui.fonts_mut(|f| f.layout_job(job));
ui.painter().galley(egui::pos2(rect.left() + space::MD, rect.center().y - g.size().y / 2.0), g, c.text_primary);
}
response.on_hover_cursor(egui::CursorIcon::PointingHand)
}
pub fn close_square(ui: &mut egui::Ui, edge: f32, label: &str) -> egui::Response {
let theme = Theme::of(ui);
let c = &theme.colors;
let (rect, response) = ui.allocate_exact_size(Vec2::splat(edge), egui::Sense::click());
response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, label));
if ui.is_rect_visible(rect) {
if response.hovered() {
let overlay = if response.is_pointer_button_down_on() { c.pressed } else { c.hover };
ui.painter().rect_filled(rect, radius::BUTTON, overlay);
}
if response.has_focus() {
super::paint::focus_ring(ui, rect, radius::BUTTON, &theme);
}
let ink = if response.hovered() { c.text_primary } else { c.text_secondary };
super::paint::cross(ui.painter(), rect.center(), 4.0, Stroke::new(1.5, ink));
}
response.on_hover_cursor(egui::CursorIcon::PointingHand)
}
pub fn fill_button(ui: &mut egui::Ui, size: Vec2, text: &str, selected: bool) -> egui::Response {
let theme = Theme::of(ui);
let c = &theme.colors;
let (rect, response) = ui.allocate_exact_size(size, egui::Sense::click());
let enabled = ui.is_enabled();
response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, enabled, selected, text));
if ui.is_rect_visible(rect) {
let base = if selected { crate::theme::over(c.bg_raised, c.accent_muted) } else { c.bg_raised };
let fill = if !enabled {
base
} else if response.is_pointer_button_down_on() {
crate::theme::over(base, c.pressed)
} else if response.hovered() {
crate::theme::over(base, c.hover)
} else {
base
};
let border = if selected { c.accent_ring } else { c.border_subtle };
ui.painter().rect(rect, radius::BUTTON, fill, Stroke::new(BORDER, border), egui::StrokeKind::Inside);
if response.has_focus() {
super::paint::focus_ring(ui, rect, radius::BUTTON, &theme);
}
if !text.is_empty() {
let mut job = egui::text::LayoutJob::simple_singleline(text.to_owned(), theme.font(Text::Body), fill_button_ink(ui, selected));
job.wrap = egui::text::TextWrapping::truncate_at_width((rect.width() - 2.0 * space::XS).max(0.0));
let g = ui.fonts_mut(|f| f.layout_job(job));
ui.painter().galley(rect.center() - g.size() / 2.0, g, fill_button_ink(ui, selected));
}
}
if enabled { response.on_hover_cursor(egui::CursorIcon::PointingHand) } else { response }
}
pub fn bar_button(ui: &mut egui::Ui, icon: &str, text: &str) -> egui::Response {
let theme = Theme::of(ui);
let c = &theme.colors;
let font = theme.font(Text::Body);
let glyph = super::paint::galley(ui, icon, font.clone());
let label = super::paint::galley(ui, text, font);
let height = ui.spacing().interact_size.y;
let width = space::XS + glyph.size().x + space::XS + label.size().x + space::XS;
let (rect, response) = ui.allocate_exact_size(Vec2::new(width, height), egui::Sense::click());
let enabled = ui.is_enabled();
response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, enabled, format!("{icon} {text}")));
if ui.is_rect_visible(rect) {
if enabled && response.hovered() {
let overlay = if response.is_pointer_button_down_on() { c.pressed } else { c.hover };
ui.painter().rect_filled(rect, radius::INPUT, overlay);
}
if response.has_focus() {
super::paint::focus_ring(ui, rect, radius::INPUT, &theme);
}
let ink = if enabled { c.text_primary } else { c.text_disabled };
let mut x = rect.left() + space::XS;
let gy = rect.center().y - glyph.size().y / 2.0;
let gw = glyph.size().x;
ui.painter().galley(egui::pos2(x, gy), glyph, if enabled { c.text_secondary } else { c.text_disabled });
x += gw + space::XS;
ui.painter().galley(egui::pos2(x, rect.center().y - label.size().y / 2.0), label, ink);
}
if enabled { response.on_hover_cursor(egui::CursorIcon::PointingHand) } else { response }
}
pub fn fill_button_ink(ui: &egui::Ui, selected: bool) -> egui::Color32 {
let c = Theme::of(ui).colors;
if !ui.is_enabled() {
c.text_disabled
} else if selected {
c.accent_text
} else {
c.text_primary
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StatusColors {
pub error: egui::Color32,
pub partial: egui::Color32,
pub note: egui::Color32,
pub info: egui::Color32,
}
pub fn status_colors(theme: &Theme) -> StatusColors {
let c = &theme.colors;
StatusColors {
error: c.danger,
partial: crate::theme::mix(c.warning, c.danger, 0.5),
note: c.warning,
info: c.info,
}
}