Skip to content

Commit

Permalink
1. Combined with the inspector, this feature can help users identify …
Browse files Browse the repository at this point in the history
…which view handles or stops the event. It is very useful in large projects.
  • Loading branch information
jm-observer committed Jan 3, 2025
1 parent d1b447f commit 68de9c5
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 25 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ raw-window-handle = "0.6.0"
wgpu = { version = "22.0.0" }
parking_lot = { version = "0.12.1" }
swash = { version = "0.1.17" }
log = "0.4.22"

[dependencies]
slotmap = "1.0.7"
Expand Down Expand Up @@ -81,6 +82,7 @@ im = { workspace = true }
wgpu = { workspace = true }
futures = { version = "0.3.30", optional = true }
crossbeam = "0.8"
log = { workspace = true , optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = { version = "0.4" }
Expand Down Expand Up @@ -137,3 +139,5 @@ tokio = ["dep:tokio"]
rfd-async-std = ["dep:rfd", "rfd/async-std"]
rfd-tokio = ["dep:rfd", "rfd/tokio"]
futures = ["dep:futures"]

log = ["dep:log"]
54 changes: 29 additions & 25 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use taffy::prelude::NodeId;

use crate::animate::{AnimStateKind, RepeatMode};
use crate::easing::{Easing, Linear};
use crate::event::EventResult;
use crate::renderer::Renderer;
use crate::style::DisplayProp;
use crate::view_state::IsHiddenState;
Expand Down Expand Up @@ -93,15 +94,15 @@ impl EventCx<'_> {
view_id: ViewId,
event: Event,
directed: bool,
) -> EventPropagation {
) -> EventResult {
if view_id.style_has_hidden() {
// we don't process events for hidden view
return EventPropagation::Continue;
return EventResult::event_continue();
}
if self.app_state.is_disabled(&view_id) && !event.allow_disabled() {
// if the view is disabled and the event is not processed
// for disabled views
return EventPropagation::Continue;
return EventResult::event_continue();
}

// offset the event positions if the event has positions
Expand Down Expand Up @@ -137,7 +138,7 @@ impl EventCx<'_> {
}
}
}
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}

if !directed {
Expand All @@ -150,7 +151,7 @@ impl EventCx<'_> {
.unconditional_view_event(child, event.clone(), false)
.is_processed()
{
return EventPropagation::Stop;
return EventResult::event_stop(child, &event);
}
if event.is_pointer() {
break;
Expand All @@ -164,37 +165,39 @@ impl EventCx<'_> {
.event_after_children(self, &event)
.is_processed()
{
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}

// CLARIFY: should this be disabled when disable_default?
if !disable_default {
match &event {
Event::PointerDown(event) => {
Event::PointerDown(pointer_down_event) => {
self.app_state.clicking.insert(view_id);
if event.button.is_primary() {
if pointer_down_event.button.is_primary() {
let rect = view_id.get_size().unwrap_or_default().to_rect();
let now_focused = rect.contains(event.pos);
let now_focused = rect.contains(pointer_down_event.pos);

if now_focused {
if self.app_state.keyboard_navigable.contains(&view_id) {
// if the view can be focused, we update the focus
self.app_state.update_focus(view_id, false);
}
if event.count == 2
if pointer_down_event.count == 2
&& view_state
.borrow()
.event_listeners
.contains_key(&EventListener::DoubleClick)
{
view_state.borrow_mut().last_pointer_down = Some(event.clone());
view_state.borrow_mut().last_pointer_down =
Some(pointer_down_event.clone());
}
if view_state
.borrow()
.event_listeners
.contains_key(&EventListener::Click)
{
view_state.borrow_mut().last_pointer_down = Some(event.clone());
view_state.borrow_mut().last_pointer_down =
Some(pointer_down_event.clone());
}

let bottom_left = {
Expand All @@ -204,17 +207,17 @@ impl EventCx<'_> {
let popout_menu = view_state.borrow().popout_menu.clone();
if let Some(menu) = popout_menu {
show_context_menu(menu(), Some(bottom_left));
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}
if self.app_state.draggable.contains(&view_id)
&& self.app_state.drag_start.is_none()
{
self.app_state.drag_start = Some((view_id, event.pos));
self.app_state.drag_start = Some((view_id, pointer_down_event.pos));
}
}
} else if event.button.is_secondary() {
} else if pointer_down_event.button.is_secondary() {
let rect = view_id.get_size().unwrap_or_default().to_rect();
let now_focused = rect.contains(event.pos);
let now_focused = rect.contains(pointer_down_event.pos);

if now_focused {
if self.app_state.keyboard_navigable.contains(&view_id) {
Expand All @@ -226,7 +229,8 @@ impl EventCx<'_> {
.event_listeners
.contains_key(&EventListener::SecondaryClick)
{
view_state.borrow_mut().last_pointer_down = Some(event.clone());
view_state.borrow_mut().last_pointer_down =
Some(pointer_down_event.clone());
}
}
}
Expand Down Expand Up @@ -284,7 +288,7 @@ impl EventCx<'_> {
.apply_event(&EventListener::PointerMove, &event)
.is_some_and(|prop| prop.is_processed())
{
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}
}
Event::PointerUp(pointer_event) => {
Expand Down Expand Up @@ -334,7 +338,7 @@ impl EventCx<'_> {
handled | (handler.borrow_mut())(&event).is_processed()
})
{
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}
}

Expand All @@ -346,15 +350,15 @@ impl EventCx<'_> {
handled | (handler.borrow_mut())(&event).is_processed()
})
{
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}
}

if view_id
.apply_event(&EventListener::PointerUp, &event)
.is_some_and(|prop| prop.is_processed())
{
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}
} else if pointer_event.button.is_secondary() {
let rect = view_id.get_size().unwrap_or_default().to_rect();
Expand All @@ -370,7 +374,7 @@ impl EventCx<'_> {
handled | (handler.borrow_mut())(&event).is_processed()
})
{
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}
}

