Palette: entries rebuild when keyboard shortcuts change

The command palette caches its entries per language and plug-in set
(#243), but each entry also shows its shortcut: after editing shortcuts
the palette kept showing the old ones until the language changed. The
cache key now includes the shortcut editor's generation. From #242.

Co-authored-by: WildfootW <wildfootw@wildfoo.tw>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic 2026-10-07 10:50:52 +07:00
parent 24f7d5fba5
commit c91ca3634f
1 changed files with 9 additions and 4 deletions

View File

@ -42,11 +42,13 @@ struct Entry {
haystack: String, haystack: String,
} }
/// The palette's entries in `lang`. They are built when the language or the installed plug-ins /// The palette's entries in `lang`. They are built when the language, the installed plug-ins or
/// change, not on every frame while a query is typed. /// the keyboard shortcuts (shown beside each entry) change, not on every frame while a query is
/// typed.
fn entries(ctx: &egui::Context, lang: crate::i18n::Lang) -> Arc<Vec<Entry>> { fn entries(ctx: &egui::Context, lang: crate::i18n::Lang) -> Arc<Vec<Entry>> {
type Cached = ((&'static str, u64), Arc<Vec<Entry>>); type Cached = ((&'static str, u64, u64), Arc<Vec<Entry>>);
let key = (lang.code(), menus::plugin_revision()); let shortcuts = crate::shortcut_editor::GENERATION.load(std::sync::atomic::Ordering::Relaxed);
let key = (lang.code(), menus::plugin_revision(), shortcuts);
let id = egui::Id::new("palette-entries"); let id = egui::Id::new("palette-entries");
if let Some((k, v)) = ctx.data(|d| d.get_temp::<Cached>(id)) if let Some((k, v)) = ctx.data(|d| d.get_temp::<Cached>(id))
&& k == key && k == key
@ -123,6 +125,9 @@ mod tests {
assert!(Arc::ptr_eq(&en, &entries(&ctx, Lang::EN)), "kept while the language stays"); assert!(Arc::ptr_eq(&en, &entries(&ctx, Lang::EN)), "kept while the language stays");
let zh_entries = entries(&ctx, zh); let zh_entries = entries(&ctx, zh);
assert!(!Arc::ptr_eq(&en, &zh_entries), "rebuilt for another language"); assert!(!Arc::ptr_eq(&en, &zh_entries), "rebuilt for another language");
// Editing a shortcut rebuilds them, so the palette shows the new one.
crate::shortcut_editor::GENERATION.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
assert!(!Arc::ptr_eq(&zh_entries, &entries(&ctx, zh)), "rebuilt after a shortcut edit");
let group = zh_entries.iter().find(|e| e.id == "object.group").unwrap(); let group = zh_entries.iter().find(|e| e.id == "object.group").unwrap();
assert!(group.shown.ends_with(crate::i18n::tr(zh, "Group")), "{}", group.shown); assert!(group.shown.ends_with(crate::i18n::tr(zh, "Group")), "{}", group.shown);
// A query matches the English label, the shown label or the id. // A query matches the English label, the shown label or the id.