About events
Events is what NWG uses to respond to user events. Event callbacks are bound and unbound to controls
through the Ui.
The
EventCallback
enum describe the callback arguments. All callback arguments are guaranteed
to start with
|ui, caller|
. Where ui is the Ui that owns the control and caller is the ID
of control the triggered the event.
For a list of all built-in events see
6. List of events .
To bind an event to a control the
bind(ID, cb_ID, callback) -> Result<(), Error>
method must be used. Unlike with actions, trying to bind an event that is not supported by a
control will return an Error.
Arguments
- ID: The control id that will react to the event
- cb_ID: A callback identifier for unbinding
- callback: A EventCallback enum that contains the function or the closure
Behaviour
- Callbacks are executed in the order they where added
Failure
- If the control id is not in the Ui a
Error::CONTROL_NOT_FOUND
is returned.
- If the callback id is not unqiue for this event a
Error::CALLBACK_ID_EXISTS
is returned.
- If the event is not supported a
Error::CALLBACK_NOT_SUPPORTED
is returned.
Example
ui.bind("HelloBtn", "SayHello", EventCallback::Click(Box::new(|ui, caller|{
println!("Caller is: {:?}", caller);
}))).unwrap();
To unbind an event to a control the
unbind(ID, cb_ID, event) -> Result<(), Error>
method must be used.
Arguments
- ID: The control id that will react to the event
- cb_ID: The callback identifier
- event: A Event enum
Failure
- If the control id is not in the Ui a
Error::CONTROL_NOT_FOUND
is returned.
- If the callback id is not unqiue for this event a
Error::CALLBACK_ID_EXISTS
is returned.
- If the event is not supported a
Error::CALLBACK_NOT_SUPPORTED
is returned.
Example
ui.unbind("HelloBtn", "SayHello", Event::Click).unwrap();
Once your setup is completed, the
nwg::dispatch_events()
function must be called
in order to block the thread and process the system events. The function will return
once a
Quit (WM_QUIT) event is received.
Behaviour
-
If you are famliar with the Winapi, this method simply wraps a simple
GetMessageW
loop.
Example