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>
impl<M> Select<M>
Sourcepub fn new(
options: impl IntoIterator<Item = impl Into<String>>,
message: M,
) -> Self
pub fn new( options: impl IntoIterator<Item = impl Into<String>>, message: M, ) -> Self
A select with nothing chosen, emitting message when it wants opening.
Sourcepub fn inert(options: impl IntoIterator<Item = impl Into<String>>) -> Self
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.
Sourcepub fn with_placeholder(self, placeholder: impl Into<String>) -> Self
pub fn with_placeholder(self, placeholder: impl Into<String>) -> Self
Sets the text shown when nothing is chosen.
Sourcepub fn with_selected(self, index: Option<usize>) -> Self
pub fn with_selected(self, index: Option<usize>) -> Self
Sets the initially chosen option. Out of range chooses nothing.
Sourcepub fn with_style(self, style: TextStyle) -> Self
pub fn with_style(self, style: TextStyle) -> Self
Sets the font and size.
Sourcepub fn selected_option(&self) -> Option<&str>
pub fn selected_option(&self) -> Option<&str>
The chosen option’s text, or None when nothing is chosen.
Sourcepub fn set_selected(&mut self, index: Option<usize>)
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.
Sourcepub fn set_options(
&mut self,
options: impl IntoIterator<Item = impl Into<String>>,
)
pub fn set_options( &mut self, options: impl IntoIterator<Item = impl Into<String>>, )
Replaces the options, dropping a selection that no longer exists.
Trait Implementations§
Source§impl<M> Describe for Select<M>
impl<M> Describe for Select<M>
Source§const DOC: &'static str = "One choice out of many, picked from a dropdown list."
const DOC: &'static str = "One choice out of many, picked from a dropdown list."
Source§const ICON: &'static Icon
const ICON: &'static Icon
Source§const PROPERTIES: &'static [Property]
const PROPERTIES: &'static [Property]
Source§impl<M: Clone + 'static> Widget<M> for Select<M>
impl<M: Clone + 'static> Widget<M> for Select<M>
Source§fn focusable(&self) -> bool
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>
fn describe(&self) -> Option<&dyn DynDescribe>
Source§fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>
fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>
describe.Source§fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured
fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured
Source§fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)
fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)
canvas, which is already clipped to this widget’s bounds
intersected with the damage region being repainted. Read moreSource§fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled
fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled
Source§fn accepts_pointer(&self) -> bool
fn accepts_pointer(&self) -> bool
true if the pointer can hit this widget. Read moreSource§fn preserves_focus(&self) -> bool
fn preserves_focus(&self) -> bool
true if a press on this widget should leave focus exactly where
it is. Read moreSource§fn animate(&mut self, now_ms: u64) -> Animation
fn animate(&mut self, now_ms: u64) -> Animation
EventCtx::request_animation — and stops being called
the moment it answers Wake::Never. Read moreAuto Trait Implementations§
impl<M> Freeze for Select<M>
impl<M> RefUnwindSafe for Select<M>where
Option<M>: RefUnwindSafe,
impl<M> Send for Select<M>
impl<M> Sync for Select<M>
impl<M> Unpin for Select<M>
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> 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DynDescribe for Twhere
T: Describe,
impl<T> DynDescribe for Twhere
T: Describe,
Source§fn kind(&self) -> &'static str
fn kind(&self) -> &'static str
Describe::KIND.Source§fn properties(&self) -> &'static [Property]
fn properties(&self) -> &'static [Property]
Describe::PROPERTIES.Source§fn get_property(&self, name: &str) -> Option<Value>
fn get_property(&self, name: &str) -> Option<Value>
Describe::get.Source§fn set_property(
&mut self,
name: &str,
value: Value,
) -> Result<(), PropertyError>
fn set_property( &mut self, name: &str, value: Value, ) -> Result<(), PropertyError>
Describe::set.