From 82061d3e9e461ea026174fdf158c2b52be097e94 Mon Sep 17 00:00:00 2001 From: Jared Moulton Date: Tue, 29 Oct 2024 15:26:29 -0600 Subject: [PATCH] key mod cmp (#642) --- examples/context/src/main.rs | 10 ++++++---- examples/counter/src/main.rs | 10 ++++++---- examples/draggable/src/main.rs | 10 ++++++---- examples/dropped_file/src/main.rs | 10 ++++++---- examples/editor/src/main.rs | 10 ++++++---- examples/files/src/main.rs | 10 ++++++---- examples/stacks/src/main.rs | 10 ++++++---- examples/syntax-editor/src/main.rs | 9 +++++---- src/views/decorator.rs | 8 ++++---- 9 files changed, 51 insertions(+), 36 deletions(-) diff --git a/examples/context/src/main.rs b/examples/context/src/main.rs index c8909889..fae00096 100644 --- a/examples/context/src/main.rs +++ b/examples/context/src/main.rs @@ -1,5 +1,5 @@ use floem::{ - keyboard::{Key, Modifiers, NamedKey}, + keyboard::{Key, NamedKey}, peniko::Color, reactive::{provide_context, use_context}, views::{empty, label, v_stack, Decorators}, @@ -47,9 +47,11 @@ fn app_view() -> impl IntoView { }); let id = view.id(); - view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| { - id.inspect() - }) + view.on_key_up( + Key::Named(NamedKey::F11), + |m| m.is_empty(), + move |_| id.inspect(), + ) } fn main() { diff --git a/examples/counter/src/main.rs b/examples/counter/src/main.rs index 427409e3..13b938c5 100644 --- a/examples/counter/src/main.rs +++ b/examples/counter/src/main.rs @@ -1,5 +1,5 @@ use floem::{ - keyboard::{Key, Modifiers, NamedKey}, + keyboard::{Key, NamedKey}, peniko::Color, reactive::{create_signal, SignalGet, SignalUpdate}, unit::UnitExt, @@ -79,9 +79,11 @@ fn app_view() -> impl IntoView { }); let id = view.id(); - view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| { - id.inspect() - }) + view.on_key_up( + Key::Named(NamedKey::F11), + |m| m.is_empty(), + move |_| id.inspect(), + ) } fn main() { diff --git a/examples/draggable/src/main.rs b/examples/draggable/src/main.rs index f83ee117..9dfb168c 100644 --- a/examples/draggable/src/main.rs +++ b/examples/draggable/src/main.rs @@ -1,5 +1,5 @@ use floem::{ - keyboard::{Key, Modifiers, NamedKey}, + keyboard::{Key, NamedKey}, peniko::Color, reactive::{create_rw_signal, RwSignal, SignalGet, SignalUpdate}, style::CursorStyle, @@ -97,9 +97,11 @@ fn app_view() -> impl IntoView { .into_view(); let id = view.id(); - view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| { - id.inspect() - }) + view.on_key_up( + Key::Named(NamedKey::F11), + |m| m.is_empty(), + move |_| id.inspect(), + ) } fn main() { diff --git a/examples/dropped_file/src/main.rs b/examples/dropped_file/src/main.rs index b38afd0a..d4036725 100644 --- a/examples/dropped_file/src/main.rs +++ b/examples/dropped_file/src/main.rs @@ -1,6 +1,6 @@ use floem::{ event::EventListener, - keyboard::{Key, Modifiers, NamedKey}, + keyboard::{Key, NamedKey}, unit::UnitExt, views::{dyn_view, Decorators}, IntoView, View, @@ -15,9 +15,11 @@ fn app_view() -> impl IntoView { }); let id = view.id(); - view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| { - id.inspect() - }) + view.on_key_up( + Key::Named(NamedKey::F11), + |m| m.is_empty(), + move |_| id.inspect(), + ) .on_event_stop(EventListener::PointerMove, |x| { println!("PointerMove {:?}", x.point()); }) diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 7f2f83c2..625cc056 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,5 +1,5 @@ use floem::{ - keyboard::{Key, Modifiers, NamedKey}, + keyboard::{Key, NamedKey}, reactive::{RwSignal, SignalGet, SignalUpdate}, views::{ button, @@ -67,9 +67,11 @@ fn app_view() -> impl IntoView { .style(|s| s.size_full().flex_col().items_center().justify_center()); let id = view.id(); - view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| { - id.inspect() - }) + view.on_key_up( + Key::Named(NamedKey::F11), + |m| m.is_empty(), + move |_| id.inspect(), + ) } fn main() { diff --git a/examples/files/src/main.rs b/examples/files/src/main.rs index f885e6cc..929d3287 100644 --- a/examples/files/src/main.rs +++ b/examples/files/src/main.rs @@ -1,7 +1,7 @@ use floem::{ action::{open_file, save_as}, file::{FileDialogOptions, FileSpec}, - keyboard::{Key, Modifiers, NamedKey}, + keyboard::{Key, NamedKey}, views::{button, h_stack, Decorators}, IntoView, View, }; @@ -87,9 +87,11 @@ fn app_view() -> impl IntoView { }); let id = view.id(); - view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| { - id.inspect() - }) + view.on_key_up( + Key::Named(NamedKey::F11), + |m| m.is_empty(), + move |_| id.inspect(), + ) } fn main() { diff --git a/examples/stacks/src/main.rs b/examples/stacks/src/main.rs index 25b38d50..c2884609 100644 --- a/examples/stacks/src/main.rs +++ b/examples/stacks/src/main.rs @@ -1,5 +1,5 @@ use floem::{ - keyboard::{Key, Modifiers, NamedKey}, + keyboard::{Key, NamedKey}, views::Decorators, IntoView, View, }; @@ -48,9 +48,11 @@ fn app_view() -> impl IntoView { .into_view(); let id = view.id(); - view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| { - id.inspect() - }) + view.on_key_up( + Key::Named(NamedKey::F11), + |m| m.is_empty(), + move |_| id.inspect(), + ) } fn main() { diff --git a/examples/syntax-editor/src/main.rs b/examples/syntax-editor/src/main.rs index e639f0d0..9f527716 100644 --- a/examples/syntax-editor/src/main.rs +++ b/examples/syntax-editor/src/main.rs @@ -1,4 +1,3 @@ -use floem::keyboard::Modifiers; use floem::peniko::Color; use floem::reactive::{RwSignal, SignalGet, SignalUpdate}; use floem::text::{Attrs, AttrsList, Stretch, Style, Weight}; @@ -244,9 +243,11 @@ mod tests { .style(|s| s.size_full().flex_col().items_center().justify_center()); let id = view.id(); - view.on_key_up(Key::Named(NamedKey::F11), Modifiers::empty(), move |_| { - id.inspect() - }) + view.on_key_up( + Key::Named(NamedKey::F11), + |m| m.is_empty(), + move |_| id.inspect(), + ) } fn main() { diff --git a/src/views/decorator.rs b/src/views/decorator.rs index 725e14b1..f79658f7 100644 --- a/src/views/decorator.rs +++ b/src/views/decorator.rs @@ -165,12 +165,12 @@ pub trait Decorators: IntoView + Sized { fn on_key_down( self, key: Key, - modifiers: Modifiers, + cmp: impl Fn(Modifiers) -> bool + 'static, action: impl Fn(&Event) + 'static, ) -> Self::DV { self.on_event(EventListener::KeyDown, move |e| { if let Event::KeyDown(ke) = e { - if ke.key.logical_key == key && ke.modifiers == modifiers { + if ke.key.logical_key == key && cmp(ke.modifiers) { action(e); return EventPropagation::Stop; } @@ -185,12 +185,12 @@ pub trait Decorators: IntoView + Sized { fn on_key_up( self, key: Key, - modifiers: Modifiers, + cmp: impl Fn(Modifiers) -> bool + 'static, action: impl Fn(&Event) + 'static, ) -> Self::DV { self.on_event(EventListener::KeyUp, move |e| { if let Event::KeyUp(ke) = e { - if ke.key.logical_key == key && ke.modifiers == modifiers { + if ke.key.logical_key == key && cmp(ke.modifiers) { action(e); return EventPropagation::Stop; }