From 92b166daced29a2a4eeef9cb89f90a70ad2955ad Mon Sep 17 00:00:00 2001 From: DarkSky Date: Thu, 3 Dec 2026 09:25:11 +0900 Subject: [PATCH] fix(desktop): keep the outline position when the list shrinks Save the clamped offset when the heading list shrinks. Use a static name and an integer hash for the scroll state key. --- .../desktop/src/views_extra/outline_rail.rs | 12 ++- .../frontend/desktop/src/views_extra/tests.rs | 79 ++++++++++++++++++- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/packages/frontend/desktop/src/views_extra/outline_rail.rs b/packages/frontend/desktop/src/views_extra/outline_rail.rs index a13a5d1e14..5fa084b090 100644 --- a/packages/frontend/desktop/src/views_extra/outline_rail.rs +++ b/packages/frontend/desktop/src/views_extra/outline_rail.rs @@ -174,14 +174,18 @@ impl RenderOnce for OutlineRail { .min(self.height - 2.0 * PANEL_PADDING - 2.0); let max_scroll = list.total_height() - list_height; let scroll = window.use_keyed_state( - format!( - "desktop.outline.rail.scroll.{}/{}/{}", - self.workspace, self.pane, self.doc + ElementId::NamedInteger( + SharedString::new_static("desktop.outline.rail.scroll"), + ui::hash(&(&self.workspace, self.pane, &self.doc)), ), app, |_, _| 0.0_f32, ); - let offset = ScrollAxis::new(*scroll.read(app), max_scroll).offset; + let stored_offset = *scroll.read(app); + let offset = ScrollAxis::new(stored_offset, max_scroll).offset; + if offset != stored_offset { + scroll.update(app, |stored_offset, _| *stored_offset = offset); + } let slice = list.window(list_height, offset, OVERSCAN_ROWS * ROW_HEIGHT); let mut rows = div() .relative() diff --git a/packages/frontend/desktop/src/views_extra/tests.rs b/packages/frontend/desktop/src/views_extra/tests.rs index 5db7668e17..962b36566e 100644 --- a/packages/frontend/desktop/src/views_extra/tests.rs +++ b/packages/frontend/desktop/src/views_extra/tests.rs @@ -565,10 +565,15 @@ fn variable_rows_keep_pixel_scroll_independent_of_wheel_step_size() { } } -/// Adds `count` H2 headings `outline-heading-` to the open showcase document. fn outline_state(count: usize) -> (DesktopAppState, Vec) { let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02"); - let entry = state.store.docs.get_mut("doc-arch").unwrap(); + let ids = add_outline_headings(&mut state, "doc-arch", count); + (state, ids) +} + +/// Adds `count` H2 headings `outline-heading-` to the document. +fn add_outline_headings(state: &mut DesktopAppState, doc: &str, count: usize) -> Vec { + let entry = state.store.docs.get_mut(doc).unwrap(); let ids: Vec = (0..count).map(|i| format!("outline-heading-{i}")).collect(); entry .session @@ -590,7 +595,7 @@ fn outline_state(count: usize) -> (DesktopAppState, Vec) { .unwrap(); entry.snapshot = entry.session.read_page_snapshot().unwrap(); entry.revision = entry.session.revision(); - (state, ids) + ids } #[test] @@ -706,3 +711,71 @@ fn outline_rail_mounts_a_bounded_window_of_512_headings_while_it_scrolls_and_rev app.hover(&rail); } } + +#[test] +fn outline_rail_keeps_first_visible_heading_when_the_list_shrinks_and_grows() { + let mut state = DesktopAppState::seed_showcase(ThemeMode::Light, "2026-12-02"); + let doc = state.store.create_doc("Outline", DocMode::Page, None, 10); + let ids = add_outline_headings(&mut state, &doc, 90); + state.open_doc_in_active_pane(&doc); + let pane = state.split.active().id; + let app = Shell::with_state(1280, state); + let prefix = format!("desktop.outline.rail.heading.{pane}."); + let first_visible_heading = || { + app.update(|_, window, _| { + window + .frame_snapshot() + .elements + .iter() + .filter(|(id, row)| { + id.starts_with(&prefix) + && row.bounds.y + row.bounds.height > row.content_mask.y + && row.bounds.y < row.content_mask.y + row.content_mask.height + }) + .min_by(|(_, a), (_, b)| a.bounds.y.total_cmp(&b.bounds.y)) + .expect("visible outline heading") + .0 + .clone() + }) + }; + let set_tail_type = |kind: &str, now| { + app.update(|shell, _, cx| { + let entry = shell.state.store.docs.get_mut(&doc).unwrap(); + entry + .session + .structural_edit(now, |page| { + for id in &ids[40..] { + page.set_block_prop( + id, + "prop:type", + y_octo::SessionValue::Any(y_octo::Any::String(kind.into())), + now, + )?; + } + Ok(()) + }) + .unwrap(); + entry.snapshot = entry.session.read_page_snapshot().unwrap(); + entry.revision = entry.session.revision(); + cx.notify(); + }); + app.window.request_frame(); + }; + + app.hover(&format!("desktop.outline.rail.{pane}")); + let center = app.position(&format!("desktop.outline.rail.panel.{pane}")); + app.scroll(center.x.into(), center.y.into(), -1.0e6); + let bottom = first_visible_heading(); + let last = format!("{prefix}{}", ids[89]); + assert!(app.contains(&last)); + + set_tail_type("text", 1000); + let shortened = first_visible_heading(); + assert_ne!(shortened, bottom); + assert!(!app.contains(&last)); + assert!(app.contains(&format!("{prefix}{}", ids[39]))); + + set_tail_type("h2", 2000); + assert_eq!(first_visible_heading(), shortened); + assert!(app.contains(&format!("{prefix}{}", ids[40]))); +}