From 865572c2d1e43b88079a53bf84493052a172a642 Mon Sep 17 00:00:00 2001 From: Leif Eggers Rossau <136928112+leifrossau@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:01:12 +0100 Subject: [PATCH 1/2] feat(ui-egui): open where the window was left The window came back to the page it last remembered, but a page in a notebook kept outside the notebooks folder (one in a synced folder, say) was never remembered: its path was only ever stored relative to the notebooks folder, so the window kept opening an old page instead. It is now remembered by its whole path, and the tree opens down to it. It also comes back at the zoom it was left at and scrolled to the same place on the page, both kept when the window closes. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/app.rs | 145 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 89ea5732..6ef1904c 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -44,6 +44,9 @@ const PLAIN_SNAP_STEP: f32 = 12.0; const AUTOSAVE_AFTER: Duration = Duration::from_millis(900); #[cfg(feature = "voice")] mod voice; +/// The zoom, and how far down the last page was scrolled, when the window was closed. +const LAST_ZOOM_KEY: &str = "last_zoom"; +const LAST_SCROLL_KEY: &str = "last_scroll"; /// The three-bar button: what OneNote puts at the top left of the navigation pane. const BARS: &str = "\u{2630}"; /// The ArtCraft community chat. Opened in the browser, never fetched by the app. @@ -1261,6 +1264,11 @@ pub struct NoteCraftApp { focus_search: bool, /// The page the window closed on has been opened again (once per run). restored: bool, + /// How far down the open page is scrolled, as last drawn, and how far down it should be + /// scrolled once it is drawn, coming back to where the window was left, with how many + /// frames that is still tried for (a page's height is known only once it is drawn). + page_scroll: f32, + restore_scroll: Option<(f32, u32)>, /// Recovery warnings that were already shown during this run. icon_recovery_reported: HashSet, /// Suppress further missing-icon warnings during this run. @@ -1421,6 +1429,8 @@ impl NoteCraftApp { ribbon_peek: false, band_bottom: 0.0, zoom_percent: 100, + page_scroll: 0.0, + restore_scroll: None, fit_page_width: false, theme_window: false, pending_copy: None, @@ -1531,31 +1541,70 @@ impl NoteCraftApp { return; } self.restored = true; + // The zoom the window was left at. + if let Some(zoom) = self.session.settings.get(LAST_ZOOM_KEY).and_then(|z| z.trim().parse::().ok()).filter(|z| (50..=250).contains(z)) { + self.zoom_percent = zoom; + } let Some(saved) = self.session.settings.get("last_page") else { return }; - let path = self.session.root().join(saved.trim().replace('/', std::path::MAIN_SEPARATOR_STR)); + let saved = saved.trim(); + // A page in a notebook kept outside the notebooks folder is remembered by its whole path. + let path = if Path::new(saved).is_absolute() { + PathBuf::from(saved) + } else { + self.session.root().join(saved.replace('/', std::path::MAIN_SEPARATOR_STR)) + }; if !path.is_dir() { return; } - // Open every container above it, so the page is visible in the tree. + // Open every container above it, up to the notebook it is in, so the page is visible in + // the tree. + let notebooks: Vec = self.session.tree().iter().map(|n| n.path.clone()).collect(); let mut at = path.parent(); while let Some(dir) = at { - if !dir.starts_with(self.session.root()) || dir == self.session.root() { + if dir == self.session.root() { break; } self.open.insert(dir.to_path_buf()); + if notebooks.iter().any(|n| n == dir) { + break; + } at = dir.parent(); } self.open_page(&path); + // Scrolled to where it was. + if let Some(y) = self.session.settings.get(LAST_SCROLL_KEY).and_then(|y| y.trim().parse::().ok()).filter(|y| y.is_finite() && *y > 0.0) { + self.restore_scroll = Some((y, 60)); + } + } + + /// What the window keeps of where it was left when it closes: the open page written, the zoom, + /// and how far down the page is scrolled. + pub fn before_close(&mut self) { + self.save_now(); + if self.secondary_window { + return; + } + let _ = self.session.settings.set(LAST_ZOOM_KEY, &self.zoom_percent.to_string()); + let _ = self.session.settings.set(LAST_SCROLL_KEY, &format!("{:.0}", self.page_scroll.max(0.0))); } /// Remembers which page is open, for the next time the window starts. fn remember_last_page(&mut self, dir: &Path) { + // A note window of its own is not where the window was left. if self.secondary_window { return; } - let Ok(rel) = dir.strip_prefix(self.session.root()) else { return }; - let value = rel.to_string_lossy().replace('\\', "/"); + // Inside the notebooks folder by its place there; a notebook kept elsewhere (a synced + // folder, say) by the page's whole path, which was not remembered at all before. + let value = match dir.strip_prefix(self.session.root()) { + Ok(rel) => rel.to_string_lossy().replace('\\', "/"), + Err(_) => dir.to_string_lossy().into_owned(), + }; let _ = self.session.settings.set("last_page", &value); + // A new page starts at its top. + if self.restore_scroll.is_none() { + self.page_scroll = 0.0; + } } /// Marks a host whose notebook files disappear with the current browser tab. @@ -8595,6 +8644,9 @@ impl NoteCraftApp { let mut recognised: Option<&'static str> = None; let mut page_rect = None; let mut hbar = crate::hscroll::Shown::default(); + // Coming back to where the window was left: the page is scrolled there once it is drawn. + let restore_scroll = if self.editor.is_some() { self.restore_scroll } else { None }; + let mut scroll_now: Option = None; // What the page's right-click menu asked for, run once the page's borrow is over. let mut page_acts: Vec = Vec::new(); let collect_now = std::mem::take(&mut self.collect_pending); @@ -8997,7 +9049,11 @@ impl NoteCraftApp { ..egui::containers::scroll_area::ScrollSource::default() }; let page_outer = ui.available_rect_before_wrap(); - let shown = egui::ScrollArea::vertical().id_salt("page-main").auto_shrink([false, false]).scroll_source(scroll).show(ui, |ui| { + let mut area = egui::ScrollArea::vertical().id_salt("page-main").auto_shrink([false, false]).scroll_source(scroll); + if let Some((y, _)) = restore_scroll { + area = area.vertical_scroll_offset(y); + } + let shown = area.show(ui, |ui| { // On a touch screen two fingers scroll the page. One finger writes, // selects and moves boxes, as the mouse does, so it cannot scroll too. if let Some(touch) = ui.input(|i| i.multi_touch()) { @@ -9030,6 +9086,7 @@ impl NoteCraftApp { let reserve = if vertical { ui.spacing().scroll.bar_width + ui.spacing().scroll.bar_outer_margin } else { 0.0 }; hbar = crate::hscroll::show(ui.ctx(), egui::Id::new("page-hscroll"), view, span, reserve); } + scroll_now = Some(shown.state.offset().y); } if changed { Self::mark_dirty(ed); @@ -9139,6 +9196,11 @@ impl NoteCraftApp { if space_over { self.space_tool = false; } + if let Some(y) = scroll_now { + self.page_scroll = y; + // Tried until the page is there, which may take a frame or two to be tall enough. + self.restore_scroll = self.restore_scroll.and_then(|(want, left)| ((y - want).abs() > 1.0 && left > 0).then_some((want, left - 1))); + } for act in page_acts { self.ribbon_act(act); } @@ -11537,7 +11599,7 @@ impl NoteCraftApp { // Closing the window writes the page first, and waits for an autosave still being // written: what was typed in the moment before closing is not lost. if ctx.input(|i| i.viewport().close_requested()) { - self.save_now(); + self.before_close(); if self.is_dirty() || self.page_conflict { ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); } @@ -12318,6 +12380,75 @@ mod alert_tests { (t, page, pdf, h) } + /// The window opens where it was left: the same page, even in a notebook kept outside the + /// notebooks folder (the one in a synced folder, say), at the same zoom and scrolled to the + /// same place. A page outside the notebooks folder was never remembered at all, so the window + /// always came back to whatever page it had last remembered inside it. + #[test] + fn the_window_opens_where_it_was_left() { + let t = notecraft_store::testing::TempDir::new("reopen-where-left"); + let settings = notecraft_store::Settings::at(t.path().join("settings.toml")); + settings.set("privacy_welcome_seen", "true").expect("settings"); + let mut session = Session::new(&t.path().join("notebooks"), settings.clone()).expect("session"); + notecraft_engine::sample::create_sample(&mut session, None).expect("sample"); + // A notebook elsewhere, opened beside the others, with a long page in it. + let away = t.path().join("away"); + std::fs::create_dir_all(&away).expect("dir"); + let mut other = Session::new(&away, settings.clone()).expect("other session"); + let Ok(Output::Path(made)) = other.run("notebook.create", ¶ms(&[("name", "Elsewhere")])) else { panic!("a notebook") }; + session.run("notebook.open", ¶ms(&[("path", &made.to_string_lossy())])).expect("open it"); + let page = made.join("new-section-1").join("untitled-page"); + let lines: String = (1..=120).map(|n| format!("