Expand All @@ -384,7 +388,7 @@ impl EventCx<'_> {
let context_menu = view_state.borrow().context_menu.clone();
if let Some(menu) = context_menu {
show_context_menu(menu(), Some(viewport_event_position));
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}
}
}
Expand Down Expand Up @@ -417,13 +421,13 @@ impl EventCx<'_> {
handled | (handler.borrow_mut())(&event).is_processed()
})
{
return EventPropagation::Stop;
return EventResult::event_stop(view_id, &event);
}
}
}
}

EventPropagation::Continue
EventResult::event_continue()
}

/// translate a window-positioned event to the local coordinate system of a view
Expand Down
87 changes: 87 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,101 @@ use floem_winit::{
window::Theme,
};
use peniko::kurbo::{Affine, Point, Size};
use slotmap::Key;

use crate::{
dropped_file::DroppedFileEvent,
keyboard::KeyEvent,
pointer::{PointerInputEvent, PointerMoveEvent, PointerWheelEvent},
touchpad::TouchpadMagnifyEvent,
ViewId,
};

pub struct EventResult(pub EventPropagation);

impl EventResult {
pub fn event_continue() -> Self {
Self(EventPropagation::Continue)
}

pub fn event_stop(view_id: ViewId, event: &Event) -> Self {
#[cfg(feature = "log")]
match event {
Event::PointerDown(_) => {
log::debug!("PointerDown processed by {}", view_id.data().as_ffi());
}
Event::PointerUp(_) => {
log::debug!("PointerUp processed by {}", view_id.data().as_ffi());
}
Event::PointerMove(_) => {
log::debug!("PointerMove processed by {}", view_id.data().as_ffi());
}
Event::PointerWheel(_) => {
log::debug!("PointerWheel processed by {}", view_id.data().as_ffi());
}
Event::PointerLeave => {
log::debug!("PointerLeave processed by {}", view_id.data().as_ffi());
}
Event::DroppedFile(_) => {
log::debug!("DroppedFile processed by {}", view_id.data().as_ffi());
}
Event::KeyDown(_) => {
log::debug!("KeyDown processed by {}", view_id.data().as_ffi());
}
Event::KeyUp(_) => {
log::debug!("KeyUp processed by {}", view_id.data().as_ffi());
}
Event::ImeEnabled => {
log::debug!("ImeEnabled processed by {}", view_id.data().as_ffi());
}
Event::ImeDisabled => {
log::debug!("ImeDisabled processed by {}", view_id.data().as_ffi());
}
Event::ImePreedit { .. } => {
log::debug!("ImePreedit processed by {}", view_id.data().as_ffi());
}
Event::ImeCommit(_) => {
log::debug!("ImeCommit processed by {}", view_id.data().as_ffi());
}
Event::WindowGotFocus => {
log::debug!("WindowGotFocus processed by {}", view_id.data().as_ffi());
}
Event::WindowLostFocus => {
log::debug!("WindowLostFocus processed by {}", view_id.data().as_ffi());
}
Event::WindowClosed => {
log::debug!("WindowClosed processed by {}", view_id.data().as_ffi());
}
Event::WindowResized(_) => {
log::debug!("WindowResized processed by {}", view_id.data().as_ffi());
}
Event::WindowMoved(_) => {
log::debug!("WindowMoved processed by {}", view_id.data().as_ffi());
}
Event::WindowMaximizeChanged(_) => {
log::debug!(
"WindowMaximizeChanged processed by {}",
view_id.data().as_ffi()
);
}
Event::ThemeChanged(_) => {
log::debug!("ThemeChanged processed by {}", view_id.data().as_ffi());
}
Event::FocusGained => {
log::debug!("FocusGained processed by {}", view_id.data().as_ffi());
}
Event::FocusLost => {
log::debug!("FocusLost processed by {}", view_id.data().as_ffi());
}
Event::TouchpadMagnify(_) => {}
}
Self(EventPropagation::Stop)
}
pub fn is_processed(&self) -> bool {
self.0.is_processed()
}
}

/// Control whether an event will continue propagating or whether it should stop.
pub enum EventPropagation {
/// Stop event propagation and mark the event as processed
Expand Down

0 comments on commit 68de9c5

Please sign in to comment.