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.
This commit is contained in:
DarkSky committed 2026-12-03 09:55:15 +09:00
1 parent 16c509a356
commit 92b166dace
2 files changed
+84 -7

No files matched your search

@@ -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()
@@ -565,10 +565,15 @@ fn variable_rows_keep_pixel_scroll_independent_of_wheel_step_size() {
}
}
/// Adds `count` H2 headings `outline-heading-<i>` to the open showcase document.
fn outline_state(count: usize) -> (DesktopAppState, Vec<String>) {
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-<i>` to the document.
fn add_outline_headings(state: &mut DesktopAppState, doc: &str, count: usize) -> Vec<String> {
let entry = state.store.docs.get_mut(doc).unwrap();
let ids: Vec<String> = (0..count).map(|i| format!("outline-heading-{i}")).collect();
entry
.session
@@ -590,7 +595,7 @@ fn outline_state(count: usize) -> (DesktopAppState, Vec<String>) {
.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])));
}