From 226e2de43a60719fd007035bcfc72dd66ad2f80f Mon Sep 17 00:00:00 2001 From: Rebornim <250083243+Rebornim@users.noreply.github.com> Date: Sun, 29 Nov 2026 12:49:21 +0000 Subject: [PATCH] Keep the current History step on screen when the list gets shorter --- crates/ui-egui/src/state.rs | 4 +++ crates/ui-egui/src/windows.rs | 19 +++++++--- crates/ui-egui/tests/it/history_panel.rs | 44 ++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index d52bca3f..7429a617 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -747,6 +747,9 @@ pub struct UiState { /// 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, u64, usize, usize)>, + /// The History list's last height in points and whether the current step was on screen in it + #[serde(skip)] + pub history_view: Option<(f32, bool)>, /// The widest line of each Log tab, measured once per line #[serde(skip)] pub log_widths: crate::log_panel::LogWidthCache, @@ -919,6 +922,7 @@ impl Default for UiState { log_anchor: None, log_id: 0, history_shown: None, + history_view: None, log_widths: Default::default(), } } diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index 31299e0f..6078a4c7 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -262,18 +262,23 @@ pub(crate) fn history(app: &mut TextureCraftApp, ui: &mut Ui) { let done_count = done.len() as i64; 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 + // 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"), app.session.steps_added(), done.len(), undone.len())); - let follow = app.ui.history_shown != shown; + // A list made shorter keeps the current step on screen if it was there; one the user scrolled away from it stays put + let height = ui.available_height(); + let shrunk = app.ui.history_view.is_some_and(|(height_before, was_in_view)| was_in_view && height < height_before); + let follow = app.ui.history_shown != shown || shrunk; app.ui.history_shown = shown; // A click on a step goes back (or forward) to it: steps to undo, or to redo when negative let mut jump = 0i64; - let area = egui::ScrollArea::vertical().id_salt("history").auto_shrink([false, false]); - crate::log_panel::panel_rows(ui, area, HISTORY_ROW_HEIGHT, total_rows, |ui, range| { + // Not animated: a scroll request then shows in the offset returned this frame, which a resize by dragging relies on + let area = egui::ScrollArea::vertical().id_salt("history").auto_shrink([false, false]).animated(false); + let current_row_top = done.len() as f32 * HISTORY_ROW_HEIGHT; + let list = crate::log_panel::panel_rows(ui, area, HISTORY_ROW_HEIGHT, total_rows, |ui, range| { if follow { - let top = ui.max_rect().top() + (done.len() as f32 - range.start as f32) * HISTORY_ROW_HEIGHT; + let top = ui.max_rect().top() + current_row_top - range.start as f32 * HISTORY_ROW_HEIGHT; let current = egui::Rect::from_x_y_ranges(ui.max_rect().x_range(), top..=top + HISTORY_ROW_HEIGHT); ui.scroll_to_rect_animation(current, None, egui::style::ScrollAnimation::none()); } @@ -295,6 +300,10 @@ pub(crate) fn history(app: &mut TextureCraftApp, ui: &mut Ui) { } }); + let current_top_in_view = current_row_top - list.state.clamped_offset().y; + let current_in_view = current_top_in_view < list.inner_rect.height() && current_top_in_view + HISTORY_ROW_HEIGHT > 0.0; + app.ui.history_view = Some((height, current_in_view)); + if jump > 0 { app.run("edit.undo", json!({ "steps": jump })); } else if jump < 0 { diff --git a/crates/ui-egui/tests/it/history_panel.rs b/crates/ui-egui/tests/it/history_panel.rs index 2880feac..ade144d7 100644 --- a/crates/ui-egui/tests/it/history_panel.rs +++ b/crates/ui-egui/tests/it/history_panel.rs @@ -1,14 +1,15 @@ //! The History panel: a click on a step goes back or forward to it, the current step is marked and kept in view +use craft_behavior::dock::Action; use egui::accesskit::Toggled; -use egui::{Event, Key, Modifiers, vec2}; +use egui::{Event, Key, Modifiers, Rect, pos2, vec2}; use egui_kittest::Harness; use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::json; use texturecraft_engine::geom::glam::Vec3; use texturecraft_engine::{Session, execute}; use texturecraft_ui_egui::TextureCraftApp; -use texturecraft_ui_egui::docking::Panel; +use texturecraft_ui_egui::docking::{Dock, Panel, permissions}; /// Show document history so row clicks exercise the same command dispatch as the application fn history_harness(session: Session) -> Harness<'static, TextureCraftApp> { @@ -326,6 +327,45 @@ fn a_repeated_step_shows_past_the_history_cap() { assert!(current_shown(&harness, "Rename layer")); } +/// A History window short enough to cut many rows off the list +const SHORT_HEIGHT: f32 = 220.0; + +/// Show History in a floating window `height` points tall, as dragging its bottom edge does +fn set_history_height(harness: &mut Harness<'_, TextureCraftApp>, height: f32) { + let rect = Rect::from_min_size(pos2(960.0, 190.0), vec2(300.0, height)); + let mut workspace = harness.state().ui.dock.workspace().clone(); + workspace.apply_all([Action::float(Panel::History, rect)], false, permissions).unwrap(); + harness.state_mut().ui.dock = Dock::from_value(serde_json::to_value(workspace).unwrap()); +} + +/// A list shortened on every frame, as a resize by dragging does, keeps the current step on screen without a new step +#[test] +fn a_history_shortened_over_several_frames_keeps_the_current_step_on_screen() { + let mut harness = history_harness(long_session(STEPS_PAST_THE_PANEL)); + + // Each height is more than a row shorter than the one before, from the 380 points the window opens at + for height in [340.0, 300.0, 260.0, SHORT_HEIGHT] { + set_history_height(&mut harness, height); + harness.step(); + } + harness.run_steps(4); + + assert!(current_shown(&harness, "Rename layer")); +} + +/// A list the user scrolled away from the current step stays there when it is made shorter +#[test] +fn shortening_a_scrolled_up_history_keeps_the_scroll() { + let mut harness = history_harness(long_session(STEPS_PAST_THE_PANEL)); + scroll_to_top(&mut harness); + + set_history_height(&mut harness, SHORT_HEIGHT); + harness.run_steps(4); + + assert!(!current_shown(&harness, "Rename layer")); + assert!(harness.query_by_label("Project opened").is_some()); +} + /// 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() {