Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 49 additions & 26 deletions apps/lightcraft/src/native_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ fn settled(had: Option<Accelerator>, want: Option<Accelerator>, native_call_work

/// Whether the per-frame accelerator sync has to run: the focus or recording state changed, or an
/// earlier sync left an item whose native accelerator is not the wanted one (a failed swap).
fn sync_needed(focus: bool, capturing: bool, synced_focus: bool, synced_capturing: bool, pending: bool) -> bool {
focus != synced_focus || capturing != synced_capturing || pending
fn sync_needed(focus: bool, capturing: bool, sidebar: bool, synced_focus: bool, synced_capturing: bool, synced_sidebar: bool, pending: bool) -> bool {
focus != synced_focus || capturing != synced_capturing || sidebar != synced_sidebar || pending
}

/// The three submenu calls a swap needs, so the failure paths can be tested without a main-thread
Expand Down Expand Up @@ -234,10 +234,10 @@ fn swap_item<T: SwapTarget + ?Sized>(target: &T, old: &T::Row, new: &T::Row, pos
false
}

/// The accelerator a native item has: none while a shortcut is being recorded, and none for the
/// keys a focused text field needs.
fn wanted_accelerator(accel: Option<Accelerator>, sc: &str, text_focus: bool, capturing: bool) -> Option<Accelerator> {
if capturing || (text_focus && yields_to_text(sc)) { None } else { accel }
/// The accelerator a native item has: none while a shortcut is being recorded, none for the keys a
/// focused text field needs, and none for the keys the sidebar has taken (Keyboard in Sidebar).
fn wanted_accelerator(accel: Option<Accelerator>, sc: &str, text_focus: bool, sidebar_keys: bool, capturing: bool) -> Option<Accelerator> {
if capturing || (text_focus && yields_to_text(sc)) || (sidebar_keys && yields_to_sidebar(sc)) { None } else { accel }
}

pub struct NativeMenu {
Expand All @@ -252,6 +252,8 @@ pub struct NativeMenu {
/// Structure of the installed menu (ids, kinds, submenu names); a change rebuilds it.
structure: String,
text_focus: bool,
/// Keyboard in Sidebar is on: the keys without Cmd, Ctrl or Alt are the sidebar's.
sidebar_keys: bool,
/// The keyboard shortcuts editor is recording a key: every accelerator is off so the key
/// press reaches it.
capturing: bool,
Expand Down Expand Up @@ -353,9 +355,18 @@ fn yields_to_text(sc: &str) -> bool {
!sc.contains("Cmd") && !sc.contains("Ctrl") || TEXT_EDIT.contains(&sc)
}

/// Accelerators that must step aside while the sidebar has the keys (Keyboard in Sidebar): the ones
/// without Cmd, Ctrl or Alt, such as F2 (Rename), which the sidebar takes for a row's rename.
fn yields_to_sidebar(sc: &str) -> bool {
!sc.contains("Cmd") && !sc.contains("Ctrl") && !sc.contains("Alt")
}

/// The shortcuts the menu bar runs itself, for `LightcraftApp::native_shortcuts`.
fn owned_by_menu<'a>(installed: impl Iterator<Item = &'a str>, text_focus: bool) -> HashSet<String> {
installed.filter(|sc| menu_delivers(sc) && !(text_focus && yields_to_text(sc))).map(str::to_string).collect()
fn owned_by_menu<'a>(installed: impl Iterator<Item = &'a str>, text_focus: bool, sidebar_keys: bool) -> HashSet<String> {
installed
.filter(|sc| menu_delivers(sc) && !(text_focus && yields_to_text(sc)) && !(sidebar_keys && yields_to_sidebar(sc)))
.map(str::to_string)
.collect()
}

/// `&` marks a mnemonic in muda labels.
Expand Down Expand Up @@ -439,6 +450,7 @@ impl NativeMenu {
rx,
structure: String::new(),
text_focus: false,
sidebar_keys: false,
capturing: false,
accelerators_pending: false,
};
Expand Down Expand Up @@ -532,6 +544,7 @@ impl NativeMenu {
help.set_as_help_menu_for_nsapp();
}
self.text_focus = false;
self.sidebar_keys = false;
self.capturing = false;
self.accelerators_pending = false;
self.publish_shortcuts(app);
Expand Down Expand Up @@ -591,7 +604,7 @@ impl NativeMenu {
fn publish_shortcuts(&self, app: &mut LightcraftApp) {
let installed = self.items.values().filter(|i| i.accel.is_some()).filter_map(|i| i.shortcut.as_deref());
// (none while the keymap editor records a shortcut: every key goes to it)
app.native_shortcuts = if self.capturing { HashSet::new() } else { owned_by_menu(installed, self.text_focus) };
app.native_shortcuts = if self.capturing { HashSet::new() } else { owned_by_menu(installed, self.text_focus, self.sidebar_keys) };
app.native_shortcuts.insert(SETTINGS_KEY.to_string());
}

Expand Down Expand Up @@ -621,7 +634,7 @@ impl NativeMenu {
let bar = menu_bar(app);
if structure_of(&bar, &app.ui.settings.keymap) != self.structure {
// (no early return: a rebuild installs every accelerator and resets the state they were synced
// to, so the sync below runs in this same frame and no key reaches the menu in between)
// to, so the sync below runs in this same frame: F2 must not reach the menu while the sidebar has the keys)
self.rebuild(app);
}
// Synchronize live labels and states row by row (accelerator ownership changes already
Expand All @@ -631,16 +644,18 @@ impl NativeMenu {

let focus = ctx.egui_wants_keyboard_input();
let capturing = app.recording_shortcut.is_some();
if sync_needed(focus, capturing, self.text_focus, self.capturing, self.accelerators_pending) {
let sidebar = app.ui.sidebar_keys;
if sync_needed(focus, capturing, sidebar, self.text_focus, self.capturing, self.sidebar_keys, self.accelerators_pending) {
self.text_focus = focus;
self.capturing = capturing;
self.sidebar_keys = sidebar;
let mut pending = false;
for it in self.items.values_mut().chain(self.app_items.iter_mut()) {
let Some(sc) = it.shortcut.as_deref() else { continue };
if it.accel.is_none() {
continue;
}
let want = wanted_accelerator(it.accel, sc, focus, capturing);
let want = wanted_accelerator(it.accel, sc, focus, sidebar, capturing);
it.set_accelerator(want);
pending |= it.current != want;
}
Expand Down Expand Up @@ -822,19 +837,23 @@ mod tests {
fn accelerators_yield_to_text_focus_and_shortcut_recording() {
let g = accelerator("G");
let cmd_s = accelerator("Cmd+S");
assert_eq!(wanted_accelerator(g, "G", false, false), g);
assert_eq!(wanted_accelerator(g, "G", true, false), None, "typing a G in a field");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", true, false), cmd_s, "⌘S still saves while typing");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", false, true), None, "the editor records the key");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", true, true), None, "recording wins over typing");
assert_eq!(wanted_accelerator(g, "G", false, false, false), g);
assert_eq!(wanted_accelerator(g, "G", true, false, false), None, "typing a G in a field");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", true, false, false), cmd_s, "⌘S still saves while typing");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", false, false, true), None, "the editor records the key");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", true, false, true), None, "recording wins over typing");
// the application menu's Settings… and Quit are tracked like the other items
for sc in [SETTINGS_KEY, "Cmd+Q"] {
let a = accelerator(sc);
assert!(a.is_some());
assert_eq!(wanted_accelerator(a, sc, true, false), a, "{sc} stays while typing");
assert_eq!(wanted_accelerator(a, sc, false, true), None, "{sc} goes while recording");
assert_eq!(wanted_accelerator(a, sc, true, false, false), a, "{sc} stays while typing");
assert_eq!(wanted_accelerator(a, sc, false, false, true), None, "{sc} goes while recording");
}
assert_eq!(wanted_accelerator(None, "Escape", false, false), None, "no accelerator stays none");
assert_eq!(wanted_accelerator(None, "Escape", false, false, false), None, "no accelerator stays none");
let f2 = accelerator("F2");
assert_eq!(wanted_accelerator(f2, "F2", false, true, false), None, "the sidebar has F2 (Rename)");
assert_eq!(wanted_accelerator(cmd_s, "Cmd+S", false, true, false), cmd_s, "⌘S still saves with the sidebar active");
assert_eq!(wanted_accelerator(f2, "F2", false, false, false), f2, "F2 is the menu's again once the sidebar lets go");
}

/// A submenu of numbers whose calls can be made to fail, to run `swap_item`'s failure paths.
Expand Down Expand Up @@ -903,10 +922,11 @@ mod tests {
assert_eq!(settled(g, None, true), None, "the swap worked");
assert_eq!(settled(g, None, false), g, "it failed: the native item still has its key");
assert_eq!(settled(None, g, false), None, "restoring a key failed");
assert!(!sync_needed(true, false, true, false, false), "nothing changed");
assert!(sync_needed(true, false, false, false, false), "focus changed");
assert!(sync_needed(false, true, false, false, false), "recording changed");
assert!(sync_needed(true, false, true, false, true), "a swap failed last time: try again with the state unchanged");
assert!(!sync_needed(true, false, false, true, false, false, false), "nothing changed");
assert!(sync_needed(true, false, false, false, false, false, false), "focus changed");
assert!(sync_needed(false, true, false, false, false, false, false), "recording changed");
assert!(sync_needed(false, false, true, false, false, false, false), "the sidebar took the keys");
assert!(sync_needed(true, false, false, true, false, false, true), "a swap failed last time: try again with the state unchanged");
}

#[test]
Expand Down Expand Up @@ -960,7 +980,7 @@ mod tests {
for sc in ["E", "C", "Shift+P", "Cmd+Shift+H", "F2"] {
assert!(installed.iter().any(|s| s == sc), "{sc} is in the menu bar");
}
let owned = owned_by_menu(installed.iter().map(String::as_str), false);
let owned = owned_by_menu(installed.iter().map(String::as_str), false, false);
for sc in ["E", "C", "Shift+P", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "P", "U"] {
assert!(installed.iter().any(|s| s == sc), "{sc} is displayed in the menu");
assert!(!owned.contains(sc), "{sc} must be left to egui");
Expand All @@ -969,6 +989,9 @@ mod tests {
assert!(owned.contains(sc), "{sc} is run by the menu bar");
}
// while typing, the menu bar gives up F2 too (the text field has it)
assert!(!owned_by_menu(installed.iter().map(String::as_str), true).contains("F2"));
assert!(!owned_by_menu(installed.iter().map(String::as_str), true, false).contains("F2"));
// …and while the sidebar has the keys, F2 is the sidebar's (a row's rename), but a Cmd shortcut stays the menu's
let in_sidebar = owned_by_menu(installed.iter().map(String::as_str), false, true);
assert!(!in_sidebar.contains("F2") && in_sidebar.contains("Cmd+Shift+H"));
}
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2472,5 +2472,8 @@
"Welcome": "Vítejte",
"Next": "Další",
"Get Started": "Začít",
"Stopped at the scan limit: there may be more photos": "Zastaveno na limitu skenování: je možné, že existují další fotografie"
"Stopped at the scan limit: there may be more photos": "Zastaveno na limitu skenování: je možné, že existují další fotografie",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "Klávesy ovládají postranní panel: šipky přesouvají, Enter otevírá, psaním hledáte, Esc vrací k fotografiím",
"This row has no name to change": "Tento řádek nemá název, který by šlo změnit",
"Keyboard in Sidebar": "Klávesnice v postranním panelu"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -3507,5 +3507,8 @@
"Start the app with --control 7980 (or LIGHTCRAFT_CONTROL_PORT), then lightcraft-cli mcp --connect attaches one to it.": "Starten Sie die App mit --control 7980 (oder LIGHTCRAFT_CONTROL_PORT), dann verbindet lightcraft-cli mcp --connect einen Agenten mit ihr.",
"Welcome": "Willkommen",
"Next": "Weiter",
"Get Started": "Loslegen"
"Get Started": "Loslegen",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "Die Tasten gelten der Seitenleiste: Pfeile bewegen, Eingabe öffnet, Tippen sucht, Esc führt zurück zu den Fotos",
"This row has no name to change": "Diese Zeile hat keinen Namen, der sich ändern ließe",
"Keyboard in Sidebar": "Tastatur in der Seitenleiste"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -2426,5 +2426,8 @@
"Start the app with --control 7980 (or LIGHTCRAFT_CONTROL_PORT), then lightcraft-cli mcp --connect attaches one to it.": "Inicia la aplicación con --control 7980 (o LIGHTCRAFT_CONTROL_PORT), y lightcraft-cli mcp --connect conecta un agente a ella.",
"Welcome": "Bienvenido",
"Next": "Siguiente",
"Get Started": "Empezar"
"Get Started": "Empezar",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "Las teclas están en la barra lateral: las flechas mueven, Intro abre, escribe para buscar, Esc vuelve a las fotos",
"This row has no name to change": "Esta fila no tiene un nombre que cambiar",
"Keyboard in Sidebar": "Teclado en la barra lateral"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/fi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2383,5 +2383,8 @@
"Top bar": "Yläpalkki",
"Tool strip": "Työkalupalkki",
"Bottom bar": "Alapalkki",
"Stopped at the scan limit: there may be more photos": "Skannauksen raja saavutettiin: valokuvia voi olla lisää"
"Stopped at the scan limit: there may be more photos": "Skannauksen raja saavutettiin: valokuvia voi olla lisää",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "Näppäimet toimivat sivupalkissa: nuolet liikuttavat, Enter avaa, kirjoita etsiäksesi, Esc palaa kuviin",
"This row has no name to change": "Tällä rivillä ei ole nimeä, jota voisi muuttaa",
"Keyboard in Sidebar": "Näppäimistö sivupalkissa"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -3609,5 +3609,8 @@
"Start the app with --control 7980 (or LIGHTCRAFT_CONTROL_PORT), then lightcraft-cli mcp --connect attaches one to it.": "Démarrez l'application avec --control 7980 (ou LIGHTCRAFT_CONTROL_PORT), puis lightcraft-cli mcp --connect y connecte un agent.",
"Welcome": "Bienvenue",
"Next": "Suivant",
"Get Started": "Commencer"
"Get Started": "Commencer",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "Les touches sont dans la barre latérale : les flèches déplacent, Entrée ouvre, tapez pour chercher, Échap revient aux photos",
"This row has no name to change": "Cette ligne n’a pas de nom à modifier",
"Keyboard in Sidebar": "Clavier dans la barre latérale"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -2397,5 +2397,8 @@
"Top bar": "Bilah atas",
"Tool strip": "Bilah alat",
"Bottom bar": "Bilah bawah",
"Stopped at the scan limit: there may be more photos": "Berhenti pada batas pemindaian: mungkin masih ada foto"
"Stopped at the scan limit: there may be more photos": "Berhenti pada batas pemindaian: mungkin masih ada foto",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "Tombol berlaku di bilah samping: panah untuk berpindah, Enter untuk membuka, ketik untuk mencari, Esc untuk kembali ke foto",
"This row has no name to change": "Baris ini tidak punya nama yang bisa diubah",
"Keyboard in Sidebar": "Keyboard di bilah samping"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -2590,5 +2590,8 @@
"Welcome": "Benvenuto",
"Next": "Avanti",
"Get Started": "Inizia",
"Stopped at the scan limit: there may be more photos": "Fermati al limite di scansione: potrebbero esserci altre foto"
"Stopped at the scan limit: there may be more photos": "Fermati al limite di scansione: potrebbero esserci altre foto",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "I tasti agiscono sulla barra laterale: le frecce spostano, Invio apre, digita per cercare, Esc torna alle foto",
"This row has no name to change": "Questa riga non ha un nome da cambiare",
"Keyboard in Sidebar": "Tastiera nella barra laterale"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -2424,5 +2424,8 @@
"Start the app with --control 7980 (or LIGHTCRAFT_CONTROL_PORT), then lightcraft-cli mcp --connect attaches one to it.": "アプリを --control 7980 (または LIGHTCRAFT_CONTROL_PORT) で起動してから、lightcraft-cli mcp --connect でエージェントを接続できます。",
"Welcome": "ようこそ",
"Next": "次へ",
"Get Started": "始める"
"Get Started": "始める",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "キーはサイドバーにあります。矢印で移動、Enter で開く、入力で検索、Esc で写真に戻ります",
"This row has no name to change": "この行には変更できる名前がありません",
"Keyboard in Sidebar": "サイドバーでキーボードを使う"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/ka.json
Original file line number Diff line number Diff line change
Expand Up @@ -3692,5 +3692,8 @@
"Tool strip": "ხელსაწყოების ზოლი",
"Bottom bar": "ქვედა ზოლი",
"Prevent G-SYNC/FreeSync flicker (applies at next launch)": "G-SYNC/FreeSync-ის ციმციმის თავიდან აცილება (ამოქმედდება შემდეგი გაშვებისას)",
"Stopped at the scan limit: there may be more photos": "სკანირების ლიმიტზე გაჩერდა: შესაძლოა სხვა ფოტოებიც არის"
"Stopped at the scan limit: there may be more photos": "სკანირების ლიმიტზე გაჩერდა: შესაძლოა სხვა ფოტოებიც არის",
"Keyboard in Sidebar": "კლავიატურა გვერდით ზოლში",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "კლავიშები გვერდით ზოლს მართავს: ისრები გადაადგილებს, Enter ხსნის, აკრიფეთ საძებნად, Esc ფოტოებს უბრუნდება",
"This row has no name to change": "ამ მწკრივს შესაცვლელი სახელი არ აქვს"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -2348,5 +2348,8 @@
"Welcome": "Witamy",
"Next": "Dalej",
"Get Started": "Zaczynamy",
"Stopped at the scan limit: there may be more photos": "Zatrzymano na limicie skanowania: możliwe, że jest więcej zdjęć"
"Stopped at the scan limit: there may be more photos": "Zatrzymano na limicie skanowania: możliwe, że jest więcej zdjęć",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "Klawisze działają na pasku bocznym: strzałki przesuwają, Enter otwiera, pisz, aby szukać, Esc wraca do zdjęć",
"This row has no name to change": "Ten wiersz nie ma nazwy do zmiany",
"Keyboard in Sidebar": "Klawiatura na pasku bocznym"
}
5 changes: 4 additions & 1 deletion crates/ui-egui/locales/pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -2270,5 +2270,8 @@
"Start the app with --control 7980 (or LIGHTCRAFT_CONTROL_PORT), then lightcraft-cli mcp --connect attaches one to it.": "Inicie o aplicativo com --control 7980 (ou LIGHTCRAFT_CONTROL_PORT) e o lightcraft-cli mcp --connect conecta um agente a ele.",
"Welcome": "Bem-vindo",
"Next": "Próximo",
"Get Started": "Começar"
"Get Started": "Começar",
"Keys are in the sidebar: arrows move, Enter opens, type to find, Esc goes back to the photos": "As teclas estão na barra lateral: as setas movem, Enter abre, digite para achar, Esc volta às fotos",
"This row has no name to change": "Esta linha não tem um nome para mudar",
"Keyboard in Sidebar": "Teclado na barra lateral"
}
Loading
Loading