From 6261e5668169eaed54b4ebeb214cf3f6a6708524 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 00:22:39 +0000 Subject: [PATCH] Collections take a wheel notch (Notch, shared with bodies); TileGrid scrolls a third of its view per notch --- crates/ui-contract/src/contract/body/hooks.rs | 9 ++---- .../src/contract/collection/props.rs | 11 ++++++++ crates/ui-contract/src/contract/container.rs | 10 +++++++ crates/ui-contract/src/contract/items.rs | 3 ++ .../ui-contract/src/contract/items/hooks.rs | 7 +++-- crates/ui-macros/src/props.rs | 1 + crates/ui-widgets/src/tile_grid/grid.rs | 12 +++++--- crates/ui-widgets/tests/it/tile_grid/keys.rs | 28 ++++++++++++++++++- 8 files changed, 68 insertions(+), 13 deletions(-) diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index ea646110b..15b43cb4e 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -15,7 +15,7 @@ use super::{BodyHooks, BodyView}; use crate::contract::cx::{BodyCx, BodyOwner}; use crate::contract::label::{self, LabelIn}; use crate::contract::run::{Measuring, ShapeHooks, ShowAt}; -use crate::contract::{Container, Cx, DragBand, Gap, Internal, Mount, Notch, PartsCx, SetPosition, Sizing, ValueOf}; +use crate::contract::{Container, Cx, DragBand, Gap, Internal, Mount, PartsCx, SetPosition, Sizing, ValueOf}; use crate::layout::{self}; use crate::primitives::SurfaceSlot; use crate::runtime::access::scope; @@ -32,7 +32,7 @@ use crate::runtime::{keys, layout_memory, pointer, wheel}; use crate::style::ComponentStyle as _; use crate::style::{ScrollBarKind, StyleCx, SurfaceKind, scroll_bar_look, surface_style}; use craft_behavior::layout_memory::measure_tolerance; -use craft_behavior::{CancelRule, CursorIcon, EditEvent, KeyRole, Mixed, Roving, ScrollPos, ScrollSpan, WheelNotch, WheelRoom, WheelTaker}; +use craft_behavior::{CancelRule, CursorIcon, EditEvent, KeyRole, Mixed, Roving, ScrollPos, ScrollSpan, WheelRoom, WheelTaker}; use egui::{Context, CornerRadius, Id, Pos2, Rect, Shape, Ui, UiBuilder, Vec2}; use std::any::Any; use std::sync::Arc; @@ -548,10 +548,7 @@ impl ShapeHooks for BodyHooks<'_> { pcx.scroll(bar.key, bar.track, bar.axis, span); } if let Some(scroll) = scroll { - let notch = match scroll.notch { - Notch::Lines(lines) => WheelNotch::Points(lines * tokens.tokens().typography.body.line_height), - Notch::ViewShare(share) => WheelNotch::ViewShare(share), - }; + let notch = scroll.notch.wheel(tokens.tokens().typography.body.line_height); // A region takes an axis only while its content overflows there, and hands the wheel on at its ends. let max = self.max_offset(view.size()); let (vertical, horizontal) = (scroll.vertical_on() && max.y > 0.0, scroll.horizontal_on() && max.x > 0.0); diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index 47ddfe093..d408c5c77 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -2,6 +2,7 @@ use super::columns::{ColumnSort, ColumnSpec}; use super::events::ListDrag; +use crate::contract::Notch; use crate::runtime::env::{Counted, Label}; use craft_behavior::{Axis, ColumnKey}; use std::any::Any; @@ -196,6 +197,8 @@ pub struct CollectionProps { /// The most rows the collection shows: past them it is as tall as its first `max_rows` rows and scrolls them. It /// owns that scroll inside a scrolling body too, since its height is bounded and the wheel passes on at its ends. pub max_rows: Option, + /// The distance one wheel notch scrolls the rows; unset, the default notch. + pub notch: Option, /// The axis the rows overflow along. A collection owns the scroll along it, so it sits only in a scrolling body /// that does not scroll along it too: a strip of tabs (`Axis::X`) sits in a page that scrolls vertically. pub overflow: Axis, @@ -281,6 +284,7 @@ impl CollectionProps { natural_height: false, min_rows: 0, max_rows: None, + notch: None, overflow: Axis::Y, pages: false, current: None, @@ -499,6 +503,13 @@ impl CollectionProps { self } + /// The distance one wheel notch scrolls the rows (see [`CollectionProps::notch`]). + #[must_use] + pub const fn notch(mut self, notch: Notch) -> Self { + self.notch = Some(notch); + self + } + /// The axis the rows overflow along (see [`CollectionProps::overflow`]). #[must_use] pub const fn overflow(mut self, axis: Axis) -> Self { diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index f124700ce..7b0e792ab 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -417,6 +417,16 @@ impl Default for Notch { } } +impl Notch { + /// The wheel distance of the notch, with a text line `line` points high. + pub fn wheel(self, line: f32) -> craft_behavior::WheelNotch { + match self { + Notch::Lines(lines) => craft_behavior::WheelNotch::Points(lines * line), + Notch::ViewShare(share) => craft_behavior::WheelNotch::ViewShare(share), + } + } +} + /// The axes a body scrolls on. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] diff --git a/crates/ui-contract/src/contract/items.rs b/crates/ui-contract/src/contract/items.rs index dc35c77d6..46cc867dc 100644 --- a/crates/ui-contract/src/contract/items.rs +++ b/crates/ui-contract/src/contract/items.rs @@ -145,6 +145,8 @@ pub(super) struct ItemsHooks<'s, 'r, C: Collection> { scrolls: bool, /// The collection shows at most `max_rows` rows: its height is bounded by its rows, whatever its sizing. capped: bool, + /// The distance one wheel notch scrolls the rows, from `CollectionProps::notch`. + notch: craft_behavior::WheelNotch, /// The axis the rows overflow along. overflow: Axis, selection: C::Selection, @@ -208,6 +210,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { from_content: (false, false), scrolls: true, capped: false, + notch: craft_behavior::WheelNotch::DEFAULT, overflow: Axis::Y, selection: C::Selection::default(), selection_at_read: C::Selection::default(), diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 4a0c2b38c..9c57a333d 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -20,7 +20,8 @@ use crate::runtime::memory::{MemoryStage, sub}; use crate::runtime::{focus, keys, layout_memory, lazy, overlay, wheel}; use crate::style::{ComponentStyle as _, ScrollBarKind, StyleCx, SurfaceKind, scroll_bar_look, surface_style}; use craft_behavior::{ - Axis, CancelReason, EditEvent, Intent, ItemLayout, Mixed, Proposal, ScrollPos, ScrollSpan, SelectionModel, Sense, WheelRoom, WheelTaker, + Axis, CancelReason, EditEvent, Intent, ItemLayout, Mixed, Proposal, ScrollPos, ScrollSpan, SelectionModel, Sense, WheelNotch, WheelRoom, + WheelTaker, }; use egui::{Context, Id, Pos2, Rect, Ui, UiBuilder, Vec2}; use std::rc::Rc; @@ -274,8 +275,9 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { Some(Rc::new(Rows { rects, offset })) } - fn parts(&mut self, _: &mut C, _: Rect, pcx: &mut PartsCx<'_, C>) { + fn parts(&mut self, comp: &mut C, _: Rect, pcx: &mut PartsCx<'_, C>) { self.header.parts(pcx); + self.notch = comp.items().notch.map_or(WheelNotch::DEFAULT, |notch| notch.wheel(pcx.tokens.typography.body.line_height)); if !self.scrolls { return; } @@ -304,6 +306,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { wheel::register( &cx.core.ctx, WheelTaker::new(cx.core.id, cx.core.rect) + .notch(self.notch) .vertical((vertical && max.y > 0.0) || horizontal) .horizontal(horizontal) .scrolls(true) diff --git a/crates/ui-macros/src/props.rs b/crates/ui-macros/src/props.rs index 4f9cb0bdf..a75252ab1 100644 --- a/crates/ui-macros/src/props.rs +++ b/crates/ui-macros/src/props.rs @@ -191,6 +191,7 @@ const COLLECTION: &[Forwarded] = &[ ("natural_height", "bool", "The collection takes its full content height and owns no scroll: for a short collection inside a scrolling page."), ("min_rows", "u16", "The collection is at least this many rows tall; rows it does not have count the control height."), ("max_rows", "u16", "The collection shows at most this many rows and scrolls the rest, inside a scrolling body too."), + ("notch", "::craft_ui::contract::Notch", "The distance one wheel notch scrolls the rows."), ( "current", "::core::option::Option", diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index 8590a9950..c5a323021 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -14,8 +14,8 @@ use super::tile::AroundPicture; use super::{CaptionPlace, Tile, TilePartEvent, ZoomStep}; use crate::contract::{ Access, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, ColumnKind, ColumnSpec, Common, Component, - ContainerProps, Cx, EditResponse, Focus as _, ListDrag, NoValue, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Role, RowState, SelectMode, - Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn, + ContainerProps, Cx, EditResponse, Focus as _, ListDrag, NoValue, Notch, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Role, RowState, + SelectMode, Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn, }; use crate::contract::{lead_position, section_part}; use crate::controls::disclosure::Disclosure; @@ -173,9 +173,13 @@ impl TileGridStyle { } } +/// The share of the grid's view one wheel notch scrolls (LightCraft's filmstrip). +const WHEEL_SHARE: f32 = 1.0 / 3.0; + /// A grid of tiles: one Tab stop, the selection rules of the collection contract, type-ahead on the tiles' names (with /// `type_ahead`), rename in place (F2, or a slow second click on the caption of the only selected tile), drag to reorder, files dropped from the OS, and the request for the app's context -/// menu. A double click on the background reports `CollectionEvent::EmptyActivated` (an import, in FilmCraft). +/// menu. A double click on the background reports `CollectionEvent::EmptyActivated` (an import, in FilmCraft). One wheel +/// notch scrolls a third of the grid's view. /// /// The picture side is `zoom` times the thumbnail side (the card thumbnail unless `thumb_size` names one): the factor the app's zoom control holds. A card is the /// picture plus what the tile adds around it, whatever the layout. Pictures are `aspect` wide over high; with @@ -288,7 +292,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { TileGrid { common: Common::new(), props: ContainerProps::new().sizing(Sizing::Fill), - items: CollectionProps::new().select(SelectMode::Multi).slow_click_rename(true), + items: CollectionProps::new().select(SelectMode::Multi).slow_click_rename(true).notch(Notch::ViewShare(WHEEL_SHARE)), selection: None, reveal: None, justified: false, diff --git a/crates/ui-widgets/tests/it/tile_grid/keys.rs b/crates/ui-widgets/tests/it/tile_grid/keys.rs index 44da0ac3d..39bfe68d9 100644 --- a/crates/ui-widgets/tests/it/tile_grid/keys.rs +++ b/crates/ui-widgets/tests/it/tile_grid/keys.rs @@ -2,7 +2,7 @@ use craft_behavior::{EditEvent, keyed}; use craft_ui::contract::{CollectionEvent, ShowCollection as _}; -use craft_ui::testing::{Fixture, trial}; +use craft_ui::testing::{Fixture, pointer, trial}; use craft_ui::tile_grid::{GridEvent, RatingKeys, Tile, TileGrid, ZoomStep}; use egui::{Key, Modifiers, Vec2}; use std::cell::RefCell; @@ -108,3 +108,29 @@ fn a_double_click_on_the_background_activates_the_empty_space() { assert_eq!(count(&seen, |event| matches!(event, CollectionEvent::Activate(1))), 1, "a double click on a tile activates it"); assert_eq!(count(&seen, |event| matches!(event, CollectionEvent::EmptyActivated)), 1, "and is no background double click"); } + +/// One wheel notch over a grid that overflows scrolls a third of its view. +#[trial] +fn a_wheel_notch_scrolls_a_third_of_the_view() { + let seen = Rc::new(RefCell::new((0..40usize).map(|_| egui::Rect::NOTHING).collect::>())); + let held = seen.clone(); + let view = Rc::new(std::cell::Cell::new(egui::Rect::NOTHING)); + let shown = view.clone(); + let mut fixture = Fixture::new(move |ui| { + let keys: Vec = (0..40).collect(); + let source = keyed(&keys, |key: &usize| *key); + let out = TileGrid::new("Library").show_items(ui, &source, &mut |key, icx| { + held.borrow_mut()[*key] = icx.rect; + Tile::new(format!("Tile {key}")) + }); + shown.set(out.response.response.rect); + }); + fixture.room(Vec2::new(600.0, 300.0)); + fixture.settle(); + let top = seen.borrow()[0].min.y; + fixture.frame(vec![pointer::moved(view.get().center())]); + fixture.frame(vec![pointer::wheel_lines(-1.0)]).idle(2); + let moved = top - seen.borrow()[0].min.y; + let share = moved / view.get().height(); + assert!((0.3..0.37).contains(&share), "one notch scrolls a third of the view: {moved} of {}", view.get().height()); +}