Skip to content
Open
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
15 changes: 14 additions & 1 deletion widget/src/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use crate::core::{Element, Event, Length, Padding, Pixels, Rectangle, Shell, Siz
use crate::overlay::menu;
use crate::text::LineHeight;
use crate::text_input::{self, TextInput};

use crate::scrollable::Viewport;
use std::cell::RefCell;
use std::fmt::Display;

Expand Down Expand Up @@ -141,6 +141,7 @@ where
on_open: Option<Message>,
on_close: Option<Message>,
on_input: Option<Box<dyn Fn(String) -> Message + 'a>>,
on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>,
padding: Padding,
size: Option<f32>,
shaping: text::Shaping,
Expand Down Expand Up @@ -180,6 +181,7 @@ where
on_input: None,
on_open: None,
on_close: None,
on_scroll: None,
padding: text_input::DEFAULT_PADDING,
size: None,
shaping: text::Shaping::default(),
Expand Down Expand Up @@ -217,6 +219,13 @@ where
self
}

/// Sets the message that will be produced when the menu of the
/// [`ComboBox`] is scrolled.
pub fn on_scroll(mut self, on_scroll: impl Fn(Viewport) -> Message + 'a) -> Self {
self.on_scroll = Some(Box::new(on_scroll));
self
}

/// Sets the [`Padding`] of the [`ComboBox`].
pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
self.padding = padding.into();
Expand Down Expand Up @@ -860,6 +869,10 @@ where
.shaping(self.shaping)
.ellipsis(self.ellipsis);

if let Some(on_scroll) = self.on_scroll.as_deref() {
menu = menu.on_scroll(on_scroll);
}

if let Some(font) = self.font {
menu = menu.font(font);
}
Expand Down
17 changes: 16 additions & 1 deletion widget/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::core::{
Vector,
};
use crate::core::{Element, Shell, Widget};
use crate::scrollable::{self, Scrollable};
use crate::scrollable::{self, Scrollable, Viewport};

/// A list of selectable options.
pub struct Menu<'a, 'b, T, Message, Theme = crate::Theme, Renderer = crate::Renderer>
Expand All @@ -29,6 +29,7 @@ where
to_string: &'a dyn Fn(&T) -> String,
on_selected: Box<dyn FnMut(T) -> Message + 'a>,
on_option_hovered: Option<&'a dyn Fn(T) -> Message>,
on_scroll: Option<&'a dyn Fn(Viewport) -> Message>,
width: f32,
padding: Padding,
text_size: Option<Pixels>,
Expand Down Expand Up @@ -65,6 +66,7 @@ where
to_string,
on_selected: Box::new(on_selected),
on_option_hovered,
on_scroll: None,
width: 0.0,
padding: Padding::ZERO,
text_size: None,
Expand Down Expand Up @@ -112,6 +114,12 @@ where
self
}

/// Sets the message that will be produced when the [`Menu`] is scrolled.
pub fn on_scroll(mut self, on_scroll: &'a dyn Fn(Viewport) -> Message) -> Self {
self.on_scroll = Some(on_scroll);
self
}

/// Sets the font of the [`Menu`].
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
self.font = Some(font.into());
Expand Down Expand Up @@ -200,6 +208,7 @@ where
to_string,
on_selected,
on_option_hovered,
on_scroll,
width,
padding,
font,
Expand All @@ -226,6 +235,12 @@ where
})
.height(menu_height);

let list = if let Some(on_scroll) = on_scroll {
list.on_scroll(on_scroll)
} else {
list
};

state.tree.diff(&list as &dyn Widget<_, _, _>);

Self {
Expand Down