Skip to content

Tutorial Scheduler Single Execution Task

ddbnl edited this page Sep 2, 2022 · 1 revision

A single execution task is a closure or function that is executed only once, after a specified delay. As the delay can be 0, it can also be executed on the next frame. The scheduler method we will use is 'schedule_once'. The first parameter of this function is a &str which is the task name. You can use this name to cancel the task before it executes (see example at the bottom of this chapter). Here is an example of scheduling a single execution task with a closure, changing a label text after 3 seconds:

- Layout:
    - Label:
        id: my_label
        text: Waiting...
use std::time::Duration;
use ez_term::*;

let (root_widget, mut state_tree, mut scheduler) = load_ui();

let my_task = |context: Context| {

    let state = context.state_tree.get_mut("my_label").as_label_mut();
    state.set_text("3 seconds have passed!".to_string());
    state.update(context.scheduler);
};

scheduler.schedule_once("my_task", Box::new(my_task), Duration::from_secs(3));
run(root_widget, state_tree, scheduler);

46_scheduler_stonce

The same example with a function:

use std::time::Duration;
use ez_term::*;
let (root_widget, mut state_tree, mut scheduler) = load_ui();

fn my_task(context: Context) {

    let state = context.state_tree.get_mut("my_label").as_label_mut();
    state.set_text("3 seconds have passed!".to_string());
    state.update(context.scheduler);
};

scheduler.schedule_once("my_task", Box::new(my_task), Duration::from_secs(3));
run(root_widget, state_tree, scheduler);

46_scheduler_stonce

It is of course possible to schedule a task from a callback. For example, scheduling a task with delay after a button is pushed. Here is an example: we will bind a callback to a button, that will change the text of a label after 3 seconds, using a closure:

- Layout:
    mode: box
    orientation: vertical
    - Button:
        id: my_button
        text: Change text
        auto_scale: true, true
        halign: center
        padding_bottom: 2
    - Label:
        id: my_label
        text: Initial text
        auto_scale: true, true
        halign: center
use std::time::Duration;
use ez_term::*;
let (root_widget, mut state_tree, mut scheduler) = load_ui();


let my_callback = |context: Context| {

    let my_task = |context: Context| {

        let state = context.state_tree.get_mut("my_label").as_label_mut();
        state.set_text("Button was pressed 3 seconds ago!".to_string());
        state.update(context.scheduler);
    };

    let state = context.state_tree.get_mut("my_label").as_label_mut();
    state.set_text("Waiting...".to_string());
    state.update(context.scheduler);

    context.scheduler.schedule_once("my_task", Box::new(my_task), Duration::from_secs(3));
    true
};
let new_callback_config = CallbackConfig::from_on_press(Box::new(my_callback));
scheduler.update_callback_config("my_button", new_callback_config);

run(root_widget, state_tree, scheduler);

47_scheduler_stonce

To cancel a run-once task use the 'cancel_task' method of the scheduler. Of course, this only works if it is called before the task is executed. If the task no longer exists this method will not panic, so it is always safe to try. Example:

scheduler.cancel_task("my_task");

Continue

The general tutorial continues with: Scheduled Recurring Exectution Tasks.

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