Merge pull request #2174 from hicaru/list_picker_events
List picker events
This commit is contained in:
commit
cb2c8dd86e
3 changed files with 41 additions and 9 deletions
|
|
@ -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)
|
- 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)
|
- Dracula, Nord, Solarized, and Gruvbox variants for `Theme`. [#2170](https://github.com/iced-rs/iced/pull/2170)
|
||||||
- `From<T> where T: Into<PathBuf>` for `svg::Handle`. [#2235](https://github.com/iced-rs/iced/pull/2235)
|
- `From<T> where T: Into<PathBuf>` 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
|
### Changed
|
||||||
- Enable WebGPU backend in `wgpu` by default instead of WebGL. [#2068](https://github.com/iced-rs/iced/pull/2068)
|
- Enable WebGPU backend in `wgpu` by default instead of WebGL. [#2068](https://github.com/iced-rs/iced/pull/2068)
|
||||||
|
|
|
||||||
|
|
@ -271,6 +271,7 @@ pub fn pick_list<'a, Message, Theme, Renderer, T>(
|
||||||
where
|
where
|
||||||
T: ToString + PartialEq + 'static,
|
T: ToString + PartialEq + 'static,
|
||||||
[T]: ToOwned<Owned = Vec<T>>,
|
[T]: ToOwned<Owned = Vec<T>>,
|
||||||
|
Message: Clone,
|
||||||
Renderer: core::text::Renderer,
|
Renderer: core::text::Renderer,
|
||||||
Theme: pick_list::StyleSheet
|
Theme: pick_list::StyleSheet
|
||||||
+ scrollable::StyleSheet
|
+ scrollable::StyleSheet
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,9 @@ pub struct PickList<
|
||||||
Theme: StyleSheet,
|
Theme: StyleSheet,
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
on_selected: Box<dyn Fn(T) -> Message + 'a>,
|
on_select: Box<dyn Fn(T) -> Message + 'a>,
|
||||||
|
on_open: Option<Message>,
|
||||||
|
on_close: Option<Message>,
|
||||||
options: Cow<'a, [T]>,
|
options: Cow<'a, [T]>,
|
||||||
placeholder: Option<String>,
|
placeholder: Option<String>,
|
||||||
selected: Option<T>,
|
selected: Option<T>,
|
||||||
|
|
@ -53,6 +55,7 @@ impl<'a, T: 'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
T: ToString + PartialEq,
|
T: ToString + PartialEq,
|
||||||
[T]: ToOwned<Owned = Vec<T>>,
|
[T]: ToOwned<Owned = Vec<T>>,
|
||||||
|
Message: Clone,
|
||||||
Theme: StyleSheet
|
Theme: StyleSheet
|
||||||
+ scrollable::StyleSheet
|
+ scrollable::StyleSheet
|
||||||
+ menu::StyleSheet
|
+ menu::StyleSheet
|
||||||
|
|
@ -68,10 +71,12 @@ where
|
||||||
pub fn new(
|
pub fn new(
|
||||||
options: impl Into<Cow<'a, [T]>>,
|
options: impl Into<Cow<'a, [T]>>,
|
||||||
selected: Option<T>,
|
selected: Option<T>,
|
||||||
on_selected: impl Fn(T) -> Message + 'a,
|
on_select: impl Fn(T) -> Message + 'a,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
on_selected: Box::new(on_selected),
|
on_select: Box::new(on_select),
|
||||||
|
on_open: None,
|
||||||
|
on_close: None,
|
||||||
options: options.into(),
|
options: options.into(),
|
||||||
placeholder: None,
|
placeholder: None,
|
||||||
selected,
|
selected,
|
||||||
|
|
@ -137,6 +142,18 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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`] is closed.
|
||||||
|
pub fn on_close(mut self, on_close: Message) -> Self {
|
||||||
|
self.on_close = Some(on_close);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`PickList`].
|
/// Sets the style of the [`PickList`].
|
||||||
pub fn style(
|
pub fn style(
|
||||||
mut self,
|
mut self,
|
||||||
|
|
@ -152,7 +169,7 @@ impl<'a, T: 'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
T: Clone + ToString + PartialEq + 'static,
|
T: Clone + ToString + PartialEq + 'static,
|
||||||
[T]: ToOwned<Owned = Vec<T>>,
|
[T]: ToOwned<Owned = Vec<T>>,
|
||||||
Message: 'a,
|
Message: Clone + 'a,
|
||||||
Theme: StyleSheet
|
Theme: StyleSheet
|
||||||
+ scrollable::StyleSheet
|
+ scrollable::StyleSheet
|
||||||
+ menu::StyleSheet
|
+ menu::StyleSheet
|
||||||
|
|
@ -212,7 +229,9 @@ where
|
||||||
layout,
|
layout,
|
||||||
cursor,
|
cursor,
|
||||||
shell,
|
shell,
|
||||||
self.on_selected.as_ref(),
|
self.on_select.as_ref(),
|
||||||
|
self.on_open.as_ref(),
|
||||||
|
self.on_close.as_ref(),
|
||||||
self.selected.as_ref(),
|
self.selected.as_ref(),
|
||||||
&self.options,
|
&self.options,
|
||||||
|| tree.state.downcast_mut::<State<Renderer::Paragraph>>(),
|
|| tree.state.downcast_mut::<State<Renderer::Paragraph>>(),
|
||||||
|
|
@ -278,7 +297,7 @@ where
|
||||||
self.text_shaping,
|
self.text_shaping,
|
||||||
self.font.unwrap_or_else(|| renderer.default_font()),
|
self.font.unwrap_or_else(|| renderer.default_font()),
|
||||||
&self.options,
|
&self.options,
|
||||||
&self.on_selected,
|
&self.on_select,
|
||||||
self.style.clone(),
|
self.style.clone(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -290,7 +309,7 @@ impl<'a, T: 'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
T: Clone + ToString + PartialEq + 'static,
|
T: Clone + ToString + PartialEq + 'static,
|
||||||
[T]: ToOwned<Owned = Vec<T>>,
|
[T]: ToOwned<Owned = Vec<T>>,
|
||||||
Message: 'a,
|
Message: Clone + 'a,
|
||||||
Theme: StyleSheet
|
Theme: StyleSheet
|
||||||
+ scrollable::StyleSheet
|
+ scrollable::StyleSheet
|
||||||
+ menu::StyleSheet
|
+ menu::StyleSheet
|
||||||
|
|
@ -473,7 +492,9 @@ pub fn update<'a, T, P, Message>(
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
cursor: mouse::Cursor,
|
cursor: mouse::Cursor,
|
||||||
shell: &mut Shell<'_, Message>,
|
shell: &mut Shell<'_, Message>,
|
||||||
on_selected: &dyn Fn(T) -> Message,
|
on_select: &dyn Fn(T) -> Message,
|
||||||
|
on_open: Option<&Message>,
|
||||||
|
on_close: Option<&Message>,
|
||||||
selected: Option<&T>,
|
selected: Option<&T>,
|
||||||
options: &[T],
|
options: &[T],
|
||||||
state: impl FnOnce() -> &'a mut State<P>,
|
state: impl FnOnce() -> &'a mut State<P>,
|
||||||
|
|
@ -481,6 +502,7 @@ pub fn update<'a, T, P, Message>(
|
||||||
where
|
where
|
||||||
T: PartialEq + Clone + 'a,
|
T: PartialEq + Clone + 'a,
|
||||||
P: text::Paragraph + 'a,
|
P: text::Paragraph + 'a,
|
||||||
|
Message: Clone,
|
||||||
{
|
{
|
||||||
match event {
|
match event {
|
||||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
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.
|
// bounds or on the drop-down, either way we close the overlay.
|
||||||
state.is_open = false;
|
state.is_open = false;
|
||||||
|
|
||||||
|
if let Some(on_close) = on_close {
|
||||||
|
shell.publish(on_close.clone());
|
||||||
|
}
|
||||||
|
|
||||||
event::Status::Captured
|
event::Status::Captured
|
||||||
} else if cursor.is_over(layout.bounds()) {
|
} else if cursor.is_over(layout.bounds()) {
|
||||||
state.is_open = true;
|
state.is_open = true;
|
||||||
state.hovered_option =
|
state.hovered_option =
|
||||||
options.iter().position(|option| Some(option) == selected);
|
options.iter().position(|option| Some(option) == selected);
|
||||||
|
|
||||||
|
if let Some(on_open) = on_open {
|
||||||
|
shell.publish(on_open.clone());
|
||||||
|
}
|
||||||
|
|
||||||
event::Status::Captured
|
event::Status::Captured
|
||||||
} else {
|
} else {
|
||||||
event::Status::Ignored
|
event::Status::Ignored
|
||||||
|
|
@ -538,7 +568,7 @@ where
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(next_option) = next_option {
|
if let Some(next_option) = next_option {
|
||||||
shell.publish((on_selected)(next_option.clone()));
|
shell.publish((on_select)(next_option.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
event::Status::Captured
|
event::Status::Captured
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue