Struct Dropdown

Source
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 to new_rw, but uses a read-only function for the active item, and requires that you manually provide an on_accept callback.

  • 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 an on_accept callback.

  • The dropdown also has methods Dropdown::main_view and Dropdown::list_item_view that 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> Dropdown<T>

Source

pub fn default_main_view(item: T) -> AnyView
where 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.

Source

pub fn custom<MF, I, LF, AIF>( active_item: AIF, main_view: MF, iterator: I, list_item_fn: LF, ) -> Dropdown<T>
where MF: Fn(T) -> AnyView + 'static, I: IntoIterator<Item = T> + Clone + 'static, LF: Fn(T) -> AnyView + Clone + 'static, T: Clone + 'static, AIF: Fn() -> T + 'static,

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 type T and returns an AnyView to 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 type T and returns an AnyView to be used for each item in the dropdown list.

Source

pub fn new<AIF, I>(active_item: AIF, iterator: I) -> Dropdown<T>
where AIF: Fn() -> T + 'static, I: IntoIterator<Item = T> + Clone + 'static, T: Clone + 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.

Source

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> + Clone + 'static, T: Clone + 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 implement SignalGet<T> and SignalUpdate<T>.

  • iterator - An iterator that provides the items to be displayed in the dropdown list.

Source

pub fn main_view(self, main_view: impl Fn(T) -> Box<dyn View> + 'static) -> Self

Overrides the main view for the dropdown.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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> CustomStylable<DropdownCustomStyle> for Dropdown<T>

Source§

type DV = Dropdown<T>

Source§

fn custom_style(self, style: impl Fn(S) -> S + 'static) -> Self::DV

Add a custom style to the view with access to this view’s specialized custom style. Read more
Source§

impl<T> Drop for Dropdown<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: 'static + Clone> View for Dropdown<T>

Source§

fn id(&self) -> ViewId

Source§

fn debug_name(&self) -> Cow<'static, str>

Source§

fn style_pass(&mut self, cx: &mut StyleCx<'_>)

Use this method to style the view’s children. Read more
Source§

fn compute_layout(&mut self, cx: &mut ComputeLayoutCx<'_>) -> Option<Rect>

Responsible for computing the layout of the view’s children. Read more
Source§

fn update(&mut self, cx: &mut UpdateCx<'_>, state: Box<dyn Any>)

Use this method to react to changes in view-related state. You will usually send state to this hook manually using the View’s Id handle Read more
Source§

fn event_before_children( &mut self, _cx: &mut EventCx<'_>, event: &Event, ) -> EventPropagation

Source§

fn view_style(&self) -> Option<Style>

Source§

fn view_class(&self) -> Option<StyleClassRef>

Source§

fn layout(&mut self, cx: &mut LayoutCx<'_>) -> NodeId

Use this method to layout the view’s children. Usually you’ll do this by calling LayoutCx::layout_node. Read more
Source§

fn event_after_children( &mut self, cx: &mut EventCx<'_>, event: &Event, ) -> EventPropagation

Source§

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.
Source§

fn scroll_to( &mut self, cx: &mut AppState, target: ViewId, rect: Option<Rect>, ) -> bool

Scrolls the view and all direct and indirect children to bring the target view to be visible. Returns true if this view contains or is the target.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ButtonExt for T
where T: IntoView + 'static,

Source§

impl<T> ClipExt for T
where T: IntoView + 'static,

Source§

fn clip(self) -> Clip

Wrap the view in a clip view.
Source§

impl<T> ContainerExt for T
where T: IntoView + 'static,

Source§

fn container(self) -> Container

Wrap the view in a container.
Source§

impl<VW, IV> Decorators for IV
where VW: View, IV: IntoView<V = VW>,

Source§

type DV = VW

The type of the decorated view. Read more
Source§

fn style(self, style: impl Fn(Style) -> Style + 'static) -> Self::DV

Alter the style of the view. Read more
Source§

fn debug_name(self, name: impl Into<String>) -> Self::DV

Add a debug name to the view that will be shown in the inspector. Read more
Source§

fn debug_name_if<S: Into<String>>( self, apply: impl Fn() -> bool + 'static, name: impl Fn() -> S + 'static, ) -> Self::DV

Conditionally add a debug name to the view that will be shown in the inspector. Read more
Source§

fn dragging_style(self, style: impl Fn(Style) -> Style + 'static) -> Self::DV

The visual style to apply when the mouse hovers over the element
Source§

fn class<C: StyleClass>(self, _class: C) -> Self::DV

Add a style class to the view
Source§

fn class_if<C: StyleClass>( self, apply: impl Fn() -> bool + 'static, _class: C, ) -> Self::DV

Conditionally add a style class to the view
Source§

fn remove_class<C: StyleClass>(self, _class: C) -> Self::DV

Remove a style class from the view
Source§

fn keyboard_navigable(self) -> Self::DV

Allows the element to be navigated to with the keyboard. Similar to setting tabindex=“0” in html.
Source§

fn disable_default_event( self, disable: impl Fn() -> (EventListener, bool) + 'static, ) -> Self::DV

Dynamically controls whether the default view behavior for an event should be disabled. When disable is true, children will still see the event, but the view event function will not be called nor the event listeners on the view. Read more
Source§

fn draggable(self) -> Self::DV

Mark the view as draggable
Source§

fn disabled(self, disabled_fn: impl Fn() -> bool + 'static) -> Self::DV

Mark the view as disabled Read more
Source§

fn on_event( self, listener: EventListener, action: impl FnMut(&Event) -> EventPropagation + 'static, ) -> Self::DV

Add an event handler for the given EventListener.
Source§

fn on_key_down( self, key: Key, cmp: impl Fn(Modifiers) -> bool + 'static, action: impl Fn(&Event) + 'static, ) -> Self::DV

Add an handler for pressing down a specific key. Read more
Source§

fn on_key_up( self, key: Key, cmp: impl Fn(Modifiers) -> bool + 'static, action: impl Fn(&Event) + 'static, ) -> Self::DV

Add an handler for a specific key being released. Read more
Source§

fn on_event_cont( self, listener: EventListener, action: impl Fn(&Event) + 'static, ) -> Self::DV

Add an event handler for the given 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::DV

Add an event handler for the given 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::DV

Add an event handler for EventListener::Click.
Source§

fn on_click_cont(self, action: impl Fn(&Event) + 'static) -> Self::DV

Add an event handler for 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::DV

Add an event handler for EventListener::Click. This event will be handled with the given handler and the event will stop propagating.
Source§

fn on_double_click( self, action: impl Fn(&Event) -> EventPropagation + 'static, ) -> Self::DV

Add an event handler for EventListener::DoubleClick
Source§

fn on_double_click_cont(self, action: impl Fn(&Event) + 'static) -> Self::DV

Add an event handler for 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::DV

Add an event handler for 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::DV

Add an event handler for EventListener::SecondaryClick. This is most often the “Right” click.
Source§

fn on_secondary_click_cont(self, action: impl Fn(&Event) + 'static) -> Self::DV

Add an event handler for 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::DV

Add an event handler for 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::DV

Set the event handler for resize events for this view. Read more
Source§

fn on_move(self, action: impl Fn(Point) + 'static) -> Self::DV

Set the event handler for move events for this view. Read more
Source§

fn on_cleanup(self, action: impl Fn() + 'static) -> Self::DV

Set the event handler for cleanup events for this view. Read more
Source§

fn animation( self, animation: impl Fn(Animation) -> Animation + 'static, ) -> Self::DV

Add an animation to the view. Read more
Source§

fn clear_focus(self, when: impl Fn() + 'static) -> Self::DV

Clear the focus from the window. Read more
Source§

fn request_focus(self, when: impl Fn() + 'static) -> Self::DV

Request that this view gets the focus for the window. Read more
Source§

fn window_scale(self, scale_fn: impl Fn() -> f64 + 'static) -> Self

Set the window scale factor. Read more
Source§

fn window_title(self, title_fn: impl Fn() -> String + 'static) -> Self

Set the window title. Read more
Source§

fn window_menu(self, menu_fn: impl Fn() -> Menu + 'static) -> Self

Set the system window menu Read more
Source§

fn context_menu(self, menu: impl Fn() -> Menu + 'static) -> Self::DV

Adds a secondary-click context menu to the view, which opens at the mouse position. Read more
Source§

fn popout_menu(self, menu: impl Fn() -> Menu + 'static) -> Self::DV

Adds a primary-click context menu, which opens below the view. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<VW> IntoView for VW
where VW: View + 'static,

Source§

type V = VW

Source§

fn into_view(self) -> <VW as IntoView>::V

Converts the value into a View.
Source§

fn into_any(self) -> AnyView

Converts the value into a AnyView.
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ScrollExt for T
where T: IntoView + 'static,

Source§

fn scroll(self) -> Scroll

Wrap the view in a scroll view.
Source§

impl<T> TooltipExt for T
where T: IntoView + 'static,

Source§

fn tooltip<V>(self, tip: impl Fn() -> V + 'static) -> Tooltip
where V: IntoView + 'static,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,