Skip to content

Commit

Permalink
Add back event handlers in ViewData
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton committed Feb 6, 2024
1 parent 5feb8f5 commit 96c7031
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/view_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ impl<T> Stack<T> {
pub struct ViewData {
pub(crate) id: Id,
pub(crate) style: Stack<Style>,
pub(crate) event_handlers: Vec<Box<EventCallback>>,
}

impl ViewData {
pub fn new(id: Id) -> Self {
Self {
id,
style: Default::default(),
event_handlers: Default::default(),
}
}
pub fn id(&self) -> Id {
Expand Down
42 changes: 18 additions & 24 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,47 +96,41 @@ pub trait Decorators: View + Sized {
///
/// NOTE: View should have `.keyboard_navigable()` in order to receive keyboard events
fn on_key_down(
self,
mut self,
key: Key,
modifiers: ModifiersState,
action: impl Fn(&Event) + 'static,
) -> Self {
self.view_data().id().update_event_listener(
EventListener::KeyDown,
Box::new(move |e| {
if let Event::KeyDown(ke) = e {
if ke.key.logical_key == key && ke.modifiers == modifiers {
action(e);
return EventPropagation::Stop;
}
self.view_data_mut().event_handlers.push(Box::new(move |e| {
if let Event::KeyDown(ke) = e {
if ke.key.logical_key == key && ke.modifiers == modifiers {
action(e);
return EventPropagation::Stop;
}
EventPropagation::Continue
}),
);
}
EventPropagation::Continue
}));
self
}

/// Add an handler for a specific key being released.
///
/// NOTE: View should have `.keyboard_navigable()` in order to receive keyboard events
fn on_key_up(
self,
mut self,
key: Key,
modifiers: ModifiersState,
action: impl Fn(&Event) + 'static,
) -> Self {
self.view_data().id().update_event_listener(
EventListener::KeyUp,
Box::new(move |e| {
if let Event::KeyUp(ke) = e {
if ke.key.logical_key == key && ke.modifiers == modifiers {
action(e);
return EventPropagation::Stop;
}
self.view_data_mut().event_handlers.push(Box::new(move |e| {
if let Event::KeyUp(ke) = e {
if ke.key.logical_key == key && ke.modifiers == modifiers {
action(e);
return EventPropagation::Stop;
}
EventPropagation::Continue
}),
);
}
EventPropagation::Continue
}));
self
}

Expand Down
6 changes: 6 additions & 0 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ impl WindowHandle {
}

if !processed {
for handler in &self.view.main.view_data().event_handlers {
if (handler)(&event).is_processed() {
processed = true;
break;
}
}
if let Some(listener) = event.listener() {
processed |= cx
.app_state
Expand Down

0 comments on commit 96c7031

Please sign in to comment.