Skip to main content

Option

Struct Option 

Source
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 Selector component is required to navigate and select options.

Implementations§

Source§

impl Option

Source

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 &str representing the text displayed for this option.
  • callback: A Callback to 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)?;
Source

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!();
}

Auto Trait Implementations§

§

impl !RefUnwindSafe for Option

§

impl !Send for Option

§

impl !Sync for Option

§

impl !UnwindSafe for Option

§

impl Freeze for Option

§

impl Unpin for Option

§

impl UnsafeUnpin for Option

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, 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.