Merge branch 'undo-redo'
This commit is contained in:
+65
-2
@@ -13,8 +13,8 @@ use dr_decode::RawImage;
|
||||
use dr_gpu::{AdjustPass, DemosaicedImage, Demosaicer, GpuContext};
|
||||
use dr_pipeline::ops::curve;
|
||||
use dr_pipeline::{
|
||||
CropRect, EditGraph, OpCapability, OpId, ParamId, ParamKind, Presentation, Preset, Scope, Unit,
|
||||
WidgetKind,
|
||||
CropRect, Edit, EditGraph, History, OpCapability, OpId, ParamId, ParamKind, Presentation,
|
||||
Preset, Scope, Unit, WidgetKind,
|
||||
};
|
||||
|
||||
use crate::labels;
|
||||
@@ -23,6 +23,15 @@ use crate::ParamRow;
|
||||
/// A loaded image plus its edit state.
|
||||
pub struct DevelopSession {
|
||||
graph: EditGraph,
|
||||
/// TRACES: FR-DEV-5
|
||||
/// Undo, kept beside the graph rather than in the window.
|
||||
///
|
||||
/// Every mutator below records into it, so a caller cannot change the edit
|
||||
/// and forget to. That is the whole reason it lives here: the callbacks in
|
||||
/// `lib.rs` are generic by construction and there are a dozen of them, and
|
||||
/// a history the *call sites* had to remember would be one press of undo
|
||||
/// away from wrong every time a control is added.
|
||||
history: History,
|
||||
demosaiced: DemosaicedImage,
|
||||
adjust: AdjustPass,
|
||||
}
|
||||
@@ -73,8 +82,10 @@ impl DevelopSession {
|
||||
) -> Self {
|
||||
let mut graph = EditGraph::default_chain();
|
||||
graph.set_orientation(orientation);
|
||||
let history = History::new(&graph);
|
||||
Self {
|
||||
graph,
|
||||
history,
|
||||
demosaiced,
|
||||
adjust: AdjustPass::new(ctx),
|
||||
}
|
||||
@@ -450,6 +461,9 @@ impl DevelopSession {
|
||||
for p in &cap.params {
|
||||
self.graph.set_param(cap.id, p.id, p.default);
|
||||
}
|
||||
// One step, though it moved every parameter the operation has: the
|
||||
// user pressed one button.
|
||||
self.history.record(&self.graph, Edit::Discrete);
|
||||
}
|
||||
|
||||
/// Reset a curve, which is to reset its operation.
|
||||
@@ -471,6 +485,8 @@ impl DevelopSession {
|
||||
return;
|
||||
};
|
||||
self.graph.set_param(op, param, value);
|
||||
let edit = Edit::for_param(&self.graph, op, param);
|
||||
self.history.record(&self.graph, edit);
|
||||
}
|
||||
|
||||
/// Return one parameter to its default.
|
||||
@@ -487,10 +503,12 @@ impl DevelopSession {
|
||||
.map(|p| p.default)
|
||||
.unwrap_or(0.0);
|
||||
self.graph.set_param(op, param, default);
|
||||
self.history.record(&self.graph, Edit::Discrete);
|
||||
}
|
||||
|
||||
pub fn reset_all(&mut self) {
|
||||
self.graph.reset();
|
||||
self.history.record(&self.graph, Edit::Discrete);
|
||||
}
|
||||
|
||||
fn lookup(&self, op_index: i32, param_index: i32) -> Option<(OpId, ParamId)> {
|
||||
@@ -654,6 +672,10 @@ impl DevelopSession {
|
||||
/// Set the crop rectangle, in fractions of the source.
|
||||
pub fn set_crop(&mut self, rect: CropRect) {
|
||||
self.graph.set_crop(rect);
|
||||
// Keyed on the operation, not on a parameter: one drag of one handle
|
||||
// moves the origin and the extent together.
|
||||
self.history
|
||||
.record(&self.graph, Edit::Op(dr_pipeline::framing::ID));
|
||||
}
|
||||
|
||||
pub fn crop(&self) -> CropRect {
|
||||
@@ -673,6 +695,7 @@ impl DevelopSession {
|
||||
self.graph.set_crop(rotate_crop(crop, turns));
|
||||
}
|
||||
self.graph.rotate_quarters(turns);
|
||||
self.history.record(&self.graph, Edit::Discrete);
|
||||
}
|
||||
|
||||
/// Straightening, in degrees. Positive turns the image clockwise.
|
||||
@@ -697,6 +720,7 @@ impl DevelopSession {
|
||||
dr_pipeline::framing::FLIP_H,
|
||||
f32::from(u8::from(!h)),
|
||||
);
|
||||
self.history.record(&self.graph, Edit::Discrete);
|
||||
}
|
||||
|
||||
pub fn toggle_flip_v(&mut self) {
|
||||
@@ -706,6 +730,7 @@ impl DevelopSession {
|
||||
dr_pipeline::framing::FLIP_V,
|
||||
f32::from(u8::from(!v)),
|
||||
);
|
||||
self.history.record(&self.graph, Edit::Discrete);
|
||||
}
|
||||
|
||||
/// Set the straightening angle, in degrees.
|
||||
@@ -715,6 +740,10 @@ impl DevelopSession {
|
||||
dr_pipeline::framing::ANGLE,
|
||||
degrees,
|
||||
);
|
||||
self.history.record(
|
||||
&self.graph,
|
||||
Edit::Param(dr_pipeline::framing::ID, dr_pipeline::framing::ANGLE),
|
||||
);
|
||||
}
|
||||
|
||||
/// Whether the framing currently changes the image — what lights the
|
||||
@@ -737,6 +766,7 @@ impl DevelopSession {
|
||||
let view = self.graph.framing().view();
|
||||
self.graph.framing_mut().reset();
|
||||
self.graph.framing_mut().set_view(view);
|
||||
self.history.record(&self.graph, Edit::Discrete);
|
||||
}
|
||||
|
||||
/// How far the viewport is zoomed in: 1.0 fits the frame, 4.0 is 4×.
|
||||
@@ -859,6 +889,10 @@ impl DevelopSession {
|
||||
/// values the sliders are showing, and nothing here pushes them.
|
||||
pub fn apply_settings(&mut self, preset: &Preset, scope: Scope) {
|
||||
preset.apply(&mut self.graph, scope);
|
||||
// A paste is undoable, and is the action most in need of it: it
|
||||
// replaces everything in scope at once, so getting it wrong costs more
|
||||
// than any single control can.
|
||||
self.history.record(&self.graph, Edit::Discrete);
|
||||
}
|
||||
|
||||
/// TRACES: FR-CAT-8
|
||||
@@ -870,6 +904,35 @@ impl DevelopSession {
|
||||
/// orientation survives it, since that was never an edit.
|
||||
pub fn apply_version(&mut self, version: &dr_pipeline::Version) {
|
||||
version.apply(&mut self.graph);
|
||||
// The stored edit becomes the floor rather than a step. It is not
|
||||
// something the user did in this sitting, and an undo that reached
|
||||
// behind it would discard a previous session's work in one press —
|
||||
// then persist that on the way out, since saving is automatic.
|
||||
self.history.reset(&self.graph);
|
||||
}
|
||||
|
||||
/// TRACES: FR-DEV-5
|
||||
/// Step the edit back one, returning whether anything moved.
|
||||
///
|
||||
/// The panel must be rebuilt from [`Self::rows`] afterwards, for the same
|
||||
/// reason a paste must: this moves values the controls are showing and
|
||||
/// nothing here pushes them.
|
||||
pub fn undo(&mut self) -> bool {
|
||||
self.history.undo(&mut self.graph)
|
||||
}
|
||||
|
||||
/// TRACES: FR-DEV-5
|
||||
/// Step the edit forward one, returning whether anything moved.
|
||||
pub fn redo(&mut self) -> bool {
|
||||
self.history.redo(&mut self.graph)
|
||||
}
|
||||
|
||||
pub fn can_undo(&self) -> bool {
|
||||
self.history.can_undo()
|
||||
}
|
||||
|
||||
pub fn can_redo(&self) -> bool {
|
||||
self.history.can_redo()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -256,6 +256,11 @@ fn reset_view_state(window: &AppWindow) {
|
||||
window.set_flip_h(false);
|
||||
window.set_flip_v(false);
|
||||
window.set_framing_modified(false);
|
||||
// A photograph that failed to decode has no session, so nothing below
|
||||
// will speak for it — and the buttons would otherwise keep offering the
|
||||
// previous image's history.
|
||||
window.set_can_undo(false);
|
||||
window.set_can_redo(false);
|
||||
}
|
||||
|
||||
/// Push the framing back to the geometry panel.
|
||||
@@ -961,6 +966,15 @@ pub fn run(paths: Vec<PathBuf>) -> Result<()> {
|
||||
Rc::new(move |window: &AppWindow, draft: bool| {
|
||||
let mut slot = session.borrow_mut();
|
||||
let Some(s) = slot.as_mut() else { return };
|
||||
|
||||
// TRACES: FR-DEV-5
|
||||
// Whether undo has anywhere to go, pushed from here because every
|
||||
// edit ends in a redraw and nothing else is on all of their paths:
|
||||
// the parameter callbacks sync rows, the framing ones sync the
|
||||
// geometry panel, and a paste arrives through neither.
|
||||
window.set_can_undo(s.can_undo());
|
||||
window.set_can_redo(s.can_redo());
|
||||
|
||||
let (mut w, mut h) = *viewport.borrow();
|
||||
|
||||
// **Half resolution while the gesture is still moving.**
|
||||
@@ -1505,6 +1519,44 @@ pub fn run(paths: Vec<PathBuf>) -> Result<()> {
|
||||
});
|
||||
}
|
||||
|
||||
// ---- undo and redo (FR-DEV-5) ---------------------------------------
|
||||
//
|
||||
// Thin, because the history lives in the session and every mutator there
|
||||
// records into it — see `DevelopSession::history`. What is left for the
|
||||
// interface is the refresh a paste also needs: the controls are showing
|
||||
// values that have just moved underneath them.
|
||||
//
|
||||
// `can-undo` and `can-redo` are not set here; `render_now` pushes them on
|
||||
// every redraw, which is every path that can change them.
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let session = session.clone();
|
||||
let redraw = redraw.clone();
|
||||
let rows = rows.clone();
|
||||
window.on_undo(move || {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
let stepped = session.borrow_mut().as_mut().is_some_and(|s| s.undo());
|
||||
if stepped {
|
||||
sync_rows(&w, &rows, &session);
|
||||
redraw(&w);
|
||||
}
|
||||
});
|
||||
}
|
||||
{
|
||||
let weak = window.as_weak();
|
||||
let session = session.clone();
|
||||
let redraw = redraw.clone();
|
||||
let rows = rows.clone();
|
||||
window.on_redo(move || {
|
||||
let Some(w) = weak.upgrade() else { return };
|
||||
let stepped = session.borrow_mut().as_mut().is_some_and(|s| s.redo());
|
||||
if stepped {
|
||||
sync_rows(&w, &rows, &session);
|
||||
redraw(&w);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---- zoom, pan and crop ---------------------------------------------
|
||||
//
|
||||
// Zoom and pan are viewing state and touch no parameter, so unlike the
|
||||
|
||||
Reference in New Issue
Block a user