History panel: scroll to the current state on document change.

Fixes storytold/photocraft#5909 when new edits land off-screen in a short dock.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
nrq6993-a11yandCursor committed 2026-10-15 13:40:39 +03:00
1 parent df055f3dcb
commit b18f6ea008
2 files changed
+39

No files matched your search

@@ -58,3 +58,31 @@ fn clicking_a_state_jumps_there_with_one_command() {
h.run_steps(2);
assert_eq!(state(&h), (4, 0, rev + 2));
}
#[test]
fn new_history_steps_scroll_the_current_state_into_view() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
for n in 0..18 {
app.run("layer.new.layer", json!({"name": format!("Layer {n}")})).unwrap();
}
let mut ready = false;
let mut h = Harness::builder().with_size(vec2(280.0, 120.0)).build_ui_state(
move |ui, app: &mut PhotocraftApp| {
if !ready {
PhotocraftApp::setup_context(ui.ctx(), theme::ThemeKind::ProMedium);
ready = true;
return;
}
history(app, ui);
},
app,
);
h.run_steps(4);
let row = h.get_all_by_label("New Layer").last().expect("the latest New Layer state").rect();
let view = h.ctx.input(|i| i.viewport_rect());
assert!(
view.contains_rect(row),
"the current history row must scroll into view: row {row:?} view {view:?}",
);
}
+11
View File
@@ -3088,6 +3088,7 @@ fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
return;
};
let doc = st.doc.clone();
let revision = st.revision;
let entries = st.history.entries();
let current = entries.len() - 1;
let redo: Vec<String> = st.history.redo_labels().map(str::to_string).collect();
@@ -3118,10 +3119,13 @@ fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
// The dock gives History a fixed height: the rows scroll above the footer.
let footer = if t.pro { widgets::footer_height(ui) + ui.spacing().item_spacing.y } else { 0.0 };
let max_h = (ui.available_height() - footer).max(40.0);
let follow_id = ui.id().with(("history-follow-rev", doc.id));
let follow = ui.ctx().data(|d| d.get_temp::<u64>(follow_id)) != Some(revision);
egui::ScrollArea::vertical().id_salt("history-rows").max_height(max_h).auto_shrink([false, false]).show(ui, |ui| {
// Snapshots head the list and scroll with the states, so many of them never push the
// footer out of a short dock.
snap_cmd = snapshot_rows(app, ui, selected_snap);
let mut current_rect = None;
for (i, (e, is_redo)) in all.enumerate() {
// The current state shows selected unless a snapshot is (Photoshop highlights one row).
let selected = i == current && selected_snap.is_none();
@@ -3132,6 +3136,9 @@ fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
if resp.drag_started() {
ui.ctx().data_mut(|d| d.insert_temp(history_drag_key(), HistoryDrag::State(i)));
}
if i == current {
current_rect = Some(rect);
}
if selected {
ui.painter().rect_filled(rect, if t.pro { 0.0 } else { t.radius_sm }, if t.pro { t.row_selected } else { t.accent_soft });
} else if resp.hovered() {
@@ -3161,6 +3168,10 @@ fn history(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ctx.data_mut(|d| d.remove::<SelectedSnapshot>(selected_snapshot_key(doc.id)));
}
}
if follow && let Some(rect) = current_rect {
ui.scroll_to_rect(rect, Some(egui::Align::BOTTOM));
}
ui.ctx().data_mut(|d| d.insert_temp(follow_id, revision));
});
if let Some(drag) = drag
&& let Some(p) = ctx.input(|i| i.pointer.interact_pos())