From ff5f067baaa25e8d8540514a4f9925c5d00745b6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 05:02:29 -0500 Subject: [PATCH] pdf-canvas-w3: net change of 2 commits - pdf-canvas-w3: written from its sheet - pdf-canvas-w3: review fixes (review-1) --- crates/behavior/src/hit.rs | 25 +++- crates/behavior/src/overlay.rs | 14 ++ crates/behavior/src/press.rs | 118 ++++++++++++++++- crates/behavior/src/sense.rs | 5 +- .../ui-canvas/src/canvas_controls/task_bar.rs | 120 +++++++++++------- .../src/canvas_controls/task_bar/stories.rs | 33 +++++ crates/ui-canvas/tests/it/canvas/tool.rs | 15 +++ .../tests/it/canvas_controls/task_bar.rs | 84 +++++++++++- crates/ui-contract/src/contract/cx/parts.rs | 4 +- crates/ui-contract/src/primitives/sensing.rs | 25 +++- crates/ui-contract/src/runtime/pointer.rs | 34 +++-- 11 files changed, 398 insertions(+), 79 deletions(-) diff --git a/crates/behavior/src/hit.rs b/crates/behavior/src/hit.rs index dea8839aa..a131f52ee 100644 --- a/crates/behavior/src/hit.rs +++ b/crates/behavior/src/hit.rs @@ -237,6 +237,12 @@ impl Shape { self } + /// The shape's stacking order among shapes of its priority: the higher order wins before the nearest centre. + pub const fn stacking(mut self, order: u32) -> Self { + self.stacking = order; + self + } + /// The shape lies in the body layer's space. pub const fn local(mut self, local: bool) -> Self { self.local = local; @@ -276,8 +282,8 @@ pub struct Winner { pub local: bool, } -/// The winner among `shapes` at `point`: stack position first, then level, sub-level, the nearest centre, the last -/// registered. A press considers only shapes that sense a click or a drag. +/// The winner among `shapes` at `point`: stack position first, then level, sub-level, stacking order, the nearest +/// centre, the last registered. A press considers only shapes that sense a click or a drag. pub fn resolve(shapes: &[Shape], point: Pos2, what: ResolveFor) -> Option> { resolve_mapped(shapes, point, what, |_| point) } @@ -292,8 +298,8 @@ pub fn resolve_mapped(shapes: &[Shape], point: Pos2, what: ResolveFo .filter(|(_, shape)| what == ResolveFor::Hover || shape.sense.takes_press()) .filter(|(_, shape)| shape.hits(at(shape))) .max_by(|(ia, a), (ib, b)| { - (a.position, a.priority.level, a.priority.sub) - .cmp(&(b.position, b.priority.level, b.priority.sub)) + (a.position, a.priority.level, a.priority.sub, a.stacking) + .cmp(&(b.position, b.priority.level, b.priority.sub, b.stacking)) .then_with(|| distance(b).partial_cmp(&distance(a)).unwrap_or(Ordering::Equal)) .then(ia.cmp(ib)) }) @@ -333,4 +339,15 @@ mod tests { let tie = [shape(big(), Sense::CLICK, 1, 0, HitLevel::Body, 0), shape(big(), Sense::CLICK, 2, 0, HitLevel::Body, 0)]; assert_eq!(owner(&tie), Some(2)); } + + #[test] + fn stacked_parts_of_one_priority_rank_by_their_order_before_the_nearest_centre() { + let at = pos2(50.0, 50.0); + let owner = |shapes: &[Shape]| resolve(shapes, at, ResolveFor::Press).map(|w| w.owner); + let near = || shape(rect(40.0, 40.0, 20.0, 20.0), Sense::CLICK, 1, 0, HitLevel::Body, 0); + let far = || shape(rect(0.0, 0.0, 200.0, 200.0), Sense::CLICK, 2, 0, HitLevel::Body, 0); + assert_eq!(owner(&[far(), near()]), Some(1), "unstacked, the nearer centre wins"); + assert_eq!(owner(&[far().stacking(1), near()]), Some(2), "the higher stacking order wins though the press is nearer the other's centre"); + assert_eq!(owner(&[near().stacking(1), far().stacking(1)]), Some(1), "one order: the nearest centre decides"); + } } diff --git a/crates/behavior/src/overlay.rs b/crates/behavior/src/overlay.rs index 84b75ee02..c020cf7c4 100644 --- a/crates/behavior/src/overlay.rs +++ b/crates/behavior/src/overlay.rs @@ -275,6 +275,20 @@ mod tests { assert_eq!(neither.bottom(), BOUNDS.bottom(), "fits neither way: slides up to stay inside"); } + #[test] + fn an_overlay_with_no_room_on_its_side_opens_on_the_other() { + let size = Vec2::new(200.0, 80.0); + let low = rect(300.0, 540.0, 100.0, 30.0); + let placed = place(&PlaceRequest::new(low, size, BOUNDS).side(Side::Below).gap(4.0)); + assert_eq!(placed.side, Side::Above, "no room below the anchor: {placed:?}"); + assert!(placed.rect.bottom() <= low.top(), "above the anchor: {placed:?}"); + assert!(BOUNDS.contains_rect(placed.rect), "{placed:?}"); + let high = rect(300.0, 100.0, 100.0, 30.0); + let placed = place(&PlaceRequest::new(high, size, BOUNDS).side(Side::Below).gap(4.0)); + assert_eq!(placed.side, Side::Below, "room below: {placed:?}"); + assert!(placed.rect.top() >= high.bottom(), "below the anchor: {placed:?}"); + } + #[test] fn an_overlay_at_least_as_wide_as_its_anchor_takes_that_width_below_it_but_not_beside_it() { let field = rect(100.0, 100.0, 300.0, 24.0); diff --git a/crates/behavior/src/press.rs b/crates/behavior/src/press.rs index d33f1470f..8e5a50d94 100644 --- a/crates/behavior/src/press.rs +++ b/crates/behavior/src/press.rs @@ -474,6 +474,11 @@ struct Open { dragging: bool, /// Moved past slop on a target that senses no drag: the click is gone and nothing starts. abandoned: bool, + /// Moved past slop in this pass on a target that clicks and passes its drag (`Sense::PASSES_DRAG`), with the + /// pointer at the pass's end: no click and no drag until [`PressMachine::hand_over`] gives the press its new + /// target. The next pass abandons a press no shape took. + + passing: Option, /// A pass reported the press: later passes within slop report `Held`, not `Down`. reported: bool, /// A pass reported the drag's start: later passes report `Drag`. @@ -485,14 +490,14 @@ struct Open { } impl Open { - /// A phase before the release is still to be reported. + /// A phase before the release is still to be reported, or the press waits for its hand-over. fn owes_phase(&self) -> bool { - !self.reported || (self.dragging && !self.drag_reported) + !self.reported || (self.dragging && !self.drag_reported) || self.passing.is_some() } /// A release now is a click. fn clicks(&self) -> bool { - !self.dragging && !self.abandoned && self.sense.contains(Sense::CLICK) + !self.dragging && !self.abandoned && self.passing.is_none() && self.sense.contains(Sense::CLICK) } } @@ -541,6 +546,14 @@ impl PressMachine { let mut pos = input.pos; let mut released: Option<(T, PressStart, Pos2, bool, bool)> = None; let mut cancelled: Option<(T, PressStart, PressCancel)> = None; + // A press passed in the last pass that no shape took is abandoned; that pass reported its `Down`. + if let Some(open) = &mut self.open + && open.passing.is_some() + { + open.passing = None; + open.abandoned = true; + open.reported = true; + } if let Some(open) = self.open && let Some((at, click)) = open.released_at && !open.owes_phase() @@ -560,10 +573,13 @@ impl PressMachine { && open.released_at.is_none() && !open.dragging && !open.abandoned + && open.passing.is_none() && at.distance(open.start.origin) > input.slop { if open.sense.contains(Sense::DRAG) { open.dragging = true; + } else if open.sense.contains(Sense::CLICK | Sense::PASSES_DRAG) { + open.passing = Some(at); } else { open.abandoned = true; } @@ -610,6 +626,7 @@ impl PressMachine { start: PressStart::new(at, button, time, clicks), dragging: false, abandoned: false, + passing: None, reported: false, drag_reported: false, released_at: None, @@ -649,6 +666,9 @@ impl PressMachine { }; } let Some(open) = self.open.as_mut() else { return PressStep::default() }; + if let Some(at) = &mut open.passing { + *at = pos; + } let phase = if !open.reported { PressPhase::Down { start: open.start } } else if open.dragging && !open.drag_reported { @@ -659,16 +679,32 @@ impl PressMachine { } else { PressPhase::Held { start: open.start, pos } }; - open.reported = true; - PressStep { target: Some(open.target), phase: Some(phase), dragging: open.drag_reported, activated: None, passes: None } + // A passing press owes its `Down` to the shape `hand_over` gives it when this pass is its first. + if open.passing.is_none() { + open.reported = true; + } + let passes = open.passing.map(|_| open.start); + PressStep { target: Some(open.target), phase: Some(phase), dragging: open.drag_reported, activated: None, passes } } /// Makes `target` the open press's target in the pass that reported `passes`, dragging from the same /// `PressStart`. It returns the `DragStart` step when an earlier pass reported the press, and the `Down` step /// (the `DragStart` follows in the next pass) when the move past slop came in the pass of the press itself. + /// `None` when no press passes in this pass or `sense` has no `DRAG`; the next pass abandons the press. pub fn hand_over(&mut self, target: T, sense: Sense) -> Option> { - let _ = (target, sense); - todo!("CV-C12") + let open = self.open.as_mut().filter(|open| !open.abandoned && sense.contains(Sense::DRAG))?; + let at = open.passing.take()?; + open.target = target; + open.sense = sense; + open.dragging = true; + let phase = if open.reported { + open.drag_reported = true; + PressPhase::DragStart { start: open.start, pos: at } + } else { + open.reported = true; + PressPhase::Down { start: open.start } + }; + Some(PressStep { target: Some(target), phase: Some(phase), dragging: open.drag_reported, activated: None, passes: None }) } } @@ -770,4 +806,72 @@ mod tests { let passes: [&[PointerEvent]; 5] = [&[press(0.0, 1.0)], &[up, second], &[far, up], &[], &[]]; assert_eq!(phases(&passes, Sense::CLICK | Sense::DRAG), "D R! D S R"); } + + const PASSES: Sense = Sense::CLICK.union(Sense::PASSES_DRAG); + + /// One pass with the button held at its end, over a root whose press winner is target 1 and passes its drag. + fn passing_pass(machine: &mut PressMachine, events: &[PointerEvent]) -> PressStep { + machine.step(PassInput::new(events, SLOP, pos2(0.0, 0.0), true), |_| Some((1, PASSES))) + } + + fn moved(x: f32) -> PointerEvent { + PointerEvent::Moved { at: pos2(x, 0.0), sample: None } + } + + fn up(x: f32) -> PointerEvent { + PointerEvent::released(pos2(x, 0.0)) + } + + #[test] + fn a_drag_past_slop_on_a_part_that_passes_it_goes_to_the_shape_below() { + let origin = pos2(0.0, 0.0); + assert_eq!(phases(&[&[press(0.0, 1.0)], &[up(SLOP / 2.0)]], PASSES), "D R!", "within slop the press is the shape's click"); + + // The press was reported in an earlier pass: the shape below drags from the pass of the move. + let mut machine = PressMachine::new(); + let down = passing_pass(&mut machine, &[press(0.0, 1.0)]); + assert!(matches!(down.phase, Some(PressPhase::Down { .. })) && down.passes.is_none(), "{down:?}"); + assert!(machine.hand_over(2, Sense::DRAG).is_none(), "a press within slop passes nothing"); + let passed = passing_pass(&mut machine, &[moved(40.0)]); + assert_eq!(passed.passes.map(|start| start.origin), Some(origin), "{passed:?}"); + let handed = machine.hand_over(2, Sense::DRAG).expect("the shape below takes the drag"); + assert_eq!(handed.target, Some(2)); + assert!( + matches!(handed.phase, Some(PressPhase::DragStart { start, pos }) if start.origin == origin && pos == pos2(40.0, 0.0)), + "{:?}", + handed.phase + ); + let drag = passing_pass(&mut machine, &[moved(60.0)]); + assert_eq!(drag.target, Some(2)); + assert!(matches!(drag.phase, Some(PressPhase::Drag { .. })), "{:?}", drag.phase); + let end = passing_pass(&mut machine, &[up(60.0)]); + assert_eq!(end.target, Some(2)); + assert!(matches!(end.phase, Some(PressPhase::Release { click: false, .. })), "{:?}", end.phase); + assert_eq!(end.activated, None, "a passed press is no click"); + + // The move past slop came in the press's own pass: the shape below gets the Down, its DragStart next pass, + // and a release in between waits for it. + let mut machine = PressMachine::new(); + let first = passing_pass(&mut machine, &[press(0.0, 1.0), moved(40.0)]); + assert!(matches!(first.phase, Some(PressPhase::Down { .. })) && first.passes.is_some(), "{first:?}"); + let handed = machine.hand_over(2, Sense::DRAG).expect("the shape below takes the drag"); + assert_eq!(handed.target, Some(2)); + assert!(matches!(handed.phase, Some(PressPhase::Down { start }) if start.origin == origin), "{:?}", handed.phase); + let start = passing_pass(&mut machine, &[up(40.0)]); + assert_eq!(start.target, Some(2)); + assert!(matches!(start.phase, Some(PressPhase::DragStart { .. })), "{:?}", start.phase); + assert!(machine.release_held(), "the release waits for the owed DragStart"); + let end = passing_pass(&mut machine, &[]); + assert_eq!(end.target, Some(2)); + assert!(matches!(end.phase, Some(PressPhase::Release { click: false, .. })), "{:?}", end.phase); + + // No shape took the passed press: it is abandoned, and its release is no click. + let mut machine = PressMachine::new(); + let _ = passing_pass(&mut machine, &[press(0.0, 1.0)]); + assert!(passing_pass(&mut machine, &[moved(40.0)]).passes.is_some()); + let end = passing_pass(&mut machine, &[up(40.0)]); + assert_eq!(end.target, Some(1)); + assert!(matches!(end.phase, Some(PressPhase::Release { click: false, .. })), "{:?}", end.phase); + assert_eq!(end.activated, None); + } } diff --git a/crates/behavior/src/sense.rs b/crates/behavior/src/sense.rs index 1ce093c50..3d3faca04 100644 --- a/crates/behavior/src/sense.rs +++ b/crates/behavior/src/sense.rs @@ -50,8 +50,9 @@ bitflags! { /// with `DRAG` by a part whose click does nothing a drag could be mistaken for (a transform box's handles). /// The component begins the gesture from `PressPhase::Held`. const FOLLOWS_IN_SLOP = 65536; - /// A shape that clicks and passes its drag: within slop the press stays its click; past slop the drag goes to - /// the shape below it that senses a drag. + /// A press on this shape that moves past slop is not the shape's: the press passes to the shape ranked under + /// it that senses a drag, with its start. Released within slop it is the shape's click. Declared with `CLICK`, + /// without `DRAG`. const PASSES_DRAG = 131072; } } diff --git a/crates/ui-canvas/src/canvas_controls/task_bar.rs b/crates/ui-canvas/src/canvas_controls/task_bar.rs index d9cb52acb..8db69249a 100644 --- a/crates/ui-canvas/src/canvas_controls/task_bar.rs +++ b/crates/ui-canvas/src/canvas_controls/task_bar.rs @@ -1,4 +1,5 @@ -//! `TaskBar`: the contextual task bar over a canvas, hung below its anchor until the user moves it. +//! `TaskBar`: the contextual task bar over a canvas: hung below its anchor until the user moves it, fixed beside its +//! anchor, or fixed at the canvas view's top edge. #[cfg(feature = "testing")] mod stories; @@ -20,7 +21,7 @@ use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; use craft_behavior::kurbo::Point; use craft_behavior::overlay::{Align, Side}; use craft_behavior::{EditEvent, IconId, Metrics, Mixed, Sense}; -use egui::{Pos2, Rect, Response, Ui}; +use egui::{Pos2, Rect, Response, Ui, Vec2}; use std::sync::Arc; /// Where the task bar sits: pinned or following its anchor, and where the user moved it. The one owner of the bar's @@ -50,9 +51,10 @@ impl TaskBarPlace { } } -/// The contextual task bar: a movable modeless layer hung below the screen bounds of `anchor` while its place is -/// unset, with a body of members and a More menu (Hide, Reset, Pin). One place, pin and Hide setting for Crop and -/// Frame. +/// The contextual task bar: a modeless layer with a body of members. +/// +/// A movable bar ([`TaskBar::new`]) hangs below the screen bounds of its anchor while its place is unset, with a More +/// menu (Hide, Reset, Pin). One place, pin and Hide setting for Crop and Frame. /// /// - Place: centred under the anchor's bounds on screen, a section gap below them, following the anchor each pass. /// Dragged by its grip (any free point of the bar), it stays where it is dropped and keeps its left edge while the @@ -61,19 +63,32 @@ impl TaskBarPlace { /// back inside, its left and top edges winning where the bar does not fit. /// - Menu: Hide bar reports `hide`; Reset bar position clears the place, so the bar follows its anchor again; Pin bar /// position keeps it where it shows. -/// - Nothing shows outside a canvas, or while the anchor's bounds or `area` is not finite or `area` has no size. /// - `place` is the caller's value (`Bind::bound`): the bar reads it before it shows and writes the pass's change /// after. +/// +/// A fixed bar ([`TaskBar::fixed`]) hangs centred a section gap below its anchor's bounds, above them when there is no +/// room below, and follows the anchor. An edge bar ([`TaskBar::at_edge`]) hangs at the top edge of `area`, the canvas +/// view's rect, and takes no anchor. Neither has a grip, a move or a More menu, and neither reports `hide`. +/// +/// Nothing shows outside a canvas, while `area` is not finite or has no size, or, for an anchored bar, while the +/// anchor's bounds are not finite. #[must_use = "show the task bar with `show`"] #[derive(crate::contract::Pattern, crate::contract::Props)] pub struct TaskBar<'a> { label: Label, - anchor: &'a [Point], + hang: Hang<'a>, place: Bind<'a, TaskBarPlace>, - #[allow(dead_code)] // P3(pdf-canvas-w3): CV-C15 kind: TaskBarKind, } +/// What a bar hangs from: the screen bounds of document points, or the canvas view's top edge, aligned in reading +/// direction. +#[derive(Clone, Copy)] +enum Hang<'a> { + Points(&'a [Point]), + Edge(Align), +} + /// Whether a task bar follows its anchor and moves, or stays where it hangs. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[non_exhaustive] @@ -112,7 +127,7 @@ pub struct TaskBarCx<'a> { impl TaskBarCx<'_> { /// Places the More menu button here, before the members that follow it (the Crop bar puts Cancel and Done after - /// it); without the call the button comes after the body. + /// it); without the call the button comes after the body. A fixed bar has no More menu: the call places nothing. pub fn more_menu(&mut self, ui: &mut Ui) { self.menu.place(ui); } @@ -129,14 +144,16 @@ enum MenuPick { /// The More button and its menu, placed once per run of the bar's body. struct MoreMenu { words: Arc, + /// The bar offers the menu: a movable bar does, a fixed one does not. + offered: bool, placed: bool, pick: Option, } impl MoreMenu { - /// Shows the button and its menu here unless this run of the body showed them already. + /// Shows the button and its menu here unless the bar offers none or this run of the body showed them already. fn place(&mut self, ui: &mut Ui) { - if std::mem::replace(&mut self.placed, true) { + if !self.offered || std::mem::replace(&mut self.placed, true) { return; } let words = Arc::clone(&self.words); @@ -161,46 +178,54 @@ impl MoreMenu { impl<'a> TaskBar<'a> { /// A bar named `label` hung below the screen bounds of `anchor` (document points) until `place` moves it. pub fn new(label: impl Into