Keyboard shortcuts window: labels in the interface language, grouped by menu

The window listed the command catalog the keys run on, which keeps English labels, so a translated interface showed English rows all under Other. It now lists each command as the menus label it, in the current language, with its English label kept for search, under the menu bar path that lists it, and relists them when the language changes while it is open.
This commit is contained in:
FredApps committed 2026-11-27 00:27:44 +07:00
1 parent ec0513414a
commit 54a3be06df
2 files changed
+71 -6

No files matched your search

+38 -6
View File
@@ -1,7 +1,7 @@
//! Help ▸ Keyboard shortcuts: the framework's shortcuts dialog, read-only, over the catalog the keys run on and the
//! keys the document view handles itself.
use craft::keymap::{Keymap, Platform, Scopes};
use craft::keymap::{CommandDef, Keymap, Platform, Scopes};
use craft::menu::shortcuts::{DialogState, FixedRow, Options, ShortcutsDialog};
use crate::PdfCraftApp;
@@ -15,6 +15,34 @@ pub(crate) struct ShortcutsSession {
state: DialogState,
keymap: Keymap,
options: Options,
/// The interface language the keymap's labels are in.
language: &'static str,
}
/// The keymap the window lists: the catalog the keys run on, its labels in the current interface
/// language and each command filed under the menu that lists it. The catalog itself keeps the
/// English labels, and an English label stays an alias, so search finds a command by either.
fn keymap(ctx: &egui::Context) -> Keymap {
let defs = crate::commands::catalog::shared_catalog(crate::commands::mac_shortcuts(ctx))
.map(|c| c.defs().iter().map(localized).collect())
.unwrap_or_default();
Keymap::new(defs, Scopes::new(), Platform::current())
}
/// `def` as the window shows it: labelled as the menus label it, under its menu bar path.
fn localized(def: &CommandDef) -> CommandDef {
let mut def = def.clone();
let english = std::mem::take(&mut def.label);
def.label = crate::i18n::menu_label(&def.command.id, &english);
if def.paths.is_empty()
&& let Some(menu) = crate::menus::menu_path(&def.command.id)
{
def.paths.push(menu.iter().map(|title| (*title).to_owned()).chain([english.clone()]).collect());
}
if def.label != english {
def.aliases.push(english);
}
def
}
/// The keys the document view handles itself, which no catalog command binds.
@@ -59,9 +87,8 @@ impl PdfCraftApp {
options.editable = false;
options.fixed = fixed_rows(ctx);
// The catalog the keys run on, in the app scope; the page view's own keys are the fixed rows.
let defs = crate::commands::catalog::shared_catalog(crate::commands::mac_shortcuts(ctx)).map(|c| c.defs().to_vec()).unwrap_or_default();
let keymap = Keymap::new(defs, Scopes::new(), Platform::current());
self.shortcuts = Some(ShortcutsSession { state: DialogState::new(), keymap, options });
let keymap = keymap(ctx);
self.shortcuts = Some(ShortcutsSession { state: DialogState::new(), keymap, options, language: crate::i18n::current().code() });
}
/// Shows the shortcuts window for one pass; closing it drops the session.
@@ -69,9 +96,14 @@ impl PdfCraftApp {
let Some(session) = self.shortcuts.as_mut() else {
return;
};
let ShortcutsSession { state, keymap, options } = session;
let ShortcutsSession { state, keymap: listed, options, language } = session;
options.fixed = fixed_rows(ui.ctx());
ShortcutsDialog::new(keymap).options(options).show(ui, state);
// The interface language changed while the window is open: list the labels in the new one.
if *language != crate::i18n::current().code() {
*listed = keymap(ui.ctx());
*language = crate::i18n::current().code();
}
ShortcutsDialog::new(listed).options(options).show(ui, state);
if !state.is_open() {
self.shortcuts = None;
}
+33
View File
@@ -219,6 +219,39 @@ const WINDOW: &[&str] = &["view.next_tab", "view.previous_tab"];
const HELP: &[&str] =
&["help.shortcuts", SEP, "help.app_page", "help.github", "help.website", "help.discord", SEP, "help.check_updates", SEP, "help.about"];
/// Where the menu bar lists command `id`: the English titles of its menu and submenu, top menu first,
/// as [`MenuBar`] builds them. The shortcuts window groups its rows by them. `None` for a command
/// the bar lists by something other than its id (tools, documents, side panels) or not at all.
pub(crate) fn menu_path(id: &str) -> Option<&'static [&'static str]> {
const PLACES: &[(&[&str], &[&str])] = &[
(FILE, &["File"]),
(CREATE, &["File", "Create"]),
(EXPORT, &["File", "Export"]),
(PROTECT, &["File", "Protect"]),
(EDIT, &["Edit"]),
(PAGES, &["Edit", "Pages"]),
(SIGN, &["Sign"]),
(WINDOW, &["Window"]),
(HELP, &["Help"]),
(ZOOM, &["View", "Zoom"]),
(&[ROTATE_CW, ROTATE_CCW], &["View", "Rotate view"]),
(NAVIGATION, &["View", "Page navigation"]),
(DISPLAY, &["View", "Page display"]),
(PANES, &["View"]),
(PANE_COMMANDS, &["View"]),
(MODES, &["View"]),
(THEMES, &["View", "Display theme"]),
(&["view.palette"], &["View"]),
];
if id == SEP {
return None;
}
if FILL.iter().any(|(fill, _)| *fill == id) {
return Some(&["Sign", "Fill & Sign"]);
}
PLACES.iter().find(|(ids, _)| ids.contains(&id)).map(|(_, path)| *path)
}
/// A registry shortcut as a craft-keymap chord: `command` is ⌘ (Ctrl elsewhere) and `mac_ctrl`
/// the Control key on macOS. `None` for a key craft-keymap doesn't name.
pub(crate) fn chord(s: Shortcut) -> Option<Chord> {