GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
use super::entries::{Row, entry, shown_shortcut};
use super::invoke::{field_event, focused_text_field, invoke, to_field, with_held_keys};
use super::keyboard::{self, AccessRow, SourcePath};
use super::tree::menu_tree_named;
use super::{Entry, Item, Tokens, Value, VectorcraftApp, widgets};

/// Render the menu bar.
/// The in-window menu bar. Returns where its titles end (x): the bar itself takes the full width.
pub fn menu_bar(app: &mut VectorcraftApp, ui: &mut egui::Ui) -> f32 {
    let ctx = ui.ctx().clone();
    // The press that opens a menu takes the keyboard from a focused text field: the field and its
    // selection are kept while the menus are open, so their Select All, Cut, Copy and Paste act on
    // its text (#516), the frame after the click that chose them (that click takes the keyboard
    // from it again).
    type Field = (egui::Id, egui::text_edit::TextEditState);
    let chosen_key = egui::Id::unique("menu-bar-field-event").with(ctx.viewport_id());
    if let Some(((field, state), e)) = ctx.data(|d| d.get_temp::<(Field, egui::Event)>(chosen_key)) {
        ctx.data_mut(|d| d.remove::<(Field, egui::Event)>(chosen_key));
        keyboard::restore_field(&ctx, field, state);
        to_field(&ctx, field, e);
    }
    keyboard::remember_field(&ctx);
    keyboard::begin_bar(&ctx);
    let tree = menu_tree_named(app.session.prefs.font_names_in_english);
    // No application menu in the window: the bar starts at File on every
    // platform. The VectorCraft menu stays in the tree: the native Mac bar takes it from there.
    let tree: Vec<(&str, Vec<Item>)> = tree.into_iter().filter(|(title, _)| *title != "VectorCraft").collect();
    let root_rows = keyboard::root_rows(&tree);
    let show_cues = keyboard::cues(&ctx);
    // Only the bar temporarily sees no resting pointer while keyboard navigation owns it.
    // Real movement/clicks keep normal pointer menus; restore before refresh/invoke/other UI.
    let pointer = keyboard::suppress_stationary_hover(&ctx).then(|| ctx.input_mut(|i| std::mem::take(&mut i.pointer)));
    let mut clicked: Option<(String, Value)> = None;
    let mut file_open = false;
    // A menu bar for its layout and menu-button style, but the titles use `Popup::menu` directly:
    // egui 0.37's `MenuButton` opens the title under a resting pointer while another menu is open,
    // and the app switches titles on movement only (`switch_on_hover`, the native behaviour).
    let (end, open) = egui::MenuBar::new()
        .ui(ui, |ui| {
            let mut titles = Vec::with_capacity(tree.len());
            for (title, items) in tree.iter() {
                let cue = root_rows.iter().find(|r| r.label == crate::i18n::t(title)).and_then(|r| r.cue.as_ref()).filter(|_| show_cues);
                let text = keyboard::text(ui, crate::i18n::t(title), "", cue, Some(13.0));
                let resp = ui.push_id(("menu-bar-root", *title), |ui| ui.add(egui::Button::new(text))).inner;
                crate::palette::note_spot(ui, resp.rect, || crate::palette::SpotAt::Menu((*title).to_string()));
                keyboard::bind_root(&ctx, title, &resp);
                egui::Popup::menu(&resp).show(|ui| bar_menu_body(app, ui, items, &keyboard::root_path(title), &mut clicked));
                if *title == "File" {
                    file_open = egui::Popup::is_id_open(ui.ctx(), egui::Popup::default_response_id(&resp));
                }
                titles.push(resp);
            }
            (ui.cursor().min.x, switch_on_hover(ui.ctx(), &titles))
        })
        .inner;
    if let Some(pointer) = pointer {
        ctx.input_mut(|i| i.pointer = pointer);
    }
    keyboard::finish_bar(&ctx, open, clicked.is_some());
    // The File menu opening reads the Scripts folder again, for File › Scripts.
    let file_key = egui::Id::unique("menu-bar-file-open");
    let file_was_open = ctx.data(|d| d.get_temp::<bool>(file_key)).unwrap_or(false);
    ctx.data_mut(|d| d.insert_temp(file_key, file_open));
    if file_open && !file_was_open {
        crate::scripts_menu::refresh(app, Some(&ctx));
    }
    // Whether a top-level menu is open: the docking renderer lowers the floating panels' OS windows
    // while one is, or the menu would open under them.
    ctx.data_mut(|d| d.insert_temp(menu_open_id(), open));

    let field = keyboard::field(&ctx);
    if !open && !keyboard::active(&ctx) && focused_text_field(&ctx).is_none() {
        keyboard::forget_field(&ctx);
    }
    if let Some((id, p)) = clicked {
        match field.and_then(|f| Some((f, field_event(app, &id)?))) {
            Some(chosen) => {
                ctx.data_mut(|d| d.insert_temp(chosen_key, chosen));
                ctx.request_repaint();
            }
            None => invoke(app, &id, p),
        }
    }
    end
}

