diff --git a/crates/behavior/src/wheel.rs b/crates/behavior/src/wheel.rs index 787737851..3c1a9360b 100644 --- a/crates/behavior/src/wheel.rs +++ b/crates/behavior/src/wheel.rs @@ -382,12 +382,15 @@ pub struct WheelFocus { pub taker: Option, /// Key focus is in a text field the app drew itself, which reads its own wheel: its share goes to no taker. pub app_field: bool, + /// The pointer is over a popup layer opened above the layer of the focused field: the field takes no share, and + /// the wheel goes by position. + pub over_popup: bool, } impl WheelFocus { /// Focus on `taker`, or on nothing that takes the wheel. pub const fn new(taker: Option) -> Self { - WheelFocus { taker, app_field: false } + WheelFocus { taker, app_field: false, over_popup: false } } /// The same focus, in a text field the app drew itself when `on`. @@ -396,6 +399,13 @@ impl WheelFocus { self.app_field = on; self } + + /// The same focus, with the pointer over a popup layer opened above the focused field's layer when `on`. + #[must_use] + pub const fn over_popup(mut self, on: bool) -> Self { + self.over_popup = on; + self + } } /// Whether `wheel` is a field's: a vertical scroll, or Shift's (which some devices send sideways), without Alt or @@ -417,8 +427,9 @@ const fn scroll_only(wheel: Wheel) -> Wheel { } /// The split rule. A focused field (a `WheelTaker::field` taker that holds `focus`, or an app's own text field) takes -/// the field's share of the scroll wherever the pointer is: a vertical or Shift wheel without Alt or Cmd/Ctrl; an app's -/// field reads it itself, so no taker gets it. A running `gesture` (an event at `now` less than [`GESTURE_GAP`] after +/// the field's share of the scroll wherever the pointer is, except over a popup opened above it +/// (`WheelFocus::over_popup`): a vertical or Shift wheel without Alt or Cmd/Ctrl; an app's field reads it itself, so +/// no taker gets it. A running `gesture` (an event at `now` less than [`GESTURE_GAP`] after /// its last one, inside its device phase) keeps the taker it started on: each scroll axis that taker takes goes to it /// wherever the pointer is, and the rest by position to a taker that is no value control (`WheelTaker::value`), so a /// control the pointer passes over does not take the scroll over. A value control that is not the focused field keeps @@ -447,7 +458,8 @@ pub fn split( if !running { *gesture = WheelGesture::default(); } - let field = focus.taker.filter(|focused| takers.iter().any(|taker| taker.field && taker.owner == *focused)); + let field = focus.taker.filter(|focused| !focus.over_popup && takers.iter().any(|taker| taker.field && taker.owner == *focused)); + let app_field = focus.app_field && !focus.over_popup; // A value control keeps its gesture only while the pointer is over it: once the pointer leaves, the gesture goes // on by position as a running one (value controls excluded) and the control hears its end now, so its edit run // commits and coming back in the same gesture does not step it again. @@ -459,7 +471,7 @@ pub fn split( gesture.owner = None; } let latch = running.then_some(gesture.owner); - let mut shares = if (field.is_some() || focus.app_field) && wheel.scroll != Vec2::ZERO && for_field(&wheel) { + let mut shares = if (field.is_some() || app_field) && wheel.scroll != Vec2::ZERO && for_field(&wheel) { let mut shares = if pinches(&wheel) { split_at(takers, at, Wheel { scroll: Vec2::ZERO, ..wheel }, focus.taker, armed, latch) } else { Vec::new() }; if let Some(owner) = field { @@ -610,6 +622,8 @@ mod tests { assert_eq!(owners(wheel(5.0, 0.0), focused), [1], "a sideways swipe stays the view's"); assert_eq!(owners(wheel(0.0, 5.0), WheelFocus::new(None).app_field(true)), [] as [u8; 0], "an app's field reads its own"); assert_eq!(owners(alt, WheelFocus::new(None).app_field(true)), [1]); + assert_eq!(owners(wheel(0.0, 5.0), focused.over_popup(true)), [1], "over a popup above the field: by position"); + assert_eq!(owners(wheel(0.0, 5.0), WheelFocus::new(None).app_field(true).over_popup(true)), [1]); } #[test] diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 3d7ac26a8..73e60c8ee 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -554,6 +554,16 @@ pub(crate) fn request_now(ctx: &Context, target: FocusTarget) { apply(ctx, acts); } +/// Whether `at` (screen points) lies in a popup layer opened above the layer of the widget that holds egui focus: a +/// press or a wheel there is the popup's, not the focused widget's. The layer under `at` is the layer test's; the +/// focused widget's layer is egui's last-pass record of it, since a stop records no layer of its own. +pub(crate) fn popup_above_focus(ctx: &Context, at: egui::Pos2) -> bool { + let focus_layer = egui_focus::focused(ctx).and_then(|widget| ctx.read_response(widget)).map(|response| response.layer_id); + crate::runtime::egui::areas::layer_at(ctx, at) + .zip(focus_layer) + .is_some_and(|(layer, below)| crate::runtime::overlay::popup_above(ctx, layer, below)) +} + /// The stop focus is on, in the viewport whose pass is running: the focused member or part of the egui /// root, else the root itself. pub(crate) fn focused_stop(ctx: &Context) -> Option { diff --git a/crates/ui-contract/src/runtime/focus/press.rs b/crates/ui-contract/src/runtime/focus/press.rs index 22a01cf44..8208f3a81 100644 --- a/crates/ui-contract/src/runtime/focus/press.rs +++ b/crates/ui-contract/src/runtime/focus/press.rs @@ -2,9 +2,8 @@ //! classified focus move applies at the end hook. use super::{Act, State, StopKey}; -use crate::runtime::egui::areas; use crate::runtime::egui::focus as egui_focus; -use crate::runtime::{overlay, pointer, sampler}; +use crate::runtime::{pointer, sampler}; use craft_behavior::PartRef; use craft_behavior::Winner; use craft_behavior::focus::{PressFocus, classify_press}; @@ -39,9 +38,7 @@ pub(crate) fn press_at(ctx: &Context) -> Option { let targets = pointer::pass_pointer(ctx).press_targets; let (winner, root) = pointer::press_winner(ctx, pos, targets).unzip(); let under = pointer::shapes_at(ctx, pos); - // The focused widget's layer is egui's last-pass record of it; a stop records no layer of its own. - let focus_layer = egui_focus::focused(ctx).and_then(|widget| ctx.read_response(widget)).map(|response| response.layer_id); - let over_focus = areas::layer_at(ctx, pos).zip(focus_layer).is_some_and(|(layer, below)| overlay::popup_above(ctx, layer, below)); + let over_focus = super::popup_above_focus(ctx, pos); PressAt { winner, root, target: targets.click, under, over_focus } }) } diff --git a/crates/ui-contract/src/runtime/wheel.rs b/crates/ui-contract/src/runtime/wheel.rs index 9f6fb826c..5be023503 100644 --- a/crates/ui-contract/src/runtime/wheel.rs +++ b/crates/ui-contract/src/runtime/wheel.rs @@ -58,6 +58,8 @@ pub(crate) fn begin(ctx: &Context) { // A text field the app drew itself holds key focus: it reads its own wheel. let app_field = wheel_in.is_some() && focus::record(ctx, viewport).host_text; let hover = pass_pointer.hover.or(frame.as_ref().and_then(|frame| frame.pos)); + // A popup the pointer is over, opened above the focused field's layer, takes the wheel by position. + let over_popup = wheel_in.is_some() && hover.is_some_and(|at| focus::popup_above_focus(ctx, at)); let pressed_on = pass_pointer .events .iter() @@ -114,7 +116,7 @@ pub(crate) fn begin(ctx: &Context) { .rotation(frame.rotation) .touch_rotation(touch_rotation) .at(at); - let focus = WheelFocus::new(focused).app_field(app_field); + let focus = WheelFocus::new(focused).app_field(app_field).over_popup(over_popup); // The device's lines and pages become points of each taker: the notch is the scroll owner's to choose. for (owner, mut share) in split(&state.last, at, share, focus, state.armed, &mut state.gesture, now) { if let Some(taker) = state.last.iter().find(|taker| taker.owner == owner) { diff --git a/crates/ui-widgets/tests/it/text/number.rs b/crates/ui-widgets/tests/it/text/number.rs index 5da40480f..c84fd0fdc 100644 --- a/crates/ui-widgets/tests/it/text/number.rs +++ b/crates/ui-widgets/tests/it/text/number.rs @@ -661,3 +661,39 @@ fn a_popup_slider_drag_on_a_given_value_runs_one_pass_a_frame() { assert!(model.get() > before, "the drag moved the app's value"); assert!(passes.iter().all(|&n| n == 1), "slider drag frames ran {passes:?} passes"); } + +/// The wheel over a field's open presets list scrolls the list, though the field keeps focus after Enter: the focused +/// field's share of the wheel stops at a popup opened above it. +#[trial] +fn the_wheel_over_an_open_presets_list_scrolls_the_list_while_the_field_keeps_focus() { + use craft_ui::contract::Role; + use craft_ui::testing::Fixture; + use craft_ui::testing::pointer::{moved, wheel_lines}; + let value = std::rc::Rc::new(Cell::new(Some(START))); + let held = value.clone(); + let presets: Vec> = (1..=60).map(|n| Entry::new(NumberPick::Number(f64::from(n)), format!("{n} px"))).collect(); + let mut fixture = Fixture::new(move |ui| { + let mut now = held.get(); + NumberField::new("Size", Bind::bound(&mut now)).id_salt("size").presets(&presets).show(ui); + held.set(now); + }); + fixture.settle(); + let field = fixture.by_label("Size").expect("the field"); + fixture.click(&field).settle(); + fixture.key(Key::A, Modifiers::COMMAND).type_text("500").key(Key::Enter, Modifiers::NONE).settle(); + let focus = fixture.focused_stop(); + assert!(focus.is_some(), "the field keeps focus after Enter"); + let chevron = fixture.by_role(Role::BUTTON).first().expect("the presets chevron").center(); + fixture.click_at(chevron).settle(); + assert_eq!(fixture.focused_stop(), focus, "opening the list leaves focus on the field"); + let first_row = |fixture: &Fixture| fixture.by_label("1 px").map(|row| row.rect); + let before = first_row(&fixture).expect("the list is open"); + let committed = value.get(); + fixture.frame(vec![moved(before.center())]); + for _ in 0..3 { + fixture.frame(vec![wheel_lines(-1.0)]); + } + fixture.settle(); + assert!(first_row(&fixture).is_none_or(|row| row.top() < before.top()), "the list scrolled"); + assert_eq!(value.get(), committed, "the field's value stays"); +}