Skip to content

Examples Widgets Checkbox

ddbnl edited this page Sep 3, 2022 · 2 revisions

This examples shows how to style and bind callbacks to checkboxes.

- Layout:
    mode: table
    orientation: lr-tb
    cols: 2
    border: true

    // This is an active checkbox
    - Label:
        text: Regular:
        auto_scale: true, true
        padding_right: 1
    - CheckBox:
        active: true
        padding_bottom: 1
        selection_order: 1

    // This is a checkbox with custom symbols and colors; its state is also turned to active.
    - Label:
        text: Styled:
        auto_scale: true, true
        padding_right: 1
    - CheckBox:
        active_symbol: 
        inactive_symbol: x
        fg_color: red
        active: true
        selection_order: 2
        padding_bottom: 1
        padding_right: 1

    // This is a checkbox to which we will bind an 'on_value_change' callback. When the value changes
    // from on to off or vice versa, the label will change text and color.
    - Label:
        text: Callback:
        auto_scale: true, true
        padding_right: 1
    - Layout:
        auto_scale: true, true
        - CheckBox:
            id: change_label_checkbox
            selection_order: 3
            padding_right: 1
        - Label:
            id: my_label
            text: Inactive
            fg_color: red
            auto_scale: true, true
use ez_term::*;

fn main () {
    let (root_widget, mut state_tree, mut scheduler) = load_ui();

    let change_label_callback = |context: Context| {

        let checkbox_state = context.state_tree.get_mut(&context.widget_path)
            .as_checkbox_mut();
        let active = checkbox_state.get_active();
        let (text, color) = if active {
            ("Active".to_string(), Color::Green)
        } else {
            ("Inactive".to_string(), Color::Red)
        };

        let label_state = context.state_tree.get_mut("my_label").as_label_mut();
        label_state.set_text(text);
        label_state.get_color_config_mut().set_fg_color(color);
        label_state.update(context.scheduler);
        false
    };

    let callback_config =
        CallbackConfig::from_on_value_change(Box::new(change_label_callback));
    scheduler.update_callback_config("change_label_checkbox", callback_config);

    run(root_widget, state_tree, scheduler);
}

checkbox_example

Tutorial Tutorial-Project-Structure
  Minimal example
EzLang
  EzLang basics
  EzLang Templates
  Ezlang Layout modes
   EzLang Box mode layouts
   EzLang Stack mode layouts
   EzLang Table mode layouts
   EzLang Float mode layouts
   EzLang Tab mode layouts
   EzLang Screen mode layouts
   EzLang Layout Scrolling
   EzLang Layout Views
  EzLang Widget overview
   EzLang Label
   EzLang Text Input
   EzLang Button
   EzLang Checkbox
   EzLang Radio button
   EzLang Dropdown
   EzLang Slider
   EzLang Canvas
  EzLang Property Binding
  EzLang Sizing
   EzLang Size hints
   EzLang Auto scaling
   EzLang Maths Sizing
   EzLang Manual Sizing
  EzLang Positioning
   EzLang Layout Mode Positioning
   EzLang Position Hints
   EzLang Position Maths
   EzLang Manual Position
   EzLang Adjusting Position
  EzLang Keyboard Selection
Scheduler
  Widget States and the State Tree
  The Scheduler Object
  Managing callbacks
   Callback Structure
   Callback Configs
   Callback: On keyboard enter
   Callback: On Left Mouse Click
   Callback: On Press
   Callback: On Select
   Callback: On Deselect
   Callback: On Right Mouse Click
   Callback: On Hover
   Callback: On Drag
   Callback: On Scroll Up
   Callback: On Scroll Down
   Callback: On Value Change
   Callback: Custom Key Binds
   Callback: Global Key Binds
   Callback: Property Binds
  Tasks
   Scheduled Single Exectution Tasks
   Scheduled Recurring Tasks
   Threaded Tasks
  Custom Properties
  Modals
  Programmatic Widgets
  Updating widgets
  Managing selection
Default global (key)binds
Performance
ExamplesLayout: Box Mode Nested
Layout: Box Mode Size Hints
Layout: Stack Mode
Layout: Table Mode Dynamic
Layout: Table Mode Static
Layout: Float Mode Manual
Layout: Float Mode Position hints
Layout: Screen Mode
Layout: Tab Mode
Layout: Scrolling
Layout: Views
Widget: Label
Widget: Text input
Widget: Button
Widget: Checkbox
Widget: Radio Button
Widget: Dropdown
Widget: Slider
Widget: Progress Bar
Widget: Canvas
Scheduler: Schedule Once
Scheduler: Schedule Once Callback
Scheduler: Schedule Recurring
Scheduler: Schedule Recurring Callback
Scheduler: Threaded Task State Tree
Scheduler: Threaded Task Custom Property
Scheduler: Create Widgets
Scheduler: Modal Popup
Reference Widgets
  Common Properties
  Label
  Text Input
  Button
  Checkbox
  Radio button
  Dropdown
  Slider
  Canvas
Scheduler
  Schedule once
  Schedule Recurring
  Schedule Threaded
  Cancel Task
  Cancel Recurring Task
  Create Widget
  Remove Widget
  Select Widget
  Deselect Widget
  Update Widget
  Force Redraw
  Open Modal
  Dismiss Modal
  Bind Global Key
  Remove Global Key
  Clear Global Keys
  Bind Property
  Create Custom Properties
  Get Property
  Get Property Mut
  Overwrite Callback Config
  Update Callback Config
  Exit
Clone this wiki locally