use self::dispatcher::DispatchInfo;
use crate::{
builtin_widgets::BuiltinFlags,
context::{define_widget_context, WidgetCtx, WidgetCtxImpl},
prelude::AppCtx,
widget_tree::WidgetId,
window::{Window, WindowId},
};
use std::rc::Rc;
pub(crate) mod dispatcher;
mod pointers;
pub use pointers::*;
use ribir_geom::Point;
mod keyboard;
pub use keyboard::*;
mod character;
pub use character::*;
mod wheel;
pub use wheel::*;
mod ime_pre_edit;
pub use ime_pre_edit::*;
mod lifecycle;
pub use lifecycle::*;
pub(crate) mod focus_mgr;
mod listener_impl_helper;
define_widget_context!(
CommonEvent,
target: WidgetId,
propagation: bool,
prevent_default: bool
);
pub type FocusEvent = CommonEvent;
pub type FocusBubbleEvent = CommonEvent;
impl CommonEvent {
#[inline]
pub fn target(&self) -> WidgetId { self.target }
#[inline]
pub fn current_target(&self) -> WidgetId { self.id }
#[inline]
pub fn stop_propagation(&mut self) { self.propagation = false }
#[inline]
pub fn is_propagation(&self) -> bool { self.propagation }
#[inline]
pub fn prevent_default(&mut self) { self.prevent_default = true; }
#[inline]
pub(crate) fn is_prevent_default(&self) -> bool { self.prevent_default }
#[inline]
pub fn modifiers(&self) -> ModifiersState { self.pick_info(DispatchInfo::modifiers) }
pub fn with_shift_key(&self) -> bool { self.modifiers().shift_key() }
pub fn with_alt_key(&self) -> bool { self.modifiers().alt_key() }
pub fn with_ctrl_key(&self) -> bool { self.modifiers().control_key() }
pub fn with_logo_key(&self) -> bool { self.modifiers().super_key() }
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.pick_info(DispatchInfo::global_pos) }
#[inline]
pub fn position(&self) -> Point { self.map_from_global(self.global_pos()) }
#[inline]
pub fn mouse_buttons(&self) -> MouseButtons { self.pick_info(DispatchInfo::mouse_buttons) }
#[inline]
pub fn button_num(&self) -> u32 { self.mouse_buttons().bits().count_ones() }
}
pub enum Event {
Mounted(LifecycleEvent),
PerformedLayout(LifecycleEvent),
Disposed(LifecycleEvent),
PointerDown(PointerEvent),
PointerDownCapture(PointerEvent),
PointerUp(PointerEvent),
PointerUpCapture(PointerEvent),
PointerMove(PointerEvent),
PointerMoveCapture(PointerEvent),
PointerCancel(PointerEvent),
PointerEnter(PointerEvent),
PointerLeave(PointerEvent),
Tap(PointerEvent),
TapCapture(PointerEvent),
ImePreEdit(ImePreEditEvent),
ImePreEditCapture(ImePreEditEvent),
Wheel(WheelEvent),
WheelCapture(WheelEvent),
Chars(CharsEvent),
CharsCapture(CharsEvent),
KeyDown(KeyboardEvent),
KeyDownCapture(KeyboardEvent),
KeyUp(KeyboardEvent),
KeyUpCapture(KeyboardEvent),
Focus(FocusEvent),
Blur(FocusEvent),
FocusIn(FocusEvent),
FocusInCapture(FocusEvent),
FocusOut(FocusEvent),
FocusOutCapture(FocusEvent),
}
impl std::ops::Deref for Event {
type Target = CommonEvent;
fn deref(&self) -> &Self::Target {
match self {
Event::Mounted(e)
| Event::PerformedLayout(e)
| Event::Disposed(e)
| Event::Focus(e)
| Event::Blur(e)
| Event::FocusIn(e)
| Event::FocusInCapture(e)
| Event::FocusOut(e)
| Event::FocusOutCapture(e) => e,
Event::PointerDown(e)
| Event::PointerDownCapture(e)
| Event::PointerUp(e)
| Event::PointerUpCapture(e)
| Event::PointerMove(e)
| Event::PointerMoveCapture(e)
| Event::PointerCancel(e)
| Event::PointerEnter(e)
| Event::PointerLeave(e)
| Event::Tap(e)
| Event::TapCapture(e) => e,
Event::ImePreEdit(e) | Event::ImePreEditCapture(e) => e,
Event::Wheel(e) | Event::WheelCapture(e) => e,
Event::Chars(e) | Event::CharsCapture(e) => e,
Event::KeyDown(e) | Event::KeyDownCapture(e) | Event::KeyUp(e) | Event::KeyUpCapture(e) => e,
}
}
}
impl std::ops::DerefMut for Event {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
Event::Mounted(e)
| Event::PerformedLayout(e)
| Event::Disposed(e)
| Event::Focus(e)
| Event::Blur(e)
| Event::FocusIn(e)
| Event::FocusInCapture(e)
| Event::FocusOut(e)
| Event::FocusOutCapture(e) => e,
Event::PointerDown(e)
| Event::PointerDownCapture(e)
| Event::PointerUp(e)
| Event::PointerUpCapture(e)
| Event::PointerMove(e)
| Event::PointerMoveCapture(e)
| Event::PointerCancel(e)
| Event::PointerEnter(e)
| Event::PointerLeave(e)
| Event::Tap(e)
| Event::TapCapture(e) => e,
Event::ImePreEdit(e) | Event::ImePreEditCapture(e) => e,
Event::Wheel(e) | Event::WheelCapture(e) => e,
Event::Chars(e) | Event::CharsCapture(e) => e,
Event::KeyDown(e) | Event::KeyDownCapture(e) | Event::KeyUp(e) | Event::KeyUpCapture(e) => e,
}
}
}
impl Event {
pub fn flags(&self) -> BuiltinFlags {
match self {
Event::Mounted(_) | Event::PerformedLayout(_) | Event::Disposed(_) => BuiltinFlags::Lifecycle,
Event::PointerDown(_)
| Event::PointerDownCapture(_)
| Event::PointerUp(_)
| Event::PointerUpCapture(_)
| Event::PointerMove(_)
| Event::PointerMoveCapture(_)
| Event::PointerCancel(_)
| Event::PointerEnter(_)
| Event::PointerLeave(_)
| Event::Tap(_)
| Event::TapCapture(_) => BuiltinFlags::Pointer,
Event::Wheel(_) | Event::WheelCapture(_) => BuiltinFlags::Wheel,
Event::ImePreEdit(_)
| Event::ImePreEditCapture(_)
| Event::Chars(_)
| Event::CharsCapture(_)
| Event::KeyDown(_)
| Event::KeyDownCapture(_)
| Event::KeyUp(_)
| Event::KeyUpCapture(_) => BuiltinFlags::KeyBoard,
Event::Focus(_) | Event::Blur(_) => BuiltinFlags::Focus,
Event::FocusIn(_)
| Event::FocusInCapture(_)
| Event::FocusOut(_)
| Event::FocusOutCapture(_) => BuiltinFlags::FocusInOut,
}
}
}
impl std::fmt::Debug for CommonEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommonEvent")
.field("target", &self.id)
.field("current_target", &self.id)
.field("is_propagation", &self.propagation)
.finish()
}
}
impl CommonEvent {
pub(crate) fn new(target: WidgetId, wnd_id: WindowId) -> Self {
Self {
target,
wnd_id,
id: target,
propagation: true,
prevent_default: false,
}
}
pub(crate) fn set_current_target(&mut self, id: WidgetId) { self.id = id; }
fn pick_info<R>(&self, f: impl FnOnce(&DispatchInfo) -> R) -> R {
f(&self.current_wnd().dispatcher.borrow().info)
}
}