PhotoCraft: An open-source, photo editor written by scratch in pure Rust
History panel menu: Delete removes the selected snapshot, as the trash does
David Mann599a2304ed3f
diff --git a/crates/ui-egui/src/history_menu_ui.rs b/crates/ui-egui/src/history_menu_ui.rsindex 6a7912e34..e0768f5ba 100644--- a/crates/ui-egui/src/history_menu_ui.rs+++ b/crates/ui-egui/src/history_menu_ui.rs@@ -28,6 +28,15 @@ fn params(cmd: &str) -> Value {if cmd == "window.panel.close" { json!({"panel": "history"}) } else { json!({}) }}+/// The command and params an item runs: with a snapshot selected in the panel, Delete removes+/// that snapshot, as the panel's trash does (#5324); otherwise it removes the current state.+fn resolve(cmd: &str, snapshot: Option<u64>) -> (&str, Value) {+ match snapshot {+ Some(id) if cmd == "history.deleteState" => ("history.deleteSnapshot", json!({"snapshot": id})),+ _ => (cmd, params(cmd)),+ }+}+fn enabled(app: &PhotocraftApp, cmd: &str) -> bool {if cmd == "window.panel.close" {return !app.session.prefs().workspace_locked;@@ -38,20 +47,22 @@ fn enabled(app: &PhotocraftApp, cmd: &str) -> bool {/// Draw the History items of the panel menu and run the one clicked.pub fn panel_menu(app: &mut PhotocraftApp, ui: &mut egui::Ui) {+ let snapshot = app.session.active().and_then(|st| crate::panels::selected_snapshot(ui.ctx(), st));let mut clicked: Option<&str> = None;for &(label, cmd) in ITEMS {if label == "---" {ui.separator();continue;}- let on = cmd.is_some_and(|c| enabled(app, c));+ let on = cmd.is_some_and(|c| enabled(app, resolve(c, snapshot).0));if ui.add_enabled(on, egui::Button::new(crate::i18n::t(label))).clicked() {clicked = cmd;}}let Some(cmd) = clicked else { return };ui.close();- if let Err(e) = app.run(cmd, params(cmd)) {+ let (cmd, params) = resolve(cmd, snapshot);+ if let Err(e) = app.run(cmd, params) {app.ui.status = e;app.ui.status_error = true;}@@ -140,6 +151,29 @@ mod tests {}}+ /// With a snapshot selected in the panel, Delete removes the snapshot and leaves the states,+ /// as the panel's trash does (#5324); without one it removes the current state.+ #[test]+ fn delete_removes_the_selected_snapshot() {+ let mut app = app();+ app.run("history.newSnapshot", json!({})).unwrap();+ let mut h = harness(app);+ let snaps = |h: &Harness<'static, PhotocraftApp>| h.state().session.active().unwrap().snapshots.len();+ let before = snaps(&h);+ let id = h.state().session.active().unwrap().snapshots.last().unwrap().id;+ let ctx = h.ctx.clone();+ crate::panels::select_snapshot(&ctx, h.state().session.active().unwrap(), id);+ h.run_steps(2);+ let steps = states(&h);+ click(&mut h, "Delete");+ assert!(!h.state().ui.status_error, "{}", h.state().ui.status);+ assert_eq!(snaps(&h), before - 1, "Delete removed the selected snapshot");+ assert_eq!(states(&h), steps, "and left the states alone");+ click(&mut h, "Delete");+ assert_eq!(snaps(&h), before - 1, "no snapshot selected now: the snapshots are kept");+ assert_eq!(states(&h).0, steps.0 - 1, "the current state is deleted");+ }+/// The labels aren't `tl!` literals, so the catalog scan can't see them: every language that/// claims complete menus must still translate them.#[test]diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 769f5a8b9..0b4fd6ce1 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -3195,7 +3195,7 @@ fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) {// The snapshot stays selected while the document is the state the click made.Ok(_) => {if let (Some(id), Some(st)) = (picked, app.session.active()) {- ctx.data_mut(|d| d.insert_temp::<SelectedSnapshot>(selected_snapshot_key(st.doc.id), (id, history_spot(st))));+ select_snapshot(&ctx, st, id);}}}@@ -3234,10 +3234,15 @@ fn selected_snapshot_key(doc: photocraft_doc::DocId) -> egui::Id {egui::Id::new(("history-snapshot-selected", doc))}+/// Select snapshot `id` at the history's current state, as a click on its row does.+pub(crate) fn select_snapshot(ctx: &egui::Context, st: &photocraft_engine::DocState, id: u64) {+ ctx.data_mut(|d| d.insert_temp::<SelectedSnapshot>(selected_snapshot_key(st.doc.id), (id, history_spot(st))));+}+/// The selected snapshot: the one last clicked, while it still exists and the history is still at/// the state that click made. Any other step (an edit, Undo, a state row) deselects it, as in/// Photoshop.-fn selected_snapshot(ctx: &egui::Context, st: &photocraft_engine::DocState) -> Option<u64> {+pub(crate) fn selected_snapshot(ctx: &egui::Context, st: &photocraft_engine::DocState) -> Option<u64> {let key = selected_snapshot_key(st.doc.id);let (id, spot) = ctx.data(|d| d.get_temp::<SelectedSnapshot>(key))?;let live = spot == history_spot(st) && st.snapshots.iter().any(|s| s.id == id);