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
impl CheckBox
Sourcepub fn new(text: &str, checked: bool) -> Self
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 checkboxchecked- The initial state of the checkbox (true for checked, false for unchecked)
§Example
use appcui::prelude::*;
let checkbox = toolbar::CheckBox::new("&Enable feature", true);Sourcepub fn set_content(&mut self, text: &str)
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
Sourcepub fn set_checked(&mut self, checked: bool)
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)
Sourcepub fn is_checked(&self) -> bool
pub fn is_checked(&self) -> bool
Returns the current checked state of the checkbox.
Returns true if the checkbox is checked or false otherwise.
Sourcepub fn set_tooltip(&mut self, text: &str)
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.
Sourcepub fn is_visible(&self) -> bool
pub fn is_visible(&self) -> bool
Returns true if the toolbar item is visible, false otherwise.
Sourcepub fn set_visible(&mut self, visible: bool)
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§
impl Freeze for CheckBox
impl RefUnwindSafe for CheckBox
impl Send for CheckBox
impl Sync for CheckBox
impl Unpin for CheckBox
impl UnsafeUnpin for CheckBox
impl UnwindSafe for CheckBox
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
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.