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,
}
pub struct Event {
pub propagate: bool,
pub future: Option<PinnedFutureAny>,
pub prevent_defaults: bool,
pub(crate) result_message: Option<CraftMessage>,
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));
}
}