line {n}

")).collect(); + session.run("page.save", ¶ms(&[("path", &page.to_string_lossy()), ("body", &lines)])).expect("a long page"); + + let harness = |app: NoteCraftApp| { + egui_kittest::Harness::builder() + .with_size(vec2(1200.0, 760.0)) + .with_accessibility_check(false) + .build_ui_state(|ui, app: &mut NoteCraftApp| app.show(ui), app) + }; + let mut h = harness(NoteCraftApp::new(session)); + for _ in 0..5 { + h.step(); + } + h.state_mut().open_path(&page.to_string_lossy()); + h.state_mut().zoom_percent = 150; + for _ in 0..5 { + h.step(); + } + // Scrolled down with the wheel, over the page. + h.input_mut().events.push(egui::Event::PointerMoved(egui::pos2(600.0, 400.0))); + for _ in 0..6 { + h.input_mut().events.push(egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: vec2(0.0, -200.0), + modifiers: Modifiers::NONE, + phase: egui::TouchPhase::Move, + source: egui::MouseWheelSource::Unknown, + }); + h.step(); + } + for _ in 0..10 { + h.step(); + } + let scrolled = h.state().page_scroll; + assert!(scrolled > 300.0, "the page is scrolled down: {scrolled}"); + // The window closes. + h.state_mut().before_close(); + drop(h); + + // And opens again, on the same settings. + let session = Session::new(&t.path().join("notebooks"), settings.clone()).expect("again"); + let mut h = harness(NoteCraftApp::new(session)); + for _ in 0..10 { + h.step(); + } + assert_eq!(h.state().editor.as_ref().map(|e| e.dir.clone()), Some(page.clone()), "the same page is open"); + assert_eq!(h.state().zoom_percent, 150, "at the same zoom"); + assert!((h.state().page_scroll - scrolled).abs() < 2.0, "scrolled to the same place: {scrolled} -> {}", h.state().page_scroll); + assert!(h.state().open.contains(&made), "and its notebook is open in the tree"); + } + /// Issue #67: with no box holding the caret, a second printout goes below what is already /// on the page, not over the first. #[test] From 29f47d5cd6f298a27b3b6e5717690e329878b7cb Mon Sep 17 00:00:00 2001 From: Leif Eggers Rossau <136928112+leifrossau@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:31:07 +0100 Subject: [PATCH 2/2] fix(ui-egui): reopen at the place left, and let the window rest The scroll was set before the page was tall enough, past its end. egui sprang it back with a great speed; the page coasted far from the place, and the scroll area kept asking for frames, so the window redrew sixty times a second with nothing happening. The place is now set once the page reaches it, tried for a few seconds while pictures arrive. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/app.rs | 43 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 6ef1904c..f38ae210 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -1268,6 +1268,8 @@ pub struct NoteCraftApp { /// scrolled once it is drawn, coming back to where the window was left, with how many /// frames that is still tried for (a page's height is known only once it is drawn). page_scroll: f32, + /// How far down the page could be scrolled, as last drawn. + page_offset_max: f32, restore_scroll: Option<(f32, u32)>, /// Recovery warnings that were already shown during this run. icon_recovery_reported: HashSet, @@ -1430,6 +1432,7 @@ impl NoteCraftApp { band_bottom: 0.0, zoom_percent: 100, page_scroll: 0.0, + page_offset_max: 0.0, restore_scroll: None, fit_page_width: false, theme_window: false, @@ -1573,7 +1576,9 @@ impl NoteCraftApp { self.open_page(&path); // Scrolled to where it was. if let Some(y) = self.session.settings.get(LAST_SCROLL_KEY).and_then(|y| y.trim().parse::().ok()).filter(|y| y.is_finite() && *y > 0.0) { - self.restore_scroll = Some((y, 60)); + // Pictures may take a while to arrive from a synced folder, and the page is only as + // tall as what has arrived: it is tried for a few seconds. + self.restore_scroll = Some((y, 300)); } } @@ -9050,7 +9055,12 @@ impl NoteCraftApp { }; let page_outer = ui.available_rect_before_wrap(); let mut area = egui::ScrollArea::vertical().id_salt("page-main").auto_shrink([false, false]).scroll_source(scroll); - if let Some((y, _)) = restore_scroll { + // Only once the page is tall enough to be scrolled that far: put past its end while + // it is still being laid out, the page sprang back from the end with a great speed, + // coasted on, and ended up far from where it was left, and the window kept drawing. + if let Some((y, _)) = restore_scroll + && y <= self.page_offset_max + 1.0 + { area = area.vertical_scroll_offset(y); } let shown = area.show(ui, |ui| { @@ -9087,6 +9097,7 @@ impl NoteCraftApp { hbar = crate::hscroll::show(ui.ctx(), egui::Id::new("page-hscroll"), view, span, reserve); } scroll_now = Some(shown.state.offset().y); + self.page_offset_max = (shown.content_size.y - shown.inner_rect.height()).max(0.0); } if changed { Self::mark_dirty(ed); @@ -12384,6 +12395,34 @@ mod alert_tests { /// notebooks folder (the one in a synced folder, say), at the same zoom and scrolled to the /// same place. A page outside the notebooks folder was never remembered at all, so the window /// always came back to whatever page it had last remembered inside it. + /// Coming back to a place further down than the page reaches yet (its pictures still on their + /// way, or the page made shorter meanwhile) does not throw the page past its end. Put there, + /// it sprang back with a great speed, coasted far from where it was left, and the window went + /// on drawing frame after frame with nothing happening. + #[test] + fn coming_back_further_down_than_the_page_reaches_leaves_the_window_still() { + let t = notecraft_store::testing::TempDir::new("reopen-past-the-end"); + let settings = notecraft_store::Settings::at(t.path().join("settings.toml")); + settings.set("privacy_welcome_seen", "true").expect("settings"); + let mut session = Session::new(&t.path().join("notebooks"), settings.clone()).expect("session"); + let sample = notecraft_engine::sample::create_sample(&mut session, None).expect("sample"); + let lines: String = (1..=60).map(|n| format!("

line {n}

")).collect(); + let page = sample.html_page.to_string_lossy().into_owned(); + session.run("page.save", ¶ms(&[("path", &page), ("body", &lines)])).expect("a page"); + settings.set("last_page", &page).expect("last page"); + settings.set(LAST_SCROLL_KEY, "100000").expect("far down"); + let mut h = egui_kittest::Harness::builder() + .with_size(vec2(1200.0, 760.0)) + .with_accessibility_check(false) + .build_ui_state(|ui, app: &mut NoteCraftApp| app.show(ui), NoteCraftApp::new(session)); + for _ in 0..20 { + h.step(); + } + let (at, end) = (h.state().page_scroll, h.state().page_offset_max); + assert!(at <= end + 1.0, "the page is not past its end: {at} of {end}"); + assert!(!h.ctx.has_requested_repaint(), "and with nothing happening the window asks for no more frames"); + } + #[test] fn the_window_opens_where_it_was_left() { let t = notecraft_store::testing::TempDir::new("reopen-where-left");