pub struct SelectList<T: SelectItem> { /* private fields */ }Expand description
A generic scrollable list with keyboard and mouse navigation.
Items must implement the SelectItem trait, which controls how each row is rendered. The list tracks a selected index with wrapping navigation (Up/Down and mouse scroll).
§Usage
use tui::{SelectList, SelectItem, Line, ViewContext};
struct FileEntry { name: String }
impl SelectItem for FileEntry {
fn render_item(&self, selected: bool, ctx: &ViewContext) -> Line {
if selected {
Line::styled(&self.name, ctx.theme.highlight_fg())
} else {
Line::new(&self.name)
}
}
}
let list = SelectList::new(
vec![FileEntry { name: "main.rs".into() }],
"No files",
);§Messages
SelectList<T> implements Component with Message = SelectListMessage:
SelectListMessage::Close— Emitted on Esc.SelectListMessage::Select(usize)— Emitted on Enter, carrying the selected index.
§Key methods
items()/items_mut()— Access the items.selected_index()/selected_item()— Query the current selection.set_items(items)— Replace all items, clamping the selection index.set_selected(index)— Programmatically move the selection.push(item)— Append a single item.retain(f)— Filter items in place, clamping the selection.
§SelectItem trait
Implement this on your item type to control rendering. The selected flag lets you apply highlight styling to the focused row.
§See also
SelectOption— A built-inSelectItemimplementation withvalue,title, and optionaldescription.RadioSelect— Single-select radio buttons (used in forms).
Implementations§
Source§impl<T: SelectItem> SelectList<T>
impl<T: SelectItem> SelectList<T>
pub fn new(items: Vec<T>, placeholder: impl Into<String>) -> Self
pub fn items(&self) -> &[T]
pub fn items_mut(&mut self) -> &mut [T]
pub fn retain(&mut self, f: impl FnMut(&T) -> bool)
pub fn selected_index(&self) -> usize
pub fn selected_item(&self) -> Option<&T>
pub fn set_items(&mut self, items: Vec<T>)
pub fn set_selected(&mut self, index: usize)
pub fn push(&mut self, item: T)
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Trait Implementations§
Source§impl<T: SelectItem> Component for SelectList<T>
impl<T: SelectItem> Component for SelectList<T>
Source§type Message = SelectListMessage
type Message = SelectListMessage
The message type emitted by this widget.
Source§async fn on_event(&mut self, event: &Event) -> Option<Vec<Self::Message>>
async fn on_event(&mut self, event: &Event) -> Option<Vec<Self::Message>>
Process an event and return the outcome. Read more
Source§fn render(&mut self, ctx: &ViewContext) -> Frame
fn render(&mut self, ctx: &ViewContext) -> Frame
Render the current state to a frame.
Auto Trait Implementations§
impl<T> Freeze for SelectList<T>
impl<T> RefUnwindSafe for SelectList<T>where
T: RefUnwindSafe,
impl<T> Send for SelectList<T>where
T: Send,
impl<T> Sync for SelectList<T>where
T: Sync,
impl<T> Unpin for SelectList<T>where
T: Unpin,
impl<T> UnsafeUnpin for SelectList<T>
impl<T> UnwindSafe for SelectList<T>where
T: 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
Mutably borrows from an owned value. Read more
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>
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 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>
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