diff --git a/crates/behavior/src/sense.rs b/crates/behavior/src/sense.rs index c64efa2fbd..99281664b3 100644 --- a/crates/behavior/src/sense.rs +++ b/crates/behavior/src/sense.rs @@ -55,6 +55,11 @@ bitflags! { /// 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; + /// The vertical wheel share, taken only while the component holds key focus or a press armed it, in any wheel + /// scope: a control whose wheel steps what focus picks rather than what the pointer is over (the focused thumb + /// of a range). Every other wheel bit the shape senses waits for focus with it. A control that senses no wheel + /// bit takes no wheel at all, so the wheel over it scrolls the region around it. + const WHEEL_Y_FOCUSED = 262144; } } @@ -63,7 +68,12 @@ impl Sense { pub const PRESS: Sense = Sense::CLICK.union(Sense::DRAG); /// The bits that make a shape register a wheel taker. - pub const WHEELS: Sense = Sense::WHEEL.union(Sense::WHEEL_X).union(Sense::WHEEL_Y).union(Sense::WHEEL_ZOOM).union(Sense::WHEEL_ZOOMS_VIEW); + pub const WHEELS: Sense = Sense::WHEEL + .union(Sense::WHEEL_X) + .union(Sense::WHEEL_Y) + .union(Sense::WHEEL_ZOOM) + .union(Sense::WHEEL_ZOOMS_VIEW) + .union(Sense::WHEEL_Y_FOCUSED); /// Whether a press can start on a shape with this sense. pub const fn takes_press(self) -> bool { diff --git a/crates/behavior/src/wheel.rs b/crates/behavior/src/wheel.rs index 1985e4bf9c..7b0bb69c2b 100644 --- a/crates/behavior/src/wheel.rs +++ b/crates/behavior/src/wheel.rs @@ -423,14 +423,17 @@ impl WheelTaker { /// The taker a shape with `sense` registers, `None` when `sense` has no wheel bit: `WHEEL` takes both scroll /// axes and zoom; `WHEEL_X`, `WHEEL_Y` and `WHEEL_ZOOM` take one share each and combine; `WHEEL_ZOOMS_VIEW` - /// makes a zoom zone. + /// makes a zoom zone; `WHEEL_Y_FOCUSED` takes the vertical share and makes the taker gated, so every axis it takes + /// waits for focus or arming. pub fn for_sense(owner: O, rect: Rect, sense: Sense) -> Option { let all = sense.contains(Sense::WHEEL); + let focused = sense.contains(Sense::WHEEL_Y_FOCUSED); let taker = WheelTaker::new(owner, rect) .horizontal(all || sense.contains(Sense::WHEEL_X)) - .vertical(all || sense.contains(Sense::WHEEL_Y)) + .vertical(all || focused || sense.contains(Sense::WHEEL_Y)) .zoom(all || sense.contains(Sense::WHEEL_ZOOM)) - .zooms_view(sense.contains(Sense::WHEEL_ZOOMS_VIEW)); + .zooms_view(sense.contains(Sense::WHEEL_ZOOMS_VIEW)) + .gated(focused); (taker.horizontal || taker.vertical || taker.zoom || taker.zooms_view).then_some(taker) } diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index f4166d548c..d43e01f522 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -238,7 +238,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { if cx.core.resolved.enabled && let Some(taker) = WheelTaker::for_sense(cx.core.id, cx.core.rect, crate::contract::Component::sense(comp)) { - wheel::register(&cx.core.ctx, taker.notch(self.notch).gated(cx.core.env.wheel() == WheelScope::Gated)); + wheel::register(&cx.core.ctx, taker.notch(self.notch).gated(taker.gated || cx.core.env.wheel() == WheelScope::Gated)); } } else if self.scrolls { // The taker covers the whole collection, header row and frame included: the wheel over the header scrolls diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index c87b86cf68..e06d776cc2 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -1021,7 +1021,7 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, e }; if !inert && registers { let sensed = craft_behavior::WheelTaker::for_sense(id, rect, own).map(|taker| taker.notch(e.wheel_notch())); - steps::register_wheel(&ctx, id, rect, sensed, e.bound().0 && !facts.binds_view, facts.text(), env.wheel); + steps::register_wheel(&ctx, sensed, e.bound().0 && !facts.binds_view, facts.text(), env.wheel); } // 8 sense p.trace.mark(Step::Sense); diff --git a/crates/ui-contract/src/contract/steps/sense.rs b/crates/ui-contract/src/contract/steps/sense.rs index d14deec4be..aa69b0aa3a 100644 --- a/crates/ui-contract/src/contract/steps/sense.rs +++ b/crates/ui-contract/src/contract/steps/sense.rs @@ -4,41 +4,27 @@ use crate::runtime::env::WheelScope; use crate::runtime::wheel; use crate::style::{Flags, StateSet}; use craft_theme::{State, StateMix}; -use egui::{Context, Id, Rect}; +use egui::{Context, Id}; /// Registers a wheel taker for next pass's split (8.6): `sensed`, the taker the wheel bits of the component's sense -/// make (`WheelTaker::for_sense`). Every value control that takes the wheel is a `WheelTaker::value` taker, in any -/// scope: a wheel gesture it owns ends with a `WheelPhase::End` share. A value control with a text part that steps by -/// the wheel (it senses a wheel axis: a number field) is a field (`WheelTaker::field`), which takes the plain wheel -/// while focused wherever the pointer is. A text field that does not step takes no wheel at all, focused or not: the -/// wheel over it scrolls the region around it, as native fields do, and a multi-line text part whose text overflows -/// registers its own scroll taker (`text_adapter`), which passes the wheel on at its ends. Any other value control -/// takes the wheel only while focused or armed, on the axes it senses (the vertical one when it senses none); inside a -/// `WheelScope::Free` scope one that senses the wheel takes it without focus. A value control that steps by the wheel -/// declares the axis it steps in its sense, so a free scope frees it; one that does not keeps the fallback and stays -/// gated. Inside a `WheelScope::Gated` scope every taker waits for focus or arming. A zoom zone that takes no axis -/// itself waits only for a gated scope: its wheel goes to the view around it. -pub(crate) fn register_wheel( - ctx: &Context, - id: Id, - rect: Rect, - sensed: Option>, - value_control: bool, - text: bool, - scope: WheelScope, -) { - let free = sensed.is_some() && scope == WheelScope::Free; - let zone = sensed.is_some_and(|taker| taker.zooms_view && !(taker.horizontal || taker.vertical || taker.zoom)); - let taker = sensed.or_else(|| (value_control && !text).then(|| craft_behavior::WheelTaker::new(id, rect).vertical(true))); - if let Some(taker) = taker { - wheel::register( - ctx, - taker - .value(value_control) - .field(value_control && text && sensed.is_some()) - .gated(scope == WheelScope::Gated || (value_control && !free && !zone)), - ); - } +/// make (`WheelTaker::for_sense`). A component that senses no wheel bit takes no wheel, value control or not: the wheel +/// over a focused check box or text field scrolls the region around it, as native controls do, and a multi-line text +/// part whose text overflows registers its own scroll taker (`text_adapter`), which passes the wheel on at its ends. +/// Every value control that takes the wheel is a `WheelTaker::value` taker, in any scope: a wheel gesture it owns ends +/// with a `WheelPhase::End` share. A value control with a text part that steps by the wheel (a number field) is a field +/// (`WheelTaker::field`), which takes the plain wheel while focused wherever the pointer is. A value control takes the +/// wheel only while focused or armed, on the axes it senses; inside a `WheelScope::Free` scope one that senses the +/// wheel takes it without focus, unless it senses `Sense::WHEEL_Y_FOCUSED`, which no scope frees (a range whose wheel +/// steps its focused thumb). Inside a `WheelScope::Gated` scope every taker waits for focus or arming. A zoom zone that +/// takes no axis itself waits only for a gated scope: its wheel goes to the view around it. +pub(crate) fn register_wheel(ctx: &Context, sensed: Option>, value_control: bool, text: bool, scope: WheelScope) { + let Some(taker) = sensed else { return }; + let free = !taker.gated && scope == WheelScope::Free; + let zone = taker.zooms_view && !(taker.horizontal || taker.vertical || taker.zoom); + wheel::register( + ctx, + taker.value(value_control).field(value_control && text).gated(taker.gated || scope == WheelScope::Gated || (value_control && !free && !zone)), + ); } /// The part states: each part's press and hover from the sensing core, with its own flags. diff --git a/crates/ui-widgets/src/value_drag/range_slider.rs b/crates/ui-widgets/src/value_drag/range_slider.rs index 00fd5547af..1b8c294015 100644 --- a/crates/ui-widgets/src/value_drag/range_slider.rs +++ b/crates/ui-widgets/src/value_drag/range_slider.rs @@ -195,10 +195,10 @@ impl Component for RangeSlider<'_> { Vec2::new(cx.style.length, cx.icon() + self.marks.below(&cx.style.track)) } - /// No wheel bits: as a value control the row takes the vertical wheel only while focused or armed, and no scope - /// frees it, since a wheel over the track has no one thumb to step. + /// The vertical wheel only while focused or armed (`Sense::WHEEL_Y_FOCUSED`), which no scope frees, since a wheel + /// over the track has no one thumb to step: it steps the focused one. fn sense(&self) -> Sense { - Sense::empty() + Sense::WHEEL_Y_FOCUSED } /// The track, and a thumb each that is its own Tab stop and takes the arrow keys. diff --git a/crates/ui-widgets/src/value_drag/thumb_row.rs b/crates/ui-widgets/src/value_drag/thumb_row.rs index bf2ba60e86..1c69504daf 100644 --- a/crates/ui-widgets/src/value_drag/thumb_row.rs +++ b/crates/ui-widgets/src/value_drag/thumb_row.rs @@ -977,10 +977,10 @@ impl Component for ThumbRow<'_, N> { Some(craft_behavior::Axis::X) } - /// No wheel bits: as a value control the row takes the vertical wheel only while focused or armed, and no scope - /// frees it, since a wheel over the track has no one thumb to step. + /// The vertical wheel only while focused or armed (`Sense::WHEEL_Y_FOCUSED`), which no scope frees, since a wheel + /// over the track has no one thumb to step: it steps the focused one. fn sense(&self) -> Sense { - Sense::empty() + Sense::WHEEL_Y_FOCUSED } fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { diff --git a/crates/ui-widgets/tests/it/text/text_field.rs b/crates/ui-widgets/tests/it/text/text_field.rs index 313ebff3b1..69a89387d9 100644 --- a/crates/ui-widgets/tests/it/text/text_field.rs +++ b/crates/ui-widgets/tests/it/text/text_field.rs @@ -1,6 +1,6 @@ //! `TextField`: the search form's clear part empties the text; a multi-line field grows to `max_lines` or fills its -//! room and scrolls its text inside; a bound caret goes both ways; the wheel over a focused field scrolls the form -//! around it. +//! room and scrolls its text inside; a bound caret goes both ways; the wheel over a focused field or check box scrolls +//! the form around it, and a focused number field or range still takes it. use craft_behavior::PartKey; use craft_ui::contract::{Bind, Props as _, Scroll, Show as _, ShowContainer as _, Sizing}; @@ -351,3 +351,51 @@ fn the_wheel_over_a_focused_text_field_scrolls_the_form_around_it() { let still = fixture.by_label("Size").expect("the Size field").rect; assert!((still.top() - field.rect.top()).abs() <= SNAP, "and the form stays put: {:?} to {still:?}", field.rect); } + +/// The wheel over a focused check box, which neither senses nor reads the wheel, scrolls the form around it; a range +/// slider, which senses the wheel only while focused (`Sense::WHEEL_Y_FOCUSED`), still steps its focused thumb and +/// keeps the form still. +#[trial] +fn the_wheel_over_a_focused_check_box_scrolls_the_form_and_a_focused_range_steps() { + use craft_behavior::Mixed; + use craft_ui::choice::Checkbox; + use craft_ui::testing::focus::AccessActionKind; + use craft_ui::value_drag::RangeSlider; + + let agreed = Rc::new(Cell::new(false)); + let shown = Rc::clone(&agreed); + let mut fixture = scrolling_form(move |ui| { + let mut on = shown.get(); + Checkbox::new(Bind::bound(&mut on)).label("Agree").id_salt("agree").show(ui); + shown.set(on); + }); + let check = fixture.by_label("Agree").expect("the Agree check box"); + fixture.access(AccessActionKind::Focus, &check).settle(); + assert!(fixture.focused_stop().is_some(), "the check box holds focus"); + fixture.frame(vec![pointer::moved(check.center())]); + fixture.frame(vec![pointer::wheel(-40.0)]); + fixture.idle(3); + let moved = fixture.by_label("Agree").expect("the Agree check box").rect; + assert!(moved.top() < check.rect.top() - 1.0, "the wheel scrolls the form, moving the check box up: {:?} to {moved:?}", check.rect); + assert!(!agreed.get(), "the wheel leaves the check box's value alone"); + + let start = (Mixed::Same(20.0), Mixed::Same(80.0)); + let range = Rc::new(Cell::new(start)); + let rect = Rc::new(Cell::new(Rect::NOTHING)); + let (held, seen) = (Rc::clone(&range), Rc::clone(&rect)); + let mut fixture = scrolling_form(move |ui| { + let mut pair = held.get(); + seen.set(RangeSlider::new(Bind::bound(&mut pair), 0.0..=100.0).label("Hue").id_salt("hue").show(ui).response.rect); + held.set(pair); + }); + let slider = rect.get(); + // A click on the span between the thumbs focuses the nearer one and moves neither. + fixture.click_at(slider.center()).settle(); + assert!(fixture.focused_stop().is_some_and(|(_, part)| part.is_some()), "the click focuses a thumb"); + assert_eq!(range.get(), start, "the click moves neither thumb"); + fixture.frame(vec![pointer::moved(slider.center())]); + fixture.frame(vec![pointer::wheel_lines(1.0)]); + fixture.idle(3); + assert_ne!(range.get(), start, "the wheel steps the focused thumb"); + assert!((rect.get().top() - slider.top()).abs() <= SNAP, "and the form stays put: {slider:?} to {:?}", rect.get()); +} diff --git a/docs/glossary.md b/docs/glossary.md index 317b2a8587..332c43a084 100644 --- a/docs/glossary.md +++ b/docs/glossary.md @@ -250,6 +250,7 @@ One row per name in the author table (Overview) and per `pub` item a component a | `Sense` | The library's sense set for a root or part | craft-behavior | 1.1 | | `Sense::TAKE_FOCUS` | A sense bit: a press this shape wins moves key focus to its component | craft-behavior | 1.1, 8.3 | | `Sense::WHEEL_X` | A sense bit: takes the horizontal wheel share without focus or arming | craft-behavior | 1.1, 8.6 | +| `Sense::WHEEL_Y_FOCUSED` | A sense bit: takes the vertical wheel share only while focused or armed, in any wheel scope | craft-behavior | 1.1, 8.6 | | `SetPosition` | An item's index, set size and level for AccessKit | `craft_ui::runtime::access` | 12.1 | | Settle | Step 15 | `craft_ui::contract` | 1.6 | | `ShellInfo` | OS, runtime, backend, capabilities, menu placement and window info | craft-host (`target`) | 21.1 |