mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-25 21:19:50 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c738c6d77c | ||
|
|
c161c7039c | ||
|
|
d240c1adcd | ||
|
|
c79a12c9ba | ||
|
|
6d9553a826 | ||
|
|
564b281813 | ||
|
|
ed712acdab | ||
|
|
4279762e28 | ||
|
|
66d56083e9 | ||
|
|
99b15ac2d1 | ||
|
|
0fdf358ba5 | ||
|
|
ad46c883ff | ||
|
|
6dd8396e20 |
@@ -35,7 +35,7 @@ jobs:
|
||||
CARGO_INCREMENTAL: 0
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
@@ -44,10 +44,10 @@ jobs:
|
||||
- name: Install the system libraries the GUI links
|
||||
run: sudo apt-get update && sudo apt-get install -y pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcursor-dev libxrandr-dev libxi-dev libgtk-3-dev libfontconfig1-dev libfreetype-dev
|
||||
- name: Check out craft-fonts
|
||||
uses: storytold/craft-common-actions/actions/checkout-craft-fonts@1754016e67ca9b74dfa4cf4678b2ac3548e9840f
|
||||
uses: storytold/craft-common-actions/actions/checkout-craft-fonts@f5141455aac7b2eb4708aa259d8ffcdf10fde939
|
||||
with:
|
||||
ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
- uses: storytold/craft-common-actions/actions/checkout-craft-i18n@1754016e67ca9b74dfa4cf4678b2ac3548e9840f
|
||||
- uses: storytold/craft-common-actions/actions/checkout-craft-i18n@f5141455aac7b2eb4708aa259d8ffcdf10fde939
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
@@ -121,17 +121,17 @@ jobs:
|
||||
shell: pwsh
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Check out craft-fonts
|
||||
uses: storytold/craft-common-actions/actions/checkout-craft-fonts@1754016e67ca9b74dfa4cf4678b2ac3548e9840f
|
||||
uses: storytold/craft-common-actions/actions/checkout-craft-fonts@f5141455aac7b2eb4708aa259d8ffcdf10fde939
|
||||
with:
|
||||
ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
- uses: storytold/craft-common-actions/actions/checkout-craft-i18n@1754016e67ca9b74dfa4cf4678b2ac3548e9840f
|
||||
- uses: storytold/craft-common-actions/actions/checkout-craft-i18n@f5141455aac7b2eb4708aa259d8ffcdf10fde939
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
@@ -181,17 +181,17 @@ jobs:
|
||||
CARGO_INCREMENTAL: 0
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Check out craft-fonts
|
||||
uses: storytold/craft-common-actions/actions/checkout-craft-fonts@1754016e67ca9b74dfa4cf4678b2ac3548e9840f
|
||||
uses: storytold/craft-common-actions/actions/checkout-craft-fonts@f5141455aac7b2eb4708aa259d8ffcdf10fde939
|
||||
with:
|
||||
ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
- uses: storytold/craft-common-actions/actions/checkout-craft-i18n@1754016e67ca9b74dfa4cf4678b2ac3548e9840f
|
||||
- uses: storytold/craft-common-actions/actions/checkout-craft-i18n@f5141455aac7b2eb4708aa259d8ffcdf10fde939
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
|
||||
@@ -33,7 +33,7 @@ jobs:
|
||||
checks: read
|
||||
attestations: write
|
||||
id-token: write
|
||||
uses: storytold/craft-common-actions/.github/workflows/nightly-release.yml@1754016e67ca9b74dfa4cf4678b2ac3548e9840f
|
||||
uses: storytold/craft-common-actions/.github/workflows/nightly-release.yml@605d58766e8f536564da8fc81d3676ac86816dea
|
||||
with:
|
||||
tooling-revision: 605d58766e8f536564da8fc81d3676ac86816dea
|
||||
producer_run_id: ${{ inputs.producer_run_id }}
|
||||
|
||||
@@ -22,7 +22,7 @@ jobs:
|
||||
actions: read
|
||||
attestations: write
|
||||
id-token: write
|
||||
uses: storytold/craft-common-actions/.github/workflows/nightly-unsigned.yml@1754016e67ca9b74dfa4cf4678b2ac3548e9840f
|
||||
uses: storytold/craft-common-actions/.github/workflows/nightly-unsigned.yml@605d58766e8f536564da8fc81d3676ac86816dea
|
||||
with:
|
||||
tooling-revision: 605d58766e8f536564da8fc81d3676ac86816dea
|
||||
app-name: wordcraft
|
||||
|
||||
@@ -28,7 +28,7 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
@@ -58,7 +58,7 @@ jobs:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
|
||||
@@ -102,8 +102,6 @@ impl WordApp {
|
||||
command_catalog_lang: "",
|
||||
key_overrides: Default::default(),
|
||||
command_states: Default::default(),
|
||||
command_states_cache: None,
|
||||
keytip_cache: None,
|
||||
fonts_frames: 0,
|
||||
clipboard_published: false,
|
||||
printing: Default::default(),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! The app's side of the framework command runtime: the catalog of commands and their default
|
||||
//! chords, the per-frame state (enabled, checked, hint) and the running of what the runtime fired.
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::keymap::Overrides;
|
||||
@@ -83,62 +83,6 @@ fn with_word_chords(catalog: AppCatalog) -> AppCatalog {
|
||||
.fold(catalog, |catalog, command| catalog.replace_defaults(command.id(), Vec::new()))
|
||||
}
|
||||
|
||||
/// Precomputed command references, translated hints, and last gate results for
|
||||
/// [`WordApp::command_input`], rebuilt when the engine registry or interface language changes.
|
||||
///
|
||||
/// Every command's gate predicate still runs on every frame so enabled/disabled state is never
|
||||
/// stale; only the per-frame `CATALOG.lines()` parsing, `i18n` hint formatting, and full
|
||||
/// `CommandStates` map rebuild are skipped when nothing changed.
|
||||
pub(crate) struct CommandStatesCache {
|
||||
registry: Arc<wordcraft_engine::Registry>,
|
||||
lang: &'static str,
|
||||
/// `(id, location, label, prebuilt CommandRef, last (enabled, reason))` for every registered
|
||||
/// engine command, in registry order.
|
||||
registered: Vec<(&'static str, &'static str, &'static str, CommandRef, Option<(bool, Option<&'static str>)>)>,
|
||||
/// `(id, prebuilt CommandRef, last enabled)` for gated `ui.*` commands.
|
||||
ui_commands: Vec<(&'static str, CommandRef, Option<bool>)>,
|
||||
/// `(id, prebuilt CommandRef, last enabled)` for unimplemented Word catalog commands.
|
||||
unimplemented: Vec<(&'static str, CommandRef, Option<bool>)>,
|
||||
/// Precomputed `CommandRef` and last enabled state for `ui.dialog {"name": "commands"}`.
|
||||
commands_dialog: (CommandRef, Option<bool>),
|
||||
/// Translated fallback hint shared by all unimplemented catalog commands.
|
||||
unavailable_hint: Option<Arc<str>>,
|
||||
/// Cached translated hints by `(spec index, disabled reason)` for the current language.
|
||||
hints: HashMap<(usize, Option<&'static str>), Option<Arc<str>>>,
|
||||
}
|
||||
|
||||
impl CommandStatesCache {
|
||||
fn new(registry: Arc<wordcraft_engine::Registry>, lang: &'static str) -> Self {
|
||||
let registered = registry.all().iter().map(|spec| (spec.id, spec.location, spec.label, CommandRef::new(spec.id), None)).collect();
|
||||
let ui_commands = ["ui.collapseRibbon", "ui.resetInterfaceSize", "ui.print", "ui.printSubmit"]
|
||||
.into_iter()
|
||||
.chain(GATED_UI_COMMANDS.iter().map(|(id, _)| *id))
|
||||
.map(|id| (id, CommandRef::new(id), None))
|
||||
.collect();
|
||||
let unimplemented = unimplemented_catalog_commands(®istry).map(|(id, _)| (id, CommandRef::new(id), None)).collect();
|
||||
let commands_dialog = (CommandRef::with_params("ui.dialog", json!({"name": "commands"})), None);
|
||||
let unavailable_hint = crate::widgets::command_hint("", "", Some("")).map(Arc::from);
|
||||
CommandStatesCache { registry, lang, registered, ui_commands, unimplemented, commands_dialog, unavailable_hint, hints: HashMap::new() }
|
||||
}
|
||||
|
||||
fn matches(&self, registry: &Arc<wordcraft_engine::Registry>, lang: &'static str) -> bool {
|
||||
self.lang == lang && Arc::ptr_eq(&self.registry, registry)
|
||||
}
|
||||
|
||||
fn hint_for(&mut self, idx: usize, location: &str, label: &str, reason: Option<&'static str>) -> Option<Arc<str>> {
|
||||
self.hints.entry((idx, reason)).or_insert_with(|| crate::widgets::command_hint(location, label, reason).map(Arc::from)).clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Gate check for a command known to live in `app.session.registry` (skips the extra registry lookup).
|
||||
fn gate_registered(app: &WordApp, id: &str) -> (bool, Option<&'static str>) {
|
||||
if !app.has_document() && !super::commands::works_without_document(id) {
|
||||
return (false, None);
|
||||
}
|
||||
let reason = app.session.why_disabled(id);
|
||||
(reason.is_none(), reason)
|
||||
}
|
||||
|
||||
impl WordApp {
|
||||
/// The app's commands as the keymap knows them: every engine command with its default
|
||||
/// chords, then the interface commands. The interface commands come last because a tie in the
|
||||
@@ -233,108 +177,36 @@ impl WordApp {
|
||||
if let Some(catalog) = self.catalog() {
|
||||
input = input.catalog(catalog);
|
||||
}
|
||||
let lang = crate::i18n::current().code();
|
||||
let fresh_cache = !self.command_states_cache.as_ref().is_some_and(|cache| cache.matches(®istry, lang));
|
||||
let mut cache = if fresh_cache {
|
||||
CommandStatesCache::new(registry, lang)
|
||||
} else {
|
||||
self.command_states_cache.take().unwrap_or_else(|| CommandStatesCache::new(registry, lang))
|
||||
};
|
||||
|
||||
if fresh_cache {
|
||||
let mut states = CommandStates::new();
|
||||
for i in 0..cache.registered.len() {
|
||||
let (id, location, label, _, _) = cache.registered[i];
|
||||
let current = gate_registered(self, id);
|
||||
cache.registered[i].4 = Some(current);
|
||||
let mut state = CommandState::new().enabled(current.0);
|
||||
if let Some(hint) = cache.hint_for(i, location, label, current.1) {
|
||||
state = state.hint(hint);
|
||||
}
|
||||
states.set(cache.registered[i].3.clone(), state);
|
||||
let mut states = CommandStates::new();
|
||||
for spec in registry.all() {
|
||||
let (enabled, reason) = gate(self, spec.id); // A toggle's checked state is bound at its ribbon button, not here.
|
||||
let mut state = CommandState::new().enabled(enabled);
|
||||
if let Some(hint) = crate::widgets::command_hint(spec.location, spec.label, reason) {
|
||||
state = state.hint(hint);
|
||||
}
|
||||
for i in 0..cache.ui_commands.len() {
|
||||
let id = cache.ui_commands[i].0;
|
||||
let enabled = command_enabled(self, id);
|
||||
cache.ui_commands[i].2 = Some(enabled);
|
||||
states.set(cache.ui_commands[i].1.clone(), CommandState::new().enabled(enabled));
|
||||
}
|
||||
for i in 0..cache.unimplemented.len() {
|
||||
let id = cache.unimplemented[i].0;
|
||||
let enabled = command_enabled(self, id);
|
||||
cache.unimplemented[i].2 = Some(enabled);
|
||||
let mut state = CommandState::new().enabled(enabled);
|
||||
if let Some(hint) = cache.unavailable_hint.as_ref() {
|
||||
state = state.hint(Arc::clone(hint));
|
||||
}
|
||||
states.set(cache.unimplemented[i].1.clone(), state);
|
||||
}
|
||||
let dialog_enabled = command_enabled(self, "ui.dialog");
|
||||
cache.commands_dialog.1 = Some(dialog_enabled);
|
||||
states.set(cache.commands_dialog.0.clone(), CommandState::new().enabled(dialog_enabled));
|
||||
if *self.command_states != states {
|
||||
self.command_states.edit(|kept| *kept = states);
|
||||
}
|
||||
self.command_states_cache = Some(cache);
|
||||
return input.states(&self.command_states);
|
||||
states.set(CommandRef::new(spec.id), state);
|
||||
}
|
||||
|
||||
// Steady-state path: re-evaluate every gate without allocating, and only touch
|
||||
// `self.command_states` when at least one command's gate actually changed.
|
||||
let mut reg_changes: Vec<(usize, bool, Option<&'static str>)> = Vec::new();
|
||||
for (i, &(id, _, _, _, last)) in cache.registered.iter().enumerate() {
|
||||
let current = gate_registered(self, id);
|
||||
if last != Some(current) {
|
||||
reg_changes.push((i, current.0, current.1));
|
||||
// Print acts on the document, so with none open it is off like the engine's commands (#1129); a command with
|
||||
// no state here resolves as enabled.
|
||||
let ids = ["ui.collapseRibbon", "ui.resetInterfaceSize", "ui.print", "ui.printSubmit"]
|
||||
.into_iter()
|
||||
.chain(GATED_UI_COMMANDS.iter().map(|(id, _)| *id));
|
||||
for id in ids {
|
||||
states.set(CommandRef::new(id), CommandState::new().enabled(command_enabled(self, id)));
|
||||
}
|
||||
let unavailable_hint: Option<Arc<str>> = crate::widgets::command_hint("", "", Some("")).map(Arc::from);
|
||||
for (id, _) in unimplemented_catalog_commands(®istry) {
|
||||
let mut state = CommandState::new().enabled(command_enabled(self, id));
|
||||
if let Some(hint) = unavailable_hint.as_ref() {
|
||||
state = state.hint(Arc::clone(hint));
|
||||
}
|
||||
states.set(CommandRef::new(id), state);
|
||||
}
|
||||
let mut ui_changes: Vec<(usize, bool)> = Vec::new();
|
||||
for (i, &(id, _, last)) in cache.ui_commands.iter().enumerate() {
|
||||
let enabled = command_enabled(self, id);
|
||||
if last != Some(enabled) {
|
||||
ui_changes.push((i, enabled));
|
||||
}
|
||||
let commands = CommandRef::with_params("ui.dialog", json!({"name": "commands"}));
|
||||
states.set(commands, CommandState::new().enabled(command_enabled(self, "ui.dialog")));
|
||||
if *self.command_states != states {
|
||||
self.command_states.edit(|kept| *kept = states);
|
||||
}
|
||||
let mut unimpl_changes: Vec<(usize, bool)> = Vec::new();
|
||||
for (i, &(id, _, last)) in cache.unimplemented.iter().enumerate() {
|
||||
let enabled = command_enabled(self, id);
|
||||
if last != Some(enabled) {
|
||||
unimpl_changes.push((i, enabled));
|
||||
}
|
||||
}
|
||||
let dialog_enabled = command_enabled(self, "ui.dialog");
|
||||
let dialog_changed = cache.commands_dialog.1 != Some(dialog_enabled);
|
||||
|
||||
if !reg_changes.is_empty() || !ui_changes.is_empty() || !unimpl_changes.is_empty() || dialog_changed {
|
||||
self.command_states.edit(|kept| {
|
||||
for (i, enabled, reason) in reg_changes {
|
||||
let (_, location, label, _, _) = cache.registered[i];
|
||||
cache.registered[i].4 = Some((enabled, reason));
|
||||
let mut state = CommandState::new().enabled(enabled);
|
||||
if let Some(hint) = cache.hint_for(i, location, label, reason) {
|
||||
state = state.hint(hint);
|
||||
}
|
||||
kept.set(cache.registered[i].3.clone(), state);
|
||||
}
|
||||
for (i, enabled) in ui_changes {
|
||||
cache.ui_commands[i].2 = Some(enabled);
|
||||
kept.set(cache.ui_commands[i].1.clone(), CommandState::new().enabled(enabled));
|
||||
}
|
||||
for (i, enabled) in unimpl_changes {
|
||||
cache.unimplemented[i].2 = Some(enabled);
|
||||
let mut state = CommandState::new().enabled(enabled);
|
||||
if let Some(hint) = cache.unavailable_hint.as_ref() {
|
||||
state = state.hint(Arc::clone(hint));
|
||||
}
|
||||
kept.set(cache.unimplemented[i].1.clone(), state);
|
||||
}
|
||||
if dialog_changed {
|
||||
cache.commands_dialog.1 = Some(dialog_enabled);
|
||||
kept.set(cache.commands_dialog.0.clone(), CommandState::new().enabled(dialog_enabled));
|
||||
}
|
||||
});
|
||||
}
|
||||
self.command_states_cache = Some(cache);
|
||||
input.states(&self.command_states)
|
||||
}
|
||||
|
||||
@@ -449,42 +321,4 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cached `command_input()` keeps steady-state idle calls zero-change while immediately reflecting
|
||||
/// table/context transitions and document-open state changes.
|
||||
#[test]
|
||||
fn command_input_cache_tracks_state_transitions_and_is_noop_when_idle() {
|
||||
let mut app = WordApp::new(Session::new(wordcraft_doc::Document::new()), Default::default());
|
||||
let _ = app.command_input();
|
||||
let rev_after_first = app.command_states.revision();
|
||||
assert!(
|
||||
!app.command_states.get(&CommandRef::new("table.deleteRow")).unwrap().is_enabled(),
|
||||
"table.deleteRow starts disabled outside a table"
|
||||
);
|
||||
assert!(app.command_states.get(&CommandRef::new("format.bold")).unwrap().is_enabled(), "format.bold starts enabled");
|
||||
|
||||
// Repeated idle calls must not mutate `command_states` or bump its revision.
|
||||
for _ in 0..5 {
|
||||
let _ = app.command_input();
|
||||
}
|
||||
assert_eq!(app.command_states.revision(), rev_after_first, "idle frames leave command_states revision untouched");
|
||||
|
||||
// Entering a table enables `table.deleteRow` immediately on the next `command_input()`.
|
||||
app.run("insert.table", json!({"rows": 2, "cols": 2})).unwrap();
|
||||
let _ = app.command_input();
|
||||
let rev_after_table = app.command_states.revision();
|
||||
assert_ne!(rev_after_table, rev_after_first, "state change bumps command_states revision");
|
||||
assert!(app.command_states.get(&CommandRef::new("table.deleteRow")).unwrap().is_enabled(), "table.deleteRow is enabled inside a table");
|
||||
|
||||
// Closing the document disables document commands immediately.
|
||||
app.no_document = Some(app.session.document_id());
|
||||
let _ = app.command_input();
|
||||
assert!(!app.command_states.get(&CommandRef::new("format.bold")).unwrap().is_enabled(), "format.bold disables when no document is open");
|
||||
|
||||
// Dropping the cache and rebuilding from scratch must produce the exact same `CommandStates`.
|
||||
let incremental = (*app.command_states).clone();
|
||||
app.command_states_cache = None;
|
||||
let _ = app.command_input();
|
||||
assert_eq!(*app.command_states, incremental, "incremental cache matches full rebuild exactly");
|
||||
}
|
||||
}
|
||||
|
||||
+52
-115
@@ -3,7 +3,7 @@
|
||||
//! machine, badges, claimed keys) is the framework's `Keytips`; this module gives it WordCraft's
|
||||
//! tabs, controls, legacy sequences and regions.
|
||||
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::behavior::overlay::MinWidth;
|
||||
use craft::keymap::CommandRef;
|
||||
@@ -59,32 +59,29 @@ pub const SEARCH_LETTER: &str = "Q";
|
||||
/// Legacy menu keys (#1697): Word 2003 menu sequences Word for Windows still takes after Alt,
|
||||
/// observed there. Hidden, as in Word: no badges, not in Customize Keyboard. They start only on
|
||||
/// letters no tab keytip or Search uses (see the test).
|
||||
fn legacy() -> &'static [(&'static str, CommandRef)] {
|
||||
static LEGACY: OnceLock<Vec<(&'static str, CommandRef)>> = OnceLock::new();
|
||||
LEGACY.get_or_init(|| {
|
||||
let cmd = CommandRef::new;
|
||||
let dialog = |name: &str| CommandRef::with_params("ui.dialog", serde_json::json!({ "name": name }));
|
||||
vec![
|
||||
("VU", cmd("view.fullScreenView")),
|
||||
("VP", cmd("view.printLayout")),
|
||||
("VW", cmd("view.webLayout")),
|
||||
("VO", cmd("view.outline")),
|
||||
("VN", cmd("view.draft")),
|
||||
("VD", cmd("view.navigationPane")),
|
||||
("VL", cmd("view.ruler")),
|
||||
("VZ", dialog("zoom")),
|
||||
("EL", cmd("select.all")),
|
||||
("EF", dialog("find")),
|
||||
("EE", dialog("replace")),
|
||||
("EG", dialog("goto")),
|
||||
("IS", dialog("symbol")),
|
||||
("IPF", cmd("insert.picture")),
|
||||
("OF", dialog("font")),
|
||||
("OP", dialog("paragraph")),
|
||||
("TW", cmd("review.wordCount")),
|
||||
("AIT", dialog("insertTable")),
|
||||
]
|
||||
})
|
||||
fn legacy() -> Vec<(&'static str, CommandRef)> {
|
||||
let cmd = CommandRef::new;
|
||||
let dialog = |name: &str| CommandRef::with_params("ui.dialog", serde_json::json!({ "name": name }));
|
||||
vec![
|
||||
("VU", cmd("view.fullScreenView")),
|
||||
("VP", cmd("view.printLayout")),
|
||||
("VW", cmd("view.webLayout")),
|
||||
("VO", cmd("view.outline")),
|
||||
("VN", cmd("view.draft")),
|
||||
("VD", cmd("view.navigationPane")),
|
||||
("VL", cmd("view.ruler")),
|
||||
("VZ", dialog("zoom")),
|
||||
("EL", cmd("select.all")),
|
||||
("EF", dialog("find")),
|
||||
("EE", dialog("replace")),
|
||||
("EG", dialog("goto")),
|
||||
("IS", dialog("symbol")),
|
||||
("IPF", cmd("insert.picture")),
|
||||
("OF", dialog("font")),
|
||||
("OP", dialog("paragraph")),
|
||||
("TW", cmd("review.wordCount")),
|
||||
("AIT", dialog("insertTable")),
|
||||
]
|
||||
}
|
||||
|
||||
/// The letter badge for a tab (empty when it has none).
|
||||
@@ -382,82 +379,13 @@ fn controls(tab: &str) -> Vec<Control> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps an ASCII alphanumeric keytip letter to a static single-character `&'static str`, avoiding
|
||||
/// per-control `String` allocations when caching [`KeytipControl`].
|
||||
fn ascii_upper_str(c: char) -> Option<&'static str> {
|
||||
const CHARS: &str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
let idx = CHARS.find(c.to_ascii_uppercase())?;
|
||||
CHARS.get(idx..idx + 1)
|
||||
}
|
||||
|
||||
/// Cached keytip controls and reserved shortcut letters per engine registry and active ribbon tab,
|
||||
/// avoiding per-frame `registry.all()` scans and `KeytipControl` / `CommandRef` allocations while
|
||||
/// idle.
|
||||
pub(crate) struct KeytipCache {
|
||||
registry: Arc<wordcraft_engine::Registry>,
|
||||
reserved: Vec<char>,
|
||||
tab: String,
|
||||
tab_controls: Vec<KeytipControl<'static>>,
|
||||
}
|
||||
|
||||
impl KeytipCache {
|
||||
fn build_tab_controls(app: &WordApp, tab: &str) -> Vec<KeytipControl<'static>> {
|
||||
let mut out: Vec<KeytipControl<'static>> = controls(tab)
|
||||
.into_iter()
|
||||
.map(|control| {
|
||||
let letters = shortcut_letter(app, &control.command.id).and_then(ascii_upper_str);
|
||||
KeytipControl::new(control.command, control.tip, letters)
|
||||
})
|
||||
.collect();
|
||||
out.push(KeytipControl::new(CommandRef::new("tools.search"), "Search", Some(SEARCH_LETTER)));
|
||||
out
|
||||
}
|
||||
|
||||
fn new(app: &WordApp, tab: &str) -> Self {
|
||||
KeytipCache {
|
||||
registry: app.session.registry.clone(),
|
||||
reserved: reserved_letters(app),
|
||||
tab: tab.to_owned(),
|
||||
tab_controls: Self::build_tab_controls(app, tab),
|
||||
}
|
||||
}
|
||||
|
||||
fn sync(&mut self, app: &WordApp, tab: &str) {
|
||||
let reg_changed = !Arc::ptr_eq(&self.registry, &app.session.registry);
|
||||
if reg_changed {
|
||||
self.registry = app.session.registry.clone();
|
||||
self.reserved = reserved_letters(app);
|
||||
}
|
||||
if reg_changed || self.tab != tab {
|
||||
self.tab.clear();
|
||||
self.tab.push_str(tab);
|
||||
self.tab_controls = Self::build_tab_controls(app, tab);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The Backstage's pages as cached static keytip controls.
|
||||
fn backstage_controls() -> &'static [KeytipControl<'static>] {
|
||||
static CONTROLS: OnceLock<Vec<KeytipControl<'static>>> = OnceLock::new();
|
||||
CONTROLS.get_or_init(|| {
|
||||
crate::backstage::KEYTIPS
|
||||
.iter()
|
||||
.map(|(page, letter)| {
|
||||
KeytipControl::new(CommandRef::with_params("ui.backstage", serde_json::json!({ "page": page })), page, Some(letter))
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// A control with the letters it asks for (used by unit tests).
|
||||
#[cfg(test)]
|
||||
/// A control with the letters it asks for.
|
||||
struct Row {
|
||||
control: Control,
|
||||
letters: Option<String>,
|
||||
}
|
||||
|
||||
/// A tab's controls with the shortcut letter each keeps, when its command has an Alt+letter shortcut.
|
||||
#[cfg(test)]
|
||||
fn rows(app: &WordApp, tab: &str) -> Vec<Row> {
|
||||
controls(tab)
|
||||
.into_iter()
|
||||
@@ -468,6 +396,22 @@ fn rows(app: &WordApp, tab: &str) -> Vec<Row> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The Search box: its badge is Alt, Q on every tab.
|
||||
fn search_row() -> Row {
|
||||
Row { control: Control { tip: "Search", command: CommandRef::new("tools.search") }, letters: Some(SEARCH_LETTER.to_string()) }
|
||||
}
|
||||
|
||||
/// The Backstage's pages, one badge each: the first letter its page has in `KEYTIPS`.
|
||||
fn backstage_rows() -> Vec<Row> {
|
||||
crate::backstage::KEYTIPS
|
||||
.iter()
|
||||
.map(|(page, letter)| Row {
|
||||
control: Control { tip: page, command: CommandRef::with_params("ui.backstage", serde_json::json!({ "page": page })) },
|
||||
letters: Some(letter.to_string()),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Shows the keytips after the shell and the regions they place badges on, and acts on what the keys chose.
|
||||
pub(crate) fn show(ui: &mut Ui, app: &mut WordApp, ribbon: &RibbonOutput<String>, screen_bar: Option<Rect>) {
|
||||
let ctx = ui.ctx().clone();
|
||||
@@ -482,15 +426,14 @@ pub(crate) fn show(ui: &mut Ui, app: &mut WordApp, ribbon: &RibbonOutput<String>
|
||||
.filter_map(|t| ribbon.tab_ids.iter().find(|(name, _)| name == t).map(|(_, id)| (Label::from(t), tab_letter(t), *id)))
|
||||
.collect();
|
||||
|
||||
let cache = match app.keytip_cache.take() {
|
||||
Some(mut cache) => {
|
||||
cache.sync(app, &app.ui.tab);
|
||||
cache
|
||||
}
|
||||
None => KeytipCache::new(app, &app.ui.tab),
|
||||
};
|
||||
let controls: &[KeytipControl<'_>] = if app.ui.backstage { backstage_controls() } else { &cache.tab_controls };
|
||||
let sequences: &[(&str, CommandRef)] = if cfg!(target_os = "macos") { &[] } else { legacy() };
|
||||
let mut rows = if app.ui.backstage { backstage_rows() } else { rows(app, &app.ui.tab) };
|
||||
if !app.ui.backstage {
|
||||
rows.push(search_row());
|
||||
}
|
||||
let controls: Vec<KeytipControl<'_>> =
|
||||
rows.iter().map(|r| KeytipControl::new(r.control.command.clone(), r.control.tip, r.letters.as_deref())).collect();
|
||||
|
||||
let sequences = if cfg!(target_os = "macos") { Vec::new() } else { legacy() };
|
||||
// A hidden toolbar has no badges, and a disabled command's button has none to press.
|
||||
let digits: Vec<CommandRef> = if app.ui.quick_access_toolbar {
|
||||
app.session
|
||||
@@ -504,6 +447,7 @@ pub(crate) fn show(ui: &mut Ui, app: &mut WordApp, ribbon: &RibbonOutput<String>
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let reserved = reserved_letters(app);
|
||||
|
||||
let mut places: Vec<Arc<Recorded>> = Vec::new();
|
||||
places.extend(ribbon.controls.clone());
|
||||
@@ -513,15 +457,8 @@ pub(crate) fn show(ui: &mut Ui, app: &mut WordApp, ribbon: &RibbonOutput<String>
|
||||
places.extend(inspect::recorded(&ctx, backstage_region()));
|
||||
}
|
||||
|
||||
let out = Keytips::new(enabled)
|
||||
.tabs(&tabs)
|
||||
.controls(controls)
|
||||
.sequences(sequences)
|
||||
.digits(&digits)
|
||||
.reserved(&cache.reserved)
|
||||
.places(&places)
|
||||
.show_over(ui);
|
||||
app.keytip_cache = Some(cache);
|
||||
let out =
|
||||
Keytips::new(enabled).tabs(&tabs).controls(&controls).sequences(&sequences).digits(&digits).reserved(&reserved).places(&places).show_over(ui);
|
||||
// PLAN: `WordApp::keytips_active` is app-hub's (rb.A11).
|
||||
app.keytips_active = out.active;
|
||||
if let Some(tab) = out.tab.as_deref().and_then(tab_of_badge) {
|
||||
|
||||
@@ -83,7 +83,7 @@ fn legacy_menu_keys_stay_off_the_ribbon_letters_and_all_resolve() {
|
||||
for (keys, command) in legacy() {
|
||||
let first = &keys[..1];
|
||||
assert!(!taken.iter().any(|l| l.starts_with(first)), "{keys} starts on a ribbon letter");
|
||||
assert!(!legacy().iter().any(|(k, _)| *k != *keys && k.starts_with(*keys)), "{keys} hides a longer sequence");
|
||||
assert!(!legacy().iter().any(|(k, _)| *k != keys && k.starts_with(keys)), "{keys} hides a longer sequence");
|
||||
let mut b = app();
|
||||
// The hosted parameter dialogs open nothing before the window's context is known.
|
||||
b.ctx = Some(egui::Context::default());
|
||||
@@ -253,25 +253,3 @@ fn keytips_option_persists() {
|
||||
b.apply_prefs(serde_json::from_str("{}").unwrap());
|
||||
assert!(b.ui.keytips_enabled, "older preferences retain enabled KeyTips");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keytip_cache_reuses_tab_controls_and_updates_on_tab_switch() {
|
||||
let mut h = harness(app());
|
||||
assert_eq!(h.state().keytip_cache.as_ref().map(|c| c.tab.as_str()), Some("Home"));
|
||||
let home_ptr = h.state().keytip_cache.as_ref().unwrap().tab_controls.as_ptr();
|
||||
let reserved_ptr = h.state().keytip_cache.as_ref().unwrap().reserved.as_ptr();
|
||||
|
||||
// Idle frames reuse the cached control and reserved-letter buffers without rebuilding.
|
||||
crate::test_harness::frames(&mut h, 4);
|
||||
assert_eq!(h.state().keytip_cache.as_ref().unwrap().tab_controls.as_ptr(), home_ptr);
|
||||
assert_eq!(h.state().keytip_cache.as_ref().unwrap().reserved.as_ptr(), reserved_ptr);
|
||||
|
||||
// Switching tabs rebuilds the cached tab controls while keeping reserved letters intact.
|
||||
h.state_mut().run("ui.tab", serde_json::json!({"tab": "Insert"})).unwrap();
|
||||
crate::test_harness::frame(&mut h);
|
||||
let cache = h.state().keytip_cache.as_ref().unwrap();
|
||||
assert_eq!(cache.tab, "Insert");
|
||||
// Tab controls plus the trailing Search badge (`SEARCH_LETTER`).
|
||||
assert_eq!(cache.tab_controls.len(), controls("Insert").len() + 1);
|
||||
assert_eq!(cache.reserved.as_ptr(), reserved_ptr);
|
||||
}
|
||||
|
||||
@@ -402,11 +402,6 @@ pub struct WordApp {
|
||||
/// The app-scope command states handed to the runtime, edited only when they change: the runtime resolves
|
||||
/// commands again only when their revision moves ([`WordApp::command_input`]).
|
||||
pub(crate) command_states: craft::ui::contract::Tracked<craft::ui::runtime::commands::CommandStates>,
|
||||
/// Precomputed command metadata, translated hints, and last gate results for [`WordApp::command_input`],
|
||||
/// avoiding per-frame allocations and catalog reparsing while idle.
|
||||
pub(crate) command_states_cache: Option<app::command_board::CommandStatesCache>,
|
||||
/// Cached keytip control lists and reserved shortcut letters per registry and active ribbon tab.
|
||||
pub(crate) keytip_cache: Option<keytips::KeytipCache>,
|
||||
/// Frames drawn so far, counted to 2: the control channel's queued requests wait until the
|
||||
/// fonts are warm.
|
||||
pub(crate) fonts_frames: u8,
|
||||
|
||||
@@ -146,8 +146,6 @@ impl WordApp {
|
||||
command_catalog_lang: _,
|
||||
key_overrides: _,
|
||||
command_states: _,
|
||||
command_states_cache: _,
|
||||
keytip_cache: _,
|
||||
fonts_frames: _,
|
||||
printing: _,
|
||||
print_feedback_seen: _,
|
||||
|
||||
Reference in New Issue
Block a user