1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
//! Events for communicating state change to views and models.
//!
//! Views and Models communicate with each other via events. An [Event] contains a message, as well as metadata to describe how events
//! should propagate through the tree. By default events will propagate up the tree from the target, visiting each ancestor as well as
//! any models attached to the ancestors.
//!
//! A message can be any static thread-safe type but is usually an enum. For example:
//! ```
//! enum AppEvent {
//! Increment,
//! Decrement,
//! }
//! ```
//! Then, to send an event up the tree from the current entity, we can use `cx.emit(...)`, for example:
//! ```no_run
//! # use vizia_core::prelude::*;
//! # use vizia_winit::application::Application;
//! # let cx = &mut Context::default();
//! pub struct AppData {
//! count: i32,
//! }
//!
//! impl Model for AppData {}
//!
//! pub enum AppEvent {
//! Increment,
//! Decrement,
//! }
//!
//! Application::new(|cx|{
//! AppData {
//! count: 0,
//! }.build(cx);
//!
//! Label::new(cx, "Increment")
//! .on_press(|cx| cx.emit(AppEvent::Increment));
//! })
//! .run();
//! ```
//!
//! Views and Models receive events through the `event()` method of the View or Model traits.
//! The event message must then be downcast to the right type using the [`map`](Event::map) or [`take`](Event::take) methods on the event:
//! ```no_run
//! # use vizia_core::prelude::*;
//! # let cx = &mut Context::default();
//! # use vizia_winit::application::Application;
//!
//! pub struct AppData {
//! count: i32,
//! }
//!
//! pub enum AppEvent {
//! Increment,
//! Decrement,
//! }
//!
//! impl Model for AppData {
//! fn event(&mut self, _cx: &mut EventContext, event: &mut Event) {
//! // `event.map()` will attempt to cast the event message to the desired type and
//! // pass a reference to the message type to the closure passed to the `map()` method.
//! event.map(|app_event, _| match app_event {
//! AppEvent::Increment => {
//! self.count += 1;
//! }
//!
//! AppEvent::Decrement => {
//! self.count -= 1;
//! }
//! });
//!
//! // Alternatively, `event.take()` will attempt to cast the event message to the
//! // desired type and return the value of the message (not a reference),
//! // removing it from the event and thus preventing it from propagating further.
//! event.take(|app_event, meta| match app_event {
//! AppEvent::Increment => {
//! self.count += 1;
//! }
//!
//! AppEvent::Decrement => {
//! self.count -= 1;
//! }
//! });
//! }
//! }
//! ```
pub mod event_manager;
pub use event_manager::EventManager;
mod event;
pub(crate) use event::TimedEvent;
pub use event::{Event, EventMeta, Propagation, TimedEventHandle};
mod event_handler;
pub(crate) use event_handler::ViewHandler;
mod timer;
pub(crate) use timer::TimerState;
pub use timer::{Timer, TimerAction};
pub use crate::window::WindowEvent;