Collections take a wheel notch (Notch, shared with bodies); TileGrid scrolls a third of its view per notch

This commit is contained in:
aiongg
2026-11-12 14:32:42 +00:00
parent 147778fc2f
commit 6261e56681
8 changed files with 68 additions and 13 deletions
@@ -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<C: Container> ShapeHooks<C> 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);
@@ -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<u16>,
/// The distance one wheel notch scrolls the rows; unset, the default notch.
pub notch: Option<Notch>,
/// 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 {
@@ -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]
+3
View File
@@ -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(),
@@ -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<C: Collection> ShapeHooks<C> 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<C: Collection> ShapeHooks<C> 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)
+1
View File
@@ -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<impl ::core::marker::Send + ::core::marker::Sync + 'static>",
+8 -4
View File
@@ -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,
+27 -1
View File
@@ -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::<Vec<_>>()));
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<usize> = (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());
}