Skip to content

Commit

Permalink
Event start stop (#170)
Browse files Browse the repository at this point in the history
* Add docs and default style for toggle button

* Update default style

* remove unused imports

* Add start and stop event methods

* Add event propagation enum

* Add more docs

* Prefer 'is_processed'

* Fix linux menu

* Fix examples
  • Loading branch information
jrmoulton authored Nov 13, 2023
1 parent 26a8297 commit 88eebf3
Show file tree
Hide file tree
Showing 20 changed files with 1,503 additions and 1,423 deletions.
9 changes: 3 additions & 6 deletions examples/animations/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ fn app_view() -> impl View {

stack({
(label(|| "Hover or click me!")
.on_click(move |_| {
.on_click_stop(move |_| {
set_counter.update(|value| *value += 1.0);
true
})
.on_event(EventListener::PointerEnter, move |_| {
.on_event_stop(EventListener::PointerEnter, move |_| {
set_is_hovered.update(|val| *val = true);
true
})
.on_event(EventListener::PointerLeave, move |_| {
.on_event_stop(EventListener::PointerLeave, move |_| {
set_is_hovered.update(|val| *val = false);
true
})
.style(|s| {
s.border(1.0)
Expand Down
12 changes: 4 additions & 8 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ fn app_view() -> impl View {
.hover(|s| s.background(Color::LIGHT_GREEN))
.active(|s| s.color(Color::WHITE).background(Color::DARK_GREEN))
})
.on_click({
.on_click_stop({
move |_| {
set_counter.update(|value| *value += 1);
true
}
})
.keyboard_navigatable(),
text("Decrement")
.on_click({
.on_click_stop({
move |_| {
set_counter.update(|value| *value -= 1);
true
}
})
.style(|s| {
Expand All @@ -49,10 +47,9 @@ fn app_view() -> impl View {
})
.keyboard_navigatable(),
text("Reset to 0")
.on_click(move |_| {
.on_click_stop(move |_| {
println!("Reset counter pressed"); // will not fire if button is disabled
set_counter.update(|value| *value = 0);
true
})
.disabled(move || counter.get() == 0)
.style(|s| {
Expand All @@ -77,13 +74,12 @@ fn app_view() -> impl View {
});

let id = view.id();
view.on_event(EventListener::KeyUp, move |e| {
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();
}
}
true
})
}

Expand Down
15 changes: 5 additions & 10 deletions examples/themes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,35 @@ fn app_view() -> impl View {
let view = stack((stack((
text("Toggle Theme")
.class(Button)
.on_click({
.on_click_stop({
move |_| {
set_theme.update(|theme| *theme = !*theme);
true
}
})
.keyboard_navigatable(),
stack((
label(move || format!("Value: {}", counter.get())).class(Label),
text("Increment")
.class(Button)
.on_click({
.on_click_stop({
move |_| {
set_counter.update(|value| *value += 1);
true
}
})
.keyboard_navigatable(),
text("Decrement")
.class(Button)
.on_click({
.on_click_stop({
move |_| {
set_counter.update(|value| *value -= 1);
true
}
})
.keyboard_navigatable(),
text("Reset to 0")
.class(Button)
.on_click(move |_| {
.on_click_stop(move |_| {
println!("Reset counter pressed"); // will not fire if button is disabled
set_counter.update(|value| *value = 0);
true
})
.disabled(move || counter.get() == 0)
.keyboard_navigatable(),
Expand All @@ -150,13 +146,12 @@ fn app_view() -> impl View {
.window_title(|| "Themes Example".to_string());

let id = view.id();
view.on_event(EventListener::KeyUp, move |e| {
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();
}
}
true
})
}

Expand Down
12 changes: 4 additions & 8 deletions examples/widget-gallery/src/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ pub fn button_view() -> impl View {
form({
(
form_item("Basic Button:".to_string(), 120.0, || {
button(|| "Click me").on_click(|_| {
button(|| "Click me").on_click_stop(|_| {
println!("Button clicked");
true
})
}),
form_item("Styled Button:".to_string(), 120.0, || {
button(|| "Click me")
.on_click(|_| {
.on_click_stop(|_| {
println!("Button clicked");
true
})
.style(|s| {
s.border(1.0)
Expand All @@ -37,15 +35,13 @@ pub fn button_view() -> impl View {
})
}),
form_item("Disabled Button:".to_string(), 120.0, || {
button(|| "Click me").disabled(|| true).on_click(|_| {
button(|| "Click me").disabled(|| true).on_click_stop(|_| {
println!("Button clicked");
true
})
}),
form_item("Secondary click button:".to_string(), 120.0, || {
button(|| "Right click me").on_secondary_click(|_| {
button(|| "Right click me").on_secondary_click_stop(|_| {
println!("Secondary mouse button click.");
true
})
}),
form_item("Toggle button - Switch:".to_string(), 120.0, || {
Expand Down
12 changes: 4 additions & 8 deletions examples/widget-gallery/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,30 @@ pub fn checkbox_view() -> impl View {
form_item("Checkbox:".to_string(), width, move || {
checkbox(is_checked)
.style(|s| s.margin(5.0))
.on_click(move |_| {
.on_click_stop(move |_| {
set_is_checked.update(|checked| *checked = !*checked);
true
})
}),
form_item("Disabled Checkbox:".to_string(), width, move || {
checkbox(is_checked)
.style(|s| s.margin(5.0))
.on_click(move |_| {
.on_click_stop(move |_| {
set_is_checked.update(|checked| *checked = !*checked);
true
})
.disabled(|| true)
}),
form_item("Labelled Checkbox:".to_string(), width, move || {
labeled_checkbox(is_checked, || "Check me!").on_click(move |_| {
labeled_checkbox(is_checked, || "Check me!").on_click_stop(move |_| {
set_is_checked.update(|checked| *checked = !*checked);
true
})
}),
form_item(
"Disabled Labelled Checkbox:".to_string(),
width,
move || {
labeled_checkbox(is_checked, || "Check me!")
.on_click(move |_| {
.on_click_stop(move |_| {
set_is_checked.update(|checked| *checked = !*checked);
true
})
.disabled(|| true)
},
Expand Down
18 changes: 8 additions & 10 deletions examples/widget-gallery/src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use floem::{
VirtualListItemSize,
},
widgets::checkbox,
EventPropagation,
};

use crate::form::{form, form_item};
Expand Down Expand Up @@ -64,20 +65,18 @@ fn enhanced_list() -> impl View {
container({
stack({
(
checkbox(is_checked).on_click(move |_| {
checkbox(is_checked).on_click_stop(move |_| {
set_is_checked.update(|checked: &mut bool| *checked = !*checked);
true
}),
label(move || item.to_string())
.style(|s| s.height(32.0).font_size(32.0)),
container({
label(move || " X ")
.on_click(move |_| {
.on_click_stop(move |_| {
print!("Item Removed");
set_long_list.update(|x| {
x.remove(index);
});
true
})
.style(|s| {
s.height(18.0)
Expand All @@ -99,11 +98,10 @@ fn enhanced_list() -> impl View {
})
.style(move |s| s.height(item_height).width(list_width).items_center())
})
.on_click(move |_| {
.on_click_stop(move |_| {
set_selected.update(|v: &mut usize| {
*v = long_list.get().iter().position(|it| *it == item).unwrap();
});
true
})
.on_event(EventListener::KeyDown, move |e| {
if let Event::KeyDown(key_event) = e {
Expand All @@ -113,18 +111,18 @@ fn enhanced_list() -> impl View {
if sel > 0 {
set_selected.update(|v| *v -= 1);
}
true
EventPropagation::Stop
}
Key::Named(NamedKey::ArrowDown) => {
if sel < long_list.get().len() - 1 {
set_selected.update(|v| *v += 1);
}
true
EventPropagation::Stop
}
_ => false,
_ => EventPropagation::Continue,
}
} else {
false
EventPropagation::Continue
}
})
.keyboard_navigatable()
Expand Down
20 changes: 9 additions & 11 deletions examples/widget-gallery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use floem::{
Decorators, VirtualListDirection, VirtualListItemSize,
},
widgets::button,
EventPropagation,
};

fn app_view() -> impl View {
Expand All @@ -46,15 +47,14 @@ fn app_view() -> impl View {
.position(|it| *it == item)
.unwrap();
stack((label(move || item).style(|s| s.font_size(18.0)),))
.on_click(move |_| {
.on_click_stop(move |_| {
set_active_tab.update(|v: &mut usize| {
*v = tabs
.get_untracked()
.iter()
.position(|it| *it == item)
.unwrap();
});
true
})
.on_event(EventListener::KeyDown, move |e| {
if let Event::KeyDown(key_event) = e {
Expand All @@ -65,21 +65,21 @@ fn app_view() -> impl View {
if active > 0 {
set_active_tab.update(|v| *v -= 1)
}
true
EventPropagation::Stop
}
Key::Named(NamedKey::ArrowDown) => {
if active < tabs.get().len() - 1 {
set_active_tab.update(|v| *v += 1)
}
true
EventPropagation::Stop
}
_ => false,
_ => EventPropagation::Continue,
}
} else {
false
EventPropagation::Continue
}
} else {
false
EventPropagation::Continue
}
})
.keyboard_navigatable()
Expand Down Expand Up @@ -126,9 +126,8 @@ fn app_view() -> impl View {

let id = list.id();
let inspector = button(|| "Open Inspector")
.on_click(move |_| {
.on_click_stop(move |_| {
id.inspect();
true
})
.style(|s| s);

Expand Down Expand Up @@ -159,13 +158,12 @@ fn app_view() -> impl View {
.window_title(|| "Widget Gallery".to_owned());

let id = view.id();
view.on_event(EventListener::KeyUp, move |e| {
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();
}
}
true
})
}

Expand Down
Loading

0 comments on commit 88eebf3

Please sign in to comment.