Skip to content

Commit

Permalink
fix: Use collateral event to check if event is allowed (#890)
Browse files Browse the repository at this point in the history
* fix: Consder pointer and mouse up for pressed nodes

* fixes

* fixes
  • Loading branch information
marc2332 authored Sep 16, 2024
1 parent 8cc06fa commit 732e03b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
10 changes: 9 additions & 1 deletion crates/components/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,22 @@ mod test {

assert_eq!(label.get(0).text(), Some("true"));

utils.push_event(PlatformEvent::Touch {
name: EventName::TouchStart,
location: (15.0, 15.0).into(),
finger_id: 1,
phase: TouchPhase::Started,
force: None,
});
utils.wait_for_update().await;

utils.push_event(PlatformEvent::Touch {
name: EventName::TouchEnd,
location: (15.0, 15.0).into(),
finger_id: 1,
phase: TouchPhase::Ended,
force: None,
});

utils.wait_for_update().await;

assert_eq!(label.get(0).text(), Some("false"));
Expand Down
4 changes: 2 additions & 2 deletions crates/components/src/native_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod test {
assert_eq!(utils.root().get(0).get(1).get(0).text(), Some("B"));

utils.push_event(PlatformEvent::Mouse {
name: EventName::PointerUp,
name: EventName::MouseUp,
cursor: (5.0, 5.0).into(),
button: Some(MouseButton::Back),
});
Expand All @@ -128,7 +128,7 @@ mod test {
assert_eq!(utils.root().get(0).get(1).get(0).text(), Some("A"));

utils.push_event(PlatformEvent::Mouse {
name: EventName::PointerUp,
name: EventName::MouseUp,
cursor: (5.0, 5.0).into(),
button: Some(MouseButton::Forward),
});
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/events/events_measurer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn measure_dom_events(
true
};

let allowed_event = nodes_state.is_event_allowed(event, node_id);
let allowed_event = nodes_state.is_event_allowed(collateral_event, node_id);

if valid_node && allowed_event {
let mut valid_event = event.clone();
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/events/nodes_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
},
};

#[derive(Clone)]
#[derive(Clone, Debug)]
struct NodeMetadata {
layer: Option<i16>,
}
Expand All @@ -30,8 +30,8 @@ pub struct NodesState {
impl NodesState {
/// Given the current state, event, and NodeID check if it is allowed to be emitted
/// For example, it will not make sense to emit a Click event on an element that was not pressed before.
pub fn is_event_allowed(&self, event: &PlatformEvent, node_id: &NodeId) -> bool {
if event.get_name().is_click() {
pub fn is_event_allowed(&self, event_name: EventName, node_id: &NodeId) -> bool {
if event_name.is_pressed() {
self.pressed_nodes.contains_key(node_id)
} else {
true
Expand Down Expand Up @@ -116,9 +116,9 @@ impl NodesState {
layer,
} in events
{
match event {
match event.get_name() {
// Update hovered nodes state
PlatformEvent::Mouse { name, .. } if name.can_change_hover_state() => {
name if name.can_change_hover_state() => {
let is_hovered = hovered_nodes.contains_key(node_id);

// Mark the Node as hovered if it wasn't already
Expand All @@ -139,7 +139,7 @@ impl NodesState {
}

// Update pressed nodes state
PlatformEvent::Mouse { name, .. } if name.can_change_press_state() => {
name if name.can_change_press_state() => {
let is_pressed = pressed_nodes.contains_key(node_id);

// Mark the Node as pressed if it wasn't already
Expand Down
14 changes: 10 additions & 4 deletions crates/native-core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,25 @@ impl EventName {

/// Check if this event can change the press state of a Node.
pub fn can_change_press_state(&self) -> bool {
matches!(self, Self::MouseDown | Self::PointerDown)
matches!(self, Self::MouseDown | Self::TouchStart | Self::PointerDown)
}

/// Check if the event means the cursor started or released a click
pub fn was_cursor_pressed_or_released(&self) -> bool {
matches!(
&self,
Self::MouseDown | Self::PointerDown | Self::MouseUp | Self::Click | Self::PointerUp
Self::MouseDown
| Self::PointerDown
| Self::MouseUp
| Self::Click
| Self::PointerUp
| Self::TouchStart
| Self::TouchEnd
)
}

/// Check if the event was a click
pub fn is_click(&self) -> bool {
/// Check if the event was pressed
pub fn is_pressed(&self) -> bool {
matches!(&self, Self::Click)
}
}

0 comments on commit 732e03b

Please sign in to comment.