/// Where the menu bar keeps whether one of its top-level menus is open.
fn menu_open_id() -> egui::Id {
    egui::Id::unique("menu-bar-open")
}

/// Whether a menu of the in-window menu bar is open (`false` when no menu bar was drawn yet).
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn menu_open(ctx: &egui::Context) -> bool {
    ctx.data(|d| d.get_temp::<bool>(menu_open_id())).unwrap_or(false)
}

/// Whether the pointer at `p` is really over the menu title `title`: inside it, and with no
/// popup above it (a tall menu that egui moves up can cover the bar; hovering that menu must
/// not switch to the title under it).
pub(super) fn pointer_reaches_title(ctx: &egui::Context, title: &egui::Response, p: egui::Pos2) -> bool {
    title.interact_rect.contains(p) && ctx.layer_id_at(p) == Some(title.layer_id)
}

/// Like a native menu bar: while one top-level menu is open, moving the pointer onto another
/// title opens that menu instead (egui alone needs a click on each title). Returns whether a menu
/// is open.
fn switch_on_hover(ctx: &egui::Context, titles: &[egui::Response]) -> bool {
    let ids: Vec<egui::Id> = titles.iter().map(egui::Popup::default_response_id).collect();
    let Some(open) = ids.iter().position(|id| egui::Popup::is_id_open(ctx, *id)) else {
        return false;
    };
    // `Response::hovered` is false while a menu's popup is open, so hit-test the titles here.
    // Only a moving pointer switches: one resting on a title leaves the open menu alone.
    let moving = ctx.input(|i| i.pointer.delta() != egui::Vec2::ZERO);
    if let Some(p) = ctx.pointer_hover_pos().filter(|_| moving)
        && let Some(i) = titles.iter().position(|title| pointer_reaches_title(ctx, title, p))
        && i != open
        && let Some(id) = ids.get(i)
    {
        egui::Popup::open_id(ctx, *id);
        ctx.request_repaint();
    }
    true
}

/// A top-level menu's popup: as wide as its widest item (label plus shortcut), at least 230 pt;
/// it scrolls when it is taller than the window.
pub(super) fn menu_body(app: &VectorcraftApp, ui: &mut egui::Ui, items: &[Item], clicked: &mut Option<(String, Value)>) {
    widgets::menu_scroll(ui, |ui| {
        widgets::menu_min_width(ui, 230.0);
        render_items(app, ui, items, true, clicked);
    });
}

