vizia_core/events/
event_handler.rs

1use crate::prelude::*;
2
3use std::any::{Any, TypeId};
4
5#[doc(hidden)]
6pub(crate) trait ViewHandler: Any {
7    fn element(&self) -> Option<&'static str> {
8        None
9    }
10
11    fn event(&mut self, cx: &mut EventContext, event: &mut Event);
12
13    fn draw(&self, cx: &mut DrawContext, canvas: &Canvas);
14
15    fn accessibility(&self, cx: &mut AccessContext, node: &mut AccessNode);
16
17    fn as_any_ref(&self) -> &dyn Any;
18
19    fn as_any_mut(&mut self) -> &mut dyn Any;
20
21    fn id(&self) -> TypeId;
22}
23
24impl dyn ViewHandler {
25    /// Attempt to cast a view handler to an immutable reference to the specified type.
26    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
27        self.as_any_ref().downcast_ref()
28    }
29
30    /// Attempt to cast a view handler to a mutable reference to the specified type.
31    pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
32        self.as_any_mut().downcast_mut()
33    }
34}