Skip to content

Examples Widgets Progress Bar

ddbnl edited this page Sep 3, 2022 · 2 revisions

This example will show how to use the progress bar in different ways. We'll set progress manually, have it update on button click, have it update from a recurring scheduled task, and have it update from a background thread.

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

    // This is a regular progressbar. We'll set its value manually.
    - Label:
        text: Static
        auto_scale: true, true
        size_hint_x: 0.5
        halign: center
        valign: middle
    - Layout:
        size_hint_x: 0.5
        auto_scale_height: true
        - ProgressBar:
            id: static_progress_bar
            size_hint_x: none
            width: parent.width - parent.static_label.width
            max: 100
            value: 50
            border: true
        - Label:
            id: static_label
            text: parent.static_progress_bar.value
            size_hint_x: none
            width: 4
            auto_scale_height: true
            valign: middle

    // We'll bind a callback to this progress bar to update on button press
    - Button:
        id: manual_button
        fg_color: yellow
        text: Increase
        size_hint_x: 0.5
        auto_scale_height: true
    - Layout:
        auto_scale_height: true
        size_hint_x: 0.5
        - ProgressBar:
            id: manual_progress_bar
            size_hint_x: none
            width: parent.width - parent.manual_label.width
            border: true
        - Label:
            id: manual_label
            text: parent.manual_progress_bar.value
            size_hint_x: none
            width: 4
            auto_scale_height: true
            valign: middle

    // We'll bind a callback to update the value with a recurring task
    - Button:
        id: task_button
        fg_color: yellow
        text: Start task
        size_hint_x: 0.5
        auto_scale_height: true
    - Layout:
        size_hint_x: 0.5
        auto_scale_height: true
        - ProgressBar:
            id: task_progress_bar
            size_hint_x: none
            width: parent.width - parent.task_label.width
            border: true
        - Label:
            id: task_label
            text: parent.task_progress_bar.value
            size_hint_x: none
            width: 4
            auto_scale_height: true
            valign: middle

    // We'll bind a callback to update the value from a background thread
    - Button:
        id: thread_button
        fg_color: yellow
        text: Start thread
        size_hint_x: 0.5
        auto_scale_height: true
    - Layout:
        size_hint_x: 0.5
        auto_scale_height: true
        - ProgressBar:
            id: thread_progress_bar
            size_hint_x: none
            width: parent.width - parent.thread_label.width
            border: true
        - Label:
            id: thread_label
            text: parent.thread_progress_bar.value
            size_hint_x: none
            width: 4
            auto_scale_height: true
            valign: middle
use std::time::Duration;
use ez_term::*;

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

    // Manual progress bar callback
    let manual_progress_callback = |context: Context| {
        let state = context.state_tree.get_mut("manual_progress_bar")
            .as_progress_bar_mut();
        let progress = state.get_value();
        if progress < state.get_max() {
            state.set_value(progress + 20);
            state.update(context.scheduler);
            if state.get_value() == state.get_max() {
                let state = context.state_tree.get_mut("manual_button").as_button_mut();
                state.set_disabled(true);
            }
        }
        false

    };
    let callback_config =
        CallbackConfig::from_on_press(Box::new(manual_progress_callback));
    scheduler.update_callback_config("manual_button", callback_config);

    // Recurring task progress bar callback
    let task_progress_callback = |context: Context| {

        let task_progress_task = |context: Context| {
            let state = context.state_tree.get_mut("task_progress_bar")
                .as_progress_bar_mut();
            let progress = state.get_value();
            if progress < state.get_max() {
                state.set_value(progress + 20);
                state.update(context.scheduler);
                true
            } else {
                let state = context.state_tree.get_mut("task_button").as_button_mut();
                state.set_disabled(false);
                state.update(context.scheduler);
                false
            }
        };
        let state = context.state_tree.get_mut("task_progress_bar")
            .as_progress_bar_mut();
        state.set_value(0);
        let state = context.state_tree.get_mut("task_button").as_button_mut();
        state.set_disabled(true);
        state.update(context.scheduler);
        context.scheduler.schedule_recurring("update_progress_bar",
                                     Box::new(task_progress_task),
                                     Duration::from_secs(1));
        false
    };
    let callback_config =
        CallbackConfig::from_on_press(Box::new(task_progress_callback));
    scheduler.update_callback_config("task_button", callback_config);

    // Threaded task progress bar callback
    let thread_progress_callback = |context: Context| {

        let progress_thread = |mut context: ThreadedContext| {
            let state = context.state_tree.get_mut("thread_progress_bar")
                .as_progress_bar_mut();
            state.set_value(0);
            state.update(&mut context.scheduler);
            for x in 1..=5 {
                std::thread::sleep(Duration::from_secs(1));
                state.set_value(x * 20);
                state.update(&mut context.scheduler);
            }
        };
        let on_finish = |context: Context| {
            let state = context.state_tree.get_mut("thread_button").as_button_mut();
            state.set_disabled(false);
            state.update(context.scheduler);
        };
        let state = context.state_tree.get_mut("thread_button").as_button_mut();
        state.set_disabled(true);
        state.update(context.scheduler);
        context.scheduler.schedule_threaded(Box::new(progress_thread),
                                     Some(Box::new(on_finish)));
        false
    };
    let callback_config =
        CallbackConfig::from_on_press(Box::new(thread_progress_callback));
    scheduler.update_callback_config("thread_button", callback_config);

    run(root_widget, state_tree, scheduler);

progress_bar_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