Skip to content

Tutorial Callback Structure

ddbnl edited this page Aug 30, 2022 · 1 revision

Basics

Callbacks can be created from a closure or from a function. We will see examples of both below.

All callbacks take a "context: Context" parameter. The Context object contains the StateTree object (context.state_tree) and the Scheduler object (context.scheduler). We can use these to manage the UI. The Context also contains the path of the widget for which the callback was called (context.widget_path). Some callbacks have more parameters: for example, mouse callbacks have a mouse_pos parameter, but we will discuss these separately for each callback when relevant.

Finally: each callback returns a bool. The bool indicates whether the event should be consumed. If the event is not consumed, the widget is allowed to execute its default behavior if it has any. For example, the checkbox widget has default "on_press" behavior: when pressed, it will toggle on/off. If you bind a custom "on_press" callback for a checkbox, you control whether the default behavior will be executed by returning 'false' (event not consumed, default is allowed to run) or 'true' (event consumed, default not allowed to run). This gives you the option to overwrite default widget behavior, or supplement it. If you want to know if returning true for a widget callback would overwrite default behavior, see the Reference section entry for that widget and check the callback chapter.

For mouse callbacks (such as on_left_click, on_hover, etc.), it is also important to think about whether you want to consume the event. A mouse click will always hit multiple widgets; if you click a button, you also click the layout that contains the button, the layout that contains the layout, etc. If a widget callback returns true, all the other widgets will not receive the event. The root layout is the first to receive an event, and the widget the last (i.e. events move along the widget path). For performance reasons you should return true for mouse callbacks with no default behavior, unless you have a reason not to do so.

Now we'll look at the skeleton of callbacks; note that we will see the context parameter and the bool return value which we discussed.

Callbacks from closure

use ez_term::*;

let my_callback = |context: Context| {
    true
};

Callbacks from function

use ez_term::*;

fn my_callback(context: Context) -> bool {
    true
};

Now that we know what a callback should look like, let's see how to bind callbacks.

Continue

The general tutorial continues with: Callback Configs.

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