Skip to main content

CheckBox

Struct CheckBox 

Source
pub struct CheckBox { /* private fields */ }
Expand description

A checkbox is a toolbar item that can be positioned on the top or bottom part of a window and can have two states (checked or unchecked).

Toolbar checkboxes display text and can be toggled between checked and unchecked states when clicked. They can also have hotkeys defined using the ‘&’ character in the checkbox caption. For example, the caption “&Option one” will set ‘Alt+O’ as a hotkey for the checkbox.

To intercept checkbox state changes, implement the ToolBarEvents trait for the window containing the checkbox and implement the on_checkbox_clicked method.

§Example

The following example creates a window with two checkboxes that display their state in a label:

use appcui::prelude::*;

#[Window(events = ToolBarEvents)]
struct CheckboxWindow {
    checkbox_one: Handle<toolbar::CheckBox>,
    checkbox_two: Handle<toolbar::CheckBox>,
    status_label: Handle<Label>,
}

impl CheckboxWindow {
    fn new() -> Self {
        let mut win = CheckboxWindow {
            base: window!("'Checkbox Demo',a:c,w:40,h:6"),
            checkbox_one: Handle::None,
            checkbox_two: Handle::None,
            status_label: Handle::None,
        };
         
        // Create a toolbar group at the bottom right of the window
        let group = win.toolbar().create_group(toolbar::GroupPosition::BottomLeft);
         
        // Add checkboxes to the toolbar group
        let mut cb1 = toolbar::CheckBox::new("Option 1", false);
        cb1.set_tooltip("First option");
        win.checkbox_one = win.toolbar().add(group, cb1);
         
        let mut cb2 = toolbar::CheckBox::new("Option 2", false);
        cb2.set_tooltip("Second option");
        win.checkbox_two = win.toolbar().add(group, cb2);
         
        // Add a label to display the checkbox states
        win.status_label = win.add(label!("'Select an option',a:c,w:30,h:1"));
         
        win
    }
}

impl ToolBarEvents for CheckboxWindow {
    fn on_checkbox_clicked(&mut self, handle: Handle<toolbar::CheckBox>, checked: bool) -> EventProcessStatus {
        let message = if handle == self.checkbox_one {
            format!("Option 1 is {}", if checked { "checked" } else { "unchecked" })
        } else if handle == self.checkbox_two {
            format!("Option 2 is {}", if checked { "checked" } else { "unchecked" })
        } else {
            return EventProcessStatus::Ignored;
        };
         
        let h = self.status_label;
        if let Some(label) = self.control_mut(h) {
            label.set_caption(&message);
        }
        EventProcessStatus::Processed
    }
}

fn main() -> Result<(), appcui::system::Error> {
    let mut app = App::new().build()?;
    app.add_window(CheckboxWindow::new());
    app.run();
    Ok(())
}

Implementations§

Source§

impl CheckBox

Source

pub fn new(text: &str, checked: bool) -> Self

Creates a new CheckBox toolbar item with the specified text and initial checked state.

The width (in characters) of the checkbox is calculated based on the number of characters in its content plus 2 characters for the checkbox symbol.

§Parameters
  • text - The caption (text) to be displayed next to the checkbox
  • checked - The initial state of the checkbox (true for checked, false for unchecked)
§Example
use appcui::prelude::*;
let checkbox = toolbar::CheckBox::new("&Enable feature", true);
Source

pub fn set_content(&mut self, text: &str)

Sets a new caption for the checkbox.

The width of the checkbox is automatically updated based on the length of the new caption. The character ‘&’ can be used to define a hotkey for the next character.

§Parameters
  • text - The new text to display next to the checkbox
Source

pub fn caption(&self) -> &str

Returns the current caption text of the checkbox.

Source

pub fn set_checked(&mut self, checked: bool)

Sets the checked state of the checkbox.

§Parameters
  • checked - The new state of the checkbox (true for checked, false for unchecked)
Source

pub fn is_checked(&self) -> bool

Returns the current checked state of the checkbox.

Returns true if the checkbox is checked or false otherwise.

Source

pub fn set_tooltip(&mut self, text: &str)

Sets the tooltip text for the toolbar item.

This method allows you to set a tooltip that will be displayed when the user hovers over the toolbar item. The tooltip text will be displayed in a small popup window that appears near the item.

§Parameters
  • text: A string slice that contains the tooltip text.
Source

pub fn tooltip(&self) -> &str

Gets the tooltip text for the toolbar item.

Source

pub fn is_visible(&self) -> bool

Returns true if the toolbar item is visible, false otherwise.

Source

pub fn set_visible(&mut self, visible: bool)

Sets the visibility of the toolbar item.

This method allows you to control the visibility of the toolbar item. When set to true, the item will be displayed on the toolbar.

Auto Trait Implementations§

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> 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> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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, 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.
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