mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 19:51:26 -05:00
Menus: a new window keymap sets the panel rows' shortcuts without a rebuild
The window builds its first keymap after the first frame's menus, and the menu model's key held the keymap's address, so the next refresh built the whole model again with nothing else changed. Only the Window menu's panel rows and the Settings rows read their shortcuts from the keymap (Build::decorate). refresh now compares a key without the keymap (rows_key) and keeps the keymap's address on its own. When only the keymap changed, it sets the shortcuts of those rows again and lays the bar out again, leaving the rows' states and the catalog as they were. state_key, which the native menu sync, Help search and the Properties panel read, still holds the keymap. The rows key also holds the user's shortcut set, which the catalog's rows are made from.
This commit is contained in:
@@ -40,9 +40,15 @@ pub(crate) struct Menus {
|
||||
/// The pass number of the last frame with a pointer button or a key press ([`pressed_pass`]), `None` before the
|
||||
/// first: the `input` of [`state_key`].
|
||||
pressed: Option<u64>,
|
||||
/// The address of the window's keymap that the panel rows took their shortcuts from ([`keymap_addr`]), `None`
|
||||
/// before the window has one.
|
||||
keymap: Option<usize>,
|
||||
/// How many times [`refresh`] built the model.
|
||||
#[cfg(test)]
|
||||
pub(crate) rebuilds: u64,
|
||||
/// How many times [`refresh`] set the keymap's shortcuts again without building the model ([`refresh_shortcuts`]).
|
||||
#[cfg(test)]
|
||||
pub(crate) shortcut_refreshes: u64,
|
||||
/// The menus as built, before a layout.
|
||||
pub(crate) bar: MenuBar,
|
||||
/// `bar` laid out for the shell's target: what the in-window bar shows.
|
||||
@@ -90,14 +96,22 @@ pub(crate) fn translate_label(english: &'static str) -> String {
|
||||
/// doesn't list (panels shown, a tool picked). While an
|
||||
/// interaction is open (for example a move, scale or shape drag, a dialog's or the font menu's
|
||||
/// preview, or a typing session), the revision, which each of its previews changes, is left out:
|
||||
/// the hash changes as the interaction begins and as it ends.
|
||||
/// the hash changes as the interaction begins and as it ends. The window's keymap, which the shown
|
||||
/// shortcuts are read from, is in the hash too.
|
||||
pub(crate) fn state_key(app: &VectorcraftApp, input: Option<u64>) -> u64 {
|
||||
let mut h = DefaultHasher::new();
|
||||
(rows_key(app, input), keymap_addr(app)).hash(&mut h);
|
||||
h.finish()
|
||||
}
|
||||
|
||||
/// [`state_key`] without the window's keymap: what the model's rows are built from. A new keymap alone changes only
|
||||
/// the shortcuts of the rows that read it, which [`refresh`] sets again without a rebuild ([`refresh_shortcuts`]).
|
||||
fn rows_key(app: &VectorcraftApp, input: Option<u64>) -> u64 {
|
||||
let mut h = DefaultHasher::new();
|
||||
input.hash(&mut h);
|
||||
app.run_count.hash(&mut h);
|
||||
// The window's keymap, which the panel rows read their shortcuts from (`Build::decorate`): the framework builds it
|
||||
// after the first frame's menus, and builds a new one when the bindings or the catalog change.
|
||||
app.ui_ctx.as_ref().and_then(craft::ui::runtime::commands::keymap).map(|k| std::sync::Arc::as_ptr(&k).addr()).hash(&mut h);
|
||||
// The user's shortcuts, which the catalog's rows are made from.
|
||||
std::sync::Arc::as_ptr(&app.shortcut_overrides).addr().hash(&mut h);
|
||||
let s = &app.session;
|
||||
(s.journal.len(), s.active_index(), s.clipboard.is_empty(), s.tool_composing()).hash(&mut h);
|
||||
if let Some(d) = s.active() {
|
||||
@@ -126,12 +140,19 @@ pub(crate) fn state_key(app: &VectorcraftApp, input: Option<u64>) -> u64 {
|
||||
}
|
||||
|
||||
/// Brings the menus up to date once per frame, before the frame's input: the model is built again
|
||||
/// when the rows' state ([`state_key`]) or the shell's target changed.
|
||||
/// when the rows' state ([`rows_key`]) or the shell's target changed. When only the window's keymap
|
||||
/// changed (the framework builds the first one after the first frame's menus), the rows that read
|
||||
/// their shortcuts from it get them again ([`refresh_shortcuts`]).
|
||||
pub(crate) fn refresh(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
let target = Target::of_shell(&WindowHost::of(ctx).shell());
|
||||
let press = last_press(app, ctx);
|
||||
let key = state_key(app, press);
|
||||
let key = rows_key(app, press);
|
||||
let keymap = keymap_addr(app);
|
||||
if app.menus.key == Some(key) && app.menus.target == Some(target) {
|
||||
if app.menus.keymap != keymap {
|
||||
refresh_shortcuts(app, target);
|
||||
app.menus.keymap = keymap;
|
||||
}
|
||||
return;
|
||||
}
|
||||
rebuild(app, target);
|
||||
@@ -141,6 +162,54 @@ pub(crate) fn refresh(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
}
|
||||
app.menus.key = Some(key);
|
||||
app.menus.target = Some(target);
|
||||
app.menus.keymap = keymap;
|
||||
}
|
||||
|
||||
/// The address of the window's keymap, which the panel rows read their shortcuts from ([`reads_keymap`]): the framework
|
||||
/// builds it after the first frame's menus, and builds a new one when the bindings or the catalog change.
|
||||
fn keymap_addr(app: &VectorcraftApp) -> Option<usize> {
|
||||
app.ui_ctx.as_ref().and_then(craft::ui::runtime::commands::keymap).map(|k| std::sync::Arc::as_ptr(&k).addr())
|
||||
}
|
||||
|
||||
/// Whether the row running `id` with `params` shows a shortcut read from the window's keymap ([`Build::decorate`]).
|
||||
fn reads_keymap(id: &str, params: &Value) -> bool {
|
||||
matches!(id, "window.panel" | "edit.preferences") && !params.is_null()
|
||||
}
|
||||
|
||||
/// Sets the shortcuts of the rows that read the window's keymap ([`reads_keymap`]) again and lays the bar out again.
|
||||
/// The rows' states and the catalog stay as the last rebuild made them.
|
||||
fn refresh_shortcuts(app: &mut VectorcraftApp, target: Target) {
|
||||
fn walk(app: &VectorcraftApp, nodes: &mut [Node]) {
|
||||
for node in nodes {
|
||||
match node {
|
||||
Node::Item(item) if reads_keymap(&item.command.id, &item.command.params) => {
|
||||
let shortcut = super::labels::item_shortcut(app, &item.command.id, &item.command.params);
|
||||
item.shortcut = shortcut;
|
||||
}
|
||||
Node::Submenu { children, .. } => walk(app, children),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut bar = std::mem::take(&mut app.menus.bar);
|
||||
for menu in &mut bar.menus {
|
||||
walk(app, &mut menu.children);
|
||||
}
|
||||
app.menus.bar = bar;
|
||||
lay_out(app, target);
|
||||
#[cfg(test)]
|
||||
{
|
||||
app.menus.shortcut_refreshes += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Lays `app.menus.bar` out for `target` into `app.menus.laid`, which is edited only when the laid-out bar differs.
|
||||
fn lay_out(app: &mut VectorcraftApp, target: Target) {
|
||||
let options = LayoutOptions::new(APP_NAME).label(&translate_label);
|
||||
let laid = layout(&app.menus.bar, target, &options).bar;
|
||||
if *app.menus.laid != laid {
|
||||
app.menus.laid.edit(|l| *l = laid);
|
||||
}
|
||||
}
|
||||
|
||||
/// The pass number of the last frame with a pointer button pressed or released or a key pressed, the frame `ctx` read
|
||||
@@ -294,14 +363,9 @@ fn rebuild(app: &mut VectorcraftApp, target: Target) {
|
||||
row_states(app, &rows, &mut states);
|
||||
crate::panels::panel_menu_states(app, &mut states);
|
||||
|
||||
let bar = MenuBar::new(menus);
|
||||
let options = LayoutOptions::new(APP_NAME).label(&translate_label);
|
||||
let laid = layout(&bar, target, &options).bar;
|
||||
app.menus.rows = Some(rows);
|
||||
app.menus.bar = bar;
|
||||
if *app.menus.laid != laid {
|
||||
app.menus.laid.edit(|l| *l = laid);
|
||||
}
|
||||
app.menus.bar = MenuBar::new(menus);
|
||||
lay_out(app, target);
|
||||
app.menus.states.edit(|s| *s = states);
|
||||
}
|
||||
|
||||
@@ -597,7 +661,7 @@ impl Build<'_> {
|
||||
// A panel row shows the chord of the panel's shortcut (`panel:<id>`), which binds `window.panel {panel}`;
|
||||
// a Window row runs it with `toggle: true`, so the framework finds no chord for the row's own command.
|
||||
// Settings' first page shows Preferences' shortcut, which binds `edit.preferences` with no page.
|
||||
"window.panel" | "edit.preferences" if !params.is_null() => {
|
||||
_ if reads_keymap(id, params) => {
|
||||
let mut item = item;
|
||||
item.shortcut = super::labels::item_shortcut(self.app, id, params);
|
||||
item
|
||||
@@ -658,6 +722,50 @@ mod tests {
|
||||
assert!(app.menus.states.get(&CommandRef::new("object.group")).is_some_and(CommandState::is_enabled));
|
||||
}
|
||||
|
||||
/// A new window keymap with nothing else changed (the framework builds the first one after the first frame's
|
||||
/// menus) sets the panel rows' shortcuts from it, in the bar and in the laid-out bar, without building the menus
|
||||
/// again.
|
||||
#[test]
|
||||
fn a_new_keymap_alone_sets_the_shortcuts_without_a_rebuild() {
|
||||
fn clear(nodes: &mut [Node]) {
|
||||
for node in nodes {
|
||||
match node {
|
||||
Node::Item(item) if reads_keymap(&item.command.id, &item.command.params) => item.shortcut = None,
|
||||
Node::Submenu { children, .. } => clear(children),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn panel_chords(bar: &MenuBar) -> usize {
|
||||
bar.items()
|
||||
.into_iter()
|
||||
.filter(|it| it.command.id == "window.panel" && it.command.params["toggle"] == true && it.shortcut.is_some())
|
||||
.count()
|
||||
}
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
for _ in 0..2 {
|
||||
crate::testing::app_frame(&mut app, &ctx, egui::RawInput::default());
|
||||
}
|
||||
let shown = panel_chords(&app.menus.bar);
|
||||
assert!(shown > 0, "the panel rows show their shortcuts' chords");
|
||||
// The menus as a refresh before the window's keymap leaves them.
|
||||
app.menus.keymap = None;
|
||||
for menu in &mut app.menus.bar.menus {
|
||||
clear(&mut menu.children);
|
||||
}
|
||||
app.menus.laid.edit(|laid| laid.menus.iter_mut().for_each(|menu| clear(&mut menu.children)));
|
||||
let (rebuilds, refreshes) = (app.menus.rebuilds, app.menus.shortcut_refreshes);
|
||||
refresh(&mut app, &ctx);
|
||||
assert_eq!(app.menus.rebuilds, rebuilds, "a new keymap alone does not build the menus again");
|
||||
assert_eq!(app.menus.shortcut_refreshes, refreshes + 1, "the new keymap sets the shortcuts");
|
||||
assert_eq!(panel_chords(&app.menus.bar), shown, "the panel rows show their chords again");
|
||||
assert_eq!(panel_chords(&app.menus.laid), shown, "the laid-out bar shows them too");
|
||||
refresh(&mut app, &ctx);
|
||||
assert_eq!((app.menus.rebuilds, app.menus.shortcut_refreshes), (rebuilds, refreshes + 1), "the same keymap sets nothing again");
|
||||
}
|
||||
|
||||
/// A rebuild asks each dimmed row's command once whether it can run, for its state and its reason together.
|
||||
#[test]
|
||||
fn a_rebuild_checks_each_dimmed_row_once() {
|
||||
|
||||
Reference in New Issue
Block a user