mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
craft-menu: NativeMenu::sync_with is keyed by a checked key; sync and had_input are removed
This commit is contained in:
+9
-10
@@ -96,7 +96,8 @@ items.
|
||||
Each frame, `apply_state(&mut bar, |command| ItemState { enabled, checked })` sets enabled and
|
||||
checked from the app's state. `checked: Some(..)` makes a plain item a check item and selects or
|
||||
clears a radio item; `None` leaves the kind as it is. The built-in items and the open documents
|
||||
are left alone. Both run on the app's bar, before the layout.
|
||||
are left alone. Both run on the app's bar, before the layout. The bar is held in a `Tracked<MenuBar>`; both write it only when an item changes, so
|
||||
its revision (the key of the in-window bar's kept labels and of `sync_with`) moves only then.
|
||||
|
||||
## Keyboard Shortcuts dialog
|
||||
|
||||
@@ -240,7 +241,7 @@ Each frame, in this order:
|
||||
2. `set_gate`, with the app's state for this frame.
|
||||
3. `take_clicks`, and run the commands it returns: the clicks since the last frame that the gate
|
||||
allows now. Minimize and Quit are already handled.
|
||||
4. `sync_with`, which lays the menus out again only when the app's state changed or a menu event
|
||||
4. `sync_with`, which lays the menus out again only when its key changed or a menu event
|
||||
arrived, and re-syncs them in place when the gate changed.
|
||||
|
||||
```rust
|
||||
@@ -256,14 +257,13 @@ if let Some(menu) = &mut self.native_menu {
|
||||
for command in menu.take_clicks(ctx) {
|
||||
self.run_command(ctx, &command);
|
||||
}
|
||||
let state = self.menu_state_hash(craft_menu::had_input(ctx).then_some(ctx.cumulative_frame_nr()));
|
||||
for c in menu.sync_with(state, || craft_menu::mac_layout(&self.menu_bar(), &LayoutOptions::new("PhotoCraft"))) {
|
||||
for c in menu.sync_with(&(&self.menu_bar, &self.workspace), || craft_menu::mac_layout(&self.menu_bar(), &LayoutOptions::new("PhotoCraft"))) {
|
||||
log::info!("menu: {} is {} on macOS and {} in the app; {}", c.shortcut, c.system, c.command, c.resolution);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`sync_with` builds the layout only when `state` changed or a menu event arrived, because
|
||||
`sync_with` builds the layout only when its key changed or a menu event arrived, because
|
||||
building the rows usually walks every command's enabled state. The backend then updates labels,
|
||||
enabled and checked in place and rebuilds only when `structure_key` (or, for the open
|
||||
documents, `window_key`) changes. Adding or removing an item in a dynamic list (recent files,
|
||||
@@ -271,11 +271,10 @@ workspaces, plugins) changes `structure_key`; relabelling one doesn't. An item t
|
||||
item once stays one, unchecked, when the app's state stops giving it a checked state, so that
|
||||
doesn't rebuild the menus.
|
||||
|
||||
`state` must cover everything the menus show. A change that comes without input, such as a load
|
||||
or save finishing on a worker or a timer, leaves the menus stale until the next input or menu
|
||||
event unless it is in the hash. An app that can't keep the hash complete calls
|
||||
`menu.sync(layout)` instead, with the layout built every frame: it is compared with the last
|
||||
one and synced only when it differs.
|
||||
The key is a `MemoProps` value: the menu model held in a `Tracked<MenuBar>` (`apply_state` and
|
||||
`apply_keymap` write it only when an item changes, so its revision moves only then) and whatever
|
||||
else the layout reads, each as a checked input, so a change that comes without input (a load
|
||||
finishing on a worker) reaches the menus the frame it lands.
|
||||
|
||||
An app that needs a click while egui is idle (to wake its own loop, or hand the command to a
|
||||
worker) sets `menu.on_command(Box::new(|command| ..))`. The hook runs from the menu event
|
||||
|
||||
+13
-41
@@ -5,7 +5,7 @@ use std::collections::HashSet;
|
||||
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
||||
|
||||
use craft_keymap::{Chord, CommandRef, Key, Mods};
|
||||
use craft_ui::contract::MemoProps;
|
||||
use craft_ui::contract::{MemoKey, MemoProps};
|
||||
use craft_ui::runtime::windows::{KeyEquivalent, WindowAction, WindowHost};
|
||||
|
||||
use crate::layout::{Clash, Layout};
|
||||
@@ -203,8 +203,8 @@ pub trait Backend {
|
||||
/// The native menu bar, while the app uses one (its in-window menus are hidden then).
|
||||
pub struct NativeMenu {
|
||||
backend: Box<dyn Backend>,
|
||||
/// The state the menus were last built from.
|
||||
state: Option<u64>,
|
||||
/// The key the menus were last built from.
|
||||
state: Option<u128>,
|
||||
clicks: Vec<CommandRef>,
|
||||
/// An event arrived (AppKit may have toggled a check item itself): re-sync.
|
||||
dirty: bool,
|
||||
@@ -388,23 +388,18 @@ impl NativeMenu {
|
||||
out
|
||||
}
|
||||
|
||||
/// Bring the native menu up to date. `state` is a hash of what the menus show (see
|
||||
/// [`had_input`]); `build` lays them out ([`crate::mac_layout`]) and runs only when `state`
|
||||
/// changed or a menu event arrived since the last sync, since building the rows usually walks
|
||||
/// every command's enabled state. A menu event re-syncs because AppKit flips a check or radio
|
||||
/// item's state itself when it is chosen: the sync sets it back to the app's state. A gate
|
||||
/// change ([`NativeMenu::set_gate`]) re-syncs the menus last laid out, greyed or not, without
|
||||
/// calling `build`.
|
||||
///
|
||||
/// `state` must cover everything the menus show. A change that comes without input, such as
|
||||
/// a load or save finishing on a worker or a timer, leaves the menus stale until the next
|
||||
/// input or menu event unless it is in the hash. An app that can't keep the hash complete
|
||||
/// uses [`NativeMenu::sync`] instead.
|
||||
/// Bring the native menu up to date. `key` is everything the layout reads, as a [`MemoProps`] value (the
|
||||
/// app's `Tracked` menu model, the language's `Words`, the keymap generation): `build` lays the menus out
|
||||
/// ([`crate::mac_layout`]) and runs only when the key changed or a menu event arrived since the last sync, since
|
||||
/// building the rows usually walks every command's enabled state. A menu event re-syncs because AppKit flips a
|
||||
/// check or radio item's state itself when it is chosen: the sync sets it back to the app's state. A gate change
|
||||
/// ([`NativeMenu::set_gate`]) re-syncs the menus last laid out, greyed or not, without calling `build`.
|
||||
///
|
||||
/// Returns the layout's clashes when they differ from the last ones returned, for the app to
|
||||
/// log; otherwise nothing.
|
||||
pub fn sync_with(&mut self, state: u64, build: impl FnOnce() -> Layout) -> Vec<Clash> {
|
||||
let relayout = self.state != Some(state) || self.dirty;
|
||||
pub fn sync_with<K: MemoProps + ?Sized>(&mut self, key: &K, build: impl FnOnce() -> Layout) -> Vec<Clash> {
|
||||
let key = MemoKey::of(key);
|
||||
let relayout = self.state != Some(key) || self.dirty;
|
||||
if !relayout && self.synced_gate == Some(self.gate) {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -413,27 +408,12 @@ impl NativeMenu {
|
||||
let mut layout = build();
|
||||
self.keep_kinds(&mut layout.bar);
|
||||
clashes = self.keep(layout);
|
||||
self.state = Some(state);
|
||||
self.state = Some(key);
|
||||
}
|
||||
self.push();
|
||||
clashes
|
||||
}
|
||||
|
||||
/// [`NativeMenu::sync_with`] without a state hash: the app lays the menus out every frame
|
||||
/// and they are synced when the layout differs from the last one (or a menu event or gate
|
||||
/// change calls for it), so they never go stale. It costs a layout per frame.
|
||||
pub fn sync(&mut self, mut layout: Layout) -> Vec<Clash> {
|
||||
self.keep_kinds(&mut layout.bar);
|
||||
if !self.dirty && self.bar.as_ref() == Some(&layout.bar) && self.clashes == layout.clashes && self.synced_gate == Some(self.gate) {
|
||||
return Vec::new();
|
||||
}
|
||||
let clashes = self.keep(layout);
|
||||
// A later `sync_with` lays the menus out again.
|
||||
self.state = None;
|
||||
self.push();
|
||||
clashes
|
||||
}
|
||||
|
||||
/// An item that was a check item once stays one, unchecked: switching its kind would change
|
||||
/// [`crate::structure_key`] and rebuild the menus.
|
||||
fn keep_kinds(&mut self, bar: &mut MenuBar) {
|
||||
@@ -514,11 +494,3 @@ fn text_editing_key(c: &Chord) -> bool {
|
||||
pub(crate) fn native_ok(c: &Chord) -> bool {
|
||||
c.has_command()
|
||||
}
|
||||
|
||||
/// A key press, typed text or click this frame. It may have changed state an app's menu hash
|
||||
/// doesn't list, so apps fold it into the `state` passed to [`NativeMenu::sync_with`], for
|
||||
/// example as `had_input(ctx).then_some(ctx.cumulative_frame_nr())`. Keys come from the key
|
||||
/// stage (`keys::keyed`), which also counts the presses a focused component took from egui.
|
||||
pub fn had_input(ctx: &egui::Context) -> bool {
|
||||
craft_ui::runtime::keys::keyed(ctx) || ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::PointerButton { pressed: false, .. })))
|
||||
}
|
||||
|
||||
@@ -88,8 +88,8 @@
|
||||
//! [`NativeMenu::on_command`] sees each allowed click as it arrives, also while egui is idle.
|
||||
//!
|
||||
//! Per frame: [`NativeMenu::poll`], then [`NativeMenu::set_gate`], [`NativeMenu::take_clicks`] and run the commands, then
|
||||
//! [`NativeMenu::sync_with`], which lays the menus out again only when the app's state changed
|
||||
//! (or [`NativeMenu::sync`], which compares a layout built every frame).
|
||||
//! [`NativeMenu::sync_with`], which lays the menus out again only when its key changed (the menu model's
|
||||
//! `Tracked` revision and the other inputs of the layout).
|
||||
//! The input filter also keeps a click or Escape that closes an open native menu away
|
||||
//! from egui.
|
||||
#![warn(missing_docs)]
|
||||
@@ -118,7 +118,7 @@ pub use bar::{EnglishHook, Labeled, MenuBarOutput, MenuBarUi};
|
||||
pub use craft_keymap::{Chord, CommandRef};
|
||||
pub use customize::Customization;
|
||||
pub use documents::DOCUMENT_COMMAND_PREFIX;
|
||||
pub use driver::{Backend, ClipboardKeys, CommandHook, Dispatch, Event, Gate, NativeMenu, had_input};
|
||||
pub use driver::{Backend, ClipboardKeys, CommandHook, Dispatch, Event, Gate, NativeMenu};
|
||||
pub use help::{HelpCommand, HelpLabels, HelpLink, HelpMenu};
|
||||
pub use keymap::{ItemState, apply_keymap, apply_state};
|
||||
pub use keytips::{KeytipControl, Keytips, KeytipsOutput};
|
||||
|
||||
@@ -133,11 +133,11 @@ pub(super) fn run() {
|
||||
};
|
||||
assert!(new_enabled(&main), "no dialog: New is enabled");
|
||||
menu.set_gate(Gate::default().modal(true));
|
||||
menu.sync_with(1, || layout(1));
|
||||
menu.sync_with(&1_u64, || layout(1));
|
||||
assert!(Retained::as_ptr(&main) == Retained::as_ptr(&main_menu()), "the gate updated the menu, not rebuilt it");
|
||||
assert!(!new_enabled(&main_menu()), "a dialog greys New");
|
||||
menu.set_gate(Gate::default());
|
||||
menu.sync_with(1, || layout(1));
|
||||
menu.sync_with(&1_u64, || layout(1));
|
||||
assert!(new_enabled(&main_menu()), "New is enabled again");
|
||||
|
||||
// A key equivalent: ⌘R reaches egui as the key press ⌘R, once, and is no click.
|
||||
@@ -179,7 +179,7 @@ pub(super) fn run() {
|
||||
|
||||
// Window ends with the open documents. Opening one rebuilds only the Window menu, which AppKit still knows as
|
||||
// its Window menu; choosing a row brings that document forward.
|
||||
menu.sync_with(2, || layout(2));
|
||||
menu.sync_with(&2_u64, || layout(2));
|
||||
assert!(Retained::as_ptr(&main) == Retained::as_ptr(&main_menu()), "a new document rebuilt only the Window menu");
|
||||
let window = submenu(&main_menu(), "Window");
|
||||
assert!(nsapp.windowsMenu().is_some_and(|w| Retained::as_ptr(&w) == Retained::as_ptr(&window)), "AppKit knows the new Window menu");
|
||||
|
||||
@@ -146,14 +146,14 @@ fn a_modal_gate_greys_the_apps_items() {
|
||||
builds.set(builds.get() + 1);
|
||||
mac_layout(&sample(false, true), &opts)
|
||||
};
|
||||
menu.sync_with(1, build);
|
||||
menu.sync_with(&1_u64, build);
|
||||
menu.set_gate(modal());
|
||||
menu.sync_with(1, build);
|
||||
menu.sync_with(1, build);
|
||||
menu.sync_with(&1_u64, build);
|
||||
menu.sync_with(&1_u64, build);
|
||||
menu.set_gate(Gate::default());
|
||||
menu.sync_with(1, build);
|
||||
menu.sync_with(&1_u64, build);
|
||||
menu.set_gate(typing(true));
|
||||
menu.sync_with(1, build);
|
||||
menu.sync_with(&1_u64, build);
|
||||
assert_eq!((builds.get(), synced.borrow().len()), (1, 4), "one layout; synced open, greyed, open, typing");
|
||||
|
||||
let synced = synced.borrow();
|
||||
|
||||
Reference in New Issue
Block a user