M14.3: keyboard shortcut editor
Keyboard shortcut presets in the engine (`engine::shortcuts`): every menu entry (including
ones with bound parameters), registry command and frontend command is bindable, scoped
application-wide or to a panel. "EffectCraft Default" is After Effects' layout and
read-only; editing it creates a custom preset that stores only its differences. Presets
are saved in the config store; the shortcut dispatcher and the menus read the active one.
- Commands: shortcuts.list, shortcuts.set {command, params?, keys}, shortcuts.reset,
shortcuts.preset {select|new|duplicate|delete|rename}, shortcuts.export/import (JSON),
shortcuts.conflicts.
- Edit > Keyboard Shortcuts: preset bar, modifier toggles (or held keys), an on-screen
keyboard coloured by scope (application-wide / panel-specific / both), key details,
a searchable list of every command, record-or-type assignment with conflict warnings,
reset to default; all widgets have automation ids.
- Tests: normalization, defaults conflict-free, conflict detection, custom preset creation
and persistence, preset lifecycle + export/import, dispatch uses the custom preset.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
47f7712364
commit
35f429363e
@@ -71,6 +71,8 @@ pub struct Hooks {
|
||||
pub audio_devices: Option<Box<dyn Fn() -> Vec<String>>>,
|
||||
/// Picks a folder (Settings paths).
|
||||
pub pick_folder: Option<Box<dyn Fn() -> Option<String>>>,
|
||||
/// Save dialog for other file kinds: (default name, extension).
|
||||
pub pick_save_file: Option<Box<dyn Fn(&str, &str) -> Option<String>>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -166,6 +168,17 @@ impl EffectcraftApp {
|
||||
last_reveal: None,
|
||||
applied_prefs: None,
|
||||
}
|
||||
.with_ui_commands()
|
||||
}
|
||||
|
||||
/// Offer the frontend's commands (tools, timeline navigation…) for keyboard shortcuts.
|
||||
fn with_ui_commands(mut self) -> Self {
|
||||
let cmds = menus::UI_COMMANDS
|
||||
.iter()
|
||||
.map(|c| effectcraft_engine::shortcuts::UiCommand { id: c.id.into(), label: c.label.into(), shortcut: c.shortcut.map(str::to_string) })
|
||||
.collect();
|
||||
self.session.set_ui_commands(cmds);
|
||||
self
|
||||
}
|
||||
|
||||
/// Apply changed settings: theme and brightness, label colours, tool tips, preview caches.
|
||||
|
||||
+42
-30
@@ -346,7 +346,7 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
|
||||
Value::Null
|
||||
}
|
||||
"app.keyboardShortcuts" => {
|
||||
app.dialog = Some(crate::Dialog::Shortcuts);
|
||||
crate::panels::shortcut_editor::open(app);
|
||||
Value::Null
|
||||
}
|
||||
"app.templates" => {
|
||||
@@ -584,6 +584,11 @@ fn entry_label(app: &EffectcraftApp, e: &MenuEntry) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// The entry's shortcut in the active keyboard shortcut preset.
|
||||
fn entry_shortcut(app: &EffectcraftApp, e: &MenuEntry) -> Option<String> {
|
||||
app.session.shortcuts().shortcut_of(&e.command, &e.params).map(str::to_string)
|
||||
}
|
||||
|
||||
/// Check-mark state of frontend toggles (engine state is answered by the engine).
|
||||
fn entry_checked(app: &EffectcraftApp, e: &MenuEntry) -> Option<bool> {
|
||||
let v = &app.ui.viewer;
|
||||
@@ -632,7 +637,7 @@ pub fn menu_items(app: &EffectcraftApp) -> Vec<MenuItem> {
|
||||
id: e.command.clone(),
|
||||
label: entry_label(app, e),
|
||||
path,
|
||||
shortcut: e.shortcut.clone(),
|
||||
shortcut: entry_shortcut(app, e),
|
||||
enabled: entry_enabled(app, e),
|
||||
checked: entry_checked(app, e),
|
||||
params: e.params.clone(),
|
||||
@@ -727,36 +732,17 @@ fn shifted_alias(k: egui::Key) -> Option<egui::Key> {
|
||||
/// A key binding: modifiers, key, command id and bound params.
|
||||
pub type Binding = (egui::Modifiers, egui::Key, String, Value);
|
||||
|
||||
/// All active bindings, most modifiers first.
|
||||
pub fn bindings() -> Vec<Binding> {
|
||||
/// All bindings of the active keyboard shortcut preset, most modifiers first.
|
||||
pub fn bindings(session: &effectcraft_engine::Session) -> Vec<Binding> {
|
||||
let mut v: Vec<Binding> = Vec::new();
|
||||
let mut push = |sc: &str, id: &str, params: Value| {
|
||||
for (sc, b) in session.shortcuts().bindings() {
|
||||
if let Some((m, k)) = parse_shortcut(sc) {
|
||||
if m.shift
|
||||
&& let Some(alias) = shifted_alias(k)
|
||||
{
|
||||
v.push((m, alias, id.to_string(), params.clone()));
|
||||
v.push((m, alias, b.command.clone(), b.params.clone()));
|
||||
}
|
||||
v.push((m, k, id.to_string(), params));
|
||||
}
|
||||
};
|
||||
// Menu entries (their shortcuts may bind params), then command defaults not in the menus.
|
||||
let entries = effectcraft_engine::menus::entries();
|
||||
for (_, e) in &entries {
|
||||
if let Some(sc) = &e.shortcut {
|
||||
push(sc, &e.command, e.params.clone());
|
||||
}
|
||||
}
|
||||
for c in effectcraft_engine::command_specs() {
|
||||
if let Some(sc) = c.shortcut
|
||||
&& !entries.iter().any(|(_, e)| e.command == c.id)
|
||||
{
|
||||
push(sc, c.id, Value::Null);
|
||||
}
|
||||
}
|
||||
for c in UI_COMMANDS {
|
||||
if let Some(sc) = c.shortcut {
|
||||
push(sc, c.id, Value::Null);
|
||||
v.push((m, k, b.command.clone(), b.params.clone()));
|
||||
}
|
||||
}
|
||||
v.sort_by_key(|(m, ..)| std::cmp::Reverse(m.command as u8 + m.shift as u8 + m.alt as u8 + m.ctrl as u8));
|
||||
@@ -788,10 +774,14 @@ pub fn handle_shortcuts(app: &mut EffectcraftApp, ctx: &egui::Context) {
|
||||
if events.is_empty() {
|
||||
return;
|
||||
}
|
||||
let binds = bindings();
|
||||
let binds = bindings(&app.session);
|
||||
for (key, mods) in events {
|
||||
// Escape closes dialogs.
|
||||
if key == egui::Key::Escape && app.dialog.is_some() {
|
||||
// Escape while recording a shortcut cancels the recording, not the editor.
|
||||
if crate::panels::shortcut_editor::recording(app) {
|
||||
continue;
|
||||
}
|
||||
// Escape in Settings is Cancel: restore the settings from when it opened.
|
||||
if app.dialog == Some(crate::Dialog::Settings)
|
||||
&& let Some(p) = app.dialog_state.prefs_snapshot.take()
|
||||
@@ -878,8 +868,8 @@ fn gutter(checked: bool) -> egui::Atom<'static> {
|
||||
fn menu_entry(app: &EffectcraftApp, ui: &mut egui::Ui, e: &MenuEntry) -> bool {
|
||||
let label = entry_label(app, e);
|
||||
let mut b = egui::Button::new((gutter(entry_checked(app, e) == Some(true)), label));
|
||||
if let Some(s) = &e.shortcut {
|
||||
b = b.shortcut_text(shortcut_text(s));
|
||||
if let Some(s) = entry_shortcut(app, e) {
|
||||
b = b.shortcut_text(shortcut_text(&s));
|
||||
}
|
||||
ui.add_enabled(entry_enabled(app, e), b).clicked()
|
||||
}
|
||||
@@ -904,8 +894,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn ui_and_menu_shortcuts_do_not_collide() {
|
||||
let app = EffectcraftApp::new(effectcraft_engine::Session::default());
|
||||
let mut seen: std::collections::BTreeMap<(String, String), (String, String)> = Default::default();
|
||||
for (m, k, id, params) in bindings() {
|
||||
for (m, k, id, params) in bindings(&app.session) {
|
||||
let key = (format!("{m:?}"), format!("{k:?}"));
|
||||
let target = (id.clone(), params.to_string());
|
||||
if let Some(prev) = seen.get(&key) {
|
||||
@@ -916,6 +907,27 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dispatch_uses_the_active_custom_preset() {
|
||||
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
|
||||
let find = |app: &EffectcraftApp, id: &str| bindings(&app.session).into_iter().filter(|b| b.2 == id).map(|b| (b.0, b.1)).collect::<Vec<_>>();
|
||||
let (m, k) = parse_shortcut("Cmd+Alt+Shift+Y").unwrap();
|
||||
assert!(find(&app, "layer.newNull").contains(&(m, k)));
|
||||
// A UI command rebound in a custom preset.
|
||||
app.session.execute("shortcuts.set", json!({"command": "tool.hand", "keys": "Ctrl+Alt+H"})).unwrap();
|
||||
assert_eq!(app.session.keymaps.active, "Custom");
|
||||
let (m2, k2) = parse_shortcut("Ctrl+Alt+H").unwrap();
|
||||
assert_eq!(find(&app, "tool.hand"), vec![(m2, k2)]);
|
||||
app.session.execute("shortcuts.set", json!({"command": "layer.newNull", "keys": "F6"})).unwrap();
|
||||
assert_eq!(find(&app, "layer.newNull"), vec![(egui::Modifiers::NONE, egui::Key::F6)]);
|
||||
// The menus show the preset's shortcut.
|
||||
let item = menu_items(&app).into_iter().find(|i| i.id == "layer.newNull").unwrap();
|
||||
assert_eq!(item.shortcut.as_deref(), Some("F6"));
|
||||
// Back to the default preset.
|
||||
app.session.execute("shortcuts.preset", json!({"op": "select", "name": effectcraft_engine::shortcuts::DEFAULT_PRESET})).unwrap();
|
||||
assert!(find(&app, "layer.newNull").contains(&(m, k)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renamed_label_shows_in_the_label_menu() {
|
||||
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
|
||||
|
||||
@@ -24,6 +24,8 @@ pub struct DialogState {
|
||||
pub settings_page: String,
|
||||
/// Settings when the dialog opened (Cancel restores them).
|
||||
pub prefs_snapshot: Option<effectcraft_engine::prefs::Prefs>,
|
||||
/// Keyboard Shortcuts editor.
|
||||
pub shortcuts: super::shortcut_editor::EditorState,
|
||||
/// The open parameter form.
|
||||
pub form: super::forms::Form,
|
||||
/// Message box (title, body).
|
||||
@@ -101,7 +103,7 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context) {
|
||||
Dialog::SolidSettings => solid(app, ctx, &t),
|
||||
Dialog::CommandPalette => palette(app, ctx, &t),
|
||||
Dialog::Settings => super::settings::show(app, ctx, &t),
|
||||
Dialog::Shortcuts => super::forms::show_shortcuts(app, ctx, &t),
|
||||
Dialog::Shortcuts => super::shortcut_editor::show(app, ctx, &t),
|
||||
Dialog::Form => super::forms::show_form(app, ctx, &t),
|
||||
Dialog::Info => super::forms::show_info(app, ctx, &t),
|
||||
Dialog::ViewOptions => super::forms::show_view_options(app, ctx, &t),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Menu dialogs: a generic parameter form (numeric Transform dialogs, Mask Feather/Opacity/
|
||||
//! Expansion, Auto-Orient, Go to Time, Add Guide, Sequence Layers, Interpret Footage, Project
|
||||
//! Settings, placeholders…), Keyboard Shortcuts, View Options and simple message boxes
|
||||
//! (Settings: `settings`). Every form runs an engine command with the collected parameters, so whatever a
|
||||
//! Settings, placeholders…), View Options and simple message boxes (Settings: `settings`;
|
||||
//! Keyboard Shortcuts: `shortcut_editor`). Every form runs an engine command with the collected parameters, so whatever a
|
||||
//! dialog does an agent can do with one `engine.execute`.
|
||||
|
||||
use egui::{Color32, vec2};
|
||||
@@ -359,55 +359,6 @@ pub fn show_info(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_shortcuts(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
|
||||
let mut q = app.dialog_state.palette_query.clone();
|
||||
let mut close = false;
|
||||
// (shortcut, where, label)
|
||||
let mut rows: Vec<(String, String, String)> = vec![];
|
||||
for (path, e) in effectcraft_engine::menus::entries() {
|
||||
if let Some(sc) = &e.shortcut {
|
||||
rows.push((crate::menus::shortcut_text(sc), path.join(" ▸ "), e.label.clone()));
|
||||
}
|
||||
}
|
||||
for c in effectcraft_engine::command_specs().iter().filter(|c| c.menu.is_empty()) {
|
||||
if let Some(sc) = c.shortcut
|
||||
&& !rows.iter().any(|r| r.2 == c.label)
|
||||
{
|
||||
rows.push((crate::menus::shortcut_text(sc), "Commands".into(), c.label.to_string()));
|
||||
}
|
||||
}
|
||||
for c in crate::menus::UI_COMMANDS {
|
||||
if let Some(sc) = c.shortcut {
|
||||
rows.push((crate::menus::shortcut_text(sc), "Panels & Tools".into(), c.label.to_string()));
|
||||
}
|
||||
}
|
||||
super::dialogs::modal(ctx, "Keyboard Shortcuts", vec2(640.0, 520.0), t, |ui| {
|
||||
ui.add(egui::TextEdit::singleline(&mut q).hint_text("Search commands or shortcuts").desired_width(f32::INFINITY));
|
||||
ui.add_space(8.0);
|
||||
let ql = q.to_lowercase();
|
||||
egui::ScrollArea::vertical().max_height(400.0).show(ui, |ui| {
|
||||
egui::Grid::new("shortcut-grid").num_columns(3).striped(true).spacing([18.0, 4.0]).show(ui, |ui| {
|
||||
for (sc, place, label) in rows.iter().filter(|r| ql.is_empty() || format!("{} {} {}", r.0, r.1, r.2).to_lowercase().contains(&ql)) {
|
||||
ui.label(egui::RichText::new(sc).monospace());
|
||||
ui.label(label);
|
||||
ui.label(egui::RichText::new(place).color(t.text_dim));
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
});
|
||||
ui.add_space(8.0);
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if ui.add(egui::Button::new(egui::RichText::new(" OK ").color(Color32::WHITE)).fill(t.accent)).clicked() {
|
||||
close = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
app.dialog_state.palette_query = q;
|
||||
if close {
|
||||
app.dialog = None;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_view_options(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
|
||||
let mut close = false;
|
||||
super::dialogs::modal(ctx, "View Options", vec2(360.0, 330.0), t, |ui| {
|
||||
|
||||
@@ -14,6 +14,7 @@ pub mod project;
|
||||
pub mod properties;
|
||||
pub mod render_queue;
|
||||
pub mod settings;
|
||||
pub mod shortcut_editor;
|
||||
pub mod text_panels;
|
||||
pub mod timeline;
|
||||
pub mod viewer;
|
||||
|
||||
@@ -0,0 +1,590 @@
|
||||
//! 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.<ctrl|cmd|alt|shift>`,
|
||||
//! `shortcuts.key.<Key>`, `shortcuts.row.<n>`, `shortcuts.record`, `shortcuts.assign`,
|
||||
//! `shortcuts.removeKey.<n>`, `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<String>,
|
||||
/// Keyboard key selected on the on-screen keyboard (shortcut key name).
|
||||
pub key: Option<String>,
|
||||
/// 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<String>,
|
||||
}
|
||||
|
||||
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<String> {
|
||||
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<Vec<(&'static str, &'static str, f32)>> {
|
||||
let mut rows: Vec<Vec<(&str, &str, f32)>> = 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<String, (bool, bool)> = 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<String> =
|
||||
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<String> = 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<String> = 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()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user