fn bar_menu_body(app: &VectorcraftApp, ui: &mut egui::Ui, items: &[Item], path: &SourcePath, clicked: &mut Option<(String, Value)>) {
    if !keyboard::active(ui.ctx()) && !keyboard::cues(ui.ctx()) {
        menu_body(app, ui, items, clicked);
        return;
    }
    widgets::menu_scroll(ui, |ui| {
        widgets::menu_min_width(ui, 230.0);
        render_items_scoped(app, ui, items, true, Some(path), clicked);
    });
}

/// `items` as menu buttons; with `checks`, items that can be on or off show a check mark (or the
/// room for one).
pub(super) fn render_items(app: &VectorcraftApp, ui: &mut egui::Ui, items: &[Item], checks: bool, clicked: &mut Option<(String, Value)>) {
    render_items_scoped(app, ui, items, checks, None, clicked);
}

fn render_items_scoped(
    app: &VectorcraftApp,
    ui: &mut egui::Ui,
    items: &[Item],
    checks: bool,
    path: Option<&SourcePath>,
    clicked: &mut Option<(String, Value)>,
) {
    let t = Tokens::get(ui.ctx());
    let rows = path.map(|p| keyboard::level(app, p, items)).unwrap_or_default();
    let mut rows = rows.iter();
    if let Some(path) = path {
        keyboard::note_level(ui.ctx(), path);
    }
    for e in items.iter().filter_map(|it| entry(app, it)) {
        match e {
            Entry::Sep => {
                ui.separator();
            }
            Entry::Header(h) => {
                ui.label(egui::RichText::new(h).size(11.0).color(t.text_dim));
            }
            Entry::Sub(label, children) => {
                if let Some(access) = rows.next() {
                    ui.push_id(access.key.salt(), |ui| {
                        let cue = access.cue.as_ref().filter(|_| keyboard::cues(ui.ctx()));
                        let text = keyboard::text(ui, label, "", cue, None);
                        if !ui.is_sizing_pass() {
                            keyboard::prepare_submenu(ui.ctx(), access, ui.next_auto_id());
                        }
                        let (response, _) = egui::menu::SubMenuButton::new(text).ui(ui, |ui| {
                            widgets::menu_scroll(ui, |ui| {
                                widgets::menu_min_width(ui, 200.0);
                                render_items_scoped(
                                    app,
                                    ui,
                                    children,
                                    checks || shows_marks(app, children),
                                    Some(&keyboard::child_path(&access.key)),
                                    clicked,
                                );
                            });
                        });
                        if !ui.is_sizing_pass() {
                            keyboard::bind_response(ui.ctx(), access, &response);
                        }
                    });
                } else {
                    ui.menu_button(label, |ui| {
                        widgets::menu_scroll(ui, |ui| {
                            widgets::menu_min_width(ui, 200.0);
                            render_items(app, ui, children, checks || shows_marks(app, children), clicked);
                        });
                    });
                }
            }
            Entry::Item(row) => render_row(app, ui, &row, checks, rows.next(), clicked),
        }
    }
}

/// Whether a submenu of a menu drawn without check marks (the canvas context menu) still shows
/// them: when every item can be on or off, or when it holds Auto Spell Check (the Spelling
/// submenu, with Check Spelling… after it), since a toggle's label alone can't say whether it is on.
fn shows_marks(app: &VectorcraftApp, items: &[Item]) -> bool {
    only_toggles(app, items) || items.iter().any(|it| matches!(it, Item::Cmd(_, "edit.autoSpellCheck", _)))
}

/// Whether every item of a submenu can be on or off.
fn only_toggles(app: &VectorcraftApp, items: &[Item]) -> bool {
    let mut rows = items.iter().filter_map(|it| entry(app, it)).filter(|e| !matches!(e, Entry::Sep)).peekable();
    rows.peek().is_some() && rows.all(|e| matches!(e, Entry::Item(r) if r.checked.is_some()))
}

