vizia_core/events/
event_handler.rs

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
use crate::prelude::*;

use std::any::{Any, TypeId};

#[doc(hidden)]
pub(crate) trait ViewHandler: Any {
    fn element(&self) -> Option<&'static str> {
        None
    }

    fn event(&mut self, cx: &mut EventContext, event: &mut Event);

    fn draw(&self, cx: &mut DrawContext, canvas: &Canvas);

    fn accessibility(&self, cx: &mut AccessContext, node: &mut AccessNode);

    fn as_any_ref(&self) -> &dyn Any;

    fn as_any_mut(&mut self) -> &mut dyn Any;

    fn id(&self) -> TypeId;
}

impl dyn ViewHandler {
    /// Attempt to cast a view handler to an immutable reference to the specified type.
    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
        self.as_any_ref().downcast_ref()
    }

    /// Attempt to cast a view handler to a mutable reference to the specified type.
    pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
        self.as_any_mut().downcast_mut()
    }
}