pub struct Option { /* private fields */ }Expand description
A UI component representing an interactive option in a Container.
Option components are displayed in the order they are added to the
Container. To make options selectable, a Selector must also be
initialized for the Container.
§Usage
The Option component is used within a Container to provide interactive
choices.
§Notes
- A
Selectorcomponent is required to navigate and select options.
Implementations§
Source§impl Option
impl Option
Sourcepub fn new(
label: &str,
callback: impl Into<Option<Callback>>,
) -> FtuiResult<Self>
pub fn new( label: &str, callback: impl Into<Option<Callback>>, ) -> FtuiResult<Self>
Creates a new Option with the specified label and callback.
§Parameters
label: A&strrepresenting the text displayed for this option.callback: ACallbackto invoked when the option is selected (optional).
§Returns
Ok(Option): A new Option instance.
Err(FtuiError): Returns an error.
§Example
// Define a callback function that quits the program when invoked.
cbk_new_callback_func!(quit_option_callback, _arg, {
std::process::exit(0);
});
// Create a `Callback` with no arguments.
let callback = Callback::no_arg(quit_option_callback);
// Create an `Option` component labeled "Quit".
// When selected, it exits the program.
let _ = Option::new("Quit", callback)?;
// Create an `Option` component labeled "Nothing".
// This option has no associated callback.
// You can detect its selection using the `is_selc()` method.
let _ = Option::new("Nothing", None)?;Sourcepub fn is_selc(&mut self) -> bool
pub fn is_selc(&mut self) -> bool
Returns whether the Option component was selected. This method acts
like a latch or semaphore in multithreading contexts. It returns the
current state of the is_selc flag and then resets it to false.
This method is useful for Option components with no Callback.
§Notes
Imagine the following timeline:
Time (ms): 0 500 2000 … |———|————|————> is_selc: | false | true | false
At time 500ms, some internal event sets is_selc = true.
When is_selc() is called (e.g., at 2000ms), it returns true
and immediately resets the flag to false.
§Returns
true: if the option was selected since the last check.false: otherwise.
§Example
// Create an `Option` component with no callback.
let mut option = Option::new(..., None)?;
// Check if the option was selected.
if option.is_selc() {
// Perform an action.
todo!();
}