Events
fn handle()
is the application entrypoint for handling events, and is passed an Event
. For a detailed view, you can read all of the variants of the Event
enum. We'll outline different event scenarios below.
Structuring the handle
function
Usually, both the top level app and individual components will all have a handle
function that takes in an Event
. These functions should:
- use a
match
expression to handle relevant events for the component - pass the
Event
to all child components'handle
functions - call
cx.request_draw()
if a redraw is necessitated. - call
cx.request_frame()
if it should trigger another call to the top levelhandle
.
User input
Mouse and touch input are called "pointers" in Zaplib, represented using PointerUp
, PointerDown
, PointerMove
, and PointerScroll
, and PointerHover
.
To see if a pointer event is meant for a component, use hits_pointer
. This matches using:
- a
ComponentId
, which is a unique identifier you can assign to your component struct withComponentId::default()
. - an
Option<Rect>
, which can represent coordinate bounds to match within. This can be manually constructed, but commonly is retrieved from a rendered instance'sArea
, as so:
// Saved somewhere in `draw`
self.area = cx.add_instances(shader, instance_data);
// In `handle`
match event.hits_pointer(cx, self.component_id, self.area.get_rect_for_first_instance(cx)) { ... }
For processing text input, use TextInput
. We also have KeyDown
and KeyUp
, useful for keyboard based navigation or shortcuts - but do not rely on these for capturing text input. Use TextCopy
for handling clipboard requests.
You may have different components of your app which take keyboard input. To manage keyboard focus between them, use set_key_focus
. Like earlier, this matches using ComponentId
.
Then, to see if a keyboard event is meant for a component, use hits_keyboard
, which will check key focus and skip irrelevant events. It also returns KeyFocus
and KeyFocusLost
if your component should handle focus changes.