pub struct Dropdown<T: 'static> { /* private fields */ }Expand description
§A customizable dropdown view for selecting an item from a list.
The Dropdown struct provides several constructors, each offering different levels of
customization and ease of use:
-
Dropdown::new_rw: The simplest constructor, ideal for quick setup with minimal customization. It uses default views and assumes direct access to a signal that can be both read from and written to for driving the selection of an item. -
Dropdown::new: Similar tonew_rw, but uses a read-only function for the active item, and requires that you manually provide anon_acceptcallback. -
Dropdown::custom: Offers full customization, letting you define custom view functions for both the main display and list items. Uses a read-only function for the active item and requires that you manually provide anon_acceptcallback. -
The dropdown also has methods
Dropdown::main_viewandDropdown::list_item_viewthat let you override the main view function and list item view function respectively.
Choose the constructor that best fits your needs based on the level of customization required.
§Usage with Enums
A common scenario is populating a dropdown menu from an enum. The widget-gallery example does this.
The below example creates a dropdown with three items, one for each character in our Character enum.
The strum crate is handy for this use case. This example uses the strum crate to create an iterator for our Character enum.
First, define the enum and implement Clone, strum::EnumIter, and Display on it:
use strum::IntoEnumIterator;
#[derive(Clone, strum::EnumIter)]
enum Character {
Ori,
Naru,
Gumo,
}
impl std::fmt::Display for Character {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Self::Ori => write!(f, "Ori"),
Self::Naru => write!(f, "Naru"),
Self::Gumo => write!(f, "Gumo"),
}
}
}Then, create a signal:
let selected = RwSignal::new(Character::Ori);Finally, create the dropdown using one of the available constructors, like Dropdown::new_rw:
Dropdown::new_rw(selected, Character::iter())§Styling
You can modify the behavior of the dropdown through the CloseOnAccept property.
If the property is set to true, the dropdown will automatically close when an item is selected.
If the property is set to false, the dropdown will not automatically close when an item is selected.
The default is true.
Styling Example:
// root view
empty().style(|s| {
s.class(dropdown::DropdownClass, |s| {
s.set(dropdown::CloseOnAccept, false)
})
});Implementations§
Source§impl<T: Clone + PartialEq> Dropdown<T>
impl<T: Clone + PartialEq> Dropdown<T>
Sourcepub fn default_main_view(item: T) -> AnyViewwhere
T: Display,
pub fn default_main_view(item: T) -> AnyViewwhere
T: Display,
Creates a default main view for the dropdown.
This function generates a view that displays the given item as text, along with a chevron-down icon to indicate that it’s a dropdown.
Sourcepub fn custom<MF, I, LF, AIF>(
active_item: AIF,
main_view: MF,
iterator: I,
list_item_fn: LF,
) -> Dropdown<T>
pub fn custom<MF, I, LF, AIF>( active_item: AIF, main_view: MF, iterator: I, list_item_fn: LF, ) -> Dropdown<T>
Creates a new customizable dropdown.
You might want to use some of the simpler constructors like Dropdown::new or Dropdown::new_rw.
§Example
let active_item = RwSignal::new(3);
Dropdown::custom(
move || active_item.get(),
|main_item| text(main_item).into_any(),
1..=5,
|list_item| text(list_item).into_any(),
)
.on_accept(move |item| active_item.set(item));This function provides full control over the dropdown’s appearance and behavior by allowing custom view functions for both the main display and list items.
§Arguments
-
active_item- A function that returns the currently selected item. -
main_view- A function that takes a value of typeTand returns anAnyViewto be used as the main dropdown display. -
iterator- An iterator that provides the items to be displayed in the dropdown list. -
list_item_fn- A function that takes a value of typeTand returns anAnyViewto be used for each item in the dropdown list.
Sourcepub fn new<AIF, I>(active_item: AIF, iterator: I) -> Dropdown<T>where
AIF: Fn() -> T + 'static,
I: IntoIterator<Item = T> + 'static,
T: Clone + PartialEq + Display + 'static,
pub fn new<AIF, I>(active_item: AIF, iterator: I) -> Dropdown<T>where
AIF: Fn() -> T + 'static,
I: IntoIterator<Item = T> + 'static,
T: Clone + PartialEq + Display + 'static,
Creates a new dropdown with a read-only function for the active item.
§Example
let active_item = RwSignal::new(3);
Dropdown::new(move || active_item.get(), 1..=5).on_accept(move |val| active_item.set(val));This function is a convenience wrapper around Dropdown::new that uses default views
for the main and list items.
See also Dropdown::new_rw.
§Arguments
-
active_item- A function that returns the currently selected item. -
iterator- An iterator that provides the items to be displayed in the dropdown list.
Sourcepub fn new_rw<AI, I>(active_item: AI, iterator: I) -> Dropdown<T>where
AI: SignalGet<T> + SignalUpdate<T> + Copy + 'static,
I: IntoIterator<Item = T> + 'static,
T: Clone + PartialEq + Display + 'static,
pub fn new_rw<AI, I>(active_item: AI, iterator: I) -> Dropdown<T>where
AI: SignalGet<T> + SignalUpdate<T> + Copy + 'static,
I: IntoIterator<Item = T> + 'static,
T: Clone + PartialEq + Display + 'static,
Creates a new dropdown with a read-write signal for the active item.
§Example:
let dropdown_active_item = RwSignal::new(3);
Dropdown::new_rw(dropdown_active_item, 1..=5);This function is a convenience wrapper around Dropdown::custom that uses default views
for the main and list items.
§Arguments
-
active_item- A read-write signal representing the currently selected item. It must implementSignalGet<T>andSignalUpdate<T>. -
iterator- An iterator that provides the items to be displayed in the dropdown list.
Sourcepub fn main_view(self, main_view: impl Fn(T) -> Box<dyn View> + 'static) -> Self
pub fn main_view(self, main_view: impl Fn(T) -> Box<dyn View> + 'static) -> Self
Overrides the main view for the dropdown.
Sourcepub fn list_item_view(
self,
list_item_fn: impl Fn(&T) -> Box<dyn View> + 'static,
) -> Self
pub fn list_item_view( self, list_item_fn: impl Fn(&T) -> Box<dyn View> + 'static, ) -> Self
Overrides the list view for each item in the dropdown list.
Sourcepub fn show_list(self, show: impl Fn() -> bool + 'static) -> Self
pub fn show_list(self, show: impl Fn() -> bool + 'static) -> Self
Sets a reactive condition for showing or hiding the dropdown list.
§Reactivity
The show function will be re-run whenever any signal it depends on changes.
Sourcepub fn on_accept(self, on_accept: impl Fn(T) + 'static) -> Self
pub fn on_accept(self, on_accept: impl Fn(T) + 'static) -> Self
Sets a callback function to be called when an item is selected from the dropdown.
Only one on_accept callback can be set at a time.
Sourcepub fn on_open(self, on_open: impl Fn(bool) + 'static) -> Self
pub fn on_open(self, on_open: impl Fn(bool) + 'static) -> Self
Sets a callback function to be called when the dropdown is opened.
Only one on_open callback can be set at a time.
Sourcepub fn dropdown_style(
self,
style: impl Fn(DropdownCustomStyle) -> DropdownCustomStyle + 'static,
) -> Self
pub fn dropdown_style( self, style: impl Fn(DropdownCustomStyle) -> DropdownCustomStyle + 'static, ) -> Self
Sets the custom style properties of the Dropdown.
Trait Implementations§
Source§impl<T: Clone + PartialEq> CustomStylable<DropdownCustomStyle> for Dropdown<T>
impl<T: Clone + PartialEq> CustomStylable<DropdownCustomStyle> for Dropdown<T>
Source§impl<T: 'static + Clone + PartialEq> View for Dropdown<T>
impl<T: 'static + Clone + PartialEq> View for Dropdown<T>
fn id(&self) -> ViewId
fn debug_name(&self) -> Cow<'static, str>
Source§fn style_pass(&mut self, cx: &mut StyleCx<'_>)
fn style_pass(&mut self, cx: &mut StyleCx<'_>)
Source§fn compute_layout(&mut self, cx: &mut ComputeLayoutCx<'_>) -> Option<Rect>
fn compute_layout(&mut self, cx: &mut ComputeLayoutCx<'_>) -> Option<Rect>
Source§fn update(&mut self, cx: &mut UpdateCx<'_>, state: Box<dyn Any>)
fn update(&mut self, cx: &mut UpdateCx<'_>, state: Box<dyn Any>)
View’s Id handle Read morefn event_before_children( &mut self, _cx: &mut EventCx<'_>, event: &Event, ) -> EventPropagation
fn view_style(&self) -> Option<Style>
fn view_class(&self) -> Option<StyleClassRef>
Source§fn layout(&mut self, cx: &mut LayoutCx<'_>) -> NodeId
fn layout(&mut self, cx: &mut LayoutCx<'_>) -> NodeId
LayoutCx::layout_node. Read morefn event_after_children( &mut self, cx: &mut EventCx<'_>, event: &Event, ) -> EventPropagation
Source§fn paint(&mut self, cx: &mut PaintCx<'_>)
fn paint(&mut self, cx: &mut PaintCx<'_>)
View-specific implementation. Will be called in PaintCx::paint_view.
Usually you’ll call paint_view for every child view. But you might also draw text, adjust the offset, clip
or draw text.Auto Trait Implementations§
impl<T> Freeze for Dropdown<T>where
T: Freeze,
impl<T> !RefUnwindSafe for Dropdown<T>
impl<T> !Send for Dropdown<T>
impl<T> !Sync for Dropdown<T>
impl<T> Unpin for Dropdown<T>where
T: Unpin,
impl<T> !UnwindSafe for Dropdown<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ContainerExt for Twhere
T: IntoView + 'static,
impl<T> ContainerExt for Twhere
T: IntoView + 'static,
Source§impl<T> Decorators for Twhere
T: IntoView,
impl<T> Decorators for Twhere
T: IntoView,
Source§fn style(self, style: impl Fn(Style) -> Style + 'static) -> Self::Intermediate
fn style(self, style: impl Fn(Style) -> Style + 'static) -> Self::Intermediate
Source§fn debug_name(self, name: impl Into<String>) -> Self::Intermediate
fn debug_name(self, name: impl Into<String>) -> Self::Intermediate
Source§fn debug_name_if<S: Into<String>>(
self,
apply: impl Fn() -> bool + 'static,
name: impl Fn() -> S + 'static,
) -> Self::Intermediate
fn debug_name_if<S: Into<String>>( self, apply: impl Fn() -> bool + 'static, name: impl Fn() -> S + 'static, ) -> Self::Intermediate
Source§fn dragging_style(
self,
style: impl Fn(Style) -> Style + 'static,
) -> Self::Intermediate
fn dragging_style( self, style: impl Fn(Style) -> Style + 'static, ) -> Self::Intermediate
Source§fn class<C: StyleClass>(self, _class: C) -> Self::Intermediate
fn class<C: StyleClass>(self, _class: C) -> Self::Intermediate
Source§fn class_if<C: StyleClass>(
self,
apply: impl Fn() -> bool + 'static,
_class: C,
) -> Self::Intermediate
fn class_if<C: StyleClass>( self, apply: impl Fn() -> bool + 'static, _class: C, ) -> Self::Intermediate
Source§fn remove_class<C: StyleClass>(self, _class: C) -> Self::Intermediate
fn remove_class<C: StyleClass>(self, _class: C) -> Self::Intermediate
Style::focusable insteadSource§fn disable_default_event(
self,
disable: impl Fn() -> (EventListener, bool) + 'static,
) -> Self::Intermediate
fn disable_default_event( self, disable: impl Fn() -> (EventListener, bool) + 'static, ) -> Self::Intermediate
Source§fn draggable(self) -> Self::Intermediate
fn draggable(self) -> Self::Intermediate
Style::draggable directly insteadSource§fn disabled(
self,
disabled_fn: impl Fn() -> bool + 'static,
) -> Self::Intermediate
fn disabled( self, disabled_fn: impl Fn() -> bool + 'static, ) -> Self::Intermediate
Style::set_disabled directly insteadSource§fn on_event(
self,
listener: EventListener,
action: impl FnMut(&Event) -> EventPropagation + 'static,
) -> Self::Intermediate
fn on_event( self, listener: EventListener, action: impl FnMut(&Event) -> EventPropagation + 'static, ) -> Self::Intermediate
EventListener.Source§fn on_key_down(
self,
key: Key,
cmp: impl Fn(Modifiers) -> bool + 'static,
action: impl Fn(&Event) + 'static,
) -> Self::Intermediate
fn on_key_down( self, key: Key, cmp: impl Fn(Modifiers) -> bool + 'static, action: impl Fn(&Event) + 'static, ) -> Self::Intermediate
Source§fn on_key_up(
self,
key: Key,
cmp: impl Fn(Modifiers) -> bool + 'static,
action: impl Fn(&Event) + 'static,
) -> Self::Intermediate
fn on_key_up( self, key: Key, cmp: impl Fn(Modifiers) -> bool + 'static, action: impl Fn(&Event) + 'static, ) -> Self::Intermediate
Source§fn on_event_cont(
self,
listener: EventListener,
action: impl Fn(&Event) + 'static,
) -> Self::Intermediate
fn on_event_cont( self, listener: EventListener, action: impl Fn(&Event) + 'static, ) -> Self::Intermediate
EventListener. This event will be handled with
the given handler and the event will continue propagating.Source§fn on_event_stop(
self,
listener: EventListener,
action: impl Fn(&Event) + 'static,
) -> Self::Intermediate
fn on_event_stop( self, listener: EventListener, action: impl Fn(&Event) + 'static, ) -> Self::Intermediate
EventListener. This event will be handled with
the given handler and the event will stop propagating.Source§fn on_click(
self,
action: impl FnMut(&Event) -> EventPropagation + 'static,
) -> Self::Intermediate
fn on_click( self, action: impl FnMut(&Event) -> EventPropagation + 'static, ) -> Self::Intermediate
EventListener::Click.Source§fn on_click_cont(self, action: impl Fn(&Event) + 'static) -> Self::Intermediate
fn on_click_cont(self, action: impl Fn(&Event) + 'static) -> Self::Intermediate
EventListener::Click. This event will be handled with
the given handler and the event will continue propagating.Source§fn on_click_stop(
self,
action: impl FnMut(&Event) + 'static,
) -> Self::Intermediate
fn on_click_stop( self, action: impl FnMut(&Event) + 'static, ) -> Self::Intermediate
EventListener::Click. This event will be handled with
the given handler and the event will stop propagating.Source§fn action(self, action: impl FnMut() + 'static) -> Self::Intermediate
fn action(self, action: impl FnMut() + 'static) -> Self::Intermediate
Source§fn on_double_click(
self,
action: impl Fn(&Event) -> EventPropagation + 'static,
) -> Self::Intermediate
fn on_double_click( self, action: impl Fn(&Event) -> EventPropagation + 'static, ) -> Self::Intermediate
EventListener::DoubleClickSource§fn on_double_click_cont(
self,
action: impl Fn(&Event) + 'static,
) -> Self::Intermediate
fn on_double_click_cont( self, action: impl Fn(&Event) + 'static, ) -> Self::Intermediate
EventListener::DoubleClick. This event will be handled with
the given handler and the event will continue propagating.Source§fn on_double_click_stop(
self,
action: impl Fn(&Event) + 'static,
) -> Self::Intermediate
fn on_double_click_stop( self, action: impl Fn(&Event) + 'static, ) -> Self::Intermediate
EventListener::DoubleClick. This event will be handled with
the given handler and the event will stop propagating.Source§fn on_secondary_click(
self,
action: impl Fn(&Event) -> EventPropagation + 'static,
) -> Self::Intermediate
fn on_secondary_click( self, action: impl Fn(&Event) -> EventPropagation + 'static, ) -> Self::Intermediate
EventListener::SecondaryClick. This is most often the “Right” click.Source§fn on_secondary_click_cont(
self,
action: impl Fn(&Event) + 'static,
) -> Self::Intermediate
fn on_secondary_click_cont( self, action: impl Fn(&Event) + 'static, ) -> Self::Intermediate
EventListener::SecondaryClick. This is most often the “Right” click.
This event will be handled with the given handler and the event will continue propagating.Source§fn on_secondary_click_stop(
self,
action: impl Fn(&Event) + 'static,
) -> Self::Intermediate
fn on_secondary_click_stop( self, action: impl Fn(&Event) + 'static, ) -> Self::Intermediate
EventListener::SecondaryClick. This is most often the “Right” click.
This event will be handled with the given handler and the event will stop propagating.Source§fn on_resize(self, action: impl Fn(Rect) + 'static) -> Self::Intermediate
fn on_resize(self, action: impl Fn(Rect) + 'static) -> Self::Intermediate
Source§fn on_move(self, action: impl Fn(Point) + 'static) -> Self::Intermediate
fn on_move(self, action: impl Fn(Point) + 'static) -> Self::Intermediate
Source§fn on_cleanup(self, action: impl Fn() + 'static) -> Self::Intermediate
fn on_cleanup(self, action: impl Fn() + 'static) -> Self::Intermediate
Source§fn animation(
self,
animation: impl Fn(Animation) -> Animation + 'static,
) -> Self::Intermediate
fn animation( self, animation: impl Fn(Animation) -> Animation + 'static, ) -> Self::Intermediate
Source§fn clear_focus(self, when: impl Fn() + 'static) -> Self::Intermediate
fn clear_focus(self, when: impl Fn() + 'static) -> Self::Intermediate
Source§fn request_focus(self, when: impl Fn() + 'static) -> Self::Intermediate
fn request_focus(self, when: impl Fn() + 'static) -> Self::Intermediate
Source§fn window_scale(
self,
scale_fn: impl Fn() -> f64 + 'static,
) -> Self::Intermediate
fn window_scale( self, scale_fn: impl Fn() -> f64 + 'static, ) -> Self::Intermediate
Source§fn window_title(
self,
title_fn: impl Fn() -> String + 'static,
) -> Self::Intermediate
fn window_title( self, title_fn: impl Fn() -> String + 'static, ) -> Self::Intermediate
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more