diff --git a/crates/behavior/src/value.rs b/crates/behavior/src/value.rs index ab5b556661..e93508bf1d 100644 --- a/crates/behavior/src/value.rs +++ b/crates/behavior/src/value.rs @@ -117,6 +117,20 @@ impl CancelReason { } } +/// Why a captured press ended, as the reason the gesture it carried ends without a commit. +impl From for CancelReason { + fn from(cancel: crate::press::PressCancel) -> Self { + use crate::press::PressCancel; + match cancel { + PressCancel::Escape => CancelReason::Escape, + PressCancel::Removed => CancelReason::Removed, + // `cancel_press_of`: the host's own state no longer matches what the press acts on. + PressCancel::Host => CancelReason::HostChanged, + _ => CancelReason::Blur, + } + } +} + /// What applying an event did to a value. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[non_exhaustive] diff --git a/crates/ui-contract/src/contract/items/drag.rs b/crates/ui-contract/src/contract/items/drag.rs index 03401daa00..3675277205 100644 --- a/crates/ui-contract/src/contract/items/drag.rs +++ b/crates/ui-contract/src/contract/items/drag.rs @@ -20,8 +20,7 @@ use crate::runtime::egui::areas; use crate::runtime::env::Label; use crate::runtime::toggle_sweep::{self, Segment, Travel}; use craft_behavior::{ - Axis, CancelReason, ColumnKey, CopyKeys, DropOperation, EditEvent, Flow, GestureId, ItemLayout, Mixed, NavShape, Outline, PointerButton, - PressCancel, PressPhase, ScrollPos, + Axis, ColumnKey, CopyKeys, DropOperation, EditEvent, Flow, GestureId, ItemLayout, Mixed, NavShape, Outline, PointerButton, PressPhase, ScrollPos, }; use egui::{Pos2, Rect, Vec2}; use std::any::Any; @@ -399,14 +398,6 @@ impl DropTarget for KindDrop { } } -fn cancel_reason(cancel: PressCancel) -> CancelReason { - match cancel { - PressCancel::Escape => CancelReason::Escape, - PressCancel::Removed => CancelReason::Removed, - _ => CancelReason::Blur, - } -} - impl ItemsHooks<'_, '_, C> { /// Scrolls the rows while the pointer, `y` below the rows' top edge, is within a row of an edge of the `view`, /// faster the further past it, up to `EDGE_ROWS_PER_SECOND` a full band in or beyond the edge: the rate of a row @@ -813,10 +804,10 @@ impl ItemsHooks<'_, '_, C> { } PressPhase::Cancel { reason, .. } => { if let Some(gesture) = self.drags.moving.take() { - self.events.push(CollectionEvent::Move(EditEvent::cancel(gesture, cancel_reason(reason), Some(Mixed::Same(Vec2::ZERO))))); + self.events.push(CollectionEvent::Move(EditEvent::cancel(gesture, reason.into(), Some(Mixed::Same(Vec2::ZERO))))); } for (gesture, before, _) in self.drags.sweep.take().map(|sweep| sweep.open).unwrap_or_default() { - self.events.push(CollectionEvent::Toggle(EditEvent::cancel(gesture, cancel_reason(reason), Some(Mixed::Same(before))))); + self.events.push(CollectionEvent::Toggle(EditEvent::cancel(gesture, reason.into(), Some(Mixed::Same(before))))); } } _ => {} diff --git a/crates/ui-widgets/src/graph/annotation.rs b/crates/ui-widgets/src/graph/annotation.rs index 04ace3219a..cb0d29a359 100644 --- a/crates/ui-widgets/src/graph/annotation.rs +++ b/crates/ui-widgets/src/graph/annotation.rs @@ -7,8 +7,8 @@ use crate::fonts::FontRef; use crate::primitives::{DocRun, DocText, Draw, IconSpec, PaintShape, StrokeWidth, TextRun}; use crate::style::{DataColor, Flags, StyleColor, UiTokens}; use craft_behavior::{ - CancelReason, CanvasTransform, EditEvent, GestureId, HitLevel, HitPriority, IconId, Mixed, PartKey, PointerButton, PressCancel, PressPhase, - PressStart, Selection, Sense, + CancelReason, CanvasTransform, EditEvent, GestureId, HitLevel, HitPriority, IconId, Mixed, PartKey, PointerButton, PressPhase, PressStart, + PressState, Selection, Sense, }; use egui::{CornerRadius, Pos2, Rect, Vec2}; @@ -190,7 +190,14 @@ fn label_rect(pin: Rect, label: Vec2) -> Rect { /// Paints the backdrop `backdrop` shown at `frame`: its fill, its edge (wider when selected), the description wrapped /// and clipped inside, and the grip. The title above is the caller's, which lays it out to place it. -fn paint_backdrop(draw: &Draw<'_>, frame: Rect, backdrop: &Backdrop<'_>, sizes: &Sizes, style: &NodeGraphStyle, look: (StyleColor, StyleColor, bool)) { +fn paint_backdrop( + draw: &Draw<'_>, + frame: Rect, + backdrop: &Backdrop<'_>, + sizes: &Sizes, + style: &NodeGraphStyle, + look: (StyleColor, StyleColor, bool), +) { let (edge, ink, selected) = look; let shape = PaintShape::Rect(frame, CornerRadius::ZERO); draw.data_fill(&shape, backdrop.fill); @@ -222,18 +229,7 @@ fn paint_marker(draw: &Draw<'_>, pin: Rect, marker: &Marker<'_>, style: &NodeGra let bounds = pin.union(label_rect(pin, size)); draw.text(label_rect(pin, size), &label); if let Some(edge) = selected { - draw.stroke(&PaintShape::Rect(bounds, CornerRadius::ZERO), StrokeWidth::Thin, edge); - } -} - -/// Why a captured press ended, as the reason its gesture ends without a commit. -fn cancel_reason(reason: PressCancel) -> CancelReason { - match reason { - PressCancel::Escape => CancelReason::Escape, - PressCancel::FocusLost => CancelReason::Blur, - PressCancel::Removed | PressCancel::Disabled => CancelReason::Removed, - PressCancel::Host => CancelReason::HostChanged, - _ => CancelReason::Dismissed, + draw.stroke(&PaintShape::Rect(bounds, style.marker_radius), StrokeWidth::Thin, edge); } } @@ -248,8 +244,7 @@ pub(super) struct Layers<'a> { pub(super) backdrops: &'a [Backdrop<'a>], pub(super) notes: &'a [Note<'a>], pub(super) markers: &'a [Marker<'a>], - // PLAN: the parts' selected flags and the paint's accent need the selected annotation, which ge.C29 does not - // carry; graph-w4 builds the layers with `Layers::of(self)`. + /// The selected annotation, read from the graph's binding this pass: the parts' flags and the paint's accent. pub(super) selected: Option, } @@ -345,7 +340,8 @@ impl<'a> Layers<'a> { } for marker in self.markers { let pin = pin_rect(origin + zoom.to_layer(marker.at).to_vec2(), sizes.icon); - let selected = (self.selected == Some(AnnotationKey::Marker(marker.id))).then(|| cx.resolve_part(MARKER.at(marker.id), &style.backdrop_edge)); + let selected = + (self.selected == Some(AnnotationKey::Marker(marker.id))).then(|| cx.resolve_part(MARKER.at(marker.id), &style.backdrop_edge)); paint_marker(&draw, pin, marker, style, selected); } } @@ -380,17 +376,17 @@ impl<'a> Layers<'a> { let parts: Vec<_> = parts.collect(); for (part, key, resize) in parts { let Some(result) = cx.part_if_declared(part) else { continue }; - let phase = result.press.phase; - if let PressPhase::Down { start, .. } = phase + let press = result.press; + if let PressPhase::Down { start, .. } = press.phase && start.button == PointerButton::Primary { pressed = Some(key); } - self.gesture(phase, key, resize, (zoom, min), cx, &mut events); + self.gesture(press, key, resize, (zoom, min), cx, &mut events); } let current = annotations.as_ref().map_or(self.selected, |bind| bind.value().copied().flatten()); - let press = crate::runtime::pointer::frame(&cx.core.ctx).press; - let elsewhere = pressed.is_none() && press.is_some_and(|press| cx.rect.contains(press.origin)); + // The collection's own press: a press on a node or on empty space, not one on a wire or on a control over the graph. + let elsewhere = pressed.is_none() && matches!(cx.press().phase, PressPhase::Down { start, .. } if start.button == PointerButton::Primary); let label = cx.core.label_arc(); let target = match pressed { Some(key) => Some(Some(key)), @@ -415,13 +411,13 @@ impl<'a> Layers<'a> { events } - /// What `phase` on the part of `key` adds to `events`: a drag runs a `Move`, or a `Resize` of the backdrop at + /// What `press` on the part of `key` adds to `events`: a drag runs a `Move`, or a `Resize` of the backdrop at /// `resize`, as Begin, Update, then Commit or Cancel, in document units; a secondary click asks for the menu at the /// release point, in the graph's layer space, which is its parent's. A press that comes up where it went down /// changes nothing. fn gesture( &self, - phase: PressPhase, + press: PressState, key: AnnotationKey, resize: Option, (zoom, min): (CanvasTransform, Vec2), @@ -439,7 +435,7 @@ impl<'a> Layers<'a> { }, }; let before = || Mixed::Same(Vec2::ZERO); - match phase { + match press.phase { PressPhase::DragStart { start, pos, .. } if start.button == PointerButton::Primary => { let id = gesture_of(start); events.push(report(EditEvent::begin(id, before(), cx.core.label_arc(), None))); @@ -460,12 +456,9 @@ impl<'a> Layers<'a> { PressPhase::Release { start, pos, click: true, .. } if start.button == PointerButton::Secondary => { events.push(AnnotationEvent::ContextMenu { key, at: pos }); } - PressPhase::Cancel { start, reason, .. } if start.button == PointerButton::Primary => { - // Only a press that moved past the slop began a gesture; the pointer still lies where the drag reached. - let pointer = crate::runtime::pointer::frame(&cx.core.ctx); - if pointer.pos.is_some_and(|pos| pos.distance(start.origin) > pointer.slop) { - events.push(report(EditEvent::cancel(gesture_of(start), cancel_reason(reason), Some(before())))); - } + // Only a press that passed the slop began a gesture. + PressPhase::Cancel { start, reason, .. } if start.button == PointerButton::Primary && press.dragged() => { + events.push(report(EditEvent::cancel(gesture_of(start), reason.into(), Some(before())))); } _ => {} } diff --git a/crates/ui-widgets/src/graph/annotation_stories.rs b/crates/ui-widgets/src/graph/annotation_stories.rs index 42a0856304..639d5cf2e4 100644 --- a/crates/ui-widgets/src/graph/annotation_stories.rs +++ b/crates/ui-widgets/src/graph/annotation_stories.rs @@ -36,10 +36,8 @@ impl NodeKind<()> for Step { const STEP: Step = Step; /// The two nodes' rects, side by side, in document units. -const NODES: [Rect; 2] = [ - Rect { min: Pos2::new(48.0, 64.0), max: Pos2::new(188.0, 136.0) }, - Rect { min: Pos2::new(228.0, 64.0), max: Pos2::new(368.0, 136.0) }, -]; +const NODES: [Rect; 2] = + [Rect { min: Pos2::new(48.0, 64.0), max: Pos2::new(188.0, 136.0) }, Rect { min: Pos2::new(228.0, 64.0), max: Pos2::new(368.0, 136.0) }]; /// A backdrop around both nodes, with room for its description below them. const FRAME: Rect = Rect { min: Pos2::new(32.0, 40.0), max: Pos2::new(384.0, 200.0) }; @@ -115,13 +113,13 @@ fn select_each(fixture: &mut Fixture) { } /// The backdrop selected; the drive selects the note and then the marker in turn. -#[story(NodeGraph<'static, ()>, covers = [], drive = Drive::Custom(select_each))] +#[story(NodeGraph<'static, ()>, covers = [Many], drive = Drive::Custom(select_each))] fn annotation_selected(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { annotated_graph(ui, data, &Annotated { frame: FRAME, notes: &NOTES, markers: &MARKERS, selected: Some(AnnotationKey::Backdrop(0)) }) } /// The backdrop's grip pressed and moved: the backdrop takes the size the drag reports. -#[story(NodeGraph<'static, ()>, covers = [], drive = Drive::Custom(drag_grip))] +#[story(NodeGraph<'static, ()>, covers = [Many], drive = Drive::Custom(drag_grip))] fn backdrop_resizing(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { annotated_graph(ui, data, &Annotated { frame: FRAME, notes: &[], markers: &[], selected: Some(AnnotationKey::Backdrop(0)) }) } diff --git a/crates/ui-widgets/src/graph/mod.rs b/crates/ui-widgets/src/graph/mod.rs index e08c73c18f..b5fe547070 100644 --- a/crates/ui-widgets/src/graph/mod.rs +++ b/crates/ui-widgets/src/graph/mod.rs @@ -7,7 +7,7 @@ mod annotation; #[cfg(feature = "suite")] -mod annotation_stories; +pub(crate) mod annotation_stories; mod kind; pub(crate) mod node; #[cfg(feature = "suite")] @@ -22,14 +22,14 @@ use crate::contract::{ PartsCx, Pointer as _, Role, RowState, SelectMode, Sizing, Styled as _, }; use crate::primitives::{PaintShape, StrokeWidth}; -use crate::style::{ComponentStyle, Flags, Stateful, States, StyleColor, StyleCx, SurfaceKind, TypeRole}; +use crate::style::{ComponentStyle, Flags, Stateful, States, StyleColor, StyleCx, SurfaceKind, TypeRole, UiTokens}; use annotation::Layers; use craft_behavior::{ CanvasTransform, EditEvent, FreeLayout, HitLevel, HitPriority, HitShape, ItemSource, Metrics, Mixed, PartKey, PartRef, PointerButton, PressPhase, Selection, SelectionChange, SelectionModel, Sense, Value, }; use craft_keymap::Mods; -use egui::{Pos2, Rect, Response, Vec2}; +use egui::{CornerRadius, Pos2, Rect, Response, Vec2}; use node::port_center; const WIRE: PartKey = PartKey::new("wire"); @@ -112,6 +112,8 @@ pub struct NodeGraphStyle { note_text: Stateful, /// The ink of a marker. marker: StyleColor, + /// The corners of a selected marker's outline. + marker_radius: CornerRadius, /// The ink of a backdrop's resize grip. grip: StyleColor, /// The least size a backdrop resizes to. @@ -364,6 +366,7 @@ impl Component for NodeGraph<'_, E> { text_role: TypeRole::Body, note_text: Stateful::new(cx.color(color.text.normal)).when(Flags::SELECTED, accent), marker: accent, + marker_radius: UiTokens::corner(cx.tokens.shape.radius.sm), grip: cx.color(color.text.muted), backdrop_min: Vec2::splat(2.0 * cx.tokens.size.hit.target), grip_side: cx.tokens.size.handle.md, @@ -385,7 +388,7 @@ impl Component for NodeGraph<'_, E> { .sense(Sense::CLICK | Sense::DRAG | Sense::CONTEXT_CLICK) .priority(HitPriority::new(HitLevel::Edge, 0)) .flags(flags) - .pointer_only("the graph's wire commands act on the selected wire"); + .pointer_only("the graph's wire commands act on the selected wire: Insert reroute dot, Swap, Delete links"); } } @@ -484,8 +487,7 @@ impl Collection for NodeGraph<'_, E> { /// Hands each node the wires that feed its inputs, by id and source port, and when its ports' names show. The /// node owns what it is given, since the graph's `wires` cannot be lent for the item's `'r`. - fn dress<'r>(&self, node: Node<'r, E>, _: &RowState) -> Node<'r, E> { - // PLAN: graph-w3 adds `Node::id()` and `Node::placed(Pos2)` in `node.rs`. + fn dress<'r>(&self, node: Self::Item<'r>, _: &RowState) -> Self::Item<'r> { let id = node.id(); let place = self.positions.get(id.0 as usize).map_or(Pos2::ZERO, |rect| rect.min); let mut fed = vec![None; self.kinds.get(id.0 as usize).map_or(0, |kind| kind.port_count())]; diff --git a/crates/ui-widgets/src/graph/node.rs b/crates/ui-widgets/src/graph/node.rs index 65874ea20e..63bcd63e45 100644 --- a/crates/ui-widgets/src/graph/node.rs +++ b/crates/ui-widgets/src/graph/node.rs @@ -6,9 +6,7 @@ use crate::contract::{ Pointer as _, Props as _, Role, Styled as _, }; use crate::primitives::{Draw, HAlign, PaintShape, StrokeWidth, TextRun}; -use crate::style::{ - ComponentStyle, Corners, Flags, HandleLook, RuleKind, Stateful, StyleColor, StyleCx, SurfaceKind, SurfaceLook, TypeRole, -}; +use crate::style::{ComponentStyle, Corners, Flags, HandleLook, RuleKind, Stateful, StyleColor, StyleCx, SurfaceKind, SurfaceLook, TypeRole}; use craft_behavior::{CursorIcon, HitLevel, HitPriority, HitShape, Insets, Metrics, PartKey, PointerButton, PressPhase, PressState, Sense}; use craft_theme::Rgba; use egui::{CornerRadius, Pos2, Rect, Response, Vec2};