AnyBot/src/app/menu_ui.rs

75 lines
3.3 KiB
Rust
Raw Normal View History

// Menus like the original's (captured from Grok Bot itself): 200 px wide, 16 px corners, rows
// 32 px tall with a 16 px icon on the left and 14 px text, a soft rounded highlight, hairline
// separators between groups, a red 刪除 with a bin, and "›" on rows that open a submenu.
use super::*;
pub const MENU_W: f32 = 200.0;
const ROW_H: f32 = 32.0;
fn row(ui: &mut egui::Ui, p: Palette, icon: Option<crate::icons::Icon>, label: &str, col: Color32, chevron: bool) -> egui::Response {
// Rows touch (no gap between them) and the menu stays its own width, not the screen's.
ui.spacing_mut().item_spacing.y = 0.0;
let galley_w = ui.painter().layout_no_wrap(label.to_string(), egui::FontId::proportional(14.0), col).size().x;
let w = ui.min_rect().width().max(MENU_W - 12.0).max(galley_w + if icon.is_some() { 64.0 } else { 36.0 });
let (rect, r) = ui.allocate_exact_size(vec2(w, ROW_H), egui::Sense::click());
if r.hovered() {
ui.painter().rect_filled(rect, 8, p.hover);
}
let x = rect.left() + 10.0;
if let Some(ic) = icon {
let icol = if col == p.danger { p.danger } else { p.text2 };
crate::icons::paint(ui.painter(), egui::Rect::from_center_size(egui::pos2(x + 8.0, rect.center().y), vec2(16.0, 16.0)), ic, icol);
}
let tx = if icon.is_some() { x + 28.0 } else { x + 2.0 };
ui.painter().text(egui::pos2(tx, rect.center().y), egui::Align2::LEFT_CENTER, label, egui::FontId::proportional(14.0), col);
if chevron {
let c = egui::pos2(rect.right() - 14.0, rect.center().y);
let st = Stroke::new(1.4, p.text3);
ui.painter().line_segment([c + vec2(-2.5, -4.5), c + vec2(2.0, 0.0)], st);
ui.painter().line_segment([c + vec2(-2.5, 4.5), c + vec2(2.0, 0.0)], st);
}
r.on_hover_cursor(egui::CursorIcon::PointingHand)
}
/// One menu row with an icon.
pub fn item(ui: &mut egui::Ui, p: Palette, icon: crate::icons::Icon, label: &str) -> egui::Response {
row(ui, p, Some(icon), &t(label), p.text, false)
}
/// A row whose words are already translated / built.
pub fn item_text(ui: &mut egui::Ui, p: Palette, icon: Option<crate::icons::Icon>, label: &str) -> egui::Response {
row(ui, p, icon, label, p.text, false)
}
/// The red one (刪除…).
pub fn danger(ui: &mut egui::Ui, p: Palette, icon: crate::icons::Icon, label: &str) -> egui::Response {
row(ui, p, Some(icon), &t(label), p.danger, false)
}
/// A row that opens a submenu on hover ("›").
pub fn sub<R>(ui: &mut egui::Ui, p: Palette, icon: crate::icons::Icon, label: &str, content: impl FnOnce(&mut egui::Ui) -> R) {
let r = row(ui, p, Some(icon), &t(label), p.text, true);
egui::containers::menu::SubMenu::new().show(ui, &r, |ui| {
ui.set_min_width(MENU_W - 12.0);
content(ui)
});
}
/// A hairline between groups.
pub fn sep(ui: &mut egui::Ui, p: Palette) {
ui.spacing_mut().item_spacing.y = 0.0;
ui.add_space(4.0);
let (rect, _) = ui.allocate_exact_size(vec2(ui.min_rect().width().max(MENU_W - 12.0), 1.0), egui::Sense::hover());
ui.painter().hline(rect.x_range().shrink(8.0), rect.center().y, Stroke::new(1.0, p.border));
ui.add_space(4.0);
}
/// Small grey words above a group of rows.
pub fn caption(ui: &mut egui::Ui, p: Palette, text: &str) {
ui.horizontal(|ui| {
ui.add_space(10.0);
ui.label(RichText::new(t(text)).size(12.0).color(p.text3));
});
}