Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stackable on_key_down and on_key_up event handlers #255

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use floem::{
event::{Event, EventListener},
keyboard::{Key, NamedKey},
keyboard::{Key, ModifiersState, NamedKey},
peniko::Color,
reactive::create_signal,
unit::UnitExt,
Expand Down Expand Up @@ -74,13 +73,11 @@ fn app_view() -> impl View {
});

let id = view.id();
view.on_event_stop(EventListener::KeyUp, move |e| {
if let Event::KeyUp(e) = e {
if e.key.logical_key == Key::Named(NamedKey::F11) {
id.inspect();
}
}
})
view.on_key_up(
Key::Named(NamedKey::F11),
ModifiersState::empty(),
move |_| id.inspect(),
)
}

fn main() {
Expand Down
6 changes: 6 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,12 @@ impl<'a> EventCx<'a> {
_ => (),
}

for handler in &view.view_data().event_handlers {
if (handler)(&event).is_processed() {
return EventPropagation::Stop;
}
}

if let Some(listener) = event.listener() {
if let Some(action) = self.get_event_listener(id, &listener) {
let should_run = if let Some(pos) = event.point() {
Expand Down
13 changes: 11 additions & 2 deletions src/view_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ use std::{collections::HashMap, marker::PhantomData, time::Duration};
use taffy::node::Node;

/// A stack of view attributes. Each entry is associated with a view decorator call.
#[derive(Default)]
pub(crate) struct Stack<T> {
stack: SmallVec<[T; 1]>,
pub(crate) stack: SmallVec<[T; 1]>,
}

impl<T> Default for Stack<T> {
fn default() -> Self {
Stack {
stack: SmallVec::new(),
}
}
}

pub(crate) struct StackOffset<T> {
Expand Down Expand Up @@ -57,13 +64,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
39 changes: 39 additions & 0 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use floem_reactive::{create_effect, create_updater};
use kurbo::{Point, Rect};
use winit::keyboard::{Key, ModifiersState};

use crate::{
action::{set_window_menu, set_window_title, update_window_scale},
Expand Down Expand Up @@ -91,6 +92,44 @@ pub trait Decorators: View + Sized {
self
}

/// Add an handler for pressing down a specific key.
fn on_key_down(
mut self,
key: Key,
modifiers: ModifiersState,
action: impl Fn(&Event) + 'static,
) -> Self {
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
}));
self
}

/// Add an handler for a specific key being released.
fn on_key_up(
mut self,
key: Key,
modifiers: ModifiersState,
action: impl Fn(&Event) + 'static,
) -> Self {
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
}));
self
}

/// Add an event hanlder for the given EventListener
///
/// This event will be handled with the given handler and the event will continue propagating
Expand Down
7 changes: 7 additions & 0 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ 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() {
if let Some(action) = cx.get_event_listener(self.view.main.id(), &listener)
{
Expand Down