//! Edit ▸ Keyboard Shortcuts: After Effects' visual shortcut editor. //! //! Top: the preset menu (EffectCraft Default = After Effects' defaults, read-only; custom presets //! can be duplicated, renamed, deleted, imported and exported) and the search field. Middle: an //! on-screen keyboard for the toggled (or held) modifiers, keys coloured by what they run in //! that combination: purple for application-wide commands, green for panel-specific ones, both //! when a key has each. Clicking a key lists everything bound to it. Bottom: every bindable //! command with its shortcuts; select one, then record keys (press them in the shortcut field) //! or assign the key selected on the keyboard. Conflicts are shown before assigning. //! //! All changes go through the `shortcuts.*` engine commands, so agents can do the same. //! Automation ids: `shortcuts.preset`, `shortcuts.search`, `shortcuts.mod.`, //! `shortcuts.key.`, `shortcuts.row.`, `shortcuts.record`, `shortcuts.assign`, //! `shortcuts.removeKey.`, `shortcuts.resetCommand`, `shortcuts.duplicate`, //! `shortcuts.rename`, `shortcuts.delete`, `shortcuts.import`, `shortcuts.export`, //! `shortcuts.resetPreset`, `shortcuts.close`. use effectcraft_engine::shortcuts::{APP_SCOPE, Bindable, DEFAULT_PRESET, normalize}; use egui::{Align2, Color32, Rect, RichText, Sense, Stroke, StrokeKind, pos2, vec2}; use serde_json::{Value, json}; use crate::theme::Tokens; use crate::{Dialog, EffectcraftApp}; /// Editor state (in `DialogState`). #[derive(Clone, Debug, Default)] pub struct EditorState { pub query: String, /// Binding key of the selected command. pub selected: Option, /// Keyboard key selected on the on-screen keyboard (shortcut key name). pub key: Option, /// Modifier toggles: Ctrl, Cmd, Alt, Shift. pub mods: [bool; 4], /// Recording a shortcut from the keyboard. pub recording: bool, /// Recorded (or typed) shortcut waiting to be assigned. pub pending: String, /// Rename field for the active preset. pub rename: Option, } const APP_COLOR: Color32 = Color32::from_rgb(0x8e, 0x6c, 0xe8); const PANEL_COLOR: Color32 = Color32::from_rgb(0x3f, 0xa8, 0x6c); pub fn open(app: &mut EffectcraftApp) { app.dialog_state.shortcuts = EditorState::default(); app.dialog = Some(Dialog::Shortcuts); } /// `true` while the editor records keys (the dispatcher must not close the dialog on Escape). pub fn recording(app: &EffectcraftApp) -> bool { app.dialog == Some(Dialog::Shortcuts) && app.dialog_state.shortcuts.recording } /// Shortcut text of a key event (`Cmd+Shift+K`). pub fn combo_text(m: egui::Modifiers, k: egui::Key) -> Option { use egui::Key::*; let key = match k { Semicolon => ";", Quote => "'", Comma => ",", Period => ".", Slash => "/", Backslash => "\\", Equals => "=", Minus => "-", Backtick => "`", OpenBracket => "[", CloseBracket => "]", Plus => "+", Colon => ";", _ => k.name(), }; let mut s = String::new(); if cfg!(target_os = "macos") { if m.ctrl { s.push_str("Ctrl+"); } if m.mac_cmd || m.command { s.push_str("Cmd+"); } } else if m.ctrl || m.command { s.push_str("Cmd+"); } if m.alt { s.push_str("Alt+"); } if m.shift { s.push_str("Shift+"); } if key == "+" { // `Cmd++` names the plus key. return normalize(&format!("{s}+")); } s.push_str(key); normalize(&s) } /// Split normalized shortcut text into (ctrl, cmd, alt, shift) and the key. fn split(sc: &str) -> ([bool; 4], String) { let (head, key) = match sc.strip_suffix("++") { Some(h) => (h.to_string(), "+".to_string()), None if sc == "+" => (String::new(), "+".into()), None => match sc.rsplit_once('+') { Some((h, k)) => (h.to_string(), k.to_string()), None => (String::new(), sc.to_string()), }, }; let has = |m: &str| head.split('+').any(|p| p == m); ([has("Ctrl"), has("Cmd"), has("Alt"), has("Shift")], key) } fn mods_text(m: [bool; 4]) -> String { let mut s = String::new(); for (on, n) in m.iter().zip(["Ctrl+", "Cmd+", "Alt+", "Shift+"]) { if *on { s.push_str(n); } } s } /// The on-screen keyboard: rows of (label, key name, width in key units). fn keyboard_rows() -> Vec> { let mut rows: Vec> = vec![]; rows.push(vec![ ("Esc", "Escape", 1.0), ("", "", 0.5), ("F1", "F1", 1.0), ("F2", "F2", 1.0), ("F3", "F3", 1.0), ("F4", "F4", 1.0), ("", "", 0.3), ("F5", "F5", 1.0), ("F6", "F6", 1.0), ("F7", "F7", 1.0), ("F8", "F8", 1.0), ("", "", 0.3), ("F9", "F9", 1.0), ("F10", "F10", 1.0), ("F11", "F11", 1.0), ("F12", "F12", 1.0), ]); let mut r: Vec<(&str, &str, f32)> = vec![("`", "`", 1.0)]; for d in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] { r.push((d, d, 1.0)); } r.extend([("-", "-", 1.0), ("=", "=", 1.0), ("Delete", "Backspace", 1.6)]); rows.push(r); let mut r: Vec<(&str, &str, f32)> = vec![("Tab", "Tab", 1.5)]; for c in ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"] { r.push((c, c, 1.0)); } r.extend([("[", "[", 1.0), ("]", "]", 1.0), ("\\", "\\", 1.1)]); rows.push(r); let mut r: Vec<(&str, &str, f32)> = vec![("Caps", "", 1.8)]; for c in ["A", "S", "D", "F", "G", "H", "J", "K", "L"] { r.push((c, c, 1.0)); } r.extend([(";", ";", 1.0), ("'", "'", 1.0), ("Return", "Enter", 1.8)]); rows.push(r); let mut r: Vec<(&str, &str, f32)> = vec![("Shift", "#shift", 2.3)]; for c in ["Z", "X", "C", "V", "B", "N", "M"] { r.push((c, c, 1.0)); } r.extend([(",", ",", 1.0), (".", ".", 1.0), ("/", "/", 1.0), ("Shift", "#shift", 2.3)]); rows.push(r); let (cmd, alt) = if cfg!(target_os = "macos") { ("⌘ Cmd", "⌥ Opt") } else { ("Ctrl", "Alt") }; rows.push(vec![ ("Ctrl", if cfg!(target_os = "macos") { "#ctrl" } else { "#cmd" }, 1.4), (alt, "#alt", 1.4), (cmd, "#cmd", 1.6), ("Space", "Space", 6.0), (cmd, "#cmd", 1.6), (alt, "#alt", 1.4), ]); rows } /// Navigation and numeric keypad block: (label, key name, column, row). const SIDE_KEYS: &[(&str, &str, f32, f32)] = &[ ("Home", "Home", 0.0, 1.0), ("PgUp", "PageUp", 1.0, 1.0), ("Del", "Delete", 0.0, 2.0), ("End", "End", 1.0, 2.0), ("PgDn", "PageDown", 2.0, 2.0), ("↑", "ArrowUp", 1.0, 4.0), ("←", "ArrowLeft", 0.0, 5.0), ("↓", "ArrowDown", 1.0, 5.0), ("→", "ArrowRight", 2.0, 5.0), ]; pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { let mut st = std::mem::take(&mut app.dialog_state.shortcuts); let mut run: Vec<(String, Value)> = vec![]; let mut close = false; // Record keys while the shortcut field listens. if st.recording { let got = ctx.input(|i| { i.events.iter().find_map(|e| match e { egui::Event::Key { key, pressed: true, modifiers, .. } => Some((*key, *modifiers)), _ => None, }) }); if let Some((k, m)) = got { st.recording = false; if k != egui::Key::Escape && let Some(txt) = combo_text(m, k) { st.pending = txt; } } } // Modifiers: toggles, or the ones held on the keyboard. let held = ctx.input(|i| i.modifiers); let held = [held.ctrl && cfg!(target_os = "macos"), held.command, held.alt, held.shift]; let mods = if held.iter().any(|m| *m) && !st.recording { held } else { st.mods }; let table = app.session.shortcuts().clone(); let presets = app.session.keymaps.names(); let active = app.session.keymaps.active.clone(); // key name → (app?, panel?) for the current modifiers. let mut keymap: std::collections::HashMap = Default::default(); for (sc, b) in table.bindings() { let (m, k) = split(sc); if m == mods { let e = keymap.entry(k).or_default(); if b.scope == APP_SCOPE { e.0 = true; } else { e.1 = true; } } } super::dialogs::modal(ctx, "Keyboard Shortcuts", vec2(1000.0, 720.0), t, |ui| { // ---- preset bar ui.horizontal(|ui| { ui.label("Preset:"); let r = egui::ComboBox::from_id_salt("sc-preset").selected_text(&active).width(220.0).show_ui(ui, |ui| { for n in &presets { if ui.selectable_label(*n == active, n).clicked() { run.push(("shortcuts.preset".into(), json!({"op": "select", "name": n}))); } } }); app.auto.add("shortcuts.preset", r.response.rect, "Preset"); let custom = active != DEFAULT_PRESET; let b = ui.button("Duplicate"); app.auto.add("shortcuts.duplicate", b.rect, "Duplicate"); if b.clicked() { run.push(("shortcuts.preset".into(), json!({"op": "duplicate", "from": active, "name": format!("{active} copy")}))); } if let Some(name) = &mut st.rename { let r = ui.add(egui::TextEdit::singleline(name).desired_width(140.0)); app.auto.add("shortcuts.renameField", r.rect, "New name"); if ui.button("Rename").clicked() || (r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter))) { run.push(("shortcuts.preset".into(), json!({"op": "rename", "name": active, "newName": name}))); st.rename = None; } } else { let b = ui.add_enabled(custom, egui::Button::new("Rename")); app.auto.add("shortcuts.rename", b.rect, "Rename"); if b.clicked() { st.rename = Some(active.clone()); } } let b = ui.add_enabled(custom, egui::Button::new("Delete")); app.auto.add("shortcuts.delete", b.rect, "Delete"); if b.clicked() { run.push(("shortcuts.preset".into(), json!({"op": "delete", "name": active}))); } let b = ui.add_enabled(custom, egui::Button::new("Reset Preset")); app.auto.add("shortcuts.resetPreset", b.rect, "Reset Preset"); if b.clicked() { run.push(("shortcuts.reset".into(), json!({}))); } let b = ui.button("Import..."); app.auto.add("shortcuts.import", b.rect, "Import"); if b.clicked() && let Some(p) = app.hooks.pick_files.as_ref().and_then(|f| f(&["json"]).into_iter().next()) { run.push(("shortcuts.import".into(), json!({"path": p}))); } let b = ui.button("Export..."); app.auto.add("shortcuts.export", b.rect, "Export"); if b.clicked() && let Some(p) = app.hooks.pick_save_file.as_ref().and_then(|f| f(&format!("{active}.json"), "json")) { run.push(("shortcuts.export".into(), json!({"preset": active, "path": p}))); } }); ui.add_space(6.0); // ---- modifiers + legend ui.horizontal(|ui| { ui.label("Modifiers:"); let names = if cfg!(target_os = "macos") { ["⌃ Ctrl", "⌘ Cmd", "⌥ Opt", "⇧ Shift"] } else { ["", "Ctrl", "Alt", "Shift"] }; for (i, (n, id)) in names.iter().zip(["ctrl", "cmd", "alt", "shift"]).enumerate() { if n.is_empty() { continue; } let r = ui.add(egui::Button::selectable(mods[i], *n)); app.auto.add(&format!("shortcuts.mod.{id}"), r.rect, n); if r.clicked() { st.mods[i] = !st.mods[i]; } } ui.add_space(20.0); for (c, l) in [(APP_COLOR, "Application-wide"), (PANEL_COLOR, "Panel-specific")] { let (r, _) = ui.allocate_exact_size(vec2(14.0, 14.0), Sense::hover()); ui.painter().rect_filled(r, 2.0, c); ui.label(RichText::new(l).color(t.text_dim)); } }); ui.add_space(6.0); // ---- keyboard let unit = 40.0; let gap = 3.0; let (area, _) = ui.allocate_exact_size(vec2(ui.available_width(), unit * 6.0 + gap * 6.0 + 4.0), Sense::hover()); let p = ui.painter_at(area); let draw_key = |app: &mut EffectcraftApp, r: Rect, label: &str, name: &str, st: &mut EditorState| { let mod_idx = match name { "#ctrl" => Some(0), "#cmd" => Some(1), "#alt" => Some(2), "#shift" => Some(3), _ => None, }; let (a, pn) = keymap.get(name).copied().unwrap_or((false, false)); let fill = match mod_idx { Some(i) if mods[i] => t.accent, _ => t.field_bg, }; p.rect_filled(r, 4.0, fill); if a && pn { let half = Rect::from_min_max(r.min, pos2(r.center().x, r.max.y)); p.rect_filled(half, 4.0, APP_COLOR); p.rect_filled(Rect::from_min_max(pos2(r.center().x, r.min.y), r.max), 4.0, PANEL_COLOR); } else if a { p.rect_filled(r, 4.0, APP_COLOR); } else if pn { p.rect_filled(r, 4.0, PANEL_COLOR); } let sel = st.key.as_deref() == Some(name) && !name.is_empty(); p.rect_stroke(r, 4.0, Stroke::new(if sel { 2.0 } else { 1.0 }, if sel { Color32::WHITE } else { t.field_border }), StrokeKind::Inside); p.text( r.left_top() + vec2(5.0, 4.0), Align2::LEFT_TOP, label, Tokens::ui(11.0), if a || pn || mod_idx.is_some_and(|i| mods[i]) { Color32::WHITE } else { t.text }, ); if name.is_empty() { return; } let id = format!("shortcuts.key.{}", name.trim_start_matches('#')); app.auto.add(&id, r, label); let resp = ui.interact(r, egui::Id::new(("sc-key", name, r.min.x as i32)), Sense::click()); if resp.clicked() { if let Some(i) = mod_idx { st.mods[i] = !st.mods[i]; } else { st.key = Some(name.to_string()); st.pending = format!("{}{}", mods_text(mods), name); } } if !name.starts_with('#') { let who: Vec = table.bindings().into_iter().filter(|(sc, _)| split(sc) == (mods, name.to_string())).map(|(_, b)| b.label.clone()).collect(); if !who.is_empty() { resp.on_hover_text(who.join("\n")); } } }; let mut y = area.min.y + 2.0; for row in keyboard_rows() { let mut x = area.min.x + 2.0; for (label, name, w) in row { let r = Rect::from_min_size(pos2(x, y), vec2(unit * w - gap, unit - gap)); if !(label.is_empty() && name.is_empty()) { draw_key(app, r, label, name, &mut st); } x += unit * w; } y += unit + gap; } let side_x = area.min.x + unit * 16.0; for (label, name, c, r) in SIDE_KEYS { let rect = Rect::from_min_size(pos2(side_x + c * unit, area.min.y + 2.0 + r * (unit + gap)), vec2(unit - gap, unit - gap)); draw_key(app, rect, label, name, &mut st); } // Key details: everything bound to the selected key. let info_x = side_x + unit * 3.5; let info = Rect::from_min_max(pos2(info_x, area.min.y + 2.0), pos2(area.max.x - 2.0, area.max.y - 2.0)); p.rect_filled(info, 4.0, t.row); let p = ui.painter_at(info.shrink(2.0)); if let Some(k) = &st.key { p.text(info.left_top() + vec2(8.0, 6.0), Align2::LEFT_TOP, format!("Key: {k}"), Tokens::semibold(12.0), t.tab_text_active); let mut yy = info.min.y + 26.0; let mut list: Vec<(String, String, String)> = table.bindings().into_iter().filter(|(sc, _)| split(sc).1 == *k).map(|(sc, b)| (sc.to_string(), b.label.clone(), b.scope.clone())).collect(); list.sort(); if list.is_empty() { p.text(pos2(info.min.x + 8.0, yy), Align2::LEFT_TOP, "Not assigned", Tokens::ui(11.5), t.text_dim); } for (sc, label, scope) in list.into_iter().take(9) { let col = if scope == APP_SCOPE { APP_COLOR } else { PANEL_COLOR }; p.text(pos2(info.min.x + 8.0, yy), Align2::LEFT_TOP, crate::menus::shortcut_text(&sc), Tokens::ui(11.5), col); p.text(pos2(info.min.x + 78.0, yy), Align2::LEFT_TOP, label, Tokens::ui(11.5), t.text); yy += 17.0; } } else { p.text(info.left_top() + vec2(8.0, 6.0), Align2::LEFT_TOP, "Click a key to see its commands.", Tokens::ui(11.5), t.text_dim); } ui.add_space(4.0); // ---- search + command list + editor ui.horizontal(|ui| { let r = ui.add(egui::TextEdit::singleline(&mut st.query).hint_text("Search commands or shortcuts").desired_width(360.0)); app.auto.add("shortcuts.search", r.rect, "Search"); }); ui.add_space(4.0); let q = st.query.to_lowercase(); let rows: Vec<&Bindable> = table .bindables .iter() .filter(|b| { if q.is_empty() { return true; } let keys = table.keys.get(&b.key).map(|k| k.join(" ")).unwrap_or_default(); let hay = format!("{} {} {} {}", b.label, b.command, b.path.join(" "), keys).to_lowercase(); q.split_whitespace().all(|w| hay.contains(w)) }) .collect(); ui.allocate_ui_with_layout(vec2(ui.available_width(), 270.0), egui::Layout::left_to_right(egui::Align::Min), |ui| { ui.set_height(270.0); // Command list. ui.vertical(|ui| { ui.set_width(620.0); let row_h = 20.0; egui::ScrollArea::vertical().id_salt("sc-rows").max_height(230.0).auto_shrink([false, false]).show_rows(ui, row_h, rows.len(), |ui, range| { for i in range { let b = rows[i]; let keys = table.keys.get(&b.key).cloned().unwrap_or_default(); let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), row_h), Sense::click()); let sel = st.selected.as_deref() == Some(b.key.as_str()); if sel { ui.painter().rect_filled(r, 0.0, t.row_selected); } else if i % 2 == 1 { ui.painter().rect_filled(r, 0.0, t.row_alt); } let pp = ui.painter(); pp.text(pos2(r.min.x + 6.0, r.center().y), Align2::LEFT_CENTER, &b.label, Tokens::ui(11.5), t.text); pp.text(pos2(r.min.x + 230.0, r.center().y), Align2::LEFT_CENTER, b.path.join(" > "), Tokens::ui(10.5), t.text_faint); let ks: Vec = keys.iter().map(|k| crate::menus::shortcut_text(k)).collect(); let col = if b.scope == APP_SCOPE { APP_COLOR } else { PANEL_COLOR }; pp.text( pos2(r.max.x - 8.0, r.center().y), Align2::RIGHT_CENTER, ks.join(", "), Tokens::ui(11.5), if keys.is_empty() { t.text_faint } else { col }, ); app.auto.add(&format!("shortcuts.row.{i}"), r, &b.label); if resp.clicked() { st.selected = Some(b.key.clone()); st.pending.clear(); } } }); ui.label(RichText::new(format!("{} of {} commands", rows.len(), table.bindables.len())).color(t.text_faint)); }); ui.separator(); // Selected command editor. ui.vertical(|ui| { ui.set_width(310.0); let Some(b) = st.selected.as_ref().and_then(|k| table.find(k)).cloned() else { ui.label(RichText::new("Select a command to change its shortcut.").color(t.text_dim)); return; }; ui.label(RichText::new(&b.label).font(Tokens::semibold(13.0)).color(t.tab_text_active)); ui.label(RichText::new(format!("{} • {}", b.command, b.scope)).color(t.text_faint)); ui.add_space(6.0); let keys = table.keys.get(&b.key).cloned().unwrap_or_default(); if keys.is_empty() { ui.label(RichText::new("No shortcut").color(t.text_dim)); } for (n, k) in keys.iter().enumerate() { ui.horizontal(|ui| { ui.label(RichText::new(crate::menus::shortcut_text(k)).monospace()); let r = ui.small_button("Remove"); app.auto.add(&format!("shortcuts.removeKey.{n}"), r.rect, "Remove"); if r.clicked() { let rest: Vec<&String> = keys.iter().filter(|x| *x != k).collect(); run.push(("shortcuts.set".into(), json!({"command": b.command, "params": b.params, "keys": rest}))); } }); } ui.add_space(6.0); ui.horizontal(|ui| { let hint = if st.recording { "Press keys…" } else { "Click, then press keys" }; let r = ui.add(egui::TextEdit::singleline(&mut st.pending).hint_text(hint).desired_width(170.0)); app.auto.add("shortcuts.record", r.rect, "Shortcut"); if r.gained_focus() || (r.clicked() && !st.recording) { st.recording = true; } if r.lost_focus() { st.recording = false; } let norm = normalize(&st.pending); let b2 = ui.add_enabled(norm.is_some(), egui::Button::new("Assign")); app.auto.add("shortcuts.assign", b2.rect, "Assign"); if b2.clicked() && let Some(k) = norm { let mut all = keys.clone(); if !all.contains(&k) { all.push(k); } run.push(("shortcuts.set".into(), json!({"command": b.command, "params": b.params, "keys": all}))); st.pending.clear(); } }); if let Some(k) = normalize(&st.pending) { let conflicts = table.conflicts(&b, &k); if !conflicts.is_empty() { ui.add_space(4.0); ui.label(RichText::new(format!("⚠ {} is used by:", crate::menus::shortcut_text(&k))).color(t.warning)); for c in conflicts.iter().take(4) { ui.label(RichText::new(format!(" {} ({})", c.label, c.scope)).color(t.warning)); } } } ui.add_space(6.0); let r = ui.add_enabled(keys != b.defaults, egui::Button::new("Reset to Default")); app.auto.add("shortcuts.resetCommand", r.rect, "Reset to Default"); if r.clicked() { run.push(("shortcuts.reset".into(), json!({"command": b.command, "params": b.params}))); } if !b.defaults.is_empty() { let d: Vec = b.defaults.iter().map(|k| crate::menus::shortcut_text(k)).collect(); ui.label(RichText::new(format!("Default: {}", d.join(", "))).color(t.text_faint)); } if active == DEFAULT_PRESET { ui.add_space(6.0); ui.label(RichText::new("Changing a shortcut creates a custom preset; the default stays as it is.").color(t.text_faint).small()); } }); }); ui.add_space(6.0); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { let r = ui.add(egui::Button::new(RichText::new(" OK ").color(Color32::WHITE)).fill(t.accent)); app.auto.add("shortcuts.close", r.rect, "OK"); close |= r.clicked(); }); }); app.dialog_state.shortcuts = st; for (cmd, params) in run { if let Err(e) = app.session.execute(&cmd, params) { app.ui.status = e.to_string(); } } if close { app.dialog = None; } } #[cfg(test)] mod tests { use super::*; #[test] fn combo_text_round_trips_through_the_parser() { let cases = [ (egui::Modifiers::COMMAND | egui::Modifiers::SHIFT, egui::Key::K), (egui::Modifiers::ALT, egui::Key::OpenBracket), (egui::Modifiers::NONE, egui::Key::F9), ]; for (m, k) in cases { let txt = combo_text(m, k).unwrap(); let (pm, pk) = crate::menus::parse_shortcut(&txt).unwrap(); assert_eq!(pk, k, "{txt}"); assert_eq!((pm.command, pm.alt, pm.shift), (m.command, m.alt, m.shift), "{txt}"); } assert_eq!(split("Cmd+Shift+K"), ([false, true, false, true], "K".into())); assert_eq!(split("Cmd++"), ([false, true, false, false], "+".into())); assert_eq!(split("F9"), ([false; 4], "F9".into())); } }