Skip to main content

SelectState

Struct SelectState 

Source
pub struct SelectState<V: SelectValue> { /* private fields */ }
Expand description

The rows, the selection, and the open menu of a Select.

Owned by the view that shows the select, which observes it so a change renders. Edit the rows and the selection through its methods; the menu’s own state (the query, the highlighted row, the scroll position) is the state’s to keep.

The selection is kept as items, not values alone, so the trigger can show a chosen item’s label after a search has replaced the rows.

Implementations§

Source§

impl<V: SelectValue> SelectState<V>

Source

pub fn new( entries: impl IntoIterator<Item = impl Into<SelectEntry<V>>>, cx: &mut Context<'_, Self>, ) -> Self

A closed, single select over entries with nothing selected.

Source

pub fn multiple( entries: impl IntoIterator<Item = impl Into<SelectEntry<V>>>, cx: &mut Context<'_, Self>, ) -> Self

A closed select over entries in which more than one item can be selected: the menu stays open while items are toggled, and the trigger counts the selection.

Adds a search field at the top of the menu, with placeholder in it while it is empty. Typing narrows the rows to those whose label or keywords the query matches as a fuzzy subsequence, ignoring case.

Source

pub fn with_search_handler( self, placeholder: impl Into<SharedString>, handler: impl Fn(SharedString, &mut Context<'_, Self>) -> Task<Vec<SelectEntry<V>>> + 'static, window: &mut Window, cx: &mut Context<'_, Self>, ) -> Self

Adds a search field whose query the application answers: for each change, and once when the menu opens, handler gets the query and returns a task with the rows to show. The task can do its work on a background thread or over the network; a newer query cancels the task before it. The rows given to the state are the ones shown until the first answer, and a chosen item stays selected when a later answer no longer lists it.

The state keeps no answers: what makes one stale is the application’s to know. A handler that remembers its answers can return Task::ready for a query it has seen, and the menu shows those rows on the next frame.

use gpui_cn::{SelectEntry, SelectItem, SelectState};
use gpui_kit::{AppContext as _, Context, Task, Window};

fn cities(window: &mut Window, cx: &mut Context<SelectState<usize>>) -> SelectState<usize> {
    SelectState::new([SelectItem::new(1, "Zurich")], cx).with_search_handler(
        "Search cities",
        |query, cx| {
            cx.background_spawn(async move {
                // Any search: fuzzy, indexed, or a request.
                vec![SelectEntry::from(SelectItem::new(2, format!("{query} City")))]
            })
        },
        window,
        cx,
    )
}
Source

pub fn with_selected(self, values: impl IntoIterator<Item = V>) -> Self

Seeds the selection. Values that are not items are dropped, and a single select keeps the first.

Source

pub fn with_row_height(self, height: Pixels) -> Self

Gives every item row this height. Without it a row is as tall as its content, a plain row’s height at least, and the list measures rows as they come into view. With it the list knows every row’s height before it is laid out, so a scroll to a far row of a long menu lands exactly; rows drawn by Select::render_item clip to it.

Source

pub fn entries(&self) -> &[SelectEntry<V>]

The rows, in menu order.

Source

pub fn set_entries( &mut self, entries: impl IntoIterator<Item = impl Into<SelectEntry<V>>>, cx: &mut Context<'_, Self>, )

Replaces the rows. With a local search, a selected value that is no longer an item is dropped from the selection; with a handler the rows come and go with the query, so the selection stays.

Source

pub fn selected(&self) -> Vec<V>

The selected values, in the order chosen.

Source

pub fn selected_value(&self) -> Option<&V>

The selected value of a single select.

Source

pub fn selected_items(&self) -> &[SelectItem<V>]

The selected items, in the order chosen.

Source

pub fn is_selected(&self, value: &V) -> bool

Whether value is selected.

Source

pub fn set_selected( &mut self, values: impl IntoIterator<Item = V>, cx: &mut Context<'_, Self>, )

Replaces the selection and reports the change. Values that are not items are dropped, and a single select keeps the first.

Source

pub fn is_multiple(&self) -> bool

Whether more than one item can be selected.

Source

pub fn is_searchable(&self) -> bool

Whether the menu has a search field.

Source

pub fn is_searching(&self) -> bool

Whether a search handler is still answering the query.

Source

pub fn is_open(&self) -> bool

Whether the menu is open.

Source

pub fn query(&self, cx: &App) -> SharedString

The search query, or an empty string without a search field.

Source

pub fn highlighted(&self) -> Option<&SelectItem<V>>

The item the keyboard is on while the menu is open.

Source

pub fn open(&mut self, window: &mut Window, cx: &mut Context<'_, Self>)

Opens the menu, highlights the selected item (or the first), decides which side of the trigger the menu takes, and moves focus into the menu: to the search field, else to the list. A search handler is asked for the rows of the empty query.

Source

pub fn close(&mut self, window: &mut Window, cx: &mut Context<'_, Self>)

Closes the menu, clears the query, cancels a running search, and returns focus to the trigger if the menu had it.

Source

pub fn choose( &mut self, value: &V, window: &mut Window, cx: &mut Context<'_, Self>, )

Chooses value while the menu is open: a single select selects it and closes, a multiple select toggles it and stays open. A disabled item, or a closed menu, ignores it.

Source

pub fn choose_highlighted( &mut self, window: &mut Window, cx: &mut Context<'_, Self>, )

Chooses the highlighted item. While a handler is still answering there is no row to choose: the highlight is on rows the answer will replace.

Source

pub fn move_highlight(&mut self, delta: isize, cx: &mut Context<'_, Self>)

Moves the highlight one selectable row up or down, wrapping at the ends, and scrolls it into view.

Source

pub fn highlight_first(&mut self, cx: &mut Context<'_, Self>)

Highlights the first selectable row.

Source

pub fn highlight_last(&mut self, cx: &mut Context<'_, Self>)

Highlights the last selectable row.

Trait Implementations§

Source§

impl<V: SelectValue> EventEmitter<SelectEvent<V>> for SelectState<V>

Source§

impl<V: SelectValue> Focusable for SelectState<V>

The trigger’s focus handle: focusing the state focuses the trigger.

Source§

fn focus_handle(&self, _: &App) -> FocusHandle

Returns the focus handle associated with this view.

Auto Trait Implementations§

§

impl<V> !RefUnwindSafe for SelectState<V>

§

impl<V> !Send for SelectState<V>

§

impl<V> !Sync for SelectState<V>

§

impl<V> !UnwindSafe for SelectState<V>

§

impl<V> Freeze for SelectState<V>
where Vec<SelectEntry<V>>: Freeze, Vec<SelectItem<V>>: Freeze, Option<SearchField<V>>: Freeze,

§

impl<V> Unpin for SelectState<V>
where Vec<SelectEntry<V>>: Unpin, Vec<SelectItem<V>>: Unpin, Option<SearchField<V>>: Unpin,

§

impl<V> UnsafeUnpin for SelectState<V>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

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

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

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

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

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> Upcast<T> for T

Source§

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

Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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