diff --git a/crates/pages/src/ui/view.rs b/crates/pages/src/ui/view.rs index 26d87f498..1c963c0d5 100644 --- a/crates/pages/src/ui/view.rs +++ b/crates/pages/src/ui/view.rs @@ -278,9 +278,18 @@ pub struct PageViewOutput { /// pasteboard colour menu at [`PasteboardOutput::context_at`]; a page name click is not used here. pub pasteboard: Option, frame: Frame, + resident: usize, } impl PageViewOutput { + /// How many page pictures the view holds after this pass: its pages' own pictures and the pictures of the part + /// in view of pages larger than the texture side, shown or kept for later, as their stores count them. The + /// stores evict the pictures drawn longest ago past their budget, so the count stays bounded however many pages + /// the view scrolls through. 0 for a pass that drew nothing. + pub fn resident_pictures(&self) -> usize { + self.resident + } + /// Takes the view a tool host reported (`ToolHostResponse::view`, from the Hand and Zoom tools) into `state`: /// the scroll offset, and the zoom when it changed (a fit ends). In a continuous view the current page follows /// the offset. @@ -485,6 +494,7 @@ impl<'a> PageView<'a> { toggle_white_space, pasteboard: seen.pasteboard, frame: seen.frame, + resident: seen.resident, } } } @@ -508,6 +518,8 @@ struct Seen { visible: Range, current_changed: bool, pasteboard: Option, + /// The page pictures held. + resident: usize, /// What the caller stores after the real run. next: Option, } @@ -532,6 +544,7 @@ impl Seen { visible: 0..0, current_changed: false, pasteboard: None, + resident: 0, next: None, } } @@ -768,10 +781,11 @@ fn run_pass(ui: &mut Ui, cx: &mut BodyCx<'_>, held: &mut Held<'_, PageViewState> }) .collect(); let pictures = PagePictures::new(source, &pages, &near, paper).label(words.text.clone()).hold(zooming || ratio != 1.0); - match describer { + let resident = match describer { Some(describer) => pictures.describer(describer).show_at(ui, cx.rect), None => pictures.show_at(ui, cx.rect), - }; + } + .resident; // The scroll bars come before the clamp, so a drag of this pass lands in this pass. let document = drawn_frame.document(&drawn); @@ -806,6 +820,7 @@ fn run_pass(ui: &mut Ui, cx: &mut BodyCx<'_>, held: &mut Held<'_, PageViewState> visible, current_changed, pasteboard: Some(pasteboard), + resident, next: Some(Next { started, state, mem, edit: words.edit.as_str().to_owned() }), }) } diff --git a/crates/pages/src/ui/view/pictures.rs b/crates/pages/src/ui/view/pictures.rs index be3a5a747..e62061ac2 100644 --- a/crates/pages/src/ui/view/pictures.rs +++ b/crates/pages/src/ui/view/pictures.rs @@ -152,7 +152,7 @@ impl Component for PagePictures<'_> { const SHOWS_LABEL: bool = false; const OVERLAYS: bool = false; type Style = PagePicturesStyle; - type Output = Response; + type Output = PagePicturesOutput; fn style(_: &StyleCx<'_>, _: PagePicturesStyleKey) -> PagePicturesStyle { PagePicturesStyle { metrics: Metrics::new() } @@ -172,11 +172,7 @@ impl Component for PagePictures<'_> { let base = painter.painter.clone(); let inside = base.clip_rect(); let side = texture_side(&ctx); - let options = ThumbnailOptions::new().max_side(side).store(TextureStoreOptions::pages()); - let thumbs = Thumbnails::::in_context(&ctx, cx.key("pictures"), options.clone()); - // A page larger than the texture side shows the part in view from a second picture of its own (its own - // store, so a page's two regions never share a key). - let details = Thumbnails::::in_context(&ctx, cx.key("detail"), options); + let PictureStores { thumbs, details } = PictureStores::of(&ctx, cx); thumbs.hold(self.hold); details.hold(self.hold); let ppp = painter.pixels_per_point(); @@ -213,8 +209,45 @@ impl Component for PagePictures<'_> { } } - fn finish(self, cx: Cx<'_, Self>) -> Response { - cx.response.clone() + fn finish(self, cx: Cx<'_, Self>) -> PagePicturesOutput { + let ctx = cx.response.ctx.clone(); + let PictureStores { thumbs, details } = PictureStores::of(&ctx, &cx); + let resident = thumbs.cache().statistics(&ctx).entries + details.cache().statistics(&ctx).entries; + PagePicturesOutput { response: cx.response.clone(), resident } + } +} + +/// What the pictures report: how many page pictures their stores hold. +#[derive(Debug)] +pub(super) struct PagePicturesOutput { + response: Response, + /// The entries of both stores: the pages' own pictures and the detail pictures. + pub(super) resident: usize, +} + +impl Outcome for PagePicturesOutput { + type Value = NoValue; + + fn response(&self) -> &Response { + &self.response + } +} + +/// The two stores of the page pictures, kept in the context under the component's id: the pages' own pictures, and +/// the pictures of the part in view of a page larger than the texture side (a store of their own, so a page's two +/// regions never share a key). +struct PictureStores { + thumbs: Thumbnails, + details: Thumbnails, +} + +impl PictureStores { + fn of(ctx: &egui::Context, cx: &Cx<'_, PagePictures<'_>>) -> Self { + let options = ThumbnailOptions::new().max_side(texture_side(ctx)).store(TextureStoreOptions::pages()); + PictureStores { + thumbs: Thumbnails::in_context(ctx, cx.key("pictures"), options.clone()), + details: Thumbnails::in_context(ctx, cx.key("detail"), options), + } } } diff --git a/crates/pages/tests/it/views.rs b/crates/pages/tests/it/views.rs index d6b292fda..abc677037 100644 --- a/crates/pages/tests/it/views.rs +++ b/crates/pages/tests/it/views.rs @@ -12,6 +12,7 @@ use craft_pages::ui::{ use craft_ui::canvas::{CanvasTool, CropOverlay, ToolAction, ToolContext, ToolHost, ToolPath, ToolPointer}; use craft_ui::contract::{Bind, Role, Show as _}; +use craft_ui::primitives::textures::TextureStoreOptions; use craft_ui::primitives::{Draw, EditorPurpose}; use craft_ui::runtime::env::Env; use craft_ui::testing::{Fixture, pointer, trial}; @@ -64,6 +65,39 @@ fn a_changed_zoom_keeps_the_page_at_the_top_in_place() { assert!((offset - (third.y0 + 0.4 * third.height())).abs() < 1.0, "offset {offset}, page {third:?}"); } +#[trial] +fn the_pictures_a_view_holds_stay_bounded_as_it_scrolls_through_many_pages() { + let budget = TextureStoreOptions::pages().budget.max_entries; + let count = 4 * budget; + let state = Rc::new(RefCell::new(state_with(|s| { + s.continuous = true; + s.fit = Fit::None; + s.zoom = 1.0; + }))); + // The most pictures held and the most pages drawn in one pass. + let most = Rc::new(Cell::new((0_usize, 0_usize))); + let (kept, seen) = (Rc::clone(&state), Rc::clone(&most)); + let pages = DemoPages::new(count); + let mut fixture = Fixture::new(move |ui| { + let mut now = kept.borrow().clone(); + let out = PageView::new("Pages", &pages, &mut now, PAPER).options(LayoutOptions::ACROBAT).show(ui, |_, _| ()); + let (held, drawn) = seen.get(); + seen.set((held.max(out.resident_pictures()), drawn.max(out.pages.len()))); + *kept.borrow_mut() = now; + }); + fixture.room(vec2(900.0, 600.0)); + fixture.settle(); + for page in 0..count { + state.borrow_mut().go_to(page); + fixture.idle(2); + } + let (held, drawn) = most.get(); + assert!(drawn > 0, "the view drew pages"); + // The pages of the last two passes, in view and one row either side, are never evicted. + let bound = budget + 2 * (drawn + 2); + assert!(held > 0 && held <= bound, "{held} pictures held over {count} pages scrolled through, at most {bound} expected"); +} + /// A wheel step of `lines` over the middle of the room. fn wheel(fixture: &mut Fixture, lines: f32) { let middle = pos2(450.0, 300.0);