craft_core 0.1.1

Core library for the Craft GUI framework.
Documentation
use crate::events::{CraftMessage, EventDispatchType, Message};
use crate::{PinnedFutureAny, WindowContext};
use std::any::Any;
use crate::geometry::Rectangle;

#[derive(Debug, Clone, Copy, Default)]
pub enum PointerCapture {
    #[default]
    None,
    Set,
    Unset,
}

/// The result of an update.
pub struct Event {
    /// Propagate craft_events to the next element. True by default.
    pub propagate: bool,
    /// A future that will produce a message when complete. The message will be sent to the origin component.
    pub future: Option<PinnedFutureAny>,
    /// Prevent default event handlers from running when an craft_event is not explicitly handled.
    /// False by default.
    pub prevent_defaults: bool,
    pub(crate) result_message: Option<CraftMessage>,
    /// Redirect future pointer events to this component. None by default.
    pub(crate) pointer_capture: PointerCapture,
    pub(crate) effects: Vec<(EventDispatchType, Message)>,
    pub(crate) ime: ImeAction,

    pub target: Option<String>,
    pub current_target: Option<String>,
    pub window: WindowContext,
}

#[derive(Debug, Clone, Copy, Default)]
pub enum ImeAction {
    #[default]
    None,
    Set(Rectangle),
    Unset,
}

impl Event {
    pub fn with_window_context(window: WindowContext) -> Self {
        Event {
            window,
            ..Default::default()
        }
    }
    
    #[cfg(not(target_arch = "wasm32"))]
    pub fn async_result<T: Send + Sync + 'static>(t: T) -> Box<dyn Any + Send + Sync + 'static> {
        Box::new(t)
    }

    #[cfg(target_arch = "wasm32")]
    pub fn async_result<T: 'static>(t: T) -> Box<dyn Any + 'static> {
        Box::new(t)
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub fn async_no_result() -> Box<dyn Any + Send + 'static> {
        Box::new(())
    }

    #[cfg(target_arch = "wasm32")]
    pub fn async_no_result() -> Box<dyn Any + 'static> {
        Box::new(())
    }

    pub(crate) fn ime_action(mut self, action: ImeAction) -> Self {
        self.ime = action;
        self
    }
}

impl Default for Event {
    fn default() -> Self {
        Event {
            propagate: true,
            future: None,
            prevent_defaults: false,
            result_message: None,
            pointer_capture: Default::default(),
            effects: Vec::new(),
            ime: ImeAction::None,
            target: None,
            current_target: None,
            window: WindowContext::new(),
        }
    }
}

impl Event {
    pub fn new() -> Event {
        Event::default()
    }

    pub fn pinned_future(&mut self, future: PinnedFutureAny) {
        self.future = Some(future);
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub fn future<F: Future<Output = Box<dyn Any + Send + Sync>> + 'static + Send>(&mut self, future: F) {
        self.future = Some(Box::pin(future));
    }

    #[cfg(target_arch = "wasm32")]
    pub fn future<F: Future<Output = Box<dyn Any>> + 'static>(&mut self, future: F) {
        self.future = Some(Box::pin(future));
    }

    pub fn prevent_defaults(&mut self) {
        self.prevent_defaults = true;
    }

    pub fn prevent_propagate(&mut self) {
        self.propagate = false;
    }

    pub(crate) fn result_message(&mut self, message: CraftMessage) {
        self.result_message = Some(message);
    }

    pub fn pointer_capture(&mut self, pointer_capture: PointerCapture) {
        self.pointer_capture = pointer_capture;
    }

    pub fn add_effect(&mut self, event_dispatch_type: EventDispatchType, message: Message) {
        self.effects.push((event_dispatch_type, message));
    }
}