Skip to content

Commit

Permalink
key mod cmp (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton authored Oct 29, 2024
1 parent 56e95cd commit 82061d3
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 36 deletions.
10 changes: 6 additions & 4 deletions examples/context/src/main.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 6 additions & 4 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
keyboard::{Key, Modifiers, NamedKey},
keyboard::{Key, NamedKey},
peniko::Color,
reactive::{create_signal, SignalGet, SignalUpdate},
unit::UnitExt,
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 6 additions & 4 deletions examples/draggable/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
keyboard::{Key, Modifiers, NamedKey},
keyboard::{Key, NamedKey},
peniko::Color,
reactive::{create_rw_signal, RwSignal, SignalGet, SignalUpdate},
style::CursorStyle,
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 6 additions & 4 deletions examples/dropped_file/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use floem::{
event::EventListener,
keyboard::{Key, Modifiers, NamedKey},
keyboard::{Key, NamedKey},
unit::UnitExt,
views::{dyn_view, Decorators},
IntoView, View,
Expand All @@ -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());
})
Expand Down
10 changes: 6 additions & 4 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
keyboard::{Key, Modifiers, NamedKey},
keyboard::{Key, NamedKey},
reactive::{RwSignal, SignalGet, SignalUpdate},
views::{
button,
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 6 additions & 4 deletions examples/files/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 6 additions & 4 deletions examples/stacks/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
keyboard::{Key, Modifiers, NamedKey},
keyboard::{Key, NamedKey},
views::Decorators,
IntoView, View,
};
Expand Down Expand Up @@ -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() {
Expand Down
9 changes: 5 additions & 4 deletions examples/syntax-editor/src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ pub trait Decorators: IntoView<V = Self::DV> + 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;
}
Expand All @@ -185,12 +185,12 @@ pub trait Decorators: IntoView<V = Self::DV> + 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;
}
Expand Down

0 comments on commit 82061d3

Please sign in to comment.