Skip to main content

Select

Struct Select 

Source
pub struct Select<M> { /* private fields */ }
Expand description

A control showing one chosen option, which asks to be opened.

enum Message { Open, Chose(usize) }
Select::new(["Auto", "Manuell", "Av"], Message::Open).with_placeholder("Velg modus");

§It does not open its own list, and nothing here can

To open a list a widget would have to create nodes from inside on_event. EventCtx can emit a message, ask for focus, ask for frames and ask to be revealed — it cannot add widgets, and giving it that power would let any widget restructure the tree from an event handler.

That is the same line already drawn three times: Tabs owns the selected index and not the pages, List owns the selection and not the viewport, and Ui::push_popup places a container the caller fills. A select that owned its list would be the first widget to own nodes, and the exception would be permanent.

So this widget emits an open message and the application opens the list — which open_select does in one call:

match message {
    Message::Open => { widgets::open_select(&mut ui, select, Message::Chose); }
    Message::Chose(index) => {
        ui.close_popup();
        ui.widget_mut::<Select<Message>>(select).unwrap().set_selected(Some(index));
    }
}

Everything the open list needs is already there: the popup flips near a screen edge, closes on Escape or a press outside — swallowing that press — and returns focus here when it goes.

§Keyboard

Enter, Space and ArrowDown open it. Left and Right deliberately do not cycle the value: a select whose value changes as somebody tabs past it is the classic accidental-edit bug, and a closed control that quietly edits itself is worse than one that needs a second keystroke.

Implementations§

Source§

impl<M> Select<M>

Source

pub fn new( options: impl IntoIterator<Item = impl Into<String>>, message: M, ) -> Self

A select with nothing chosen, emitting message when it wants opening.

Source

pub fn inert(options: impl IntoIterator<Item = impl Into<String>>) -> Self

A dropdown that emits nothing, for a chosen value the application reads rather than one it is told about.

The message a Select carries is its request to be opened — the popup is a scene the application pushes, since a widget cannot own other nodes — so one without a message is a closed control showing selected and nothing else. That is the honest meaning of an inert select, and it is what a form file wants for a value it displays.

A form that wants the list to open names on-change, and the engine wires it; see docs/forms.md.

Source

pub fn with_placeholder(self, placeholder: impl Into<String>) -> Self

Sets the text shown when nothing is chosen.

Source

pub fn with_selected(self, index: Option<usize>) -> Self

Sets the initially chosen option. Out of range chooses nothing.

Source

pub fn with_role(self, role: Role) -> Self

Sets the control’s surface role.

Source

pub fn with_style(self, style: TextStyle) -> Self

Sets the font and size.

Source

pub const fn selected(&self) -> Option<usize>

The chosen index, if any.

Source

pub fn selected_option(&self) -> Option<&str>

The chosen option’s text, or None when nothing is chosen.

Source

pub fn set_selected(&mut self, index: Option<usize>)

Chooses an option without emitting anything. Out of range chooses nothing, as List::set_selected does and for the same reason: nothing chosen is a state this control can show.

Source

pub fn options(&self) -> &[String]

The options, in order.

Source

pub fn set_options( &mut self, options: impl IntoIterator<Item = impl Into<String>>, )

Replaces the options, dropping a selection that no longer exists.

Source

pub fn set_style(&mut self, style: TextStyle)

Replaces the font and size.

Source

pub const fn style(&self) -> TextStyle

The font and size the text draws in.

Trait Implementations§

Source§

impl<M: Clone> Clone for Select<M>

Source§

fn clone(&self) -> Select<M>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Debug> Debug for Select<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M> Describe for Select<M>

Source§

const KIND: &'static str = "select"

The name a form file uses for this widget. Kebab-case.
Source§

const DOC: &'static str = "One choice out of many, picked from a dropdown list."

One line saying what this widget is, for somebody choosing one. Read more
Source§

const GROUP: Group = Group::Input

Which shelf of the catalogue this belongs on.
Source§

const ICON: &'static Icon

The widget’s glyph: a small portrait of the thing, for a palette to draw beside — or instead of — its name. Read more
Source§

const PROPERTIES: &'static [Property]

Every property, in the order an inspector should show them.
Source§

fn get(&self, name: &str) -> Option<Value>

The current value. Read more
Source§

fn apply(&mut self, name: &str, value: Value) -> Result<(), Mismatch>

Applies a value, reporting only what went wrong. Read more
Source§

fn set(&mut self, name: &str, value: Value) -> Result<(), PropertyError>

Applies a value, reporting what went wrong and where.
Source§

impl<M: Clone + 'static> Widget<M> for Select<M>

Source§

fn focusable(&self) -> bool

A select with no options is not a tab stop: there is nothing to open.

Source§

fn describe(&self) -> Option<&dyn DynDescribe>

This widget’s property description, if it has one. Read more
Source§

fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>

The mutable half of describe.
Source§

fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured

How big this widget would like to be, given what the caller can promise. Read more
Source§

fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)

Draws into canvas, which is already clipped to this widget’s bounds intersected with the damage region being repainted. Read more
Source§

fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled

Reacts to an event routed to this widget.
Source§

fn accepts_pointer(&self) -> bool

Returns true if the pointer can hit this widget. Read more
Source§

fn preserves_focus(&self) -> bool

Returns true if a press on this widget should leave focus exactly where it is. Read more
Source§

fn animate(&mut self, now_ms: u64) -> Animation

Advances time-based state. Called only while this widget has asked to animate — see EventCtx::request_animation — and stops being called the moment it answers Wake::Never. Read more
Source§

fn snap(&mut self, now_ms: u64) -> Animation

Lands whatever is in flight at its end state, without animating it. Read more

Auto Trait Implementations§

§

impl<M> Freeze for Select<M>
where Option<M>: Freeze,

§

impl<M> RefUnwindSafe for Select<M>

§

impl<M> Send for Select<M>
where Option<M>: Send,

§

impl<M> Sync for Select<M>
where Option<M>: Sync,

§

impl<M> Unpin for Select<M>
where Option<M>: Unpin,

§

impl<M> UnsafeUnpin for Select<M>
where Option<M>: UnsafeUnpin,

§

impl<M> UnwindSafe for Select<M>
where Option<M>: UnwindSafe,

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> AsAny for T
where T: Any,

Source§

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

Borrows as dyn Any.
Source§

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

Mutably borrows as dyn Any.
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynDescribe for T
where T: Describe,

Source§

fn kind(&self) -> &'static str

Source§

fn properties(&self) -> &'static [Property]

Source§

fn get_property(&self, name: &str) -> Option<Value>

Source§

fn set_property( &mut self, name: &str, value: Value, ) -> Result<(), PropertyError>

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.