Documentation / Designing

Events and actions

Making a widget do something - and reaching your own code when the built-in actions run out.

An event is a trigger on a widget and a list of actions to run when it fires. Both come from the SquareLine Studio model, so if you have used that, this is the same thing with a few additions: swipe triggers on every widget, and hardware actions that reach the data layer. The generator writes one ui_event_<Name>() handler per widget that has events, registers it with lv_obj_add_event_cb(), and dispatches on the event code inside.

The Events section of a button with one Clicked trigger running a Change Screen action.
The Events section of a button with one Clicked trigger running a Change Screen action.

Adding one

Select the widget, scroll the Inspector to Events, click add, pick the trigger, then add actions to it. Each action has its own fields - a Change Screen needs a screen, an Increment Slider needs a target and an amount. An action with an empty required field is reported by the validator as an error, since the generator cannot emit it.

Triggers

TriggerFires when
PressedThe finger goes down.
ClickedPressed briefly then released, without scrolling. The one to use for buttons.
Short clickedA brief tap.
Long pressedHeld past the long-press time.
Long pressed repeatRepeatedly while still held after a long press - for auto-repeat.
ReleasedThe finger lifts, click or not.
Focused / DefocusedKeyboard or encoder focus moves onto or off the widget.
Value changedA slider, arc, switch, checkbox, dropdown, roller, spinbox or textarea changed.
Ready / CancelA process finished or was abandoned - the keyboard's tick and cross.
Checked / UncheckedA checkable object toggled.
Gesture (any)A swipe in any direction.
Swipe up / down / left / rightA swipe in that direction.
KeyA key was sent to the widget from a keypad or encoder.
Edited / InsertText changed or text was inserted in a textarea.
Screen loaded / unloaded, Screen load start / unload startScreens only.

The four Screen triggers are only offered on a screen, and are the place for anything that should happen as a page appears - refreshing a readout, starting an animation. Screen loaded fires after the load animation; Screen load start before it.

Actions

ActionFieldsDoes
Call functionFunction nameCalls a function you write in ui_events.c.
Change ScreenScreen, fade mode, speed, delay, auto deleteLoads another screen with an animation.
Delete screenScreenFrees a screen from RAM.
Increment Arc / Bar / SliderTarget, value (and animation time)Adds a signed amount to the target's value.
Step spinboxTarget, directionIncrements or decrements the spinbox by its step.
Modify FlagTarget, flag, Add / Remove / ToggleChanges an object flag - toggling Hidden is the usual case.
Set FlagTarget, flag, on/offSets a flag to a known state.
Set OpacityTarget, opacitySets the target's main-part opacity.
Set PropertyTarget, property, valueSets Text, Value, Value (animated), Image source, Range min or max, Selected index, Background colour or Text colour.
Play AnimationTarget, property, start, end, duration, delay, path, repeat count, playbackAnimates X, Y, width, height, opacity, scale or rotation with an easing path.
Set text value from arc / sliderTarget label, source, prefix, postfixWrites the source's value into a label as text.
Set text value when checkedTarget label, source, checked text, unchecked textWrites one of two strings depending on a checkbox or switch.
Switch ThemeLight / DarkRe-initialises the theme at runtime.
Set variableVariable, mode, valueWrites a data-layer variable. See below.
Set digital outputOutput, Activate / Deactivate / ToggleDrives a digital output, on an MCU pin or an extender.
Write to peripheralPeripheral, channel, valueWrites to a writable channel such as a PCA9685 output.
Refresh inputs now-Reads every peripheral, analog and digital input immediately.

Change Screen, in detail

The most used action. Besides the target screen it takes a fade mode - None, Over left/right/top/bottom, Move left/right/top/bottom, Fade in, Fade out, Out left/right/top/bottom - a speed in milliseconds, a delay, and auto delete, which frees the screen being left. It becomes _ui_screen_change() in ui_helpers.c, which creates the target if it has been freed and calls lv_screen_load_anim(). The preview runs it too, switching the canvas to the target screen.

Call function

The escape hatch. Give it a function name and the generator declares it in ui_events.h and writes a stub in ui_events.c:

void my_callback(lv_event_t * e)
{
    // TODO: your code here
}

ui_events.c is the one generated file that is yours: the stubs are regenerated on every export but a file you have edited is never overwritten, so your code survives. Inside a callback you have the whole LVGL API and every ui_<Name> object from ui.h.

Hardware actions

The last four actions belong to the data layer and are the bridge from a touch to the firmware's state. Set variable writes a variable - a fixed value, a formula, an amount added, a toggle for a Boolean, or the value of a widget - and every binding that reads it updates on the next refresh. Set digital output drives an output on an MCU pin or an I/O extender. Write to peripheral pushes a value to a writable channel (a PCA9685 PWM channel, a NeoPixel colour). Refresh inputs now forces an immediate read of everything rather than waiting for the poll interval. The same four are available to automations.

In the preview

Events run in preview: a Clicked button changes screen, toggles a flag, steps a value, sets a variable. Since a mouse cannot swipe, a pad at the bottom of the canvas sends the four gestures and shows whether anything on the screen listens for each. Play Animation, Switch Theme and peripheral writes cannot be simulated and say so in the Console tab rather than silently doing nothing.

Event bubbling

A tap on a label inside a panel is a tap on the label, not the panel. Set the label's Event bubble flag and the panel's handler sees it as well; set Gesture bubble for swipes to travel the same way. A screen's swipe triggers rely on this: a swipe that starts on a widget without Gesture bubble stops there.