Track who is hovered, apart from what consumes
Hover was per-sensor state that only changed when the walk reached that sensor, so ending it depended on the walk, which consumption cuts short. `CursorSenses` now keeps the set of widgets the cursor was inside, in a new `Event::Global` slot for state a whole event type owns rather than each widget -- which is also where the input restructure keeps its pointer capture. The walk visits only widgets the cursor is inside and stops at the layer that consumes, as before. Whoever was in the set and is not now has been left or covered, and gets its `HoverEnd` afterwards, however early the walk stopped. Two things fall out. `SensorState` is gone: whether a hover is starting, on or ending is the difference between the two sets. And the consumption line loses its `&& in_shape`, since being inside is now the reason the widget is looked at rather than something to test again. Nine tests, five of which fail on `main`. `hover_starts_and_ends_once_each` pins the lifecycle, and `covering_a_widget_ends_its_hover` now returns the cursor so an uncovered widget hovers again.
This commit is contained in:
1 parent
5494642dec
commit
36fec09d11
4 files changed
+116
-36
No files matched your search
@@ -79,6 +79,7 @@ type EventData<Rsc, E> = (E, Rc<dyn for<'a> EventFn<Rsc, <E as Event>::Data<'a>>
|
|||||||
pub struct TypeEventManager<Rsc: HasEvents, E: Event> {
|
pub struct TypeEventManager<Rsc: HasEvents, E: Event> {
|
||||||
// TODO: reduce visiblity!!
|
// TODO: reduce visiblity!!
|
||||||
pub active: HashMap<LayerId, HashMap<WidgetId, E::State>>,
|
pub active: HashMap<LayerId, HashMap<WidgetId, E::State>>,
|
||||||
|
pub global: E::Global,
|
||||||
map: HashMap<WidgetId, Vec<EventData<Rsc, E>>>,
|
map: HashMap<WidgetId, Vec<EventData<Rsc, E>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,6 +108,7 @@ impl<Rsc: HasEvents, E: Event> Default for TypeEventManager<Rsc, E> {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
active: Default::default(),
|
active: Default::default(),
|
||||||
|
global: Default::default(),
|
||||||
map: Default::default(),
|
map: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ pub use rsc::*;
|
|||||||
pub trait Event: Sized + 'static + Clone {
|
pub trait Event: Sized + 'static + Clone {
|
||||||
type Data<'a>: Clone = ();
|
type Data<'a>: Clone = ();
|
||||||
type State: Default = ();
|
type State: Default = ();
|
||||||
|
/// State the whole event type keeps, as opposed to `State`, which each
|
||||||
|
/// widget keeps its own of.
|
||||||
|
type Global: Default = ();
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn should_run<'a>(&self, data: &Self::Data<'a>) -> Option<Self::Data<'a>> {
|
fn should_run<'a>(&self, data: &Self::Data<'a>) -> Option<Self::Data<'a>> {
|
||||||
Some(data.clone())
|
Some(data.clone())
|
||||||
|
|||||||
+64
-36
@@ -27,7 +27,10 @@ pub struct CursorSenses(Vec<CursorSense>);
|
|||||||
|
|
||||||
impl Event for CursorSenses {
|
impl Event for CursorSenses {
|
||||||
type Data<'a> = CursorData<'a>;
|
type Data<'a> = CursorData<'a>;
|
||||||
type State = SensorState;
|
/// Who the cursor was inside on the last input, which is what says whose
|
||||||
|
/// hover has ended -- including a widget a higher layer has since covered,
|
||||||
|
/// which this walk never reaches.
|
||||||
|
type Global = Vec<WidgetId>;
|
||||||
fn should_run<'a>(&self, data: &Self::Data<'a>) -> Option<Self::Data<'a>> {
|
fn should_run<'a>(&self, data: &Self::Data<'a>) -> Option<Self::Data<'a>> {
|
||||||
if let Some(sense) = should_run(self, &data.cursor, data.hover) {
|
if let Some(sense) = should_run(self, &data.cursor, data.hover) {
|
||||||
let mut data = data.clone();
|
let mut data = data.clone();
|
||||||
@@ -141,11 +144,6 @@ pub struct Sensor<Ctx: HasEvents, Data> {
|
|||||||
|
|
||||||
pub type SenseShape = UiRegion;
|
pub type SenseShape = UiRegion;
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
|
||||||
pub struct SensorState {
|
|
||||||
pub hover: ActivationState,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CursorData<'a> {
|
pub struct CursorData<'a> {
|
||||||
/// where this widget was hit
|
/// where this widget was hit
|
||||||
@@ -181,49 +179,79 @@ impl SensorUi for UiRenderState {
|
|||||||
// this would probably be done through a generic parameter that adds yet another rsc /
|
// this would probably be done through a generic parameter that adds yet another rsc /
|
||||||
// state like thing, but local to render state, and is passed to UiRsc events so you can
|
// state like thing, but local to render state, and is passed to UiRsc events so you can
|
||||||
// update it there?
|
// update it there?
|
||||||
let mut active = std::mem::take(&mut rsc.events_mut().get_type::<CursorSense>().active);
|
let active = std::mem::take(&mut rsc.events_mut().get_type::<CursorSense>().active);
|
||||||
|
let was = std::mem::take(&mut rsc.events_mut().get_type::<CursorSense>().global);
|
||||||
let position_only = cursor.position_only();
|
let position_only = cursor.position_only();
|
||||||
let mut consumed = false;
|
let mut now: Vec<WidgetId> = Vec::new();
|
||||||
|
let region_of = |id| Some(self.active.get(&id)?.region.to_px(window_size));
|
||||||
|
|
||||||
for layer in self.layers.indices().rev() {
|
for layer in self.layers.indices().rev() {
|
||||||
let mut consumed_here = false;
|
let mut consumed = false;
|
||||||
for (id, sensor) in active.get_mut(&layer).into_flat_iter() {
|
for id in active.get(&layer).into_flat_iter().map(|(id, _)| *id) {
|
||||||
let shape = self.active.get(id).unwrap().region;
|
let Some(region) = region_of(id) else {
|
||||||
let region = shape.to_px(window_size);
|
continue;
|
||||||
// Once a layer above has taken the input, everything under it
|
};
|
||||||
// is covered rather than skipped: it is not hovered, so a
|
if !cursor.exists || !region.contains(cursor.pos) {
|
||||||
// widget that was gets to end its hover.
|
|
||||||
let in_shape = !consumed && cursor.exists && region.contains(cursor.pos);
|
|
||||||
sensor.hover.update(in_shape);
|
|
||||||
if sensor.hover == ActivationState::Off {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let cursor = cursor.clone();
|
now.push(id);
|
||||||
|
let hover = match was.contains(&id) {
|
||||||
|
true => ActivationState::On,
|
||||||
|
false => ActivationState::Start,
|
||||||
|
};
|
||||||
|
// A press or a scroll stops where something answered it, so a
|
||||||
|
// button over a list does not swallow the list's scrolling.
|
||||||
|
consumed |= deliver(self, rsc, state, id, hover, &cursor, region);
|
||||||
|
// A cursor doing neither stops at whatever it is over, so
|
||||||
|
// hovering does not reach through.
|
||||||
|
consumed |= position_only;
|
||||||
|
}
|
||||||
|
// Applied after the layer, never during it: senses on one layer do
|
||||||
|
// not block each other.
|
||||||
|
if consumed {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Whatever the cursor was inside and is not now, whether it left or a
|
||||||
|
// layer above took the input before the walk reached it. A widget that
|
||||||
|
// stopped being drawn has no region to report and is simply dropped.
|
||||||
|
for id in was {
|
||||||
|
if !now.contains(&id)
|
||||||
|
&& let Some(region) = region_of(id)
|
||||||
|
{
|
||||||
|
deliver(self, rsc, state, id, ActivationState::End, &cursor, region);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let senses = rsc.events_mut().get_type::<CursorSense>();
|
||||||
|
senses.active = active;
|
||||||
|
senses.global = now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs one widget's cursor senses, and says whether they used up the input.
|
||||||
|
fn deliver<Rsc: HasEvents>(
|
||||||
|
render: &UiRenderState,
|
||||||
|
rsc: &mut Rsc,
|
||||||
|
state: &mut Rsc::State,
|
||||||
|
id: WidgetId,
|
||||||
|
hover: ActivationState,
|
||||||
|
cursor: &CursorState,
|
||||||
|
region: PixelRegion,
|
||||||
|
) -> bool {
|
||||||
let data = CursorData {
|
let data = CursorData {
|
||||||
pos: cursor.pos - region.top_left,
|
pos: cursor.pos - region.top_left,
|
||||||
size: region.bot_right - region.top_left,
|
size: region.bot_right - region.top_left,
|
||||||
scroll_delta: cursor.scroll_delta,
|
scroll_delta: cursor.scroll_delta,
|
||||||
hover: sensor.hover,
|
hover,
|
||||||
cursor,
|
cursor: cursor.clone(),
|
||||||
// this does not have any meaning;
|
// this does not have any meaning;
|
||||||
// might wanna set up Event to have a prepare stage
|
// might wanna set up Event to have a prepare stage
|
||||||
sense: CursorSense::Hovering,
|
sense: CursorSense::Hovering,
|
||||||
render: self,
|
render,
|
||||||
};
|
};
|
||||||
// A press or a scroll stops where something answered it, so a
|
rsc.run_event::<CursorSense>(id, data, state)
|
||||||
// button over a list does not swallow the list's scrolling. A
|
|
||||||
// cursor doing neither stops at the widget it is over, so
|
|
||||||
// hovering does not reach through one -- but not at a widget
|
|
||||||
// it has just left, which is here only to end its hover.
|
|
||||||
let answered = rsc.run_event::<CursorSense>(*id, data, state);
|
|
||||||
consumed_here |= answered || (position_only && in_shape);
|
|
||||||
}
|
|
||||||
// Applied after the layer, never during it: senses on one layer
|
|
||||||
// do not block each other.
|
|
||||||
consumed |= consumed_here;
|
|
||||||
}
|
|
||||||
rsc.events_mut().get_type::<CursorSense>().active = active;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn should_run(
|
pub fn should_run(
|
||||||
|
|||||||
@@ -317,4 +317,51 @@ fn covering_a_widget_ends_its_hover() {
|
|||||||
[CursorSense::HoverEnd],
|
[CursorSense::HoverEnd],
|
||||||
"a widget covered by one that took the input is no longer hovered"
|
"a widget covered by one that took the input is no longer hovered"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let off_again = ui.cursor((80.0, 50.0));
|
||||||
|
ui.run(off_again);
|
||||||
|
assert_eq!(
|
||||||
|
below_hover.take(),
|
||||||
|
[CursorSense::HoverStart],
|
||||||
|
"uncovering it hovers it again"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hover_starts_and_ends_once_each() {
|
||||||
|
let ui = &mut Ui::new();
|
||||||
|
// Only the left half is the widget, so the cursor can leave it without
|
||||||
|
// leaving the window.
|
||||||
|
let (widget, gap) = (full(ui), full(ui));
|
||||||
|
let hover = ui.listen(&widget, CursorSense::HoverStart | CursorSense::HoverEnd);
|
||||||
|
let row = ui.rsc.ui.widgets.add_strong(Span {
|
||||||
|
children: vec![widget.any(), gap.any()],
|
||||||
|
dir: Dir::RIGHT,
|
||||||
|
gap: 0.0,
|
||||||
|
});
|
||||||
|
ui.stack(vec![row.any()]);
|
||||||
|
|
||||||
|
let inside = ui.cursor((20.0, 50.0));
|
||||||
|
ui.run(inside);
|
||||||
|
assert_eq!(hover.take(), [CursorSense::HoverStart]);
|
||||||
|
|
||||||
|
let further_in = ui.cursor((30.0, 50.0));
|
||||||
|
ui.run(further_in);
|
||||||
|
assert_eq!(hover.take(), [], "staying inside is not a second start");
|
||||||
|
|
||||||
|
let outside = ui.cursor((80.0, 50.0));
|
||||||
|
ui.run(outside);
|
||||||
|
assert_eq!(hover.take(), [CursorSense::HoverEnd]);
|
||||||
|
|
||||||
|
let further_out = ui.cursor((90.0, 50.0));
|
||||||
|
ui.run(further_out);
|
||||||
|
assert_eq!(hover.take(), [], "an ended hover does not end again");
|
||||||
|
|
||||||
|
let back_inside = ui.cursor((20.0, 50.0));
|
||||||
|
ui.run(back_inside);
|
||||||
|
assert_eq!(
|
||||||
|
hover.take(),
|
||||||
|
[CursorSense::HoverStart],
|
||||||
|
"re-entering starts it"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user