From 0599e9862ecf871cb5c4d938f77cd6a9a9ca3aa8 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 07:35:19 +0000 Subject: [PATCH] Runtime: a component clicks by the press lifecycle; egui's click options stay the app's --- crates/pages/src/ui/column_bar/stories.rs | 2 +- crates/pages/src/ui/margins/stories.rs | 2 +- crates/ui-canvas/src/canvas/crop/stories.rs | 2 +- .../ui-canvas/src/canvas/navigate/stories.rs | 2 +- crates/ui-canvas/src/canvas/rulers/stories.rs | 2 +- .../src/canvas/scrollbars/stories.rs | 2 +- crates/ui-canvas/src/canvas/split/stories.rs | 2 +- .../src/canvas_controls/guides/stories.rs | 2 +- .../ui-contract/src/contract/body/window.rs | 2 +- .../ui-contract/src/contract/items/press.rs | 8 +- crates/ui-contract/src/primitives/sensing.rs | 45 ++++++---- .../ui-contract/src/runtime/egui/fixture.rs | 32 +++---- crates/ui-contract/src/runtime/egui/input.rs | 20 ----- .../ui-contract/src/runtime/egui/response.rs | 86 +++++++++++-------- crates/ui-contract/src/runtime/frame.rs | 8 +- crates/ui-contract/src/runtime/pointer.rs | 4 +- crates/ui-contract/src/testing/contract.rs | 5 -- .../src/testing/host_key_trials.rs | 51 ++++++++++- crates/ui-contract/src/testing/input.rs | 11 +-- crates/ui-contract/src/testing/suite/drive.rs | 2 +- .../ui-contract/src/testing/suite/pointer.rs | 4 +- crates/ui-panels/src/docking/stories.rs | 2 +- crates/ui-widgets/src/buttons/button.rs | 6 ++ docs/craft-framework.md | 1 + 24 files changed, 180 insertions(+), 123 deletions(-) diff --git a/crates/pages/src/ui/column_bar/stories.rs b/crates/pages/src/ui/column_bar/stories.rs index e9fec70e7..46ad88d2b 100644 --- a/crates/pages/src/ui/column_bar/stories.rs +++ b/crates/pages/src/ui/column_bar/stories.rs @@ -8,7 +8,7 @@ use egui::Ui; fn drag_first_divider(fixture: &mut Fixture) { let Some(divider) = fixture.by_label("Column 1 width") else { return }; - let travel = 2.0 * craft_ui::testing::input::max_click_dist(fixture.ctx()); + let travel = 2.0 * craft_ui::testing::input::slop(fixture.ctx()); fixture.drag(÷r, egui::Vec2::new(travel, 0.0)); } diff --git a/crates/pages/src/ui/margins/stories.rs b/crates/pages/src/ui/margins/stories.rs index 592290c25..311dea411 100644 --- a/crates/pages/src/ui/margins/stories.rs +++ b/crates/pages/src/ui/margins/stories.rs @@ -19,7 +19,7 @@ const DRAG_THRESHOLDS: f32 = 12.0; fn drag_left_edge(fixture: &mut Fixture) { let Some(target) = fixture.by_label("Left Margin") else { return }; - let travel = DRAG_THRESHOLDS * craft_ui::testing::input::max_click_dist(fixture.ctx()); + let travel = DRAG_THRESHOLDS * craft_ui::testing::input::slop(fixture.ctx()); fixture.drag(&target, Vec2::new(travel, 0.0)); } diff --git a/crates/ui-canvas/src/canvas/crop/stories.rs b/crates/ui-canvas/src/canvas/crop/stories.rs index db2818c93..af7c742d6 100644 --- a/crates/ui-canvas/src/canvas/crop/stories.rs +++ b/crates/ui-canvas/src/canvas/crop/stories.rs @@ -64,7 +64,7 @@ fn polygon(points: &[Point]) -> BezPath { fn drag_top_left(fixture: &mut Fixture) { let Some(id) = fixture.root_id() else { return }; let Some(grip) = fixture.part(id, FRAME.at(BoxPart::Corner(0).index())) else { return }; - let travel = 2.0 * crate::testing::input::max_click_dist(fixture.ctx()); + let travel = 2.0 * crate::testing::input::slop(fixture.ctx()); fixture.drag(&grip, Vec2::new(travel, 0.0)); } diff --git a/crates/ui-canvas/src/canvas/navigate/stories.rs b/crates/ui-canvas/src/canvas/navigate/stories.rs index 8b2bfcb53..cf96f5477 100644 --- a/crates/ui-canvas/src/canvas/navigate/stories.rs +++ b/crates/ui-canvas/src/canvas/navigate/stories.rs @@ -31,7 +31,7 @@ fn in_corner(ui: &mut Ui, rotation: f64, mut compass: impl FnMut(&mut Ui, egui:: fn drag_rim(fixture: &mut Fixture) { let Some(id) = fixture.root_id() else { return }; let Some(rim) = fixture.part(id, RIM) else { return }; - let travel = 2.0 * crate::testing::input::max_click_dist(fixture.ctx()); + let travel = 2.0 * crate::testing::input::slop(fixture.ctx()); fixture.drag(&rim, Vec2::new(travel, 0.0)); } diff --git a/crates/ui-canvas/src/canvas/rulers/stories.rs b/crates/ui-canvas/src/canvas/rulers/stories.rs index a99a06724..49861242a 100644 --- a/crates/ui-canvas/src/canvas/rulers/stories.rs +++ b/crates/ui-canvas/src/canvas/rulers/stories.rs @@ -27,7 +27,7 @@ fn pixels() -> RulerText { fn drag_corner(fixture: &mut Fixture) { let Some(id) = fixture.root_id() else { return }; let Some(corner) = fixture.part(id, CORNER) else { return }; - let travel = DRAG_THRESHOLDS * crate::testing::input::max_click_dist(fixture.ctx()); + let travel = DRAG_THRESHOLDS * crate::testing::input::slop(fixture.ctx()); fixture.drag(&corner, Vec2::splat(travel)); } diff --git a/crates/ui-canvas/src/canvas/scrollbars/stories.rs b/crates/ui-canvas/src/canvas/scrollbars/stories.rs index 6b415d50b..940607344 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/stories.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/stories.rs @@ -39,7 +39,7 @@ fn view_over(ui: &Ui, rect: Rect) -> CanvasView { fn drag_thumb(fixture: &mut Fixture) { let Some(id) = fixture.root_id() else { return }; let Some(track) = fixture.part(id, HORIZONTAL) else { return }; - let threshold = crate::testing::input::max_click_dist(fixture.ctx()); + let threshold = crate::testing::input::slop(fixture.ctx()); let grip = Target { node: track.node, rect: Rect::from_center_size(track.rect.left_center() + vec2(GRIP_THRESHOLDS * threshold, 0.0), Vec2::ZERO) }; fixture.drag(&grip, Vec2::new(DRAG_THRESHOLDS * threshold, 0.0)); diff --git a/crates/ui-canvas/src/canvas/split/stories.rs b/crates/ui-canvas/src/canvas/split/stories.rs index 033f53d04..e26e50d6e 100644 --- a/crates/ui-canvas/src/canvas/split/stories.rs +++ b/crates/ui-canvas/src/canvas/split/stories.rs @@ -37,7 +37,7 @@ fn split(ui: &mut Ui, layout: SplitLayout, at: Bind<'_, SplitAt>, shown: SplitAt fn drag_divider(fixture: &mut Fixture) { let Some(id) = fixture.root_id() else { return }; let Some(grip) = fixture.part(id, DIVIDER.at(0)) else { return }; - let travel = 2.0 * crate::testing::input::max_click_dist(fixture.ctx()); + let travel = 2.0 * crate::testing::input::slop(fixture.ctx()); fixture.drag(&grip, Vec2::new(travel, 0.0)); } diff --git a/crates/ui-canvas/src/canvas_controls/guides/stories.rs b/crates/ui-canvas/src/canvas_controls/guides/stories.rs index 540070e0d..37c51a1d1 100644 --- a/crates/ui-canvas/src/canvas_controls/guides/stories.rs +++ b/crates/ui-canvas/src/canvas_controls/guides/stories.rs @@ -26,7 +26,7 @@ fn guides() -> Vec { fn drag_first(fixture: &mut Fixture) { let Some(id) = fixture.root_id() else { return }; let Some(line) = fixture.part(id, GuideKey::new(1).part(GUIDE)) else { return }; - let travel = 2.0 * crate::testing::input::max_click_dist(fixture.ctx()); + let travel = 2.0 * crate::testing::input::slop(fixture.ctx()); fixture.drag(&line, Vec2::new(travel, 0.0)); } diff --git a/crates/ui-contract/src/contract/body/window.rs b/crates/ui-contract/src/contract/body/window.rs index 505b2f5b7..97d330961 100644 --- a/crates/ui-contract/src/contract/body/window.rs +++ b/crates/ui-contract/src/contract/body/window.rs @@ -434,7 +434,7 @@ impl BodyHooks<'_> { }; let Some(to) = to.filter(|to| *to != moving.at.min) else { return }; if !moving.begun { - if to.distance(moving.start.min) <= input::slop(&ctx) { + if to.distance(moving.start.min) <= crate::runtime::frame::slop(&ctx) { return; } moving.begun = true; diff --git a/crates/ui-contract/src/contract/items/press.rs b/crates/ui-contract/src/contract/items/press.rs index d45b7a1af..ccbfc305e 100644 --- a/crates/ui-contract/src/contract/items/press.rs +++ b/crates/ui-contract/src/contract/items/press.rs @@ -348,10 +348,12 @@ impl ItemsHooks<'_, '_, C> { |hit: Option| hit.is_some_and(|hit| hit.owner == opener.item && hit.part == Some(opener.part)); let view = response::MemberView { hovered: on_part(outcome.hover), - pressed: on_part(outcome.target) && outcome.phase.is_some(), - clicked: on_part(outcome.activated), + press: response::PressView { + pressed: on_part(outcome.target) && outcome.phase.is_some_and(|phase| phase.button_down()), + clicked: on_part(outcome.activated), + ..response::PressView::default() + }, pointer: ctx.pointer_latest_pos(), - ..response::MemberView::default() }; let response = response::member(ui, opener_id(opener.item, opener.part), rect, enabled, view); Some((opener, response)) diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index a211f8546..61ebf4e38 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -7,7 +7,7 @@ use crate::runtime::egui::areas::{self, Layer, LayerMap}; use crate::runtime::egui::pass; -use crate::runtime::egui::response::{self, MemberView}; +use crate::runtime::egui::response::{self, MemberView, PressView}; use crate::runtime::env::ArenaId; use crate::runtime::frame::PerViewport; use crate::runtime::memory::sub; @@ -476,14 +476,11 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { let response = response::widget(ui, spec.id, Rect::from_min_max(shown.min, shown.max.max(shown.min)), sense); // Each edge or handle part that reaches outside the root rect gets its own widget, resolved as the root. let mut widgets = vec![spec.id, sub(spec.id, "transform")]; - for part in spec.own.iter().filter(|part| !part.over && part.priority.level >= HitLevel::Edge) { - let bounds = part.shape.bounds(); - if !spec.rect.contains_rect(bounds) { - let id = sub(spec.id, ("edge", part.key, part.index)); - response::widget(ui, id, bounds.intersect(clip), part.sense); - crate::runtime::egui::access::hide(ctx, id); - widgets.push(id); - } + for part in spec.own.iter().filter(|part| !part.over && own_widget(spec.rect, part)) { + let id = sub(spec.id, ("edge", part.key, part.index)); + response::widget(ui, id, part.shape.bounds().intersect(clip), part.sense); + crate::runtime::egui::access::hide(ctx, id); + widgets.push(id); } widgets.extend(over_parts(spec.own).map(|part| over_widget(spec.id, part))); let background = (!spec.own_sense.is_empty()) @@ -507,7 +504,12 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { pointer::record_shapes_in(ctx, read.viewport, layer, spec.id, own_shapes(spec.own, spec.id, read.target()).chain(background)); let mut response = response; let outcome = outcome_in(ctx, &read, arena, layer, spec.enabled, sense); - response::keep_press_flags(&mut response, outcome.target.is_some_and(|target| target.owner == spec.id)); + // The root's response reports the presses on its own widget: a part with a widget of its own reports in its + // part result, as a member does in its response. + let on_root = |hit: &Hit| { + hit.owner == spec.id && hit.part.is_none_or(|part| !spec.own.iter().any(|decl| decl.part_ref() == part && own_widget(spec.rect, decl))) + }; + response::press_flags(&mut response, press_view(&outcome, on_root, on_root)); let (activation, activated_member) = match outcome.activated { Some(hit) if hit.owner == spec.id => (true, None), // A press on a member's part is the part's activation; it does not pick the member. @@ -531,6 +533,12 @@ fn over_parts(own: &[PartDecl]) -> impl Iterator { own.iter().filter(|part| part.over && part.sense.takes_press()) } +/// Whether a root's own `part` has an egui widget beside the root's: an `over` part that takes a press, or an edge +/// or handle part that reaches outside the root's `rect`. +fn own_widget(rect: Rect, part: &PartDecl) -> bool { + if part.over { part.sense.takes_press() } else { part.priority.level >= HitLevel::Edge && !rect.contains_rect(part.shape.bounds()) } +} + fn over_widget(root: Id, part: &PartDecl) -> Id { sub(root, ("over", part.key, part.index)) } @@ -660,22 +668,29 @@ pub(crate) fn sense_members(ui: &Ui, arena: ArenaId, specs: &[MemberSpec<'_>]) - response: response::member(ui, spec.id, spec.rect, spec.enabled, view), parts: part_results_in(&outcome, &known, (mods, sample, pointer_pos), spec.id, spec.parts), press: shape_press_in(&outcome, mods, sample, spec.id), - activated: view.clicked, + activated: view.press.clicked, } }) .collect() } fn member_view(outcome: &Outcome, id: Id, local: bool, pointer_pos: Option) -> MemberView { - let phase = outcome.target.filter(|target| target.owner == id).and(outcome.phase); MemberView { hovered: outcome.hover.is_some_and(|hit| hit.owner == id), - pressed: phase.is_some(), + press: press_view(outcome, |hit| hit.owner == id, |hit| hit.owner == id && hit.part.is_none()), + pointer: pointer_pos.map(|at| if local { outcome.xf.map_or(at, |map| map.to_layer(at)) } else { at }), + } +} + +/// The press flags of the shapes `holds` takes, with the click of the shapes `clicks` takes. +fn press_view(outcome: &Outcome, holds: impl Fn(&Hit) -> bool, clicks: impl Fn(&Hit) -> bool) -> PressView { + let phase = outcome.target.filter(|target| holds(target)).and(outcome.phase); + PressView { + pressed: phase.is_some_and(|phase| phase.button_down()), drag_started: matches!(phase, Some(PressPhase::DragStart { .. })), dragged: phase.is_some_and(|phase| phase.is_drag()), drag_stopped: matches!(phase, Some(PressPhase::Release { click: false, .. })), - clicked: outcome.activated.is_some_and(|hit| hit.owner == id && hit.part.is_none()), - pointer: pointer_pos.map(|at| if local { outcome.xf.map_or(at, |map| map.to_layer(at)) } else { at }), + clicked: outcome.activated.is_some_and(|hit| clicks(&hit)), } } diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index 42a16c1fb..4cb845411 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -241,14 +241,18 @@ pub(crate) fn allow_zoom_keys(ctx: &Context) { ctx.options_mut(|options| options.zoom_with_keyboard = true); } -/// egui's longest movement that still counts as a click. -pub(crate) fn max_click_dist(ctx: &Context) -> f32 { - ctx.options(|options| options.input_options.max_click_dist) +/// Sets egui's click options, as an app does for its own widgets: the longest movement and the longest press that +/// still count as a click. +pub(crate) fn set_click_options(ctx: &Context, distance: f32, seconds: f64) { + ctx.options_mut(|options| { + options.input_options.max_click_dist = distance; + options.input_options.max_click_duration = seconds; + }); } -/// Sets egui's longest press that still counts as a click. -pub(crate) fn set_max_click_duration(ctx: &Context, seconds: f64) { - ctx.options_mut(|options| options.input_options.max_click_duration = seconds); +/// egui's click options: the longest movement and the longest press that still count as a click. +pub(crate) fn click_options(ctx: &Context) -> (f32, f64) { + ctx.options(|options| (options.input_options.max_click_dist, options.input_options.max_click_duration)) } /// A fresh context with no plugin and AccessKit on. @@ -347,9 +351,10 @@ pub(crate) fn host_focus(ctx: &Context, id: egui::Id) { ctx.memory_mut(|memory| memory.request_focus(id)); } -/// A host's own clickable egui widget `id` at `rect`, a focus stop for egui's own focus moves. -pub(crate) fn host_button(ui: &Ui, id: egui::Id, rect: Rect) { - ui.interact(rect, id, egui::Sense::click()); +/// A host's own clickable egui widget `id` at `rect`, a focus stop for egui's own focus moves; returns whether egui +/// reports a click on it, by egui's click options. +pub(crate) fn host_button(ui: &Ui, id: egui::Id, rect: Rect) -> bool { + ui.interact(rect, id, egui::Sense::click()).clicked() } /// A host's own egui drag area `id` at `rect`; returns whether egui reports it dragged and whether its drag stopped @@ -580,14 +585,9 @@ pub(crate) fn raw_widget(ui: &mut Ui, rect: egui::Rect) { ui.interact(rect, crate::runtime::egui::memory::unique("raw_widget"), egui::Sense::click()); } -/// Whether the `Response` reports a click. +/// Whether a component's `Response` reports a click, by pointer or by keyboard activation. pub(crate) fn clicked(response: &egui::Response) -> bool { - response.clicked() -} - -/// Whether the `Response` reports a primary-button pointer click. -pub(crate) fn clicked_by_primary(response: &egui::Response) -> bool { - response.clicked_by(egui::PointerButton::Primary) + super::response::clicked(response) } /// The widget id egui's hit test chose as the click target at the latest pointer position. diff --git a/crates/ui-contract/src/runtime/egui/input.rs b/crates/ui-contract/src/runtime/egui/input.rs index 764ca7dc5..58078ecb0 100644 --- a/crates/ui-contract/src/runtime/egui/input.rs +++ b/crates/ui-contract/src/runtime/egui/input.rs @@ -45,17 +45,6 @@ pub(crate) fn request_repaint_of(ctx: &Context, viewport: ViewportId) { ctx.request_repaint_of(viewport); } -/// H2: a click is a release within `slop`, whatever its duration, so egui's click and the press lifecycle agree. -pub(crate) fn set_click_options(ctx: &Context, slop: f32) { - let (dist, duration) = ctx.options(|options| (options.input_options.max_click_dist, options.input_options.max_click_duration)); - if dist != slop || duration != f64::INFINITY { - ctx.options_mut(|options| { - options.input_options.max_click_dist = slop; - options.input_options.max_click_duration = f64::INFINITY; - }); - } -} - /// The options the runtime owns at install: interface scale and quit are commands, so egui's own shortcuts go. pub(crate) fn clear_shortcuts(ctx: &Context) { ctx.options_mut(|options| { @@ -195,8 +184,6 @@ pub(crate) struct PointerRead { pub zoom: f32, /// The two-finger gesture: translation, zoom, rotation and centre. pub multi_touch: Option<(Vec2, f32, f32, Pos2)>, - /// egui's longest movement that still makes a click. - pub slop: f32, } const ALL_BUTTONS: [egui::PointerButton; 5] = [ @@ -217,14 +204,8 @@ fn button_of(button: egui::PointerButton) -> PointerButton { } } -/// How far a press may move and still be a click, in points. -pub(crate) fn slop(ctx: &Context) -> f32 { - ctx.options(|options| options.input_options.max_click_dist) -} - /// Converts the running pass's pointer input. pub(crate) fn pointer_read(ctx: &Context) -> PointerRead { - let slop = slop(ctx); let os = crate::runtime::windows::os_of(ctx); ctx.input(|input| { let mut events = Vec::new(); @@ -297,7 +278,6 @@ pub(crate) fn pointer_read(ctx: &Context) -> PointerRead { stirred, zoom: input.zoom_delta(), multi_touch, - slop, } }) } diff --git a/crates/ui-contract/src/runtime/egui/response.rs b/crates/ui-contract/src/runtime/egui/response.rs index ff6786fdb..53158498d 100644 --- a/crates/ui-contract/src/runtime/egui/response.rs +++ b/crates/ui-contract/src/runtime/egui/response.rs @@ -4,9 +4,10 @@ use craft_behavior::Sense; use egui::response::Flags; use egui::{Id, LayerId, Pos2, Rect, Response, Ui, Vec2}; -/// Whether the `Response` reports a click. +/// Whether a component's `Response` reports a click: a primary pointer click by the press lifecycle, or a key or +/// AccessKit activation. egui's own click options play no part. pub(crate) fn clicked(response: &Response) -> bool { - response.clicked() + response.flags.intersects(Flags::CLICKED | Flags::FAKE_PRIMARY_CLICKED) } /// Whether the press path released a pointer click on the component of `response`: a root's own widget or a @@ -53,17 +54,24 @@ pub(crate) fn is_pressed(response: &Response) -> bool { response.is_pointer_button_down_on() } -/// What a member's `Response` reports, from the arena's resolution. +/// What the press lifecycle says of one component's shapes in a pass, for its `Response`. #[derive(Clone, Copy, Debug, Default)] -pub(crate) struct MemberView { - pub hovered: bool, - /// The member holds the open press. +pub(crate) struct PressView { + /// The component holds the open press and the button is down. pub pressed: bool, pub drag_started: bool, pub dragged: bool, /// The press ended without a click. pub drag_stopped: bool, + /// A primary click. pub clicked: bool, +} + +/// What a member's `Response` reports, from the arena's resolution. +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct MemberView { + pub hovered: bool, + pub press: PressView, /// The pointer position in the member's layer space. pub pointer: Option, } @@ -76,28 +84,15 @@ pub(crate) fn member(ui: &Ui, id: Id, rect: Rect, enabled: bool, view: MemberVie fn member_on(ctx: &egui::Context, layer_id: LayerId, clip: Rect, id: Id, rect: Rect, enabled: bool, view: MemberView) -> Response { let mut flags = Flags::empty(); let hovered = enabled && view.hovered; - let mine = enabled && view.pressed; + // egui gives the interaction position through the pass the press ends in. + let mine = enabled && (view.press.pressed || view.press.drag_stopped || view.press.clicked); if enabled { flags |= Flags::ENABLED; } if hovered { flags |= Flags::CONTAINS_POINTER | Flags::HOVERED; } - if mine { - flags |= Flags::IS_POINTER_BUTTON_DOWN_ON; - if view.drag_started { - flags |= Flags::DRAG_STARTED | Flags::DRAGGED; - } else if view.dragged { - flags |= Flags::DRAGGED; - } - if view.drag_stopped { - flags |= Flags::DRAG_STOPPED; - } - } - if enabled && view.clicked { - flags |= Flags::CLICKED | Flags::HOVERED; - } - Response { + let mut response = Response { ctx: ctx.clone(), layer_id, id, @@ -107,23 +102,40 @@ fn member_on(ctx: &egui::Context, layer_id: LayerId, clip: Rect, id: Id, rect: R interact_pointer_pos_or_nan: if hovered || mine { view.pointer.unwrap_or(Pos2::NAN) } else { Pos2::NAN }, intrinsic_size_or_nan: Vec2::NAN, flags, - } + }; + press_flags(&mut response, view.press); + response } -/// Removes egui's click and drag flags from the root's `Response` unless the root's own shapes won the press: -/// a press on a member belongs to the member, and a press the press-target rule refused belongs to the widget -/// egui chose. -pub(crate) fn keep_press_flags(response: &mut Response, own: bool) { - if !own { - response.flags.remove( - Flags::CLICKED - | Flags::FAKE_PRIMARY_CLICKED - | Flags::LONG_TOUCHED - | Flags::DRAG_STARTED - | Flags::DRAGGED - | Flags::DRAG_STOPPED - | Flags::IS_POINTER_BUTTON_DOWN_ON, - ); +/// Sets the press flags of a component's `Response` from the press lifecycle, in place of egui's: a component clicks +/// and drags by the framework's rule (a release within the slop, whatever its duration), whatever click options the +/// app gives egui. A key or AccessKit click is step 17's (`rewrite_clicks`). A disabled response reports none. +pub(crate) fn press_flags(response: &mut Response, view: PressView) { + response.flags.remove( + Flags::CLICKED + | Flags::FAKE_PRIMARY_CLICKED + | Flags::LONG_TOUCHED + | Flags::DRAG_STARTED + | Flags::DRAGGED + | Flags::DRAG_STOPPED + | Flags::IS_POINTER_BUTTON_DOWN_ON, + ); + if !response.flags.contains(Flags::ENABLED) { + return; + } + if view.pressed { + response.flags |= Flags::IS_POINTER_BUTTON_DOWN_ON; + if view.drag_started { + response.flags |= Flags::DRAG_STARTED | Flags::DRAGGED; + } else if view.dragged { + response.flags |= Flags::DRAGGED; + } + if view.drag_stopped { + response.flags |= Flags::DRAG_STOPPED; + } + } + if view.clicked { + response.flags |= Flags::CLICKED | Flags::HOVERED; } } diff --git a/crates/ui-contract/src/runtime/frame.rs b/crates/ui-contract/src/runtime/frame.rs index 88bf75269..6e1ff0183 100644 --- a/crates/ui-contract/src/runtime/frame.rs +++ b/crates/ui-contract/src/runtime/frame.rs @@ -314,6 +314,12 @@ fn frame_hit(ctx: &Context) -> HitSizes { pass_state::with::(ctx, |state| state.hit.as_ref().map(|(_, hit)| *hit)).unwrap_or_else(|| resolved_hit(&AppSizes::default())) } +/// How far a press may move and still be a click, in points: the app's hit tolerances (`HitSizes::slop`). egui's own +/// click options stay the app's. +pub(crate) fn slop(ctx: &Context) -> f32 { + frame_hit(ctx).slop +} + /// Called by `install`: the passes the context ran before the plugin existed have no hooks to wait for. pub(crate) fn seed_hooked_passes(ctx: &Context) { let root_passes = pass::pass_nr(ctx, ViewportId::ROOT); @@ -514,8 +520,6 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) { // H0: one press per pass. let frame = pass::frame_nr(ctx, viewport); let held_back = PerViewport::::with(ctx, viewport, |record| raw_input::hold_back_presses(input, frame, &mut record.held)); - // H2: click options. - raw_input::set_click_options(ctx, frame_hit(ctx).slop); // H3: the key stage strips what the runtime owns; egui decides on a repaint from the events left. ime::input_hook(ctx, &mut input.events, viewport); let stripped = keys::input_hook(ctx, input); diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index a4708529f..a6317805c 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -125,7 +125,7 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes) { let mut events = read.events.clone(); for event in &mut events { if let PointerEvent::Pressed { button: PointerButton::Primary, pos, time, clicks, .. } = event { - *clicks = count_clicks(memory.last_press, *pos, *time, read.slop); + *clicks = count_clicks(memory.last_press, *pos, *time, hit.slop); memory.last_press = Some((*pos, *time, *clicks)); } } @@ -153,7 +153,7 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes) { multi_touch: read.multi_touch.map(|(translation, zoom, rotation, center)| MultiTouch::new(translation, zoom, rotation, center)), scroll, zoom: wheel_zoom, - slop: read.slop, + slop: hit.slop, target: hit.target, }; let pass_pointer = PassPointer { diff --git a/crates/ui-contract/src/testing/contract.rs b/crates/ui-contract/src/testing/contract.rs index d0e83625a..f7c0ae238 100644 --- a/crates/ui-contract/src/testing/contract.rs +++ b/crates/ui-contract/src/testing/contract.rs @@ -39,11 +39,6 @@ pub fn clicked(response: &egui::Response) -> bool { crate::runtime::egui::fixture::clicked(response) } -/// Whether `response` reports a primary-button pointer click. -pub fn clicked_by_primary(response: &egui::Response) -> bool { - crate::runtime::egui::fixture::clicked_by_primary(response) -} - /// Places a clickable egui widget in `rect` by hand, the way a bypass of the components does. pub fn raw_widget(ui: &mut egui::Ui, rect: egui::Rect) { crate::runtime::egui::fixture::raw_widget(ui, rect); diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 4ff57307c..50544f604 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -8,10 +8,11 @@ use crate::runtime::focus::{FocusTarget, KeepKeys, KeyUse, host_canvas, request} use crate::runtime::frame::{ChromeToggle, take_output}; use crate::runtime::keys::with_window_intents; use crate::testing::focus::{Stop, egui_focused, named_id, register_stop, widget}; -use crate::testing::input::{key_press, key_reached}; +use crate::testing::input::{key_press, key_reached, slop}; use crate::testing::pointer::{self, moved, press, release}; +use crate::testing::sensing::{self, ProbeRoot}; use crate::testing::{Frames, trial}; -use craft_behavior::{Input, Intent}; +use craft_behavior::{Input, Intent, Sense}; use egui::{Event, Id, ImeEvent, Key, Modifiers, Pos2, Rect, Ui, ViewportId}; use std::cell::RefCell; use std::rc::Rc; @@ -220,6 +221,52 @@ fn escape_ends_an_egui_drag_on_a_host_widget() { frames.frame(vec![release(far)], &mut show); } +/// A press at `at`, held `held` seconds and released `by` points to the right: whether the host's egui button and the +/// framework root reported a click. +fn click_gesture(frames: &mut Frames, show: &mut impl FnMut(&mut Ui), clicks: &RefCell<(bool, bool)>, at: Pos2, by: f32, held: f64) -> (bool, bool) { + *clicks.borrow_mut() = (false, false); + let to = at + egui::vec2(by, 0.0); + frames.frame(vec![moved(at)], &mut *show); + frames.frame(vec![press(at)], &mut *show); + frames.advance(held); + frames.frame(vec![moved(to)], &mut *show); + frames.frame(vec![release(to)], &mut *show); + frames.frame(Vec::new(), &mut *show); + *clicks.borrow() +} + +#[trial] +fn the_app_keeps_its_click_options_and_a_framework_root_clicks_by_its_own() { + let host_rect = Rect::from_min_size(Pos2::new(300.0, 300.0), egui::vec2(160.0, 40.0)); + let probe_rect = Rect::from_min_size(Pos2::new(300.0, 400.0), egui::vec2(160.0, 40.0)); + let clicks = Rc::new(RefCell::new((false, false))); + let shared = Rc::clone(&clicks); + let mut show = move |ui: &mut Ui| { + let host = fixture::host_button(ui, named_id("host button"), host_rect); + let reports = sensing::show(ui, &[ProbeRoot::new("probe", probe_rect, Sense::CLICK)]); + let mut seen = shared.borrow_mut(); + seen.0 |= host; + seen.1 |= reports.first().is_some_and(|report| report.root.clicked); + }; + let mut frames = Frames::new(); + let reach = slop(&frames.ctx); + // The app's own rule for its widgets: a longer reach than the framework's slop and a short press. + let (distance, seconds) = (4.0 * reach, 0.3); + fixture::set_click_options(&frames.ctx, distance, seconds); + frames.frame(Vec::new(), &mut show); + assert_eq!(fixture::click_options(&frames.ctx), (distance, seconds), "the runtime leaves the app's click options alone"); + + let long = 2.0 * seconds; + assert_eq!(click_gesture(&mut frames, &mut show, &clicks, host_rect.center(), 0.0, 0.0), (true, false), "a quick press clicks the host button"); + assert_eq!(click_gesture(&mut frames, &mut show, &clicks, probe_rect.center(), 0.0, 0.0), (false, true), "a quick press clicks the root"); + assert!(!click_gesture(&mut frames, &mut show, &clicks, host_rect.center(), 0.0, long).0, "a long press is no click by the app's rule"); + assert!(click_gesture(&mut frames, &mut show, &clicks, probe_rect.center(), 0.0, long).1, "the root clicks whatever the press's duration"); + let between = 2.0 * reach; + assert!(click_gesture(&mut frames, &mut show, &clicks, host_rect.center(), between, 0.0).0, "the app's reach is its own"); + assert!(!click_gesture(&mut frames, &mut show, &clicks, probe_rect.center(), between, 0.0).1, "the root's reach is the framework's slop"); + assert_eq!(fixture::click_options(&frames.ctx), (distance, seconds), "and stays so"); +} + #[trial] fn copy_with_a_label_selection_copies_the_label() { const TEXT: &str = "host label text"; diff --git a/crates/ui-contract/src/testing/input.rs b/crates/ui-contract/src/testing/input.rs index 9dfc49c45..09b5cc303 100644 --- a/crates/ui-contract/src/testing/input.rs +++ b/crates/ui-contract/src/testing/input.rs @@ -28,12 +28,7 @@ pub fn allow_zoom_keys(ctx: &Context) { fixture::allow_zoom_keys(ctx); } -/// egui's longest movement that still counts as a click. -pub fn max_click_dist(ctx: &Context) -> f32 { - fixture::max_click_dist(ctx) -} - -/// Sets egui's longest press that still counts as a click. -pub fn set_max_click_duration(ctx: &Context, seconds: f64) { - fixture::set_max_click_duration(ctx, seconds); +/// How far a press may move and still be a click, in points: the framework's slop. +pub fn slop(ctx: &Context) -> f32 { + crate::runtime::frame::slop(ctx) } diff --git a/crates/ui-contract/src/testing/suite/drive.rs b/crates/ui-contract/src/testing/suite/drive.rs index 77c347000..5e608e97c 100644 --- a/crates/ui-contract/src/testing/suite/drive.rs +++ b/crates/ui-contract/src/testing/suite/drive.rs @@ -161,7 +161,7 @@ fn step(fixture: &mut Fixture, variant: usize, target: &Target) { /// A drag from `from` by twice the slop along `toward`, ended by a release or, with `escape`, by Escape before it. fn drag(fixture: &mut Fixture, from: Pos2, toward: Vec2, escape: bool) { - let by = 2.0 * crate::testing::input::max_click_dist(fixture.ctx()) * toward; + let by = 2.0 * crate::testing::input::slop(fixture.ctx()) * toward; if !escape { fixture.drag_at(from, by); return; diff --git a/crates/ui-contract/src/testing/suite/pointer.rs b/crates/ui-contract/src/testing/suite/pointer.rs index d9f9c82b4..f13ea43e9 100644 --- a/crates/ui-contract/src/testing/suite/pointer.rs +++ b/crates/ui-contract/src/testing/suite/pointer.rs @@ -33,7 +33,7 @@ fn slop_separates_click_from_drag(subject: &Subject) -> Result<(), String> { let probe = subject.probe; for story in &subject.stories { let (fixture, plan) = subject.base(story).parts(); - let slop = input::max_click_dist(fixture.ctx()); + let slop = input::slop(fixture.ctx()); match plan { Plan::Activate if !probe.carries_value() && fixture.tree().declares_key(root_target(fixture).node, Declared::Click) => { let part = fixture.click_part().zip(fixture.root_id()); @@ -123,7 +123,7 @@ fn parts_take_presses_in_their_hit_areas(subject: &Subject) -> Result<(), String let off_middle = Pos2::new(grip.rect.center().x + 0.25 * grip.rect.width(), grip.rect.center().y); let mut fixture = input_fixture(story); let before = fixture.data().snapshot(); - let travel = 2.0 * input::max_click_dist(fixture.ctx()); + let travel = 2.0 * input::slop(fixture.ctx()); // A refused value is moved while the pointer drags and put back at the release, so it is read before the release. let refused = story.covers.contains(&Required::Invalid); let moved_live = Cell::new(false); diff --git a/crates/ui-panels/src/docking/stories.rs b/crates/ui-panels/src/docking/stories.rs index a53dacf2e..596b0e907 100644 --- a/crates/ui-panels/src/docking/stories.rs +++ b/crates/ui-panels/src/docking/stories.rs @@ -54,7 +54,7 @@ fn dock(ui: &mut Ui, bind: Bind<'_, Layout>, shown: &Layout) -> St fn drag_handle(fixture: &mut Fixture) { let Some(id) = fixture.root_id() else { return }; let Some(grip) = fixture.part(id, HANDLE.at(0)) else { return }; - let travel = 2.0 * crate::testing::input::max_click_dist(fixture.ctx()); + let travel = 2.0 * crate::testing::input::slop(fixture.ctx()); fixture.drag(&grip, Vec2::new(travel, 0.0)); } diff --git a/crates/ui-widgets/src/buttons/button.rs b/crates/ui-widgets/src/buttons/button.rs index b829299ff..cccacc26d 100644 --- a/crates/ui-widgets/src/buttons/button.rs +++ b/crates/ui-widgets/src/buttons/button.rs @@ -95,6 +95,12 @@ impl ButtonOutput { }; ButtonOutput { response: cx.response.clone(), clicks, mods } } + + /// Whether the button was clicked this pass, by pointer or by key, by the framework's click rule. It stands in + /// for egui's `Response::clicked`, which also needs a click by egui's input options, which are the app's. + pub fn clicked(&self) -> bool { + crate::contract::clicked(&self.response) + } } impl Deref for ButtonOutput { diff --git a/docs/craft-framework.md b/docs/craft-framework.md index a16c0225a..1d9251e21 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -158,6 +158,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. ` | An app's own focused widget | While egui focus is on a widget no framework component made, or an egui popup is open, and no framework press or drag is captured, Tab, Shift+Tab, Escape and egui's clipboard events stay in egui's input for it, and the runtime routes nothing for them. While egui focus is on such a widget, the arrows and Tab move egui focus among the app's widgets as egui does; while a framework stop has the focus, egui's arrow moves are off. | | Releases | The release of every key the runtime took is taken too. | | Focus surrender | egui's own surrender is off; the runtime moves focus on press. A press a framework component takes moves focus to it; a press no component takes drops egui focus, unless it lands on the widget that holds egui focus (an app's own focused field keeps it). | +| Clicks | egui's click options (`InputOptions::max_click_dist`, `max_click_duration`) stay the app's, for its own widgets. A framework component clicks and drags by the framework's press lifecycle: a release within the slop (`HitSizes::slop`), whatever its duration. Read a component's click with `contract::clicked(&response)` or its output's `clicked()`; egui's `Response::clicked()` on a component's response also needs a click by egui's options. | | Zoom keys | egui's keyboard zoom is off. The interface scale is `RootEnv::scale`. | | Quit shortcuts | egui's quit shortcuts are cleared. Quit goes through the close path. | | Close path | The runtime's window host owns the root window's close request. `embed::logic` asks the app through its closure, then hands the close to the platform quit where there is one (macOS). |