From 0b0a5337f6bbf01ccfaabe79a8c6aedcd8769c0b Mon Sep 17 00:00:00 2001 From: Rebornim <250083243+Rebornim@users.noreply.github.com> Date: Sun, 29 Nov 2026 09:49:35 +0000 Subject: [PATCH] Bring the current History step into view when a full history repeats a name --- crates/engine/src/painting.rs | 7 ++---- crates/engine/src/session.rs | 27 +++++++++++++++++++++--- crates/engine/tests/session.rs | 22 +++++++++++++++++++ crates/ui-egui/src/state.rs | 4 ++-- crates/ui-egui/src/windows.rs | 2 +- crates/ui-egui/tests/it/history_panel.rs | 17 ++++++++++++++- 6 files changed, 67 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/painting.rs b/crates/engine/src/painting.rs index f2395365..6d26d432 100644 --- a/crates/engine/src/painting.rs +++ b/crates/engine/src/painting.rs @@ -555,7 +555,7 @@ impl Session { self.stroke.is_some() } - /// Commit the active stroke (one undo step). + /// Commit the active stroke as one undo step pub fn end_stroke(&mut self) -> Result<()> { let Some(mut stroke) = self.stroke.take() else { return Ok(()) }; let size = (stroke.canvas.alpha.width(), stroke.canvas.alpha.height()); @@ -586,10 +586,7 @@ impl Session { return Ok(()); } } - self.undo.push((stroke.label.clone(), stroke.before)); - if self.undo.len() > crate::session::MAX_HISTORY { - self.undo.remove(0); - } + self.add_step(stroke.label.clone(), stroke.before); self.redo.clear(); self.mark_modified(); diff --git a/crates/engine/src/session.rs b/crates/engine/src/session.rs index faf91c9a..9388a3ec 100644 --- a/crates/engine/src/session.rs +++ b/crates/engine/src/session.rs @@ -440,6 +440,8 @@ pub struct Session { pub caches: Vec, pub(crate) undo: Vec<(String, Document)>, pub(crate) redo: Vec<(String, Document)>, + /// New undo steps so far: it moves even when a full history keeps its length + pub(crate) steps_added: u64, /// Set by [`crate::execute`] from a command's `merge` param: its edits continue the previous /// undo step with the same label (a slider drag is one step however many values it applies). pub(crate) merge_edits: bool, @@ -548,6 +550,7 @@ impl Session { caches, undo: Vec::new(), redo: Vec::new(), + steps_added: 0, merge_edits: false, tool: ToolState::default(), active_set: 0, @@ -757,7 +760,7 @@ impl Session { if !self.painting_paths && let Some(draft) = self.path_draft.take() { - self.undo.push((crate::path_tool::NEW_PATH_LABEL.to_string(), draft.base)); + self.add_step(crate::path_tool::NEW_PATH_LABEL.to_string(), draft.base); } // Point changes of an edited path undo only until another step lands @@ -767,11 +770,22 @@ impl Session { self.mark_modified(); if !(merge && self.undo.last().is_some_and(|(l, _)| l == label)) { - self.undo.push((label.to_string(), before)); + self.add_step(label.to_string(), before); } + + self.redo.clear(); + } + + /// Append an undo step, dropping the oldest ones from a full history + pub(crate) fn add_step(&mut self, label: String, before: Document) { + self.undo.push((label, before)); let over = self.undo.len().saturating_sub(MAX_HISTORY); self.undo.drain(..over); - self.redo.clear(); + + // A repaint of paths runs strokes on a temporary history, none of which is a step + if !self.painting_paths { + self.steps_added = self.steps_added.wrapping_add(1); + } } /// A change from `other` to the current document that is not an undo step (a path preview) @@ -975,6 +989,13 @@ impl Session { pub fn redo_labels(&self) -> Vec { self.redo.iter().rev().map(|(l, _)| l.clone()).collect() } + + /// Changes with every new undo step, not when a merged edit continues one + pub fn steps_added(&self) -> u64 { + self.steps_added + } + + /// The done steps, the oldest first pub fn history_labels(&self) -> Vec { self.undo.iter().map(|(l, _)| l.clone()).collect() } diff --git a/crates/engine/tests/session.rs b/crates/engine/tests/session.rs index 0fc0b848..d95c4358 100644 --- a/crates/engine/tests/session.rs +++ b/crates/engine/tests/session.rs @@ -1155,3 +1155,25 @@ fn interleaved_sessions_render_depth_from_their_own_mesh() { assert!((hidden[0] - hidden[1]).abs() < 0.05, "the rear surface is occluded: {hidden:?}"); assert!(visible[0] > visible[1] + 0.5, "the rear surface is exposed: {visible:?}"); } + +/// A full history keeps its length and may repeat its newest name, so views follow the count of steps added +#[test] +fn a_full_history_still_counts_each_new_step() { + let mut s = session(); + for _ in 0..texturecraft_engine::session::MAX_HISTORY { + execute(&mut s, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap(); + } + let (labels, added) = (s.history_labels(), s.steps_added()); + + execute(&mut s, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap(); + + assert_eq!(s.history_labels(), labels); + assert!(s.steps_added() > added); + + // A path preview paints through strokes of its own, none of which is a step + let added = s.steps_added(); + execute(&mut s, "tool.path", &json!({"uvPoints": [[0.2, 0.5], [0.8, 0.5]]})).unwrap(); + + assert_eq!(s.history_labels(), labels); + assert_eq!(s.steps_added(), added); +} diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index d3a053d2..d52bca3f 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -744,9 +744,9 @@ pub struct UiState { /// Identity of the log that owns the selected rows. #[serde(skip)] pub log_id: u64, - /// The History panel's last shown container and steps (done, undone, newest name): a change scrolls to the current step + /// The History panel's last shown container and steps (added so far, done, undone): a change scrolls to the current step #[serde(skip)] - pub history_shown: Option<(egui::Id, usize, usize, Option)>, + pub history_shown: Option<(egui::Id, u64, usize, usize)>, /// The widest line of each Log tab, measured once per line #[serde(skip)] pub log_widths: crate::log_panel::LogWidthCache, diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index dbf29eeb..31299e0f 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -263,7 +263,7 @@ pub(crate) fn history(app: &mut TextureCraftApp, ui: &mut Ui) { let total_rows = 1 + done.len() + undone.len(); // Only new steps or a new container (with its own scroll) bring the current one into view; a tool change keeps the user's scroll - let shown = Some((ui.make_persistent_id("history"), done.len(), undone.len(), done.last().cloned())); + let shown = Some((ui.make_persistent_id("history"), app.session.steps_added(), done.len(), undone.len())); let follow = app.ui.history_shown != shown; app.ui.history_shown = shown; diff --git a/crates/ui-egui/tests/it/history_panel.rs b/crates/ui-egui/tests/it/history_panel.rs index f9a82b9a..2880feac 100644 --- a/crates/ui-egui/tests/it/history_panel.rs +++ b/crates/ui-egui/tests/it/history_panel.rs @@ -293,7 +293,7 @@ fn scrolling_up_is_not_pulled_back() { assert!(!current_shown(&harness, "Rename layer")); } -/// Once the history is full, a new step keeps the step count; its new name still brings it into view +/// Once the history is full, a new step keeps the step count and still comes into view #[test] fn the_newest_step_shows_past_the_history_cap() { let steps = texturecraft_engine::session::MAX_HISTORY + 60; @@ -311,6 +311,21 @@ fn the_newest_step_shows_past_the_history_cap() { assert!(current_shown(&harness, &newest)); } +/// A full history with a new step named like the newest one keeps its counts and names, and still comes into view +#[test] +fn a_repeated_step_shows_past_the_history_cap() { + let mut harness = history_harness(long_session(texturecraft_engine::session::MAX_HISTORY)); + let capped = harness.state().session.history_labels(); + scroll_to_top(&mut harness); + assert!(!current_shown(&harness, "Rename layer")); + + execute(&mut harness.state_mut().session, "layer.rename", &json!({"name": "Once more"})).unwrap(); + harness.run_steps(4); + + assert_eq!(harness.state().session.history_labels(), capped); + assert!(current_shown(&harness, "Rename layer")); +} + /// A History list that moves to another container (rail popup to floating window) still shows the current step #[test] fn an_undocked_history_shows_the_newest_step() {