Merge pull request #1019 from pravdomil/claude/confirm-unguarded-deletes
# Conflicts: # crates/ui-egui/src/panels/shortcut_editor.rs
This commit is contained in:
@@ -12,7 +12,8 @@
|
||||
//! 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.rename`, `shortcuts.delete` (asks first: `dialog.deletePreset.delete`,
|
||||
//! `dialog.deletePreset.cancel`), `shortcuts.import`, `shortcuts.export`,
|
||||
//! `shortcuts.resetPreset`, `shortcuts.close`.
|
||||
|
||||
use crate::i18n::{tr, tr_args};
|
||||
@@ -39,6 +40,8 @@ pub struct EditorState {
|
||||
pub pending: String,
|
||||
/// Rename field for the active preset.
|
||||
pub rename: Option<String>,
|
||||
/// The preset the Delete Preset prompt asks about.
|
||||
pub confirm_delete: Option<String>,
|
||||
}
|
||||
|
||||
const APP_COLOR: Color32 = Color32::from_rgb(0x8e, 0x6c, 0xe8);
|
||||
@@ -54,6 +57,11 @@ pub fn recording(app: &EffectcraftApp) -> bool {
|
||||
app.dialog == Some(Dialog::Shortcuts) && app.dialog_state.shortcuts.recording
|
||||
}
|
||||
|
||||
/// `true` while Delete Preset asks over the editor (Escape answers it, not the editor).
|
||||
pub fn confirming(app: &EffectcraftApp) -> bool {
|
||||
app.dialog == Some(Dialog::Shortcuts) && app.dialog_state.shortcuts.confirm_delete.is_some()
|
||||
}
|
||||
|
||||
/// Shortcut text of a key event (`Cmd+Shift+K`).
|
||||
pub fn combo_text(m: egui::Modifiers, k: egui::Key) -> Option<String> {
|
||||
use egui::Key::*;
|
||||
@@ -274,10 +282,11 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
|
||||
st.rename = Some(active.clone());
|
||||
}
|
||||
}
|
||||
// A deleted preset can't be brought back: Delete asks first (the prompt below).
|
||||
let b = ui.add_enabled(custom, egui::Button::new(tr("Delete")));
|
||||
app.auto.add("shortcuts.delete", b.rect, "Delete");
|
||||
if b.clicked() {
|
||||
run.push(("shortcuts.preset".into(), json!({"op": "delete", "name": active})));
|
||||
st.confirm_delete = Some(active.clone());
|
||||
}
|
||||
let b = ui.add_enabled(custom, egui::Button::new(tr("Reset Preset")));
|
||||
app.auto.add("shortcuts.resetPreset", b.rect, "Reset Preset");
|
||||
@@ -571,6 +580,41 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
|
||||
close |= r.clicked();
|
||||
});
|
||||
});
|
||||
// Delete Preset, over the editor.
|
||||
if let Some(name) = st.confirm_delete.clone() {
|
||||
let mut answer: Option<bool> = None;
|
||||
super::dialogs::modal(ctx, "Delete Preset", vec2(460.0, 150.0), t, |ui| {
|
||||
ui.label(RichText::new(format!("Delete the preset “{name}”?")).color(t.tab_text_active));
|
||||
ui.add_space(4.0);
|
||||
ui.label(RichText::new(tr("You can't undo this.")).color(t.text_dim).size(11.0));
|
||||
ui.add_space(18.0);
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
let r = ui.add(egui::Button::new(RichText::new(tr(" Delete ")).color(Color32::WHITE)).fill(t.danger));
|
||||
app.auto.add("dialog.deletePreset.delete", r.rect, "Delete");
|
||||
if r.clicked() {
|
||||
answer = Some(true);
|
||||
}
|
||||
let r = ui.button(tr("Cancel"));
|
||||
app.auto.add("dialog.deletePreset.cancel", r.rect, "Cancel");
|
||||
if r.clicked() {
|
||||
answer = Some(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
if answer.is_none() && ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
|
||||
answer = Some(false);
|
||||
}
|
||||
// Cmd+D (Ctrl elsewhere) is Delete.
|
||||
if answer.is_none() && super::dialogs::ui_command_key(ctx, egui::Key::D) {
|
||||
answer = Some(true);
|
||||
}
|
||||
if let Some(delete) = answer {
|
||||
st.confirm_delete = None;
|
||||
if delete {
|
||||
run.push(("shortcuts.preset".into(), json!({"op": "delete", "name": name})));
|
||||
}
|
||||
}
|
||||
}
|
||||
app.dialog_state.shortcuts = st;
|
||||
for (cmd, params) in run {
|
||||
if let Err(e) = app.session.execute(&cmd, params) {
|
||||
@@ -657,6 +701,49 @@ mod tests {
|
||||
assert_eq!(app.dialog, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleting_a_preset_asks_first() {
|
||||
fn click(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str) {
|
||||
let [x, y, w, h] = app.auto.find(id).unwrap_or_else(|| panic!("{id} is not on screen")).rect;
|
||||
let pos = pos2(x + w / 2.0, y + h / 2.0);
|
||||
let button = |pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
for event in [egui::Event::PointerMoved(pos), button(true), button(false)] {
|
||||
frame(app, ctx, vec![event]);
|
||||
}
|
||||
// Two more: the automation ids of the clicked frame age out.
|
||||
frame(app, ctx, vec![]);
|
||||
frame(app, ctx, vec![]);
|
||||
}
|
||||
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
|
||||
let ctx = egui::Context::default();
|
||||
crate::theme::install(&ctx, &app.tokens, crate::i18n::language(&app));
|
||||
app.session.execute("shortcuts.preset", json!({"op": "duplicate", "from": DEFAULT_PRESET, "name": "Mine"})).unwrap();
|
||||
app.session.execute("shortcuts.preset", json!({"op": "select", "name": "Mine"})).unwrap();
|
||||
let has = |app: &EffectcraftApp| app.session.keymaps.names().iter().any(|n| n == "Mine");
|
||||
open(&mut app);
|
||||
for _ in 0..3 {
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
}
|
||||
assert!(app.auto.find("dialog.deletePreset.delete").is_none());
|
||||
click(&mut app, &ctx, "shortcuts.delete");
|
||||
assert!(has(&app) && confirming(&app), "the click only asks");
|
||||
click(&mut app, &ctx, "dialog.deletePreset.cancel");
|
||||
assert!(has(&app) && app.auto.find("dialog.deletePreset.delete").is_none(), "Cancel keeps the preset");
|
||||
assert_eq!(app.dialog, Some(Dialog::Shortcuts), "the editor stays open");
|
||||
click(&mut app, &ctx, "shortcuts.delete");
|
||||
click(&mut app, &ctx, "dialog.deletePreset.delete");
|
||||
assert!(!has(&app), "confirmed: the preset is gone");
|
||||
assert!(!confirming(&app) && app.dialog == Some(Dialog::Shortcuts));
|
||||
// Cmd+D is Delete; Enter is not.
|
||||
app.session.execute("shortcuts.preset", json!({"op": "duplicate", "from": DEFAULT_PRESET, "name": "Mine"})).unwrap();
|
||||
app.session.execute("shortcuts.preset", json!({"op": "select", "name": "Mine"})).unwrap();
|
||||
app.dialog_state.shortcuts.confirm_delete = Some("Mine".into());
|
||||
crate::panels::dialogs::press_key(&mut app, &ctx, egui::Key::Enter, egui::Modifiers::NONE);
|
||||
assert!(has(&app) && confirming(&app));
|
||||
crate::panels::dialogs::press_key(&mut app, &ctx, egui::Key::D, egui::Modifiers::COMMAND);
|
||||
assert!(!has(&app) && !confirming(&app));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recording_a_shortcut_does_not_also_type_the_key_into_the_field() {
|
||||
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
|
||||
|
||||
Reference in New Issue
Block a user