From 4d188a9106435217e6808220d25e72453717cbee Mon Sep 17 00:00:00 2001 From: yoriswind Date: Mon, 2 Nov 2026 23:07:41 -0800 Subject: [PATCH] fix(actions): preserve cleared source context and Undo Clear --- crates/engine/src/cmd/actions_cmds/history.rs | 13 ++++- crates/engine/src/cmd/history_source_cmds.rs | 2 +- crates/engine/src/session/history.rs | 7 ++- crates/ops/src/lib.rs | 54 ++++++++++++++++--- 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/actions_cmds/history.rs b/crates/engine/src/cmd/actions_cmds/history.rs index cdb4814a658..91724a654ee 100644 --- a/crates/engine/src/cmd/actions_cmds/history.rs +++ b/crates/engine/src/cmd/actions_cmds/history.rs @@ -91,13 +91,20 @@ fn consolidate(s: &mut Session, group: &ActionHistory) { history.clear_states(); } if let Some(source) = history.preserve_action_brush_source(&st.history) { + let (source, restore_on_clear_undo) = match source { + photocraft_ops::ActionBrushSource::Current(source) => (source, false), + photocraft_ops::ActionBrushSource::Cleared(source) => (source, true), + }; // The selected row will no longer exist, but the History Brush must still paint its // exact pixels. Snapshots already retain source pixels without adding an Undo row. let id = st.next_snapshot; st.next_snapshot = id.saturating_add(1); st.snapshots.push(crate::history_cmds::Snapshot { id, name: format!("{}: {}", group.name, source.label), doc: source.doc, layers: source.layers }); - st.source_snapshot = Some(id); - history.reset_brush_source(); + if restore_on_clear_undo { + history.set_cleared_brush_snapshot(id); + } else { + history.set_action_brush_snapshot(id); + } } st.history = history; st.trim_history(); @@ -134,6 +141,7 @@ struct HistoryBinding { past: usize, rows: Vec<(Option, String)>, source: Option, + source_snapshot: Option, cleared: bool, } @@ -145,6 +153,7 @@ impl HistoryBinding { past: history.past_len(), rows: labels.enumerate().map(|(i, label)| (history.row_id(i), label)).collect(), source: history.brush_source(), + source_snapshot: history.brush_source_snapshot(), cleared: history.is_cleared(), } } diff --git a/crates/engine/src/cmd/history_source_cmds.rs b/crates/engine/src/cmd/history_source_cmds.rs index 908e6d11d32..c4c0f5e2251 100644 --- a/crates/engine/src/cmd/history_source_cmds.rs +++ b/crates/engine/src/cmd/history_source_cmds.rs @@ -43,7 +43,7 @@ pub enum HistorySource { /// The document's History Brush source, `None` once the source state or snapshot is gone. pub fn source(st: &DocState) -> Option { - match st.source_snapshot { + match st.source_snapshot.or_else(|| st.history.brush_source_snapshot()) { Some(id) => st.snapshots.iter().any(|s| s.id == id).then_some(HistorySource::Snapshot(id)), None => st.history.brush_source().map(HistorySource::State), } diff --git a/crates/engine/src/session/history.rs b/crates/engine/src/session/history.rs index b3f830fda77..b099c81a97d 100644 --- a/crates/engine/src/session/history.rs +++ b/crates/engine/src/session/history.rs @@ -2,12 +2,17 @@ use super::documents::fix_active; use super::{Arc, DocState, Document, LayerId, LayerTarget, Session, channel_cmds, layer_multi_cmds}; impl Session { + /// Undo a document step or restore Clear History's rows and source without editing pixels. pub fn undo(&mut self) -> bool { // A background job is computing from the current state: it must not move under it. if self.active_job().is_some() { return false; } - crate::actions_cmds::before_action_edit(self); + // Undo Clear History only restores metadata. It must not establish the new document + // baseline that an actual edit after Clear History would create. + if !self.active().is_some_and(|st| st.history.is_cleared()) { + crate::actions_cmds::before_action_edit(self); + } let keep_visibility = !self.visibility_undoable(); let Some(st) = self.active_mut() else { return false }; st.coalesce = None; diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rs index 7d4ded34a45..b5aea2b730e 100644 --- a/crates/ops/src/lib.rs +++ b/crates/ops/src/lib.rs @@ -41,6 +41,13 @@ pub struct HistoryState { pub id: u64, } +/// The context in which a selected action row must remain available after it is folded away. +#[derive(Clone, Debug)] +pub enum ActionBrushSource { + Current(HistoryState), + Cleared(HistoryState), +} + #[derive(Clone, Debug)] pub struct History { /// Past states; the last one is the state *before* the current document. @@ -79,6 +86,9 @@ pub struct History { next_id: u64, /// The History Brush's source state, set in the History panel (`None`: the default). brush_source: Option, + /// A snapshot-backed alias of a folded row. Clear History moves this with its row source; + /// a snapshot selected explicitly by the user remains separate in the document state. + brush_source_snapshot: Option, } /// What [`History::rollback`] needs to take the most recent [`History::record`] back. @@ -104,6 +114,7 @@ struct Cleared { undo: VecDeque, redo: Vec, brush_source: Option, + brush_source_snapshot: Option, } /// The undo label of the step [`History::clear_states`] adds (Edit › Undo Clear History). @@ -134,6 +145,7 @@ impl History { current_id: 0, next_id: 1, brush_source: None, + brush_source_snapshot: None, } } @@ -255,18 +267,44 @@ impl History { /// Keep source selections when temporary action rows are consolidated. Return a selected /// intermediate state so the caller can retain its pixels as a normal history snapshot. /// A source already deleted before consolidation remains unresolved. - pub fn preserve_action_brush_source(&mut self, later: &Self) -> Option { + pub fn preserve_action_brush_source(&mut self, later: &Self) -> Option { let current = self.current_id; let prior_current = later.current_id; - let collapsed = later + let collapsed = |id| { + if id == prior_current || self.held_states().any(|state| state.id == id) { None } else { later.held_states().find(|state| state.id == id).cloned() } + }; + let source = later .brush_source - .and_then(|id| if id == prior_current || self.row_of(id).is_some() { None } else { later.held_states().find(|state| state.id == id).cloned() }); + .and_then(collapsed) + .map(ActionBrushSource::Current) + .or_else(|| later.cleared.as_ref().and_then(|cleared| cleared.brush_source).and_then(collapsed).map(ActionBrushSource::Cleared)); let map = |source: Option| source.map(|id| if id == prior_current { current } else { id }); self.brush_source = map(later.brush_source); + self.brush_source_snapshot = later.brush_source_snapshot; if let (Some(dst), Some(src)) = (&mut self.cleared, &later.cleared) { dst.brush_source = map(src.brush_source); + dst.brush_source_snapshot = src.brush_source_snapshot; + } + source + } + + /// The snapshot retaining a folded row's pixels in the currently active source context. + pub fn brush_source_snapshot(&self) -> Option { + self.brush_source_snapshot + } + + /// Retain a folded row through a native snapshot, without adding a visible Undo state. + pub fn set_action_brush_snapshot(&mut self, snapshot: u64) { + self.brush_source = None; + self.brush_source_snapshot = Some(snapshot); + } + + /// Keep a folded source inactive until Undo Clear History restores its source context. + pub fn set_cleared_brush_snapshot(&mut self, snapshot: u64) { + if let Some(cleared) = &mut self.cleared { + cleared.brush_source = None; + cleared.brush_source_snapshot = Some(snapshot); } - collapsed } /// Undo: `current` becomes redoable; returns the document to make current and the layers it @@ -274,10 +312,11 @@ impl History { pub fn undo(&mut self, current: Arc) -> Option<(Arc, LayerTarget)> { // Undo Clear History: the cleared states come back; the document doesn't change. if let Some(cleared) = self.cleared.take() { - let Cleared { undo, redo, brush_source } = *cleared; + let Cleared { undo, redo, brush_source, brush_source_snapshot } = *cleared; self.undo = undo; self.redo = redo; self.brush_source = brush_source; + self.brush_source_snapshot = brush_source_snapshot; self.tiles = None; return Some((current, self.current_layers.clone())); } @@ -368,6 +407,7 @@ impl History { pub fn set_brush_source(&mut self, i: usize) -> bool { let Some(id) = self.row_id(i) else { return false }; self.brush_source = Some(id); + self.brush_source_snapshot = None; true } @@ -548,6 +588,7 @@ impl History { /// Make the History Brush's source the default again (the oldest state held), pinning no state. pub fn reset_brush_source(&mut self) { self.brush_source = None; + self.brush_source_snapshot = None; } /// History panel › Clear History: take every state but the current one off the list without @@ -562,7 +603,8 @@ impl History { let undo = std::mem::take(&mut self.undo); let redo = std::mem::take(&mut self.redo); let brush_source = self.brush_source.take(); - self.cleared = Some(Box::new(Cleared { undo, redo, brush_source })); + let brush_source_snapshot = self.brush_source_snapshot.take(); + self.cleared = Some(Box::new(Cleared { undo, redo, brush_source, brush_source_snapshot })); self.last_record = None; self.tiles = None; true