diff --git a/crates/pages/src/ui/thumbnails.rs b/crates/pages/src/ui/thumbnails.rs index 0c555b40c8..85287010b9 100644 --- a/crates/pages/src/ui/thumbnails.rs +++ b/crates/pages/src/ui/thumbnails.rs @@ -191,6 +191,9 @@ pub struct PageThumbnailsOutput { /// While files from the OS are held over the grid, the page they would land before (the page count after the /// last). pub file_gap: Option, + /// A middle-button auto-scroll is latched on the grid ([`PageThumbnails::middle_button`] with + /// `MiddleButton::AutoScroll`). + pub auto_scrolling: bool, } impl Outcome for PageThumbnailsOutput { @@ -209,7 +212,8 @@ impl Outcome for PageThumbnailsOutput { /// with the page label (`PageSource::page_label`). The current page is the grid's current tile; picked pages are /// its selected tiles. Pictures come from `Thumbnails` over [`PageSource`]. /// - Pointer (behaviour: Photoshop's panels, then PdfCraft, as the grid): a plain click goes to the page -/// ([`ThumbnailEvent::Go`]); Cmd/Ctrl-click toggles a page in the picked set and Shift-click extends it. +/// ([`ThumbnailEvent::Go`]) and picks it; Cmd/Ctrl-click toggles a page in the picked set and Shift-click extends +/// it. With [`picks_beside_current`](Self::picks_beside_current) the picked set is kept beside the current page. /// - Drag (when editable): the grid's reorder rule; the grid draws the insert line and scrolls near its edges. /// - Keys (one Tab stop, the grid's): arrows go to the next page, Shift extends, Home and End, Enter activates, /// Delete and Backspace delete (the current page when nothing is picked), Cmd/Ctrl+] and [ move the picked @@ -238,6 +242,7 @@ pub struct PageThumbnails<'a> { invert_middle: bool, id: IdSource, reveal: Option, + picks_beside_current: bool, } impl<'a> PageThumbnails<'a> { @@ -264,9 +269,20 @@ impl<'a> PageThumbnails<'a> { invert_middle: false, id: IdSource::Auto, reveal: None, + picks_beside_current: false, } } + /// Keeps the picked pages beside the current page (Acrobat's Pages panel): a plain click goes to its page and + /// empties the pick, and Cmd/Ctrl- and Shift-clicks change the pick without going to the page, the current page + /// counting as picked while nothing is. Off (PowerPoint's slide strip, Organize), a plain click picks the page it + /// goes to. + #[must_use] + pub fn picks_beside_current(mut self, on: bool) -> Self { + self.picks_beside_current = on; + self + } + /// How the thumbnails are arranged: the side strip, or the Organize grid. #[must_use] pub fn arrangement(mut self, arrangement: ThumbnailArrangement) -> Self { @@ -403,6 +419,7 @@ impl<'a> PageThumbnails<'a> { invert_middle, id, reveal, + picks_beside_current, } = self; let organize = arrangement == ThumbnailArrangement::Organize; let env = Env::of(ui); @@ -444,6 +461,7 @@ impl<'a> PageThumbnails<'a> { .zoom(grid_zoom) .caption(caption) .current(Some(current)) + .picks_beside_current(picks_beside_current) .groups(&groups) .tail(&tail); if let Some(page) = reveal { @@ -550,7 +568,8 @@ impl<'a> PageThumbnails<'a> { _ => {} } } - PageThumbnailsOutput { response: shown.response.response, events, drop_target, file_gap } + let auto_scrolling = shown.response.auto_scrolling; + PageThumbnailsOutput { response: shown.response.response, events, drop_target, file_gap, auto_scrolling } } } diff --git a/crates/pages/src/ui/thumbnails/stories.rs b/crates/pages/src/ui/thumbnails/stories.rs index 3fbde6eff8..d2543a420e 100644 --- a/crates/pages/src/ui/thumbnails/stories.rs +++ b/crates/pages/src/ui/thumbnails/stories.rs @@ -23,6 +23,14 @@ fn strip_with_sections_and_a_picked_range(ui: &mut Ui, data: &mut StoryData) -> PageThumbnails::new("Pages", &pages, picked, 4, PAPER).size(zoom).sections(§ions).marks(&marks).new_page_row(true).show(ui).into() } +/// Acrobat's Pages panel: the picked pages beside the current one, which a plain click empties. +#[story(PageThumbnails<'static>, covers = [], drive = Drive::Never)] +fn strip_with_picks_beside_the_current_page(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let (picked, zoom) = data.value("state", (Selection::new(vec![1_usize, 5], Some(5), Some(1)), 1.0_f64)); + let pages = DemoPages::new(PAGES); + PageThumbnails::new("Pages", &pages, picked, 3, PAPER).size(zoom).picks_beside_current(true).show(ui).into() +} + /// The actions in each page's band in the Organize stories. fn organize_actions() -> [(IconId, CommandRef); 2] { [(IconId::new("rotate_cw"), CommandRef::new("pages.rotate")), (IconId::new("trash"), CommandRef::new("pages.delete"))] diff --git a/crates/pages/src/ui/view.rs b/crates/pages/src/ui/view.rs index 61c256e1f2..fdeea54e8d 100644 --- a/crates/pages/src/ui/view.rs +++ b/crates/pages/src/ui/view.rs @@ -319,7 +319,7 @@ impl PageViewOutput { let size = view.rect.size(); state.offset = frame.offset_of(view.center, f64::from(size.x), f64::from(size.y)); if state.continuous && !self.layout.rects.is_empty() { - state.current = page_on_line(&self.layout, state.offset.1 + f64::from(size.y) / 2.0); + state.current = shown_most(&self.layout, state.current, state.offset.1, f64::from(size.y)); } } } @@ -861,7 +861,7 @@ impl Pending { let offset = (want.0.clamp(xs.0, xs.1), want.1.clamp(ys.0, ys.1)); state.offset = offset; if state.continuous && count > 0 && !went && mem.offset.is_some_and(|last| last != offset) { - state.current = page_on_line(&geo.layout, offset.1 + vh / 2.0); + state.current = shown_most(&geo.layout, state.current, offset.1, vh); } mem.offset = Some(offset); mem.layout = build.cache.borrow().clone(); @@ -1254,13 +1254,32 @@ fn centred_offset(old: &Placed, new: &Placed, offset: (f64, f64), old_view: (f64 Some((r.x0 + fx * r.width() - vw / 2.0, r.y0 + fy * r.height() - below - vh / 2.0)) } -/// The page that owns the horizontal line `y` (content coordinates); a tie at a shared edge goes half a point to the -/// earlier page. -fn page_on_line(layout: &PagedLayout, y: f64) -> usize { - let last = layout.rects.len().saturating_sub(1); - layout.rects.iter().zip(&layout.bands).position(|(r, &(_, bottom))| r.y0 + bottom + 0.5 > y).unwrap_or(last) +/// The current page of a continuous view whose top is at `top` (content coordinates) and `height` tall: the page that +/// shows the most of its height, the topmost of those within [`PAGE_TIE`]. The page `current` was stays current +/// while it shows as much as that page, or shows whole: a short page gone to keeps the mark from a taller one below. +// pdfcraft: crates/ui-egui/src/canvas.rs:1763 `draw_page` (best overlap), :2065 (the current page stays). +fn shown_most(layout: &PagedLayout, current: usize, top: f64, height: f64) -> usize { + let shown = |page: usize| { + let band = band_rect(layout, page); + ((band.y1.min(top + height) - band.y0.max(top)).max(0.0), band.height()) + }; + let mut best = (current, -1.0); + for page in 0..layout.rects.len() { + let (seen, _) = shown(page); + if seen > best.1 + PAGE_TIE { + best = (page, seen); + } + } + match (current < layout.rects.len()).then(|| shown(current)) { + Some((seen, whole)) if seen >= best.1 - PAGE_TIE || seen >= whole - PAGE_TIE => current, + _ => best.0, + } } +/// Shown heights (screen points) closer than this are equal: one page size can meet the view a fraction of a point +/// differently once the offset is applied. +const PAGE_TIE: f64 = 0.5; + /// Zoom by `delta` keeping the page point under `pointer` where it is. fn zoom_about(build: &Geometry, state: &mut PageViewState, old: &Placed, rect: Rect, pointer: egui::Pos2, delta: f64) -> Arc { let inside = (f64::from(pointer.x - rect.min.x), f64::from(pointer.y - rect.min.y)); diff --git a/crates/pages/tests/it/thumbnails.rs b/crates/pages/tests/it/thumbnails.rs index 392999f734..34b410d891 100644 --- a/crates/pages/tests/it/thumbnails.rs +++ b/crates/pages/tests/it/thumbnails.rs @@ -126,3 +126,116 @@ fn an_organize_check_toggles_its_page_without_modifiers_and_goes_to_it() { let picked = picked.borrow(); assert!(picked.contains(&0) && picked.contains(&1) && !picked.contains(&2), "both checked pages are picked: {picked:?}"); } + +/// Each open of the Organize grid reveals the page the host asks for, once: the second open too, after a click +/// picked a page of the first and the grid was hidden in between. +#[trial] +fn every_open_of_the_grid_reveals_the_asked_page() { + let open = Rc::new(std::cell::Cell::new(true)); + let reveal = Rc::new(std::cell::Cell::new(Some(30_usize))); + let current = Rc::new(std::cell::Cell::new(30_usize)); + let picked: Rc>> = Rc::default(); + let (shown, asked, at, held) = (Rc::clone(&open), Rc::clone(&reveal), Rc::clone(¤t), Rc::clone(&picked)); + let pages = DemoPages::new(60); + let mut zoom = 1.0; + let mut fixture = Fixture::new(move |ui| { + if shown.get() { + let mut picked = held.borrow_mut(); + let out = PageThumbnails::new("Pages", &pages, &mut picked, at.get(), PAPER) + .arrangement(ThumbnailArrangement::Organize) + .size(&mut zoom) + .reveal(asked.take()) + .show(ui); + for event in out.events { + if let ThumbnailEvent::Go(page) = event { + at.set(page); + } + } + } + }); + fixture.room(vec2(600.0, 600.0)); + fixture.settle(); + let in_view = |fixture: &Fixture, n: usize| { + let grid = fixture.by_role(Role::LIST_BOX).first().map(|node| node.rect).expect("the grid"); + fixture.by_label(&format!("Page {n}")).is_some_and(|node| grid.contains_rect(node.rect)) + }; + assert!(in_view(&fixture, 31), "the first open shows page 31"); + let page = tile(&fixture, 32); + fixture.click_at(page).settle(); + open.set(false); + fixture.settle(); + current.set(45); + *picked.borrow_mut() = Selection::default(); + open.set(true); + reveal.set(Some(45)); + fixture.settle(); + assert!(in_view(&fixture, 46), "the second open shows page 46"); + assert!(!in_view(&fixture, 31), "and not the first open's pages"); +} + +/// The grid reports its latched middle-button auto-scroll while it runs, so a host can show and end it. +#[trial] +fn the_grid_reports_its_latched_auto_scroll() { + let latched = Rc::new(std::cell::Cell::new(false)); + let seen = Rc::clone(&latched); + let pages = DemoPages::new(60); + let mut picked = Selection::default(); + let mut fixture = Fixture::new(move |ui| { + let out = PageThumbnails::new("Pages", &pages, &mut picked, 0, PAPER) + .arrangement(ThumbnailArrangement::Organize) + .middle_button(craft_behavior::MiddleButton::AutoScroll) + .show(ui); + seen.set(out.auto_scrolling); + }); + fixture.room(vec2(600.0, 600.0)); + fixture.settle(); + let at = tile(&fixture, 2); + let middle = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Middle, pressed, modifiers: egui::Modifiers::NONE }; + fixture.frame(vec![egui::Event::PointerMoved(at), middle(true)]); + fixture.frame(vec![middle(false)]); + fixture.frame(Vec::new()); + assert!(latched.get(), "a middle click latches the grid's auto-scroll"); + fixture.key(egui::Key::Escape, egui::Modifiers::NONE); + fixture.frame(Vec::new()); + assert!(!latched.get(), "Escape ends it"); +} + +/// In the strip the picked pages are kept beside the current page: Cmd/Ctrl- and Shift-clicks change them without +/// going to a page, and a plain click goes to its page and empties them; the next Shift-click ranges from there. +#[trial] +fn strip_picks_stay_beside_the_current_page() { + let picked: Rc>> = Rc::default(); + let held = Rc::clone(&picked); + let current = Rc::new(std::cell::Cell::new(0_usize)); + let shown = Rc::clone(¤t); + let pages = DemoPages::new(5); + let mut fixture = Fixture::new(move |ui| { + let mut picked = held.borrow_mut(); + let out = PageThumbnails::new("Pages", &pages, &mut picked, shown.get(), PAPER).picks_beside_current(true).show(ui); + for event in out.events { + if let ThumbnailEvent::Go(page) = event { + shown.set(page); + } + } + }); + fixture.room(vec2(300.0, 1600.0)); + fixture.settle(); + let got = |picked: &RefCell>| (0..5).filter(|page| picked.borrow().contains(page)).collect::>(); + + let page = tile(&fixture, 3); + fixture.hold(egui::Modifiers::COMMAND).click_at(page).settle(); + fixture.hold(egui::Modifiers::NONE); + assert_eq!(got(&picked), [0, 2], "a Cmd/Ctrl-click picks the page beside the current one"); + assert_eq!(current.get(), 0, "a Cmd/Ctrl-click does not go to the page"); + + let page = tile(&fixture, 2); + fixture.click_at(page).settle(); + assert_eq!(current.get(), 1, "a plain click goes to the page"); + assert!(got(&picked).is_empty(), "a plain click empties the pick: {:?}", got(&picked)); + + let page = tile(&fixture, 4); + fixture.hold(egui::Modifiers::SHIFT).click_at(page).settle(); + fixture.hold(egui::Modifiers::NONE); + assert_eq!(got(&picked), [1, 2, 3], "a Shift-click ranges from the page the plain click went to"); + assert_eq!(current.get(), 1, "a Shift-click does not go to the page"); +} diff --git a/crates/pages/tests/it/views.rs b/crates/pages/tests/it/views.rs index fb2b690c2e..0070eb2edf 100644 --- a/crates/pages/tests/it/views.rs +++ b/crates/pages/tests/it/views.rs @@ -1119,6 +1119,53 @@ fn right_scrolls_a_page_wider_than_the_view_then_turns_at_its_edge() { assert!(left >= 0.0 && shown.rects[1].x1 - shown.offset.0 > view, "the next page shows from its left edge: {left}"); } +/// In a continuous view the current page is the one that shows the most of itself, the topmost of a tie; the page +/// gone to stays current while it shows wholly or as much as any other, as the reader scrolls (production's rule, +/// not the page under the view's middle). +#[trial] +fn the_current_page_shows_the_most_of_itself_and_a_page_gone_to_stays_while_it_shows_whole() { + let Rig { mut fixture, state, out, .. } = rig( + 6, + state_with(|s| { + s.fit = Fit::None; + s.zoom = 0.25; + }), + ); + let shown = |out: &Out, page: usize| { + let r = out.rects[page]; + let (top, bottom) = (out.offset.1, out.offset.1 + f64::from(out.view.height())); + (r.y1.min(bottom) - r.y0.max(top)).max(0.0) + }; + // The last page, gone to, sits wholly at the bottom of the view; a line up the wheel leaves it current. + state.borrow_mut().go_to(5); + fixture.settle(); + wheel(&mut fixture, 1.0); + fixture.settle(); + { + let out = out.borrow(); + assert!(shown(&out, 5) >= out.rects[5].height() - 0.5, "precondition: the last page shows whole"); + assert_eq!(state.borrow().current, 5, "the page gone to stays current while it shows whole"); + } + // From the top the wheel scrolls down until the second page shows more of itself than the first. + state.borrow_mut().go_to(0); + fixture.settle(); + for _ in 0..40 { + let more = { + let out = out.borrow(); + shown(&out, 1) > shown(&out, 0) + 1.0 && shown(&out, 1) > shown(&out, 2) + 1.0 + }; + if more { + break; + } + wheel(&mut fixture, -1.0); + fixture.settle(); + } + let out = out.borrow(); + let most = (0..6).max_by(|&a, &b| shown(&out, a).total_cmp(&shown(&out, b)).then(b.cmp(&a))).unwrap_or(0); + assert_eq!(most, 1, "precondition: the second page shows the most"); + assert_eq!(state.borrow().current, 1, "the page that shows the most of itself is current"); +} + /// Pages shorter than the view sit in its middle, a page or the column of a scrolling view, and so does a page that /// fits only without its margins. The scroll area used to be at least the view's height, from the top. #[trial] diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index 6ad31fcaad..d0e2b48d8f 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -509,6 +509,16 @@ impl<'a> Canvas<'a> { PressPhase::Cancel { .. } if latching => motion.middle.cancel(), _ => {} } + // A latch ends when the window loses focus or the pointer leaves it, and when the canvas missed a frame, so it + // never comes back with the window or the canvas. + let frame_nr = frame::running(&ctx).map_or(0, |pass| pass.frame_nr); + let left = crate::runtime::pointer::with_pass_pointer(&ctx, |pass| { + pass.focus_lost || pass.events.iter().any(|event| matches!(event, craft_behavior::PointerEvent::Gone)) + }); + let away = motion.latched_in.is_some_and(|last| frame_nr > last.saturating_add(1)); + if motion.middle.active() && (left || away || !crate::runtime::egui::viewports::current(&ctx).focused) { + motion.middle.cancel(); + } // A latch with the button up ends on Escape, claimed only while it runs, and when the canvas loses focus. if motion.middle.active() && !held { if cx.take_claimed(LATCH_ESCAPE) || !cx.is_focused() { @@ -538,6 +548,7 @@ impl<'a> Canvas<'a> { if held_pan { cx.cursor(CursorIcon::Grabbing, CursorPriority::Drag); } + motion.latched_in = motion.middle.active().then_some(frame_nr); let auto_scrolling = motion.middle.active() || held_pan; let mark = motion.middle.anchor(); if let Some(delta) = self.background_press(cx, swallowed).1 { diff --git a/crates/ui-canvas/src/canvas/zoom_motion.rs b/crates/ui-canvas/src/canvas/zoom_motion.rs index fa6ade6e5c..141b8d7f02 100644 --- a/crates/ui-canvas/src/canvas/zoom_motion.rs +++ b/crates/ui-canvas/src/canvas/zoom_motion.rs @@ -134,6 +134,8 @@ pub(crate) struct ViewMotion { pub(crate) glide: TouchpadGlide, /// The middle button's latched scroll. pub(crate) middle: MiddleScroll, + /// The frame of the latch's last pass: a latch whose canvas missed a frame (another tab showed) ends. + pub(crate) latched_in: Option, } impl ViewMotion { diff --git a/crates/ui-canvas/tests/it/canvas/navigate.rs b/crates/ui-canvas/tests/it/canvas/navigate.rs index f6ac7c26e7..1705fa3fa2 100644 --- a/crates/ui-canvas/tests/it/canvas/navigate.rs +++ b/crates/ui-canvas/tests/it/canvas/navigate.rs @@ -378,6 +378,60 @@ fn latch_rig() -> LatchRig { (fixture, view, canvas) } +/// A latched middle scroll ends when the window loses focus, when the pointer leaves the window and when the canvas +/// is not shown for a pass (another tab), and does not come back with it; a press, a move and a release that arrive in +/// one frame latch it as a click does, measured from the press. +#[trial] +fn a_latched_middle_scroll_ends_on_blur_pointer_gone_and_a_hidden_canvas_and_latches_from_one_frame() { + let mut options = NavigationOptions::default(); + options.middle_button = MiddleButton::AutoScroll; + let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0))); + let open = Rc::new(Cell::new(true)); + let (shown, visible) = (Rc::clone(&view), Rc::clone(&open)); + let mut fixture = Fixture::new(move |ui| { + if !visible.get() { + return; + } + let mut given = shown.get(); + given.rect = ui.available_rect_before_wrap(); + let canvas = Canvas::new("Document").document(Size::new(4000.0, 4000.0)).navigation(options).view(given).show(ui, |_, _| {}); + shown.set(canvas.output.view.unwrap_or(given)); + }); + fixture.settle(); + let anchor = view.get().rect.center(); + let below = anchor + Vec2::new(0.0, 80.0); + let latch = |fixture: &mut Fixture| { + middle(fixture, anchor, true); + middle(fixture, anchor, false); + fixture.frame(vec![pointer::moved(below)]); + }; + + latch(&mut fixture); + assert!(scrolls(&mut fixture, &view), "a click latches the scroll"); + fixture.frame(vec![pointer::window_blurred()]); + assert!(!scrolls(&mut fixture, &view), "a window blur ends the latch"); + fixture.frame(vec![egui::Event::WindowFocused(true), pointer::moved(below)]); + assert!(!scrolls(&mut fixture, &view), "the window's return does not bring it back"); + + latch(&mut fixture); + assert!(scrolls(&mut fixture, &view), "a click latches again"); + fixture.frame(vec![egui::Event::PointerGone]); + assert!(!scrolls(&mut fixture, &view), "the pointer leaving the window ends the latch"); + + let button = |pressed, at| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Middle, pressed, modifiers: Modifiers::NONE }; + fixture.frame(vec![pointer::moved(anchor), button(true, anchor), pointer::moved(below), button(false, below)]); + assert!(scrolls(&mut fixture, &view), "a press, a move and a release in one frame latch the scroll"); + fixture.key(Key::Escape, Modifiers::NONE); + + latch(&mut fixture); + assert!(scrolls(&mut fixture, &view), "a click latches again"); + open.set(false); + fixture.idle(2); + open.set(true); + fixture.frame(vec![pointer::moved(below)]); + assert!(!scrolls(&mut fixture, &view), "a canvas shown again does not resume the latch"); +} + /// The middle button going down (`pressed`) or up at `at`, with the pointer there. The testing kit has no /// middle-button helper. fn middle(fixture: &mut Fixture, at: Pos2, pressed: bool) { diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index 3fb6dc6c51..c657ddddc2 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -255,6 +255,10 @@ pub struct CollectionProps { /// The key of the current item (the page a document shows, the slide on screen). It is not the lead or the /// selection: the item shows the current mark and its access node reports itself as the current one of its set. pub current: Option>, + /// The selection is a pick kept beside the current item (Acrobat's Pages panel): a plain click on an item moves + /// the lead there and empties the selection, and Cmd/Ctrl- and Shift-clicks pick without moving the lead, counting + /// the current item as picked while nothing is. Off, a plain click selects the item it lands on. + pub picks_beside_current: bool, /// While a drag whose payload is neither the collection's own nor one of its drop kinds rests over an item that is /// not selected, the item becomes the lead and the selection in that pass. pub select_under_drag: bool, @@ -343,6 +347,7 @@ impl CollectionProps { overflow: Axis::Y, pages: false, current: None, + picks_beside_current: false, select_under_drag: false, } } @@ -628,6 +633,13 @@ impl CollectionProps { self } + /// Keeps the selection a pick beside the current item (see [`CollectionProps::picks_beside_current`]). + #[must_use] + pub const fn picks_beside_current(mut self, on: bool) -> Self { + self.picks_beside_current = on; + self + } + /// The empty-state text. #[must_use] pub fn empty(mut self, text: impl Into