use crate::ui_kit::chrome_toolbar::{LargeButton, RowIcon, ToolButton, ToolRow};
use eframe::egui;
use crate::icons::artwork;
pub const TOOLBAR_BTN: f32 = 28.0;
pub const LARGE_BTN: f32 = 64.0;
pub fn wrapped<R>(ui: &mut egui::Ui, contents: impl FnOnce(&mut egui::Ui) -> R) -> egui::InnerResponse<R> {
ui.horizontal_wrapped(contents)
}
fn square(ui: &egui::Ui) -> egui::Vec2 {
crate::viewport::touch::target_size(ui.ctx(), egui::vec2(TOOLBAR_BTN, TOOLBAR_BTN))
}
pub(crate) fn glyph_button<'a>(ui: &egui::Ui, glyph: &'a str) -> ToolButton<'a> {
ToolButton::new(glyph, square(ui))
}
pub fn button(ui: &mut egui::Ui, glyph: &str, tooltip: &str) -> egui::Response {
let button = glyph_button(ui, glyph);
ui.add(button).on_hover_text(tooltip)
}
pub fn large_button(ui: &mut egui::Ui, glyph: &str, label: &str, selected: bool, toggle: bool, enabled: bool) -> egui::Response {
ui.add_enabled(enabled, LargeButton::new(glyph, label, LARGE_BTN).selected(toggle && selected))
}
pub fn button_enabled(
ui: &mut egui::Ui,
enabled: bool,
glyph: &str,
tooltip: &str,
) -> egui::Response {
let button = glyph_button(ui, glyph);
ui.add_enabled(enabled, button).on_hover_text(tooltip)
}
pub fn toggle(ui: &mut egui::Ui, selected: bool, glyph: &str, tooltip: &str) -> egui::Response {
let button = glyph_button(ui, glyph).selected(selected);
ui.add(button).on_hover_text(tooltip)
}
pub fn toggle_enabled(ui: &mut egui::Ui, enabled: bool, selected: bool, glyph: &str, tooltip: &str) -> egui::Response {
ui.add_enabled(enabled, glyph_button(ui, glyph).selected(selected)).on_hover_text(tooltip)
}
pub struct SelectResult {
pub changed: Option<String>,
pub header_rect: egui::Rect,
pub item_rects: Vec<(String, egui::Rect)>,
}
const SELECT_ICON: f32 = 18.0;
const SELECT_CHEVRON: &str = "\u{25BE}";
const SELECT_TICK: &str = "\u{2713}";
fn switcher_row<'a>(label: &'a str, glyph: &'a str, width: f32) -> ToolRow<'a> {
assert!(artwork(glyph).is_some(), "workbench icon must be catalogued");
ToolRow::new(RowIcon::Workbench(glyph, SELECT_ICON), label, egui::vec2(width, TOOLBAR_BTN))
}
fn switcher_row_ui(
ui: &mut egui::Ui,
label: &str,
glyph: &str,
width: f32,
selected: bool,
marker: Option<&str>,
) -> egui::Response {
if let Some(marker) = marker {
assert!(artwork(marker).is_some(), "switcher marker must be catalogued");
}
switcher_row(label, glyph, width).selected(selected).marker(marker).show(ui).0
}
fn switcher_width(ui: &egui::Ui, options: &[(&str, &str, &str)]) -> f32 {
options
.iter()
.fold(0.0_f32, |widest, (label, _, glyph)| widest.max(ToolRow::width(ui, RowIcon::Workbench(glyph, SELECT_ICON), label, true)))
}
pub fn select(
ui: &mut egui::Ui,
id_source: &str,
current: &str,
options: &[(&str, &str, &str)],
) -> SelectResult {
egui_extras::install_image_loaders(ui.ctx());
let width = switcher_width(ui, options);
let current_option = options.iter().find(|(_, id, _)| *id == current)
.expect("selected workbench must be registered");
let header_width = switcher_width(ui, &[*current_option]);
let mut changed = None;
let mut item_rects = Vec::new();
let header = ui
.push_id(("toolbar-select", id_source), |ui| {
let (label, _, glyph) = current_option;
let phone = crate::viewport::touch::enabled(ui.ctx())
&& ui.ctx().content_rect().width() < crate::viewport::touch::NARROW_WIDTH;
let response = if phone {
button(ui, glyph, &format!("{label} — Switch workbench"))
} else {
switcher_row_ui(ui, label, glyph, header_width, false, Some(SELECT_CHEVRON))
.on_hover_text("Switch workbench")
};
egui::Popup::menu(&response).width(width).show(|ui| {
let height = (ui.ctx().content_rect().height() - 80.0).max(80.0);
ui.set_max_height(height);
egui::ScrollArea::vertical()
.max_height(height)
.show(ui, |ui| {
for (label, id, glyph) in options {
let selected = *id == current;
let marker = selected.then_some(SELECT_TICK);
let resp = switcher_row_ui(ui, label, glyph, width, selected, marker);
item_rects.push(((*id).to_string(), resp.rect));
if resp.clicked() {
if !selected {
changed = Some((*id).to_string());
}
ui.close();
}
}
});
});
response
})
.inner;
SelectResult {
changed,
header_rect: header.rect,
item_rects,
}
}
pub(crate) fn menu_row_widget<'a>(glyph: &'a str, label: &'a str, width: f32, selected: bool) -> MenuRow<'a> {
MenuRow { glyph, label, width, selected }
}
pub(crate) struct MenuRow<'a> {
glyph: &'a str,
label: &'a str,
width: f32,
selected: bool,
}
impl egui::Widget for MenuRow<'_> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
let size = egui::vec2(self.width, TOOLBAR_BTN);
if artwork(self.glyph).is_some() {
ToolRow::new(RowIcon::Glyph(self.glyph), self.label, size).selected(self.selected).show(ui).0
} else {
let text = format!("{} {}", self.glyph, self.label);
ToolRow::new(RowIcon::None, &text, size).selected(self.selected).show(ui).0
}
}
}
pub fn menu_row(
ui: &mut egui::Ui,
glyph: &str,
label: &str,
selected: bool,
width: f32,
) -> egui::Response {
ui.add(menu_row_widget(glyph, label, width, selected))
}
fn menu_row_width(ui: &egui::Ui, glyph: &str, label: &str) -> f32 {
if artwork(glyph).is_some() {
ToolRow::width(ui, RowIcon::Glyph(glyph), label, false)
} else {
ToolRow::width(ui, RowIcon::None, &format!("{glyph} {label}"), false)
}
}
pub fn menu_width(ui: &egui::Ui, rows: &[(&str, &str)]) -> f32 {
menu_width_salted(ui, "menu-measure", rows)
}
pub fn menu_width_salted(ui: &egui::Ui, _salt: &str, rows: &[(&str, &str)]) -> f32 {
rows.iter().fold(0.0_f32, |widest, (glyph, label)| widest.max(menu_row_width(ui, glyph, label)))
}