Skip to main content

Select

Struct Select 

Source
pub struct Select<'a, T: PartialEq + Clone> { /* private fields */ }
Expand description

A styled drop-down select.

Bind the selection to any PartialEq + Clone type — an enum, an index, or a String — and supply a list of (value, label) pairs. Labels accept &'static str, String, or any Cow<'a, str>, so static option lists don’t allocate.

#[derive(Clone, PartialEq)]
enum Unit { Us, Ms, S }

let mut unit = Unit::Ms;
ui.add(Select::new("unit", &mut unit).options([
    (Unit::Us, "μs"),
    (Unit::Ms, "ms"),
    (Unit::S,  "s"),
]));

For string-valued selects where each option is both the value and the label, use Select::strings:

let mut unit = String::from("ms");
ui.add(Select::strings("unit", &mut unit, ["us", "ms", "s"]));

Implementations§

Source§

impl<'a, T: PartialEq + Clone> Select<'a, T>

Source

pub fn new(id_salt: impl Hash, value: &'a mut T) -> Self

Create a select keyed by id_salt and bound to value. Add selectable options via Select::options.

Source

pub fn label(self, label: impl Into<WidgetText>) -> Self

Show a label above the select.

Source

pub fn options<I, S>(self, options: I) -> Self
where I: IntoIterator<Item = (T, S)>, S: Into<Cow<'a, str>>,

Set the selectable options as (value, label) pairs. Labels are carried as Cow<'a, str>, so &'static str labels never allocate.

Source

pub fn width(self, width: f32) -> Self

Override the select width in points. Defaults to the intrinsic size of the selected label plus padding.

Source§

impl<'a> Select<'a, String>

Source

pub fn strings<I, S>( id_salt: impl Hash, value: &'a mut String, options: I, ) -> Self
where I: IntoIterator<Item = S>, S: Into<Cow<'a, str>>,

Convenience constructor for string-valued selects. Each item is used as both the value and the displayed label.

let mut unit = String::from("ms");
ui.add(Select::strings("unit", &mut unit, ["us", "ms", "s"]));

Trait Implementations§

Source§

impl<'a, T: PartialEq + Clone> Debug for Select<'a, T>

Source§

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

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

impl<'a, T: PartialEq + Clone> Widget for Select<'a, T>

Source§

fn ui(self, ui: &mut Ui) -> Response

Allocate space, interact, paint, and return a Response. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Select<'a, T>

§

impl<'a, T> RefUnwindSafe for Select<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Select<'a, T>
where T: Send,

§

impl<'a, T> Sync for Select<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Select<'a, T>
where T: Unpin,

§

impl<'a, T> UnsafeUnpin for Select<'a, T>

§

impl<'a, T> !UnwindSafe for Select<'a, 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> 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, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(value: T, _simd: S) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

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.