UI: keyboard shortcuts for dialog answers
Unsaved Changes: Enter is Save, Cmd+Backspace is Don't Save. Revert Project: Cmd+R is Revert (the button is now red). Delete Items: Cmd+D is Delete. Crash recovery: Enter is Open Auto-Save. Marker: Enter is OK, Cmd+Backspace is Delete (from a focused text field too). The macOS menu bar hands Cmd+D and Cmd+R back to the open prompt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
67434e9038
commit
50eddc1207
@@ -345,6 +345,17 @@ fn text_field_event(command: &str, clipboard: Option<String>) -> Option<Vec<egui
|
||||
pub fn activate(app: &mut EffectcraftApp, ctx: &egui::Context, menu: &NativeMenu, id: &str) -> Result<Value, String> {
|
||||
let item = menu.find(id).ok_or_else(|| format!("no native menu item `{id}`"))?;
|
||||
let (command, params) = (item.command.clone(), item.params.clone());
|
||||
// The menu bar takes Cmd+D and Cmd+R before the window sees them: while a confirmation that
|
||||
// answers to the key is open, hand it back. Choosing the item with the mouse (no Cmd held)
|
||||
// still runs it.
|
||||
if let Some(key) = crate::panels::dialogs::command_key(app)
|
||||
&& item.accelerator.as_deref().is_some_and(|a| ["Cmd+", "Ctrl+"].iter().any(|m| a.strip_prefix(m) == Some(key.name())))
|
||||
&& ctx.input(|i| i.modifiers.command)
|
||||
{
|
||||
app.synthetic.push(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND });
|
||||
ctx.request_repaint();
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
if ctx.egui_wants_keyboard_input() {
|
||||
let clip = if command == "edit.paste" { app.hooks.clipboard_text.as_ref().and_then(|f| f()) } else { None };
|
||||
if let Some(events) = text_field_event(&command, clip) {
|
||||
@@ -469,6 +480,32 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_confirmation_gets_its_command_key_back_from_the_menu_bar() {
|
||||
let mut app = app();
|
||||
let spec = build(&app);
|
||||
let dup = spec.items().into_iter().find(|i| i.command == "edit.duplicate").map(|i| i.id.clone()).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
app.dialog_state.delete_items.command = Some((json!({"items": []}), json!({"items": 1, "layers": 1, "comps": 1})));
|
||||
app.dialog = Some(crate::Dialog::DeleteItems);
|
||||
// Chosen with the mouse, Duplicate still runs.
|
||||
ctx.run_ui(Default::default(), |ui| {
|
||||
let _ = activate(&mut app, ui.ctx(), &spec, &dup);
|
||||
})
|
||||
.textures_delta
|
||||
.clear();
|
||||
assert!(app.synthetic.is_empty());
|
||||
// Cmd+D goes to the prompt instead.
|
||||
let mut raw = egui::RawInput::default();
|
||||
raw.events.push(egui::Event::ModifiersChanged(egui::Modifiers::COMMAND));
|
||||
ctx.run_ui(raw, |ui| {
|
||||
let _ = activate(&mut app, ui.ctx(), &spec, &dup);
|
||||
})
|
||||
.textures_delta
|
||||
.clear();
|
||||
assert!(matches!(app.synthetic.as_slice(), [egui::Event::Key { key: egui::Key::D, pressed: true, .. }]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accelerators_convert() {
|
||||
assert_eq!(accelerator("Cmd+Shift+S").as_deref(), Some("Cmd+Shift+S"));
|
||||
|
||||
Reference in New Issue
Block a user