From 6cf7c4645dab706081f164558e0b5ae228783338 Mon Sep 17 00:00:00 2001 From: Rinat Date: Fri, 1 Dec 2023 10:34:15 +0500 Subject: Add `on_opened` and `on_closed` handlers for `PickList` --- widget/src/helpers.rs | 1 + widget/src/pick_list.rs | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index e13e900a..3f34d165 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -271,6 +271,7 @@ pub fn pick_list<'a, Message, Theme, Renderer, T>( where T: ToString + PartialEq + 'static, [T]: ToOwned>, + Message: Clone, Renderer: core::text::Renderer, Theme: pick_list::StyleSheet + scrollable::StyleSheet diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 475115c2..a25369b8 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -35,6 +35,8 @@ pub struct PickList< Renderer: text::Renderer, { on_selected: Box Message + 'a>, + on_opened: Option, + on_closed: Option, options: Cow<'a, [T]>, placeholder: Option, selected: Option, @@ -53,6 +55,7 @@ impl<'a, T: 'a, Message, Theme, Renderer> where T: ToString + PartialEq, [T]: ToOwned>, + Message: Clone, Theme: StyleSheet + scrollable::StyleSheet + menu::StyleSheet @@ -72,6 +75,8 @@ where ) -> Self { Self { on_selected: Box::new(on_selected), + on_opened: None, + on_closed: None, options: options.into(), placeholder: None, selected, @@ -137,6 +142,18 @@ where self } + /// Sets the message that will be produced when the [`PickList`] Menu is openned menu. + pub fn on_opened(mut self, msg: Message) -> Self { + self.on_opened = Some(msg); + self + } + + /// Sets the message that will be produced when the [`PickList`] Menu is closed menu. + pub fn on_closed(mut self, msg: Message) -> Self { + self.on_closed = Some(msg); + self + } + /// Sets the style of the [`PickList`]. pub fn style( mut self, @@ -152,7 +169,7 @@ impl<'a, T: 'a, Message, Theme, Renderer> Widget where T: Clone + ToString + PartialEq + 'static, [T]: ToOwned>, - Message: 'a, + Message: Clone + 'a, Theme: StyleSheet + scrollable::StyleSheet + menu::StyleSheet @@ -213,6 +230,8 @@ where cursor, shell, self.on_selected.as_ref(), + self.on_opened.as_ref(), + self.on_closed.as_ref(), self.selected.as_ref(), &self.options, || tree.state.downcast_mut::>(), @@ -290,7 +309,7 @@ impl<'a, T: 'a, Message, Theme, Renderer> where T: Clone + ToString + PartialEq + 'static, [T]: ToOwned>, - Message: 'a, + Message: Clone + 'a, Theme: StyleSheet + scrollable::StyleSheet + menu::StyleSheet @@ -474,6 +493,8 @@ pub fn update<'a, T, P, Message>( cursor: mouse::Cursor, shell: &mut Shell<'_, Message>, on_selected: &dyn Fn(T) -> Message, + on_opened: Option<&Message>, + on_closed: Option<&Message>, selected: Option<&T>, options: &[T], state: impl FnOnce() -> &'a mut State

, @@ -481,6 +502,7 @@ pub fn update<'a, T, P, Message>( where T: PartialEq + Clone + 'a, P: text::Paragraph + 'a, + Message: Clone, { match event { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) @@ -492,12 +514,20 @@ where // bounds or on the drop-down, either way we close the overlay. state.is_open = false; + if let Some(on_closed) = on_closed { + shell.publish(on_closed.clone()); + } + event::Status::Captured } else if cursor.is_over(layout.bounds()) { state.is_open = true; state.hovered_option = options.iter().position(|option| Some(option) == selected); + if let Some(on_opened) = on_opened { + shell.publish(on_opened.clone()); + } + event::Status::Captured } else { event::Status::Ignored -- cgit From 59a79e55d4f6a55daa922abfdd5caed02753e042 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Feb 2024 00:03:23 +0100 Subject: Rename `PickList` handlers for consistency --- widget/src/pick_list.rs | 50 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index a25369b8..840c94fa 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -34,9 +34,9 @@ pub struct PickList< Theme: StyleSheet, Renderer: text::Renderer, { - on_selected: Box Message + 'a>, - on_opened: Option, - on_closed: Option, + on_select: Box Message + 'a>, + on_open: Option, + on_close: Option, options: Cow<'a, [T]>, placeholder: Option, selected: Option, @@ -71,12 +71,12 @@ where pub fn new( options: impl Into>, selected: Option, - on_selected: impl Fn(T) -> Message + 'a, + on_select: impl Fn(T) -> Message + 'a, ) -> Self { Self { - on_selected: Box::new(on_selected), - on_opened: None, - on_closed: None, + on_select: Box::new(on_select), + on_open: None, + on_close: None, options: options.into(), placeholder: None, selected, @@ -142,15 +142,15 @@ where self } - /// Sets the message that will be produced when the [`PickList`] Menu is openned menu. - pub fn on_opened(mut self, msg: Message) -> Self { - self.on_opened = Some(msg); + /// Sets the message that will be produced when the [`PickList`] is opened. + pub fn on_open(mut self, on_open: Message) -> Self { + self.on_open = Some(on_open); self } - /// Sets the message that will be produced when the [`PickList`] Menu is closed menu. - pub fn on_closed(mut self, msg: Message) -> Self { - self.on_closed = Some(msg); + /// Sets the message that will be produced when the [`PickList`] is closed. + pub fn on_close(mut self, on_close: Message) -> Self { + self.on_close = Some(on_close); self } @@ -229,9 +229,9 @@ where layout, cursor, shell, - self.on_selected.as_ref(), - self.on_opened.as_ref(), - self.on_closed.as_ref(), + self.on_select.as_ref(), + self.on_open.as_ref(), + self.on_close.as_ref(), self.selected.as_ref(), &self.options, || tree.state.downcast_mut::>(), @@ -297,7 +297,7 @@ where self.text_shaping, self.font.unwrap_or_else(|| renderer.default_font()), &self.options, - &self.on_selected, + &self.on_select, self.style.clone(), ) } @@ -492,9 +492,9 @@ pub fn update<'a, T, P, Message>( layout: Layout<'_>, cursor: mouse::Cursor, shell: &mut Shell<'_, Message>, - on_selected: &dyn Fn(T) -> Message, - on_opened: Option<&Message>, - on_closed: Option<&Message>, + on_select: &dyn Fn(T) -> Message, + on_open: Option<&Message>, + on_close: Option<&Message>, selected: Option<&T>, options: &[T], state: impl FnOnce() -> &'a mut State

, @@ -514,8 +514,8 @@ where // bounds or on the drop-down, either way we close the overlay. state.is_open = false; - if let Some(on_closed) = on_closed { - shell.publish(on_closed.clone()); + if let Some(on_close) = on_close { + shell.publish(on_close.clone()); } event::Status::Captured @@ -524,8 +524,8 @@ where state.hovered_option = options.iter().position(|option| Some(option) == selected); - if let Some(on_opened) = on_opened { - shell.publish(on_opened.clone()); + if let Some(on_open) = on_open { + shell.publish(on_open.clone()); } event::Status::Captured @@ -568,7 +568,7 @@ where }; if let Some(next_option) = next_option { - shell.publish((on_selected)(next_option.clone())); + shell.publish((on_select)(next_option.clone())); } event::Status::Captured -- cgit From 79b80fafb85386847c1d6338c306b842279b5821 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Feb 2024 00:11:31 +0100 Subject: Update `CHANGELOG` --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05d5ebb3..af45feb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Mouse movement events for `MouseArea`. [#2147](https://github.com/iced-rs/iced/pull/2147) - Dracula, Nord, Solarized, and Gruvbox variants for `Theme`. [#2170](https://github.com/iced-rs/iced/pull/2170) - `From where T: Into` for `svg::Handle`. [#2235](https://github.com/iced-rs/iced/pull/2235) +- `on_open` and `on_close` handlers for `PickList`. [#2174](https://github.com/iced-rs/iced/pull/2174) ### Changed - Enable WebGPU backend in `wgpu` by default instead of WebGL. [#2068](https://github.com/iced-rs/iced/pull/2068) -- cgit