MessageFilter typedef
A function that filters messages before they reach the model.
The filter receives the current model and the incoming message.
Return the message (possibly modified) to allow it through,
or return null to filter it out completely.
This is useful for:
- Preventing quit on unsaved changes
- Modifying messages before they reach the model
- Logging or debugging message flow
- Implementing global key bindings
Example:
Msg? preventQuitFilter(Model model, Msg msg) {
if (msg is KeyMsg && msg.key.ctrl && msg.key.runes.firstOrNull == 0x63) {
// Ctrl+C pressed
if (model is MyModel && model.hasUnsavedChanges) {
// Block quit and show warning instead
return const ShowUnsavedWarningMsg();
}
}
return msg; // Allow message through
}
Implementation
typedef MessageFilter = Msg? Function(Model model, Msg msg);