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

Event start stop #170

Merged
merged 9 commits into from
Nov 13, 2023
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
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