Skip to content

Commit

Permalink
refactor: Simplify events naming internally
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Feb 16, 2024
1 parent 6156b12 commit 537a2fa
Show file tree
Hide file tree
Showing 22 changed files with 375 additions and 283 deletions.
19 changes: 10 additions & 9 deletions crates/components/src/drag_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ pub fn DropZone<T: 'static + Clone + PartialEq>(props: DropZoneProps<T>) -> Elem
#[cfg(test)]
mod test {
use freya::prelude::*;
use freya_testing::{events::pointer::MouseButton, launch_test, FreyaEvent};
use freya_core::events::EventName;
use freya_testing::{events::pointer::MouseButton, launch_test, PlatformEvent};

#[tokio::test]
pub async fn drag_drop() {
Expand Down Expand Up @@ -190,24 +191,24 @@ mod test {
let root = utils.root();
utils.wait_for_update().await;

utils.push_event(FreyaEvent::Mouse {
name: "mousedown".to_string(),
utils.push_event(PlatformEvent::Mouse {
name: EventName::MouseDown,
cursor: (5.0, 5.0).into(),
button: Some(MouseButton::Left),
});

utils.wait_for_update().await;

utils.push_event(FreyaEvent::Mouse {
name: "mouseover".to_string(),
utils.push_event(PlatformEvent::Mouse {
name: EventName::MouseOver,
cursor: (5.0, 5.0).into(),
button: Some(MouseButton::Left),
});

utils.wait_for_update().await;

utils.push_event(FreyaEvent::Mouse {
name: "mouseover".to_string(),
utils.push_event(PlatformEvent::Mouse {
name: EventName::MouseOver,
cursor: (5.0, 300.0).into(),
button: Some(MouseButton::Left),
});
Expand All @@ -216,8 +217,8 @@ mod test {

assert_eq!(root.get(0).get(0).get(0).get(0).text(), Some("Moving"));

utils.push_event(FreyaEvent::Mouse {
name: "click".to_string(),
utils.push_event(PlatformEvent::Mouse {
name: EventName::Click,
cursor: (5.0, 300.0).into(),
button: Some(MouseButton::Left),
});
Expand Down
23 changes: 12 additions & 11 deletions crates/components/src/gesture_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ mod test {
use std::time::Duration;

use freya::prelude::*;
use freya_core::prelude::EventName;
use freya_elements::events::touch::TouchPhase;
use freya_testing::{launch_test, FreyaEvent};
use freya_testing::{launch_test, PlatformEvent};
use tokio::time::sleep;

use crate::gesture_area::DOUBLE_TAP_MIN;
Expand Down Expand Up @@ -214,16 +215,16 @@ mod test {

assert_eq!(utils.root().get(1).text(), Some("EMPTY"));

utils.push_event(FreyaEvent::Touch {
name: "touchstart".to_string(),
utils.push_event(PlatformEvent::Touch {
name: EventName::TouchStart,
location: (1.0, 1.0).into(),
phase: TouchPhase::Started,
finger_id: 0,
force: None,
});

utils.push_event(FreyaEvent::Touch {
name: "touchend".to_string(),
utils.push_event(PlatformEvent::Touch {
name: EventName::TouchEnd,
location: (1.0, 1.0).into(),
phase: TouchPhase::Ended,
finger_id: 0,
Expand All @@ -235,8 +236,8 @@ mod test {

sleep(Duration::from_millis(DOUBLE_TAP_MIN as u64)).await;

utils.push_event(FreyaEvent::Touch {
name: "touchstart".to_string(),
utils.push_event(PlatformEvent::Touch {
name: EventName::TouchStart,
location: (1.0, 1.0).into(),
phase: TouchPhase::Started,
finger_id: 0,
Expand Down Expand Up @@ -279,8 +280,8 @@ mod test {

assert_eq!(utils.root().get(1).text(), Some("EMPTY"));

utils.push_event(FreyaEvent::Touch {
name: "touchstart".to_string(),
utils.push_event(PlatformEvent::Touch {
name: EventName::TouchStart,
location: (1.0, 1.0).into(),
phase: TouchPhase::Started,
finger_id: 0,
Expand All @@ -292,8 +293,8 @@ mod test {

assert_eq!(utils.root().get(1).text(), Some("TapDown"));

utils.push_event(FreyaEvent::Touch {
name: "touchend".to_string(),
utils.push_event(PlatformEvent::Touch {
name: EventName::TouchEnd,
location: (1.0, 1.0).into(),
phase: TouchPhase::Ended,
finger_id: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ use freya_elements::{
};
use torin::prelude::*;

use crate::{events::FreyaEvent, prelude::PotentialEvent};
use crate::{events::PlatformEvent, prelude::PotentialEvent};

use super::event_name::EventName;

/// Event emitted to the DOM.
#[derive(Debug, Clone, PartialEq)]
pub struct DomEvent {
pub name: String,
pub name: EventName,
pub node_id: NodeId,
pub element_id: ElementId,
pub data: DomEventData,
Expand All @@ -31,16 +33,7 @@ impl PartialOrd for DomEvent {

impl Ord for DomEvent {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.name.as_str() {
"mouseleave" | "pointerleave" => {
if self.name == other.name {
std::cmp::Ordering::Equal
} else {
std::cmp::Ordering::Less
}
}
_ => std::cmp::Ordering::Greater,
}
self.name.cmp(&other.name)
}
}

Expand All @@ -55,20 +48,19 @@ impl DomEvent {
node_area: Option<Area>,
scale_factor: f64,
) -> Self {
let is_pointer_event = event.is_pointer_event();
let event_name = event.get_name().to_string();
let name = event.get_name();

let bubbles = event.does_bubble();
let bubbles = name.does_bubble();

match event {
FreyaEvent::Mouse { cursor, button, .. } => {
PlatformEvent::Mouse { cursor, button, .. } => {
let screen_coordinates = cursor / scale_factor;
let element_x =
(cursor.x - node_area.unwrap_or_default().min_x() as f64) / scale_factor;
let element_y =
(cursor.y - node_area.unwrap_or_default().min_y() as f64) / scale_factor;

let event_data = if is_pointer_event {
let event_data = if name.is_pointer() {
DomEventData::Pointer(PointerData::new(
screen_coordinates,
(element_x, element_y).into(),
Expand All @@ -87,34 +79,34 @@ impl DomEvent {
Self {
node_id,
element_id,
name: event_name,
name,
data: event_data,
bubbles,
layer,
}
}
FreyaEvent::Wheel { scroll, .. } => Self {
PlatformEvent::Wheel { scroll, .. } => Self {
node_id,
element_id,
name: event_name,
name,
data: DomEventData::Wheel(WheelData::new(scroll.x, scroll.y)),
bubbles,
layer,
},
FreyaEvent::Keyboard {
PlatformEvent::Keyboard {
ref key,
code,
modifiers,
..
} => Self {
node_id,
element_id,
name: event_name,
name,
data: DomEventData::Keyboard(KeyboardData::new(key.clone(), code, modifiers)),
bubbles,
layer,
},
FreyaEvent::Touch {
PlatformEvent::Touch {
location,
finger_id,
phase,
Expand All @@ -124,7 +116,7 @@ impl DomEvent {
let element_x = location.x - node_area.unwrap_or_default().min_x() as f64;
let element_y = location.y - node_area.unwrap_or_default().min_y() as f64;

let event_data = if is_pointer_event {
let event_data = if name.is_pointer() {
DomEventData::Pointer(PointerData::new(
location,
(element_x, element_y).into(),
Expand All @@ -147,23 +139,14 @@ impl DomEvent {
Self {
node_id,
element_id,
name: event_name,
name,
data: event_data,
bubbles,
layer,
}
}
}
}

pub fn does_move_cursor(&self) -> bool {
return does_event_move_cursor(self.name.as_str());
}

// Check if this even can change the hover state of an Element.
pub fn can_change_element_hover_state(&self) -> bool {
["mouseover", "mouseenter", "pointerover", "pointerenter"].contains(&self.name.as_str())
}
}

/// Data of a DOM event.
Expand All @@ -187,7 +170,3 @@ impl DomEventData {
}
}
}

pub fn does_event_move_cursor(event_name: &str) -> bool {
["pointerover", "pointerenter", "mouseover", "mouseenter"].contains(&event_name)
}
Loading

0 comments on commit 537a2fa

Please sign in to comment.