/// One menu item: its label (with a check mark, or the room for one, under `checks`), its shortcut,
/// and Type › Font's samples.
fn render_row(app: &VectorcraftApp, ui: &mut egui::Ui, row: &Row, checks: bool, access: Option<&AccessRow>, clicked: &mut Option<(String, Value)>) {
    let sc = row.shortcut.map(shown_shortcut).unwrap_or_default();
    let Some((id, p)) = row.command else {
        ui.add_enabled_ui(false, |ui| {
            let cue = access.and_then(|r| r.cue.as_ref()).filter(|_| keyboard::cues(ui.ctx()));
            let text = keyboard::text(ui, row.label.as_ref(), "", cue, None);
            ui.add(egui::Button::new(text).shortcut_text(sc));
        })
        .response
        .on_disabled_hover_text(tl!("Coming soon — tracked in the parity plan"));
        return;
    };
    let prefix = match row.checked.filter(|_| checks) {
        Some(true) => "✓  ",
        Some(false) => "     ",
        None => "",
    };
    let cue = access.and_then(|r| r.cue.as_ref()).filter(|_| keyboard::cues(ui.ctx()));
    let text = keyboard::text(ui, row.label.as_ref(), prefix, cue, None);
    // Type → Font: each family's sample after its name (Enable in-menu font previews).
    let sampled = p.get("font").and_then(Value::as_str).filter(|_| id == "text.setStyle" && app.session.prefs.font_preview);
    let draw = |ui: &mut egui::Ui| match sampled {
        Some(family) => {
            let slot = egui::IdSalt::new(("font-sample", family));
            let button = egui::Button::new(text).right_text(egui::Atom::custom(slot, crate::font_menu::MENU_SAMPLE_SIZE));
            let out = ui.add_enabled_ui(row.enabled, |ui| button.atom_ui(ui)).inner;
            if let Some(rect) = out.rect(slot) {
                crate::font_menu::menu_item_sample(ui, rect, family, app.session.prefs.font_samples_on_white);
            }
            out.response
        }
        None => ui.add_enabled(row.enabled, egui::Button::new(text).shortcut_text(sc)),
    };
    let r = match access {
        Some(access) => ui.push_id(access.key.salt(), draw).inner,
        None => draw(ui),
    };
    let keyboard_chosen = !ui.is_sizing_pass() && access.is_some_and(|access| keyboard::bind_response(ui.ctx(), access, &r));
    let r = match (row.enabled, disabled_hint(app, id)) {
        (false, Some(why)) => r.on_disabled_hover_text(why),
        _ => r,
    };
    if r.clicked() || keyboard_chosen {
        *clicked = row.target().map(|target| {
            if access.is_none() || r.clicked_by(egui::PointerButton::Primary) { with_held_keys(target, ui.input(|i| i.modifiers)) } else { target } // access-key Alt is not an Alt-click alternate action
        });
        if access.is_some() {
            keyboard::chosen(ui.ctx());
        }
        ui.close();
    }
}

/// Why a dimmed menu item is dimmed, for the ones that say.
fn disabled_hint(app: &VectorcraftApp, id: &str) -> Option<&'static str> {
    match id {
        "ui.expandDialog" if vectorcraft_engine::cmd::expand::has_appearance(&app.session) => Some(crate::dialogs::expand::appearance_first()),
        _ => None,
    }
}

/// What the Home screen remembers when it opens (`app.home`): the active document and the
/// document count. When either changes, the Home screen gives way to the document.
pub(crate) fn home_key(app: &VectorcraftApp) -> (Option<u64>, usize) {
    (app.session.active().map(|d| d.uid), app.session.documents().len())
}

/// Whether the Home screen is up: chosen with the Home button (`app.home`), or no document is open
/// and Preferences › General › Show The Home Screen When No Documents Are Open is on (#394). Off,
/// an app with no document shows an empty window, and the Home button still opens the screen.
pub(crate) fn home_showing(app: &VectorcraftApp) -> bool {
    app.ui.home.is_some() || (app.session.active().is_none() && app.session.prefs.show_home_screen)
}