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>
impl<V: SelectValue> SelectState<V>
Sourcepub fn new(
entries: impl IntoIterator<Item = impl Into<SelectEntry<V>>>,
cx: &mut Context<'_, Self>,
) -> Self
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.
Sourcepub fn multiple(
entries: impl IntoIterator<Item = impl Into<SelectEntry<V>>>,
cx: &mut Context<'_, Self>,
) -> Self
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.
Sourcepub fn with_search(
self,
placeholder: impl Into<SharedString>,
window: &mut Window,
cx: &mut Context<'_, Self>,
) -> Self
pub fn with_search( self, placeholder: impl Into<SharedString>, window: &mut Window, cx: &mut Context<'_, Self>, ) -> Self
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.
Sourcepub 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
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,
)
}Sourcepub fn with_selected(self, values: impl IntoIterator<Item = V>) -> Self
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.
Sourcepub fn with_row_height(self, height: Pixels) -> Self
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.
Sourcepub fn entries(&self) -> &[SelectEntry<V>]
pub fn entries(&self) -> &[SelectEntry<V>]
The rows, in menu order.
Sourcepub fn set_entries(
&mut self,
entries: impl IntoIterator<Item = impl Into<SelectEntry<V>>>,
cx: &mut Context<'_, Self>,
)
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.
Sourcepub fn selected_value(&self) -> Option<&V>
pub fn selected_value(&self) -> Option<&V>
The selected value of a single select.
Sourcepub fn selected_items(&self) -> &[SelectItem<V>]
pub fn selected_items(&self) -> &[SelectItem<V>]
The selected items, in the order chosen.
Sourcepub fn is_selected(&self, value: &V) -> bool
pub fn is_selected(&self, value: &V) -> bool
Whether value is selected.
Sourcepub fn set_selected(
&mut self,
values: impl IntoIterator<Item = V>,
cx: &mut Context<'_, Self>,
)
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.
Sourcepub fn is_multiple(&self) -> bool
pub fn is_multiple(&self) -> bool
Whether more than one item can be selected.
Sourcepub fn is_searchable(&self) -> bool
pub fn is_searchable(&self) -> bool
Whether the menu has a search field.
Sourcepub fn is_searching(&self) -> bool
pub fn is_searching(&self) -> bool
Whether a search handler is still answering the query.
Sourcepub fn query(&self, cx: &App) -> SharedString
pub fn query(&self, cx: &App) -> SharedString
The search query, or an empty string without a search field.
Sourcepub fn highlighted(&self) -> Option<&SelectItem<V>>
pub fn highlighted(&self) -> Option<&SelectItem<V>>
The item the keyboard is on while the menu is open.
Sourcepub fn open(&mut self, window: &mut Window, cx: &mut Context<'_, Self>)
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.
Sourcepub fn close(&mut self, window: &mut Window, cx: &mut Context<'_, Self>)
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.
Sourcepub fn choose(
&mut self,
value: &V,
window: &mut Window,
cx: &mut Context<'_, Self>,
)
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.
Sourcepub fn choose_highlighted(
&mut self,
window: &mut Window,
cx: &mut Context<'_, Self>,
)
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.
Sourcepub fn move_highlight(&mut self, delta: isize, cx: &mut Context<'_, Self>)
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.
Sourcepub fn highlight_first(&mut self, cx: &mut Context<'_, Self>)
pub fn highlight_first(&mut self, cx: &mut Context<'_, Self>)
Highlights the first selectable row.
Sourcepub fn highlight_last(&mut self, cx: &mut Context<'_, Self>)
pub fn highlight_last(&mut self, cx: &mut Context<'_, Self>)
Highlights the last selectable row.
Trait Implementations§
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.
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
fn focus_handle(&self, _: &App) -> FocusHandle
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>
impl<V> Unpin for SelectState<V>
impl<V> UnsafeUnpin for SelectState<V>where
Vec<SelectEntry<V>>: UnsafeUnpin,
Vec<SelectItem<V>>: UnsafeUnpin,
Option<SearchField<V>>: UnsafeUnpin,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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