vizia_core/events/
mod.rs

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