summaryrefslogtreecommitdiffstats
path: root/widget/src/pick_list.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-06-14 11:25:05 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-06-14 11:25:05 +0200
commit87db76a11ffd0e3e2350c2b2531fde1c56ffeea6 (patch)
treeade93bd6fea2eeeb4a2a8792e5b8b80b82995c3c /widget/src/pick_list.rs
parentb0205e03d8e4794850e55e8c4bf83a40dd41aa9d (diff)
downloadiced-87db76a11ffd0e3e2350c2b2531fde1c56ffeea6.tar.gz
iced-87db76a11ffd0e3e2350c2b2531fde1c56ffeea6.tar.bz2
iced-87db76a11ffd0e3e2350c2b2531fde1c56ffeea6.zip
Make `overlay::Menu` publish messages on selection
Diffstat (limited to 'widget/src/pick_list.rs')
-rw-r--r--widget/src/pick_list.rs44
1 files changed, 19 insertions, 25 deletions
diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs
index dc53a9c8..832aae6b 100644
--- a/widget/src/pick_list.rs
+++ b/widget/src/pick_list.rs
@@ -157,11 +157,11 @@ where
From<<Renderer::Theme as StyleSheet>::Style>,
{
fn tag(&self) -> tree::Tag {
- tree::Tag::of::<State<T>>()
+ tree::Tag::of::<State>()
}
fn state(&self) -> tree::State {
- tree::State::new(State::<T>::new())
+ tree::State::new(State::new())
}
fn width(&self) -> Length {
@@ -209,7 +209,7 @@ where
self.on_selected.as_ref(),
self.selected.as_ref(),
&self.options,
- || tree.state.downcast_mut::<State<T>>(),
+ || tree.state.downcast_mut::<State>(),
)
}
@@ -249,7 +249,7 @@ where
self.selected.as_ref(),
&self.handle,
&self.style,
- || tree.state.downcast_ref::<State<T>>(),
+ || tree.state.downcast_ref::<State>(),
)
}
@@ -259,7 +259,7 @@ where
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
- let state = tree.state.downcast_mut::<State<T>>();
+ let state = tree.state.downcast_mut::<State>();
overlay(
layout,
@@ -269,6 +269,7 @@ where
self.text_shaping,
self.font.unwrap_or_else(|| renderer.default_font()),
&self.options,
+ &self.on_selected,
self.style.clone(),
)
}
@@ -295,15 +296,14 @@ where
/// The local state of a [`PickList`].
#[derive(Debug)]
-pub struct State<T> {
+pub struct State {
menu: menu::State,
keyboard_modifiers: keyboard::Modifiers,
is_open: bool,
hovered_option: Option<usize>,
- last_selection: Option<T>,
}
-impl<T> State<T> {
+impl State {
/// Creates a new [`State`] for a [`PickList`].
pub fn new() -> Self {
Self {
@@ -311,12 +311,11 @@ impl<T> State<T> {
keyboard_modifiers: keyboard::Modifiers::default(),
is_open: bool::default(),
hovered_option: Option::default(),
- last_selection: Option::default(),
}
}
}
-impl<T> Default for State<T> {
+impl Default for State {
fn default() -> Self {
Self::new()
}
@@ -436,7 +435,7 @@ pub fn update<'a, T, Message>(
on_selected: &dyn Fn(T) -> Message,
selected: Option<&T>,
options: &[T],
- state: impl FnOnce() -> &'a mut State<T>,
+ state: impl FnOnce() -> &'a mut State,
) -> event::Status
where
T: PartialEq + Clone + 'a,
@@ -446,7 +445,7 @@ where
| Event::Touch(touch::Event::FingerPressed { .. }) => {
let state = state();
- let event_status = if state.is_open {
+ if state.is_open {
// Event wasn't processed by overlay, so cursor was clicked either outside it's
// bounds or on the drop-down, either way we close the overlay.
state.is_open = false;
@@ -460,16 +459,6 @@ where
event::Status::Captured
} else {
event::Status::Ignored
- };
-
- if let Some(last_selection) = state.last_selection.take() {
- shell.publish((on_selected)(last_selection));
-
- state.is_open = false;
-
- event::Status::Captured
- } else {
- event_status
}
}
Event::Mouse(mouse::Event::WheelScrolled {
@@ -544,12 +533,13 @@ pub fn mouse_interaction(
/// Returns the current overlay of a [`PickList`].
pub fn overlay<'a, T, Message, Renderer>(
layout: Layout<'_>,
- state: &'a mut State<T>,
+ state: &'a mut State,
padding: Padding,
text_size: Option<f32>,
text_shaping: text::Shaping,
font: Renderer::Font,
options: &'a [T],
+ on_selected: &'a dyn Fn(T) -> Message,
style: <Renderer::Theme as StyleSheet>::Style,
) -> Option<overlay::Element<'a, Message, Renderer>>
where
@@ -570,7 +560,11 @@ where
&mut state.menu,
options,
&mut state.hovered_option,
- &mut state.last_selection,
+ |option| {
+ state.is_open = false;
+
+ (on_selected)(option)
+ },
)
.width(bounds.width)
.padding(padding)
@@ -603,7 +597,7 @@ pub fn draw<'a, T, Renderer>(
selected: Option<&T>,
handle: &Handle<Renderer::Font>,
style: &<Renderer::Theme as StyleSheet>::Style,
- state: impl FnOnce() -> &'a State<T>,
+ state: impl FnOnce() -> &'a State,
) where
Renderer: text::Renderer,
Renderer::Theme: StyleSheet,