orbtk-api 0.3.1-alpha3

API crate that provides base api and elements for OrbTk like widgets basis.
use dces::prelude::Entity;

use crate::{
    prelude::*,
    proc_macros::{Event, IntoHandler},
};

/// Used to request keyboard focus on the window.
#[derive(Event, Clone)]
pub enum FocusEvent {
    RequestFocus(Entity),
    RemoveFocus(Entity),
}

pub type FocusHandlerFn = dyn Fn(&mut StatesContext, FocusEvent) -> bool + 'static;

#[derive(IntoHandler)]
pub struct FocusEventHandler {
    pub handler: Rc<FocusHandlerFn>,
}

impl EventHandler for FocusEventHandler {
    fn handle_event(&self, states: &mut StatesContext, event: &EventBox) -> bool {
        if let Ok(event) = event.downcast_ref::<FocusEvent>() {
            return (self.handler)(states, event.clone());
        }

        false
    }

    fn handles_event(&self, event: &EventBox) -> bool {
        event.is_type::<FocusEvent>()
    }
}