PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Undo and redo Camera Raw adjustments from the keyboard. (#4136)
Vikram Bhaskaran8215b0b969b5
diff --git a/crates/ui-egui/src/camera_raw_ui.rs b/crates/ui-egui/src/camera_raw_ui.rsindex f3e2aa8fb..d2f78a35c 100644--- a/crates/ui-egui/src/camera_raw_ui.rs+++ b/crates/ui-egui/src/camera_raw_ui.rs@@ -69,6 +69,11 @@ pub struct CameraRawDialog {/// The open-time dialog's camera tone curve: Shadows / Highlights act on the raw data there,/// in linear light before this curve, and the preview undoes it to show the same.raw_curve: Option<Vec<[f32; 2]>>,+ /// Settings before each finished edit, so Ctrl+Z works inside the dialog (#4100).+ undo: Vec<CameraRaw>,+ redo: Vec<CameraRaw>,+ /// Params at pointer-down. One drag is one undo step.+ edit_origin: Option<CameraRaw>,}impl CameraRawDialog {@@ -638,6 +643,9 @@ fn open_pixels(render_ms: 0.0,raw_open: None,raw_curve: None,+ undo: Vec::new(),+ redo: Vec::new(),+ edit_origin: None,};d.render(ctx);app.camera_raw = Some(d);@@ -676,8 +684,10 @@ pub fn menu(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: &Valfor (key, value) in fields {match key.as_str() {"set" | "scope" | "view" => {}- "before" | "commit" | "cancel" | "saveSettings" | "loadSettings" if value.is_boolean() => {}- "before" | "commit" | "cancel" | "saveSettings" | "loadSettings" => return Some(Err(format!("Camera Raw {key} must be a boolean"))),+ "before" | "commit" | "cancel" | "saveSettings" | "loadSettings" | "undo" | "redo" if value.is_boolean() => {}+ "before" | "commit" | "cancel" | "saveSettings" | "loadSettings" | "undo" | "redo" => {+ return Some(Err(format!("Camera Raw {key} must be a boolean")));+ }_ => return Some(Err(format!("unknown Camera Raw ui property {key}"))),}}@@ -754,14 +764,23 @@ fn menu_update(app: &mut PhotocraftApp, ctx: &egui::Context, ui: &Value) -> Optiif d.params.point_curve != next_params.point_curve {d.curve_state = Default::default();}- d.dirty |= d.params != next_params;- d.params = next_params;+ if d.params != next_params {+ remember_edit(d);+ d.dirty = true;+ d.params = next_params;+ }if let Some(b) = ui.get("before").and_then(Value::as_bool) {d.show_before = b;}if let Some(update) = scope_update {update.apply(d, &mut app.ui.camera_raw_scope);}+ if ui.get("undo").and_then(Value::as_bool) == Some(true) {+ undo_edit(d);+ }+ if ui.get("redo").and_then(Value::as_bool) == Some(true) {+ redo_edit(d);+ }if d.dirty {d.render(ctx);}@@ -805,8 +824,11 @@ fn load_settings(app: &mut PhotocraftApp) -> Result<Value, String> {if d.params.point_curve != next.point_curve {d.curve_state = Default::default();}- d.dirty |= d.params != next;- d.params = next;+ if d.params != next {+ remember_edit(d);+ d.dirty = true;+ d.params = next;+ }app.ui.status_error = false;app.ui.status = if report.ignored.is_empty() {crate::i18n::fmt(tl!("Loaded Camera Raw settings from {file}"), &[("file", &file)])@@ -827,6 +849,49 @@ fn report_file_error(app: &mut PhotocraftApp, r: Result<Value, String>) {}}+const RAW_UNDO_LIMIT: usize = 40;++fn remember_edit(d: &mut CameraRawDialog) {+ if d.undo.len() == RAW_UNDO_LIMIT {+ d.undo.remove(0);+ }+ d.undo.push(d.params.clone());+ d.redo.clear();+}++fn undo_edit(d: &mut CameraRawDialog) -> bool {+ let Some(prev) = d.undo.pop() else { return false };+ d.redo.push(d.params.clone());+ if d.params.point_curve != prev.point_curve {+ d.curve_state = Default::default();+ }+ d.params = prev;+ d.dirty = true;+ d.edit_origin = None;+ true+}++fn redo_edit(d: &mut CameraRawDialog) -> bool {+ let Some(next) = d.redo.pop() else { return false };+ d.undo.push(d.params.clone());+ if d.params.point_curve != next.point_curve {+ d.curve_state = Default::default();+ }+ d.params = next;+ d.dirty = true;+ d.edit_origin = None;+ true+}++fn history_keys(d: &mut CameraRawDialog, ctx: &egui::Context) {+ let cmd_shift = egui::Modifiers::COMMAND | egui::Modifiers::SHIFT;+ if ctx.input_mut(|i| i.consume_key(cmd_shift, egui::Key::Z)) {+ redo_edit(d);+ } else if ctx.input_mut(|i| i.consume_key(egui::Modifiers::COMMAND, egui::Key::Z)) {+ undo_edit(d);+ }+}+fn commit(app: &mut PhotocraftApp) -> Result<Value, String> {let d = app.camera_raw.as_ref().ok_or(tl!("Camera Raw isn't open"))?;let result = match d.target {@@ -995,6 +1060,11 @@ fn draw(app: &mut PhotocraftApp, ctx: &egui::Context, own_window: bool) {let mut action: Option<&str> = None;egui::Area::new(egui::Id::new("camera-raw-dialog")).order(egui::Order::Foreground).fixed_pos(screen.min).show(ctx, |ui| {let Some(d) = app.camera_raw.as_mut() else { return };+ history_keys(d, ctx);+ let pointer = ctx.input(|i| i.pointer.primary_down());+ if pointer && d.edit_origin.is_none() {+ d.edit_origin = Some(d.params.clone());+ }if let Some(display) = ¤t_display&& display.key != d.display.key{@@ -1201,6 +1271,14 @@ fn draw(app: &mut PhotocraftApp, ctx: &egui::Context, own_window: bool) {if fu.button(tl!("Load Settings…")).on_hover_text(tl!("Load settings from a Camera Raw .xmp file")).clicked() {action = Some("load");}+ if !ctx.input(|i| i.pointer.primary_down())+ && let Some(origin) = d.edit_origin.take()+ && origin != d.params+ {+ let edited = std::mem::replace(&mut d.params, origin);+ remember_edit(d);+ d.params = edited;+ }});super::camera_raw_scope_ui::persist(app, ctx);if ctx.input(|i| i.key_pressed(egui::Key::Y))diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rsindex 209691b87..ce486f802 100644--- a/crates/ui-egui/src/control.rs+++ b/crates/ui-egui/src/control.rs@@ -1391,7 +1391,11 @@ mod tests {assert!(app.ui.dialogs.is_empty());let update = json!({"id":"filter.cameraRaw","params":{"ui":{"set":{"exposure":0.5}}}});let original = app.session.active().unwrap().doc.clone();- let changed = call(&mut app, &ctx, "ui.menu.invoke", update);+ let changed = call(&mut app, &ctx, "ui.menu.invoke", update.clone());+ let undone = call(&mut app, &ctx, "ui.menu.invoke", json!({"id":"filter.cameraRaw","params":{"ui":{"undo":true}}}));+ assert_eq!(undone["result"]["params"]["exposure"], 0.0, "{undone}");+ let redone = call(&mut app, &ctx, "ui.menu.invoke", json!({"id":"filter.cameraRaw","params":{"ui":{"redo":true}}}));+ assert_eq!(redone["result"]["params"]["exposure"], 0.5, "{redone}");assert_eq!(changed["ok"], true, "{changed}");assert_eq!(changed["result"]["params"]["exposure"], 0.5);assert!(std::sync::Arc::ptr_eq(&app.session.active().unwrap().doc, &original), "a preview must not commit");