Make overlay::Menu publish messages on selection
This commit is contained in:
parent
b0205e03d8
commit
87db76a11f
2 changed files with 42 additions and 42 deletions
|
|
@ -19,7 +19,7 @@ pub use iced_style::menu::{Appearance, StyleSheet};
|
|||
|
||||
/// A list of selectable options.
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct Menu<'a, T, Renderer = crate::Renderer>
|
||||
pub struct Menu<'a, T, Message, Renderer = crate::Renderer>
|
||||
where
|
||||
Renderer: text::Renderer,
|
||||
Renderer::Theme: StyleSheet,
|
||||
|
|
@ -27,7 +27,7 @@ where
|
|||
state: &'a mut State,
|
||||
options: &'a [T],
|
||||
hovered_option: &'a mut Option<usize>,
|
||||
last_selection: &'a mut Option<T>,
|
||||
on_selected: Box<dyn FnMut(T) -> Message + 'a>,
|
||||
width: f32,
|
||||
padding: Padding,
|
||||
text_size: Option<f32>,
|
||||
|
|
@ -37,9 +37,10 @@ where
|
|||
style: <Renderer::Theme as StyleSheet>::Style,
|
||||
}
|
||||
|
||||
impl<'a, T, Renderer> Menu<'a, T, Renderer>
|
||||
impl<'a, T, Message, Renderer> Menu<'a, T, Message, Renderer>
|
||||
where
|
||||
T: ToString + Clone,
|
||||
Message: 'a,
|
||||
Renderer: text::Renderer + 'a,
|
||||
Renderer::Theme:
|
||||
StyleSheet + container::StyleSheet + scrollable::StyleSheet,
|
||||
|
|
@ -50,13 +51,13 @@ where
|
|||
state: &'a mut State,
|
||||
options: &'a [T],
|
||||
hovered_option: &'a mut Option<usize>,
|
||||
last_selection: &'a mut Option<T>,
|
||||
on_selected: impl FnMut(T) -> Message + 'a,
|
||||
) -> Self {
|
||||
Menu {
|
||||
state,
|
||||
options,
|
||||
hovered_option,
|
||||
last_selection,
|
||||
on_selected: Box::new(on_selected),
|
||||
width: 0.0,
|
||||
padding: Padding::ZERO,
|
||||
text_size: None,
|
||||
|
|
@ -121,7 +122,7 @@ where
|
|||
/// The `target_height` will be used to display the menu either on top
|
||||
/// of the target or under it, depending on the screen position and the
|
||||
/// dimensions of the [`Menu`].
|
||||
pub fn overlay<Message: 'a>(
|
||||
pub fn overlay(
|
||||
self,
|
||||
position: Point,
|
||||
target_height: f32,
|
||||
|
|
@ -174,7 +175,10 @@ where
|
|||
Renderer::Theme:
|
||||
StyleSheet + container::StyleSheet + scrollable::StyleSheet,
|
||||
{
|
||||
pub fn new<T>(menu: Menu<'a, T, Renderer>, target_height: f32) -> Self
|
||||
pub fn new<T>(
|
||||
menu: Menu<'a, T, Message, Renderer>,
|
||||
target_height: f32,
|
||||
) -> Self
|
||||
where
|
||||
T: Clone + ToString,
|
||||
{
|
||||
|
|
@ -182,7 +186,7 @@ where
|
|||
state,
|
||||
options,
|
||||
hovered_option,
|
||||
last_selection,
|
||||
on_selected,
|
||||
width,
|
||||
padding,
|
||||
font,
|
||||
|
|
@ -195,7 +199,7 @@ where
|
|||
let container = Container::new(Scrollable::new(List {
|
||||
options,
|
||||
hovered_option,
|
||||
last_selection,
|
||||
on_selected,
|
||||
font,
|
||||
text_size,
|
||||
text_line_height,
|
||||
|
|
@ -306,14 +310,14 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
struct List<'a, T, Renderer>
|
||||
struct List<'a, T, Message, Renderer>
|
||||
where
|
||||
Renderer: text::Renderer,
|
||||
Renderer::Theme: StyleSheet,
|
||||
{
|
||||
options: &'a [T],
|
||||
hovered_option: &'a mut Option<usize>,
|
||||
last_selection: &'a mut Option<T>,
|
||||
on_selected: Box<dyn FnMut(T) -> Message + 'a>,
|
||||
padding: Padding,
|
||||
text_size: Option<f32>,
|
||||
text_line_height: text::LineHeight,
|
||||
|
|
@ -323,7 +327,7 @@ where
|
|||
}
|
||||
|
||||
impl<'a, T, Message, Renderer> Widget<Message, Renderer>
|
||||
for List<'a, T, Renderer>
|
||||
for List<'a, T, Message, Renderer>
|
||||
where
|
||||
T: Clone + ToString,
|
||||
Renderer: text::Renderer,
|
||||
|
|
@ -372,14 +376,15 @@ where
|
|||
cursor: mouse::Cursor,
|
||||
renderer: &Renderer,
|
||||
_clipboard: &mut dyn Clipboard,
|
||||
_shell: &mut Shell<'_, Message>,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
) -> event::Status {
|
||||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
|
||||
if cursor.is_over(layout.bounds()) {
|
||||
if let Some(index) = *self.hovered_option {
|
||||
if let Some(option) = self.options.get(index) {
|
||||
*self.last_selection = Some(option.clone());
|
||||
shell.publish((self.on_selected)(option.clone()));
|
||||
return event::Status::Captured;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -417,7 +422,8 @@ where
|
|||
|
||||
if let Some(index) = *self.hovered_option {
|
||||
if let Some(option) = self.options.get(index) {
|
||||
*self.last_selection = Some(option.clone());
|
||||
shell.publish((self.on_selected)(option.clone()));
|
||||
return event::Status::Captured;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -521,7 +527,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Message, Renderer> From<List<'a, T, Renderer>>
|
||||
impl<'a, T, Message, Renderer> From<List<'a, T, Message, Renderer>>
|
||||
for Element<'a, Message, Renderer>
|
||||
where
|
||||
T: ToString + Clone,
|
||||
|
|
@ -529,7 +535,7 @@ where
|
|||
Renderer: 'a + text::Renderer,
|
||||
Renderer::Theme: StyleSheet,
|
||||
{
|
||||
fn from(list: List<'a, T, Renderer>) -> Self {
|
||||
fn from(list: List<'a, T, Message, Renderer>) -> Self {
|
||||
Element::new(list)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue