mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
ui-contract: collection rows name the row under the pointer through hover-only parts
Each built item row of a collection declares a hover-only part over its rect, above the collection's background and below its other parts and the row's own shapes. Sensing reports every root's own parts under the pointer, on top or not (pointer::Under::parts, read with pointer::parts_under), and a memoized view's Sensed includes those of its roots. The collection takes a row's hovered and pointer-over state, the hover lead and the rest wait from the row part under the pointer instead of reading the hover position, so a move within one row replays a view around the list and a move onto another row runs it. The edge band still reads the hover position: it is drawn at the line nearest the pointer. Trials: a list of 10 and of 40 rows in a memoized view replays on moves within a row, runs once on a move to another row, and the hover fill follows; a press and its release, a wheel over a list, and a modifier change under the pointer run the view; a replayed hovered view keeps its cursor; a tooltip runs its view at the wake and when its layer first paints; Cx::hover_pos reads the point over its part only and marks it as following the pointer for the pointer-move gate.
This commit is contained in:
1 parent
4fe31f5dc3
commit
c07dc92f2c
8 files changed
+371
-36
No files matched your search
@@ -107,6 +107,10 @@ const SECTION: craft_behavior::PartKey = craft_behavior::PartKey::new("section")
|
||||
const EDGE: craft_behavior::PartKey = craft_behavior::PartKey::new("items_edge");
|
||||
/// The sub-node of a toggle cell of an item row; the index is the hash of the row's key and the column.
|
||||
const TOGGLE_CELL: craft_behavior::PartKey = craft_behavior::PartKey::new("toggle_cell");
|
||||
/// The hover-only part over an item row's rect; the index is the hash of the row's key. The parts under the pointer
|
||||
/// (`pointer::parts_under`) name the row it is over, under the row's own shapes, so a memoized view around the
|
||||
/// collection replays while the pointer moves within one row.
|
||||
const ROW: craft_behavior::PartKey = craft_behavior::PartKey::new("items_row");
|
||||
/// The part an item row's own access node stands for in the collection: AccessKit requests on the row's node reach
|
||||
/// the collection under this part, the index being the hash of the row's key.
|
||||
const ITEM_NODE: craft_behavior::PartKey = craft_behavior::PartKey::new("item_node");
|
||||
@@ -274,6 +278,9 @@ pub(super) struct ItemsHooks<'s, 'r, C: Collection> {
|
||||
pressed: Option<(usize, crate::contract::collection::RowPressed)>,
|
||||
/// The rows above and below the edge-press band declared this pass.
|
||||
edge: Option<(C::Key, C::Key)>,
|
||||
/// The item rows that declare their `ROW` part this pass: the hash of the row's key, the row and its rect in the
|
||||
/// rows' view.
|
||||
row_parts: Vec<(u64, usize, Rect)>,
|
||||
}
|
||||
|
||||
/// The primary-ness, click count, position in the layout's coordinates and scroll offset of a press on a row.
|
||||
@@ -338,6 +345,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> {
|
||||
renaming_at_read: None,
|
||||
pressed: None,
|
||||
edge: None,
|
||||
row_parts: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! Where rows sit: the rows' rect and scroll range, which rows a pass builds, and rows on the screen.
|
||||
|
||||
use super::ItemsHooks;
|
||||
use crate::contract::{Collection, Cx};
|
||||
use craft_behavior::{Flow, ItemLayout, ScrollPos, SelectionModel};
|
||||
use egui::{Pos2, Rect};
|
||||
use super::{ItemsHooks, ROW};
|
||||
use crate::contract::{Collection, Cx, PartsCx};
|
||||
use craft_behavior::{Flow, HitLevel, HitPriority, ItemLayout, ScrollPos, SelectionModel};
|
||||
use egui::{Context, Id, Pos2, Rect};
|
||||
|
||||
impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> {
|
||||
/// The rect of the rows: the content rect below the header row.
|
||||
@@ -15,6 +15,20 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> {
|
||||
self.below_header(cx.core.content_rect)
|
||||
}
|
||||
|
||||
/// Declares each item row's `ROW` part over its rect in the rows' view: hover only, above the collection's
|
||||
/// background and below its other parts and every row's own shapes.
|
||||
pub(super) fn row_parts(&self, pcx: &mut PartsCx<'_, C>) {
|
||||
for &(hash, _, rect) in &self.row_parts {
|
||||
pcx.part(ROW.at(hash), rect).exact_hit().priority(HitPriority::new(HitLevel::Background, 1));
|
||||
}
|
||||
}
|
||||
|
||||
/// The item row whose `ROW` part, of the collection `id`, contains the pointer this pass, on top or not.
|
||||
pub(super) fn row_under_pointer(&self, ctx: &Context, id: Id) -> Option<usize> {
|
||||
let part = crate::runtime::pointer::parts_under(ctx, id).into_iter().find(|part| part.key == ROW)?;
|
||||
self.row_parts.iter().find(|(hash, ..)| part.index == Some(*hash)).map(|&(_, at, _)| at)
|
||||
}
|
||||
|
||||
/// Whether the scroll bar shows over the rows' rect `view`: the rows scroll and run past its height.
|
||||
pub(super) fn shows_bar(&self, view: Rect) -> bool {
|
||||
self.scrolls && self.content.y > view.height()
|
||||
|
||||
@@ -211,7 +211,14 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
let mut layout = self.layout.take()?;
|
||||
Arc::make_mut(&mut layout).set_view(view.size(), offset);
|
||||
let (_, order) = self.built(&layout, view, offset);
|
||||
let rects = order.into_iter().map(|at| (self.source.key(at), self.screen(&layout, view, at, offset, rtl))).collect();
|
||||
let rects: Vec<(C::Key, Rect)> = order.iter().map(|&at| (self.source.key(at), self.screen(&layout, view, at, offset, rtl))).collect();
|
||||
self.row_parts.clear();
|
||||
for (&at, (key, rect)) in order.iter().zip(&rects) {
|
||||
let shown = rect.intersect(view);
|
||||
if !self.source.section_stop(at) && shown.is_positive() {
|
||||
self.row_parts.push((key_hash(key), at, shown));
|
||||
}
|
||||
}
|
||||
self.layout = Some(layout);
|
||||
Some(Rc::new(Rows { rects, offset, view: clear }))
|
||||
}
|
||||
@@ -219,6 +226,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
fn parts(&mut self, comp: &mut C, _: Rect, pcx: &mut PartsCx<'_, C>) {
|
||||
self.header.parts(pcx, self.offset_x as f32);
|
||||
self.edge_parts(comp, pcx);
|
||||
self.row_parts(pcx);
|
||||
self.notch = comp.items().wheel_notch(pcx.tokens.typography.body.line_height);
|
||||
if !self.scrolls {
|
||||
return;
|
||||
@@ -304,7 +312,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
self.marquee_step(comp, cx, &layout, (rect, offset, rtl), &props);
|
||||
}
|
||||
if props.hover_lead != HoverLead::Off {
|
||||
self.hover_lead(comp, cx, &layout, seen, props.hover_lead);
|
||||
self.hover_lead(comp, cx, &layout, props.hover_lead);
|
||||
}
|
||||
// The moves a control in the rows left that do not page them (an arrow a region's fallback collection takes)
|
||||
// navigate the rows as the collection's own keys do.
|
||||
@@ -332,7 +340,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
self.requested_rename(comp, cx, &props, &layout);
|
||||
self.requested_reveal(comp, &layout);
|
||||
self.type_ahead(comp, cx, &layout, lead_at);
|
||||
self.rest_lead(cx, &layout, seen);
|
||||
self.rest_lead(cx, &layout);
|
||||
request::record_lead(&cx.core.ctx, cx.core.id, self.lead.key.as_ref().and_then(|key| self.index_of(key)));
|
||||
if let Some(at) = self.page_reveal.take().filter(|&at| layout.shown(at)) {
|
||||
cx.core.reveal = Some(self.screen(&layout, rect, at, ScrollPos::default(), rtl));
|
||||
@@ -434,11 +442,9 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
// The pointer counts as over a row only while the sensing pass puts it on this collection, so a popup or another
|
||||
// widget above it takes the hover as it does for the rows' parts.
|
||||
let outcome = crate::primitives::sensing::read_outcome(&ctx, ArenaId::new(cx.core.id));
|
||||
// The row under the pointer follows the pointer over the rows.
|
||||
let screen = crate::runtime::egui::areas::layer_to_screen(&ctx, ui.layer_id()) * rect;
|
||||
let pointer = (outcome.hover.any() || outcome.contains).then(|| crate::runtime::pointer::hover_point_in(&ctx, screen)).flatten();
|
||||
let hover_at = pointer.filter(|_| outcome.hover.any());
|
||||
let over_at = pointer.filter(|_| outcome.contains);
|
||||
let under = self.row_under_pointer(&ctx, id);
|
||||
let hover_at = under.filter(|_| outcome.hover.any());
|
||||
let over_at = under.filter(|_| outcome.contains);
|
||||
self.hovered = None;
|
||||
let menu_open = self.drags.menu.is_some() && crate::contract::overlay::container_open::opened_from(&ctx, cx.core.id);
|
||||
// A double click on a column's edge fits the column to its widest cell among the rows built here, in view.
|
||||
@@ -474,8 +480,8 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
let state = RowState {
|
||||
dragging: self.drags.grabbed.as_ref() == Some(&key),
|
||||
drop_target: matches!(&self.drags.pending, Some(PendingDrop::Drop(DropAt::Onto(over))) if *over == key),
|
||||
hovered: hover_at.is_some_and(|pos| row.contains(pos)),
|
||||
pointer_over: over_at.is_some_and(|pos| row.contains(pos)),
|
||||
hovered: hover_at == Some(at),
|
||||
pointer_over: over_at == Some(at),
|
||||
pressed: self.pressed.as_ref().filter(|(held, _)| *held == at).map(|(_, pressed)| pressed.clone()),
|
||||
menu_open: self.drags.menu.as_ref().is_some_and(|request| request.key == key) && menu_open,
|
||||
activated_by_key: self.keyed_activation.as_ref() == Some(&key),
|
||||
|
||||
@@ -193,15 +193,17 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The pointer's position, in screen space, while it is over the collection, which then follows the pointer.
|
||||
fn hovered_pos(cx: &Cx<'_, C>) -> Option<Pos2> {
|
||||
let response = &cx.core.response;
|
||||
let screen = crate::runtime::egui::areas::layer_to_screen(&cx.core.ctx, response.layer_id) * response.rect;
|
||||
response.hovered().then(|| crate::runtime::pointer::hover_point_in(&cx.core.ctx, screen)).flatten()
|
||||
/// The row under the pointer while it is over the collection and the row can be picked and shows: the row whose
|
||||
/// `ROW` part contains it.
|
||||
fn hovered_row(&self, cx: &Cx<'_, C>, layout: &C::ItemLayout) -> Option<usize> {
|
||||
if !cx.core.response.hovered() {
|
||||
return None;
|
||||
}
|
||||
self.row_under_pointer(&cx.core.ctx, cx.core.id).filter(|&row| self.source.enabled(row) && !self.source.is_section(row) && layout.shown(row))
|
||||
}
|
||||
|
||||
/// The row under `pos` (screen space) in the rows' placement `at`: the pointer-to-row mapping of `hover_lead`,
|
||||
/// which `hover_lead`, the rest check and the drag-select step all call.
|
||||
/// The row under `pos` (screen space) in the rows' placement `at`: the drag-select step maps the drag session's
|
||||
/// pointer with it.
|
||||
pub(super) fn row_under(&self, layout: &C::ItemLayout, at: Placement, pos: Pos2) -> Option<usize> {
|
||||
let Placement { rect, offset, rtl } = at;
|
||||
let pos = self.xf.map_or(pos, |map| map.to_layer(pos));
|
||||
@@ -220,13 +222,13 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
/// The lead follows a moving pointer over the rows; with `HoverLead::LeadAndSelection` the selection follows the
|
||||
/// lead the way it does for the arrow keys. A pass moves the pointer when it holds a move event: the pointer entering over a row moves the lead
|
||||
/// with no movement to measure, and a modifier change under a resting pointer leaves the lead where the keys put it.
|
||||
pub(super) fn hover_lead(&mut self, comp: &C, cx: &Cx<'_, C>, layout: &C::ItemLayout, at: Placement, hover: HoverLead) {
|
||||
pub(super) fn hover_lead(&mut self, comp: &C, cx: &Cx<'_, C>, layout: &C::ItemLayout, hover: HoverLead) {
|
||||
let moved =
|
||||
crate::runtime::pointer::with_pass_raw(&cx.core.ctx, |pass| pass.events.iter().any(|event| matches!(event, PointerEvent::Moved { .. })));
|
||||
if !moved {
|
||||
return;
|
||||
}
|
||||
let Some(row) = Self::hovered_pos(cx).and_then(|pos| self.row_under(layout, at, pos)) else { return };
|
||||
let Some(row) = self.hovered_row(cx, layout) else { return };
|
||||
if self.lead.key.as_ref() != Some(&self.source.key(row)) {
|
||||
self.move_lead(row, LeadSource::Pointer);
|
||||
if hover == HoverLead::LeadAndSelection {
|
||||
@@ -237,9 +239,9 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
|
||||
/// The pointer resting on the lead row for the tooltip delay reports `CollectionEvent::Rested`, once per lead
|
||||
/// change. Motion inside the row does not restart the timer, as for a tooltip.
|
||||
pub(super) fn rest_lead(&mut self, cx: &mut Cx<'_, C>, layout: &C::ItemLayout, at: Placement) {
|
||||
pub(super) fn rest_lead(&mut self, cx: &mut Cx<'_, C>, layout: &C::ItemLayout) {
|
||||
let lead = self.lead.key.clone();
|
||||
let over = Self::hovered_pos(cx).and_then(|pos| self.row_under(layout, at, pos));
|
||||
let over = self.hovered_row(cx, layout);
|
||||
let held = lead.is_some() && self.lead.rested != lead && over.is_some_and(|row| lead.as_ref() == Some(&self.source.key(row)));
|
||||
let (ctx, id) = (cx.core.ctx.clone(), cx.core.id);
|
||||
if crate::contract::overlay::dwell(&ctx, &mut *cx.core.memory, id, "rest", held, crate::contract::tooltip::DELAY)
|
||||
|
||||
@@ -499,11 +499,12 @@ pub(crate) fn replay(ctx: &Context, id: Id) {
|
||||
/// - nothing in it moves: no animation off its end value, no repaint asked from inside it, no texture it shows still
|
||||
/// loading, no wake time reached;
|
||||
/// - what pointer sensing gives it is what its run saw (`pointer::Sensed`: the hover, part and pointer-over facts of
|
||||
/// its roots and egui widgets), so a move within one part of it leaves it alone, and a move onto another part, in
|
||||
/// or out runs it; while it is under the pointer, the modifiers are the ones its run saw; the pointer did not move
|
||||
/// in a rect its run follows the pointer in (`pointer::follows_pointer`, which a hover position read marks), now or
|
||||
/// then; no wheel share or owed ease of this pass goes to a taker it registered; no tap of this pass lies in it;
|
||||
/// while a drag session runs, the pointer is not over it or an overlay layer it shows;
|
||||
/// its roots and egui widgets, and the parts of its roots under the pointer), so a move within one part of it, a
|
||||
/// collection's row among them, leaves it alone, and a move onto another part, in or out runs it; while it is under
|
||||
/// the pointer, the modifiers are the ones its run saw; the pointer did not move in a rect its run follows the
|
||||
/// pointer in (`pointer::follows_pointer`, which a hover position read marks), now or then; no wheel share or owed
|
||||
/// ease of this pass goes to a taker it registered; no tap of this pass lies in it; while a drag session runs, the
|
||||
/// pointer is not over it or an overlay layer it shows;
|
||||
/// - no press that is held started on it or an overlay layer it shows, none of its widgets is dragged, and none of
|
||||
/// that held in its last run;
|
||||
/// - the focus is where its run left it (`replay::Held`: the same stop and egui widget of it, or none), and while the
|
||||
|
||||
@@ -682,8 +682,8 @@ pub fn follows_pointer(ctx: &Context, rect: Rect) {
|
||||
}
|
||||
|
||||
/// The running pass's hover point (screen points) when it lies in `rect` (screen points), for code that shows what
|
||||
/// it reads there (a readout, a cursor drawn at the point, the row under it): `rect` then follows the pointer
|
||||
/// ([`follows_pointer`]). The one way component code reads where the pointer hovers.
|
||||
/// it reads there (a readout, a cursor drawn at the point, a band at the line nearest it): `rect` then follows the
|
||||
/// pointer ([`follows_pointer`]). The one way component code reads where the pointer hovers.
|
||||
pub fn hover_point_in(ctx: &Context, rect: Rect) -> Option<Pos2> {
|
||||
let at = with_frame(ctx, |_, hover| hover).filter(|at| rect.contains(*at))?;
|
||||
follows_pointer(ctx, rect);
|
||||
@@ -1018,6 +1018,9 @@ pub(crate) struct Under {
|
||||
pub hover_top: Option<(Winner<Id>, Id)>,
|
||||
/// Every root with a shape under the point, on top or not: the roots that contain the pointer.
|
||||
pub over: SmallVec<[Id; 4]>,
|
||||
/// The roots' own parts with a shape under the point, on top or not, each with its root: a collection's row that
|
||||
/// contains the pointer under the row's own shapes.
|
||||
pub parts: SmallVec<[(Id, PartRef); 4]>,
|
||||
}
|
||||
|
||||
impl Under {
|
||||
@@ -1082,6 +1085,8 @@ pub(crate) struct Sensed {
|
||||
hover: SmallVec<[(u8, Id, Id, Option<PartRef>); 3]>,
|
||||
/// The run's roots with a shape under the point.
|
||||
over: SmallVec<[Id; 4]>,
|
||||
/// The own parts of the run's roots with a shape under the point (`Under::parts`).
|
||||
parts: SmallVec<[(Id, PartRef); 4]>,
|
||||
/// The run's egui widgets that contain the point, then its click and drag targets there.
|
||||
widgets: SmallVec<[Id; 4]>,
|
||||
}
|
||||
@@ -1089,7 +1094,7 @@ pub(crate) struct Sensed {
|
||||
impl Sensed {
|
||||
/// Nothing of the run lies under the pointer.
|
||||
pub(crate) fn is_empty(&self) -> bool {
|
||||
self.hover.is_empty() && self.over.is_empty() && self.widgets.is_empty()
|
||||
self.hover.is_empty() && self.over.is_empty() && self.parts.is_empty() && self.widgets.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1110,6 +1115,7 @@ pub(crate) fn sensed(ctx: &Context, roots: &IdSet, widgets: &IdSet) -> Sensed {
|
||||
}
|
||||
}
|
||||
sensed.over.extend(under.over.iter().copied().filter(|root| roots.contains(root)));
|
||||
sensed.parts.extend(under.parts.iter().copied().filter(|(root, _)| roots.contains(root)));
|
||||
}
|
||||
if let Some((contains, click, drag)) = &record.hover_widgets
|
||||
&& !widgets.is_empty()
|
||||
@@ -1125,6 +1131,17 @@ pub(crate) fn hover_point(ctx: &Context) -> Option<Pos2> {
|
||||
with_frame(ctx, |_, hover| hover)
|
||||
}
|
||||
|
||||
/// The own parts of the root `root` with a shape under the running pass's hover point, on top or not
|
||||
/// (`Under::parts`), by the pass's one resolution: the parts that contain the pointer.
|
||||
pub(crate) fn parts_under(ctx: &Context, root: Id) -> SmallVec<[PartRef; 4]> {
|
||||
PerViewport::<Shapes>::with(ctx, pass::current_viewport(ctx), |record| {
|
||||
record
|
||||
.hover
|
||||
.as_ref()
|
||||
.map_or_else(SmallVec::new, |(_, _, under)| under.parts.iter().filter(|(owner, _)| *owner == root).map(|(_, part)| *part).collect())
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether a shape the component `id` registered shows hover in the running pass, by the pass's one resolution of
|
||||
/// what lies under the pointer: it takes the press there, or it is the topmost shape and takes none. A
|
||||
/// container reads it for the components in its body before they run. While a press is open it holds for none: only
|
||||
@@ -1193,6 +1210,14 @@ fn find_under(ctx: &Context, viewport: ViewportId, at: Pos2, targets: PressTarge
|
||||
if !under.over.contains(&root) {
|
||||
under.over.push(root);
|
||||
}
|
||||
for &index in &kept {
|
||||
let shape = &entry.shapes[index];
|
||||
if let Some(part) = shape.part.filter(|_| shape.owner == root)
|
||||
&& !under.parts.contains(&(root, part))
|
||||
{
|
||||
under.parts.push((root, part));
|
||||
}
|
||||
}
|
||||
// The winner among the shapes at `indices` by `kind`, with its rank.
|
||||
let pick = |indices: &[usize], kind: ResolveFor| {
|
||||
let mut shapes: Vec<Shape<Id>> = indices.iter().map(|&index| entry.shapes[index].clone()).collect();
|
||||
|
||||
@@ -889,3 +889,82 @@ fn a_move_that_changes_nothing_under_the_pointer_needs_no_pass() {
|
||||
assert!(needs(Some(follow.center()), follow.center() + Vec2::new(5.0, 0.0)), "a move in a rect that follows the pointer runs a pass");
|
||||
assert!(needs(None, inside), "the pointer coming in runs a pass");
|
||||
}
|
||||
|
||||
/// The part of `Knob` over its start half.
|
||||
const KNOB: PartKey = PartKey::new("knob");
|
||||
|
||||
/// A leaf whose knob part covers its start half, and which logs where the pointer hovers on the knob each pass
|
||||
/// (`Cx::hover_pos`).
|
||||
#[derive(craft_ui::contract::Component)]
|
||||
struct Knob<'a> {
|
||||
#[common]
|
||||
common: craft_ui::contract::Common,
|
||||
seen: &'a RefCell<Vec<Option<Pos2>>>,
|
||||
}
|
||||
|
||||
impl craft_ui::contract::Component for Knob<'_> {
|
||||
const ROLE: craft_ui::contract::Role = craft_ui::contract::Role::GROUP;
|
||||
type Style = craft_ui_test_kit::contract::PlainStyle;
|
||||
type Output = egui::Response;
|
||||
|
||||
fn style(_: &craft_ui::style::StyleCx<'_>, _: KnobStyleKey) -> Self::Style {
|
||||
craft_ui_test_kit::contract::PlainStyle::min(Vec2::new(200.0, 40.0))
|
||||
}
|
||||
|
||||
fn parts(&mut self, rect: Rect, cx: &mut craft_ui::contract::PartsCx<'_, Self>) {
|
||||
cx.part(KNOB, Rect::from_min_max(rect.min, rect.center_bottom()));
|
||||
}
|
||||
|
||||
fn update(&mut self, cx: &mut craft_ui::contract::Cx<'_, Self>) {
|
||||
use craft_ui::contract::Pointer as _;
|
||||
self.seen.borrow_mut().push(cx.hover_pos(KNOB));
|
||||
}
|
||||
|
||||
fn finish(self, cx: craft_ui::contract::Cx<'_, Self>) -> egui::Response {
|
||||
cx.response.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// The knob at `KNOB_AT`, logging into `seen`.
|
||||
fn show_knob(seen: &RefCell<Vec<Option<Pos2>>>) -> impl FnMut(&mut Ui) + '_ {
|
||||
use craft_ui::contract::Show as _;
|
||||
move |ui: &mut Ui| {
|
||||
Knob { common: craft_ui::contract::Common::new(), seen }.show_at(ui, KNOB_AT);
|
||||
}
|
||||
}
|
||||
|
||||
const KNOB_AT: Rect = Rect { min: Pos2::new(100.0, 100.0), max: Pos2::new(300.0, 140.0) };
|
||||
|
||||
/// `Cx::hover_pos` gives the hover point while the part is hovered, and nothing over the rest of the component.
|
||||
#[trial]
|
||||
fn hover_pos_reads_the_point_over_its_part_only() {
|
||||
let seen = RefCell::new(Vec::new());
|
||||
let mut show = show_knob(&seen);
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, &mut show);
|
||||
let on_knob = Pos2::new(130.0, 120.0);
|
||||
frames.frame(vec![pointer::moved(on_knob)], &mut show);
|
||||
assert_eq!(seen.borrow().last(), Some(&Some(on_knob)), "the point over the knob, in the component's layer space");
|
||||
frames.frame(vec![pointer::moved(Pos2::new(260.0, 120.0))], &mut show);
|
||||
assert_eq!(seen.borrow().last(), Some(&None), "no point over the rest of the component");
|
||||
}
|
||||
|
||||
/// A component that read the hover position on its part follows the pointer there: a move within the part needs a
|
||||
/// pass (`move_needs_pass`); over the rest of the component, which it read nothing on, a move needs none.
|
||||
#[trial]
|
||||
fn a_hover_position_read_follows_the_pointer() {
|
||||
use craft_ui::runtime::pointer::move_needs_pass;
|
||||
let seen = RefCell::new(Vec::new());
|
||||
let mut show = show_knob(&seen);
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, &mut show);
|
||||
let (on_knob, off_knob) = (Pos2::new(130.0, 120.0), Pos2::new(260.0, 120.0));
|
||||
let step = Vec2::new(5.0, 0.0);
|
||||
frames.frame(vec![pointer::moved(on_knob)], &mut show);
|
||||
assert!(move_needs_pass(&frames.ctx, egui::ViewportId::ROOT, Some(on_knob), on_knob + step), "a move within the read part runs a pass");
|
||||
frames.frame(vec![pointer::moved(off_knob)], &mut show);
|
||||
assert!(
|
||||
!move_needs_pass(&frames.ctx, egui::ViewportId::ROOT, Some(off_knob), off_knob + step),
|
||||
"a move over the rest of the component, which read no position, runs none"
|
||||
);
|
||||
}
|
||||
@@ -4,16 +4,21 @@
|
||||
//! does not run it.
|
||||
|
||||
use crate::values::kit::{AT, INITIAL, Scrub, Seen};
|
||||
use craft_behavior::MotionMode;
|
||||
use craft_theme::{Contrast, Mode, Size};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::choice::{Radio, RadioGroup};
|
||||
use craft_ui::contract::{Bind, CommandSet, MemoProps, MemoView, Props as _, Show as _, ShowContainer as _, Sizing, Tracked, bind_tracked};
|
||||
use craft_ui::choice::{Checkbox, Radio, RadioGroup};
|
||||
use craft_ui::contract::{
|
||||
Bind, CommandSet, MemoProps, MemoView, Props as _, Show as _, ShowCollection as _, ShowContainer as _, Sizing, Tracked, bind_tracked,
|
||||
};
|
||||
use craft_ui::display::Spinner;
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::list::{List, ListRow};
|
||||
use craft_ui::runtime::commands::{CommandInput, CommandScope, CommandState, CommandStates, Commands as _};
|
||||
use craft_ui::runtime::env::RootEnv;
|
||||
use craft_ui::runtime::{frame, inspect};
|
||||
use craft_ui::testing::layout::in_region;
|
||||
use craft_ui::testing::pointer::{moved, press, release};
|
||||
use craft_ui::testing::pointer::{moved, press, release, wheel};
|
||||
use craft_ui::testing::{Fixture, Frames, MemoRun, last_frame_memo_runs, last_pass_measuring_runs, trial};
|
||||
use egui::{Context, Id, Pos2, Rect, Ui, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
@@ -30,6 +35,8 @@ thread_local! {
|
||||
static SEEN: Seen = Seen::default();
|
||||
/// The id and rect of the button each view of `Scene` showed when it last ran.
|
||||
static BUTTONS: [Cell<(Id, Rect)>; 2] = const { [Cell::new((Id::NULL, Rect::NOTHING)), Cell::new((Id::NULL, Rect::NOTHING))] };
|
||||
/// The rect of each row of the rows view, by key, as its last run built it.
|
||||
static ROW_RECTS: RefCell<Vec<Rect>> = const { RefCell::new(Vec::new()) };
|
||||
/// The buttons of `Shown`, `Beside`, `Published` and `Measured`, as the command scene's views last showed them.
|
||||
static COMMAND_BUTTONS: [Cell<Id>; 4] = const { [Cell::new(Id::NULL), Cell::new(Id::NULL), Cell::new(Id::NULL), Cell::new(Id::NULL)] };
|
||||
}
|
||||
@@ -251,6 +258,199 @@ fn a_view_that_follows_the_pointer_runs_on_every_move() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A press on a part of a settled view runs that view, and only it, and so does its release.
|
||||
#[trial]
|
||||
fn a_press_and_its_release_run_the_view() {
|
||||
let scene = Scene::new(false, false);
|
||||
let mut frames = Frames::new();
|
||||
scene.settle(&mut frames);
|
||||
let at = Scene::button(1).1.center();
|
||||
scene.frame(&mut frames, vec![moved(at)]);
|
||||
scene.settle(&mut frames);
|
||||
let [reader, other] = scene.frame(&mut frames, vec![press(at)]);
|
||||
assert!(other > 0 && reader == 0, "the press runs the view under it alone: {reader} {other}");
|
||||
let [reader, other] = scene.frame(&mut frames, vec![release(at)]);
|
||||
assert!(other > 0 && reader == 0, "the release runs it alone: {reader} {other}");
|
||||
}
|
||||
|
||||
/// A modifier change runs the settled view under the pointer, whose parts sense with the held modifiers, and not the
|
||||
/// view beside it; both settle again with the modifier held.
|
||||
#[trial]
|
||||
fn a_modifier_change_under_the_pointer_runs_the_view() {
|
||||
let scene = Scene::new(false, false);
|
||||
let mut frames = Frames::new();
|
||||
scene.settle(&mut frames);
|
||||
scene.frame(&mut frames, vec![moved(Scene::button(1).1.center())]);
|
||||
scene.settle(&mut frames);
|
||||
frames.hold(egui::Modifiers::SHIFT);
|
||||
let [reader, other] = scene.frame(&mut frames, vec![]);
|
||||
assert!(other > 0, "Shift going down runs the view under the pointer");
|
||||
assert_eq!(reader, 0, "the view away from the pointer stays settled");
|
||||
scene.settle(&mut frames);
|
||||
assert_eq!(scene.frame(&mut frames, vec![]), [0, 0], "with Shift held, both views settle again");
|
||||
}
|
||||
|
||||
/// Frames with motion off: a hover shows and fades in one frame, with no fade that runs its view.
|
||||
fn still_frames() -> Frames {
|
||||
let mut frames = Frames::new();
|
||||
frames.root(RootEnv::default().motion(MotionMode::Off));
|
||||
frames
|
||||
}
|
||||
|
||||
/// A view showing a list of `count` rows, which notes each row's rect as it builds it.
|
||||
#[derive(MemoProps)]
|
||||
struct Rows {
|
||||
count: usize,
|
||||
}
|
||||
|
||||
fn rows(props: &mut Rows, ui: &mut Ui) {
|
||||
let keys: Vec<usize> = (0..props.count).collect();
|
||||
let source = craft_behavior::keyed(&keys, |key: &usize| *key);
|
||||
List::new("Rows").show_items(ui, &source, &mut |key, item| {
|
||||
ROW_RECTS.with(|rects| {
|
||||
let mut rects = rects.borrow_mut();
|
||||
if rects.len() <= *key {
|
||||
rects.resize(*key + 1, Rect::NOTHING);
|
||||
}
|
||||
rects[*key] = item.rect;
|
||||
});
|
||||
ListRow::new(format!("Row {key}"))
|
||||
});
|
||||
}
|
||||
|
||||
/// The rect of the row `key` of the rows view, as its last run built it.
|
||||
fn row_rect(key: usize) -> Rect {
|
||||
ROW_RECTS.with(|rects| rects.borrow()[key])
|
||||
}
|
||||
|
||||
/// The rows view of `count` rows in the scene's room.
|
||||
fn show_rows(count: usize) -> impl Fn(&mut Ui) + Copy {
|
||||
move |ui: &mut Ui| {
|
||||
in_region(ui, Rect::from_min_size(ORIGIN, ROOM), |ui| {
|
||||
MemoView::new("rows", Rows { count }, rows).show(ui);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// The fills painted as rects inside `row` across most of its height: a row's hover fill among them.
|
||||
fn fills_in(shapes: &[egui::Shape], row: Rect) -> usize {
|
||||
shapes
|
||||
.iter()
|
||||
.filter(|shape| {
|
||||
matches!(shape, egui::Shape::Rect(rect)
|
||||
if rect.fill.a() > 0 && row.expand(0.5).contains_rect(rect.rect) && rect.rect.height() > row.height() / 2.0)
|
||||
})
|
||||
.count()
|
||||
}
|
||||
|
||||
/// A pointer moving within one row of a list in a settled view replays the view; a move onto another row runs it
|
||||
/// once, and the hover fill moves to that row. A list of 10 rows, and one of 40 that scrolls.
|
||||
#[trial]
|
||||
fn a_list_in_a_settled_view_runs_only_when_the_pointer_changes_rows() {
|
||||
for count in [10, 40] {
|
||||
let show = show_rows(count);
|
||||
let mut frames = still_frames();
|
||||
frames.idle(30, show);
|
||||
let (first, second) = (row_rect(1), row_rect(2));
|
||||
frames.frame(vec![moved(first.center())], show);
|
||||
frames.idle(30, show);
|
||||
for step in 1..=3 {
|
||||
frames.frame(vec![moved(first.center() + Vec2::new(10.0 * step as f32, 0.0))], show);
|
||||
assert_eq!(runs_of(&frames.ctx, "Rows"), 0, "{count} rows: move {step} within the row replays the view");
|
||||
}
|
||||
let shapes = frames.frame_shapes(show);
|
||||
assert!(fills_in(&shapes, first) > 0, "{count} rows: the row under the pointer shows the hover fill");
|
||||
assert_eq!(fills_in(&shapes, second), 0, "{count} rows: the next row shows none");
|
||||
frames.frame(vec![moved(second.center())], show);
|
||||
assert_eq!(runs_of(&frames.ctx, "Rows"), 1, "{count} rows: the move onto the next row runs the view once");
|
||||
frames.idle(30, show);
|
||||
assert_eq!(runs_of(&frames.ctx, "Rows"), 0, "{count} rows: the view settles again");
|
||||
let shapes = frames.frame_shapes(show);
|
||||
assert!(fills_in(&shapes, second) > 0, "{count} rows: the row the pointer reached shows the hover fill");
|
||||
assert_eq!(fills_in(&shapes, first), 0, "{count} rows: the row it left shows none");
|
||||
}
|
||||
}
|
||||
|
||||
/// A wheel over the rows of a list in a settled view runs the view, whose list takes the wheel and scrolls.
|
||||
#[trial]
|
||||
fn a_wheel_over_a_scroll_area_in_a_settled_view_runs_it() {
|
||||
let show = show_rows(40);
|
||||
let mut frames = still_frames();
|
||||
frames.idle(30, show);
|
||||
let before = row_rect(1);
|
||||
frames.frame(vec![moved(before.center())], show);
|
||||
frames.idle(30, show);
|
||||
frames.frame(vec![wheel(-30.0)], show);
|
||||
assert!(runs_of(&frames.ctx, "Rows") > 0, "the wheel runs the view whose list takes it");
|
||||
frames.idle(30, show);
|
||||
assert_ne!(row_rect(1), before, "the rows scrolled");
|
||||
}
|
||||
|
||||
/// A view showing a check box, whose hover asks for the pointing hand.
|
||||
#[derive(MemoProps)]
|
||||
struct Checked {}
|
||||
|
||||
fn checked(_: &mut Checked, ui: &mut Ui) {
|
||||
let response = Checkbox::new(Bind::given(true)).label("Grid").show(ui).response;
|
||||
BUTTONS.with(|buttons| buttons[0].set((response.id, response.rect)));
|
||||
}
|
||||
|
||||
/// A settled view replayed under the pointer asks for the cursor its run asked for.
|
||||
#[trial]
|
||||
fn a_replayed_hovered_view_keeps_its_cursor() {
|
||||
let show = |ui: &mut Ui| {
|
||||
in_region(ui, Rect::from_min_size(ORIGIN, ROOM), |ui| {
|
||||
MemoView::new("checked", Checked {}, checked).show(ui);
|
||||
});
|
||||
};
|
||||
let mut frames = still_frames();
|
||||
frames.idle(30, show);
|
||||
let at = Scene::button(0).1.center();
|
||||
frames.frame(vec![moved(at)], show);
|
||||
assert_eq!(frames.cursor_icon(), egui::CursorIcon::PointingHand, "the hovered check box asks for the pointing hand");
|
||||
frames.idle(30, show);
|
||||
frames.frame(vec![moved(at + Vec2::new(1.0, 0.0))], show);
|
||||
assert_eq!(runs_of(&frames.ctx, "Checked"), 0, "the move within the check box replays the view");
|
||||
assert_eq!(frames.cursor_icon(), egui::CursorIcon::PointingHand, "the replay asks for the cursor its run asked for");
|
||||
}
|
||||
|
||||
/// A view showing a button with a tooltip.
|
||||
#[derive(MemoProps)]
|
||||
struct Tipped {}
|
||||
|
||||
fn tipped(_: &mut Tipped, ui: &mut Ui) {
|
||||
let response = Button::new("Delta").tooltip("Hint").show(ui).response;
|
||||
BUTTONS.with(|buttons| buttons[0].set((response.id, response.rect)));
|
||||
}
|
||||
|
||||
/// A tooltip shows once the pointer has rested on its button for the delay: the settled view replays while it waits,
|
||||
/// runs once at the wake its run asked for, which opens the tooltip's layer, and once more in the next frame, when the
|
||||
/// layer, sized in the first, paints the tooltip; then it replays with the tooltip shown.
|
||||
#[trial]
|
||||
fn a_tooltip_shows_after_its_delay_with_one_run_at_its_wake() {
|
||||
let show = |ui: &mut Ui| {
|
||||
in_region(ui, Rect::from_min_size(ORIGIN, ROOM), |ui| {
|
||||
MemoView::new("tipped", Tipped {}, tipped).show(ui);
|
||||
});
|
||||
};
|
||||
let mut frames = still_frames();
|
||||
frames.idle(30, show);
|
||||
frames.frame(vec![moved(Scene::button(0).1.center())], show);
|
||||
frames.idle(2, show);
|
||||
// A second of frames, past the delay.
|
||||
let seen: Vec<(usize, bool)> = (0..60)
|
||||
.map(|_| {
|
||||
frames.frame(vec![], show);
|
||||
(runs_of(&frames.ctx, "Tipped"), painted(&frames, "Hint"))
|
||||
})
|
||||
.collect();
|
||||
let shown = seen.iter().position(|(_, shown)| *shown).expect("the tooltip shows within a second");
|
||||
let ran: Vec<usize> = (0..seen.len()).filter(|&frame| seen[frame].0 > 0).collect();
|
||||
assert!(shown > 0, "the tooltip waits for its delay: {seen:?}");
|
||||
assert_eq!(ran, [shown - 1, shown], "the view runs at the wake and in the frame the tooltip first paints: {seen:?}");
|
||||
assert!(seen[shown..].iter().all(|(_, shown)| *shown), "the tooltip stays shown: {seen:?}");
|
||||
}
|
||||
|
||||
/// Drags the editor right in steps; returns per frame from the press on the passes, the runs of each view, and whether
|
||||
/// the reader showed the dragged value.
|
||||
fn drag(scene: &Scene) -> Vec<(usize, [usize; 2], bool)> {
|
||||
|
||||
Reference in new issue
Block a user