use crate::{
context::EventCtx,
widget_tree::{WidgetId, WidgetTree},
};
use std::ptr::NonNull;
pub(crate) mod dispatcher;
mod pointers;
pub use pointers::*;
use ribir_geom::Point;
pub use winit::event::{ModifiersState, ScanCode, VirtualKeyCode};
mod focus;
pub use focus::*;
mod keyboard;
pub use keyboard::*;
mod character;
pub use character::*;
mod wheel;
pub use wheel::*;
pub(crate) mod focus_mgr;
mod listener_impl_helper;
use self::dispatcher::DispatchInfo;
#[derive(Clone)]
pub struct EventCommon {
pub(crate) target: WidgetId,
pub(crate) current_target: WidgetId,
pub(crate) cancel_bubble: bool,
pub(crate) prevent_default: bool,
tree: NonNull<WidgetTree>,
info: NonNull<DispatchInfo>,
}
impl EventCommon {
#[inline]
pub fn target(&self) -> WidgetId { self.target }
#[inline]
pub fn current_target(&self) -> WidgetId { self.current_target }
#[inline]
pub fn stop_bubbling(&mut self) { self.cancel_bubble = true }
#[inline]
pub fn bubbling_canceled(&self) -> bool { self.cancel_bubble }
#[inline]
pub fn prevent_default(&mut self) { self.prevent_default = true; }
#[inline]
pub fn modifiers(&self) -> ModifiersState { self.dispatch_info().modifiers() }
pub fn with_shift_key(&self) -> bool { self.dispatch_info().modifiers().shift() }
pub fn with_alt_key(&self) -> bool { self.dispatch_info().modifiers().alt() }
pub fn with_ctrl_key(&self) -> bool { self.dispatch_info().modifiers().ctrl() }
pub fn with_logo_key(&self) -> bool { self.dispatch_info().modifiers().logo() }
pub fn with_command_key(&self) -> bool {
#[cfg(target_os = "macos")]
return self.with_logo_key();
#[cfg(not(target_os = "macos"))]
return self.with_ctrl_key();
}
#[inline]
pub fn global_pos(&self) -> Point { self.dispatch_info().global_pos() }
#[inline]
pub fn position(&self) -> Point {
let tree = unsafe { self.tree.as_ref() };
tree
.store
.map_from_global(self.global_pos(), self.current_target, &tree.arena)
}
#[inline]
pub fn mouse_buttons(&self) -> MouseButtons { self.dispatch_info().mouse_buttons() }
#[inline]
pub fn button_num(&self) -> u32 { self.mouse_buttons().bits().count_ones() }
#[inline]
pub fn context(&mut self) -> EventCtx {
let WidgetTree { arena, store, wnd_ctx, .. } = unsafe { self.tree.as_ref() };
EventCtx {
id: self.current_target(),
arena,
store,
wnd_ctx,
info: self.dispatch_info_mut(),
}
}
pub fn next_focus(&self) {
let tree = unsafe { self.tree.as_ref() };
tree.wnd_ctx.next_focus(&tree.arena);
}
pub fn prev_focus(&self) {
let tree = unsafe { self.tree.as_ref() };
tree.wnd_ctx.prev_focus(&tree.arena);
}
fn dispatch_info_mut(&mut self) -> &mut DispatchInfo {
unsafe { self.info.as_mut() }
}
fn dispatch_info(&self) -> &DispatchInfo {
unsafe { self.info.as_ref() }
}
}
pub trait EventListener {
type Event: std::borrow::BorrowMut<EventCommon>;
fn dispatch(&self, event: &mut Self::Event);
}
impl std::fmt::Debug for EventCommon {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommonEvent")
.field("target", &self.target)
.field("current_target", &self.current_target)
.field("cancel_bubble", &self.cancel_bubble)
.finish()
}
}
impl EventCommon {
pub(crate) fn new(target: WidgetId, tree: &WidgetTree, info: &DispatchInfo) -> Self {
Self {
target,
current_target: target,
cancel_bubble: <_>::default(),
prevent_default: <_>::default(),
tree: NonNull::from(tree),
info: NonNull::from(info),
}
}
}