use crate::component::ComponentId;
use crate::geometry::PointF;
use crate::prop::PropValue;
use crate::ui::Ui;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Modifiers {
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub meta: bool,
pub caps_lock: bool,
pub num_lock: bool,
pub scroll_lock: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PointerButton {
#[default]
Primary,
Secondary,
Middle,
Back,
Forward,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PointerKind {
#[default]
Mouse,
Touch,
Pen,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct PointerEvent {
pub position: PointF,
pub button: PointerButton,
pub kind: PointerKind,
pub modifiers: Modifiers,
pub click_count: u8,
pub pointer_id: u64,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ClickPointer {
pub position: PointF,
pub form_position: PointF,
pub button: PointerButton,
pub modifiers: Modifiers,
pub kind: PointerKind,
pub click_count: u8,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct WheelEvent {
pub position: PointF,
pub delta: PointF,
pub modifiers: Modifiers,
pub touch: bool,
pub lines: bool,
}
pub const WHEEL_LINE: f32 = 100.0;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScrollOptions {
pub wheel_step: f32,
pub smooth: bool,
pub smooth_ms: f32,
pub middle_button: bool,
}
impl Default for ScrollOptions {
fn default() -> Self {
Self { wheel_step: WHEEL_LINE, smooth: true, smooth_ms: 180.0, middle_button: cfg!(any(target_os = "windows", target_arch = "wasm32")) }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SwipeDirection {
#[default]
Left,
Right,
Up,
Down,
}
impl SwipeDirection {
pub fn name(self) -> &'static str {
match self {
SwipeDirection::Left => "left",
SwipeDirection::Right => "right",
SwipeDirection::Up => "up",
SwipeDirection::Down => "down",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SwipeEvent {
pub direction: SwipeDirection,
pub start: PointF,
pub distance: PointF,
pub velocity: PointF,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct GestureEvent {
pub phase: GesturePhase,
pub center: PointF,
pub scale: f32,
pub delta_scale: f32,
pub rotation: f32,
pub delta_rotation: f32,
pub pan: PointF,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum GesturePhase {
#[default]
Begin,
Update,
End,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Key {
#[default]
Unknown,
Enter,
Tab,
Escape,
Backspace,
Delete,
Insert,
Home,
End,
PageUp,
PageDown,
Left,
Right,
Up,
Down,
Space,
F(u8),
Char(char),
Shift,
Control,
Alt,
Meta,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct KeyEvent {
pub key: Key,
pub modifiers: Modifiers,
pub repeat: bool,
pub code: crate::keycode::KeyCode,
pub scancode: u32,
pub form: Option<ComponentId>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum EventArgs {
#[default]
None,
Pointer(PointerEvent),
Wheel(WheelEvent),
Gesture(GestureEvent),
Swipe(SwipeEvent),
Key(KeyEvent),
Text(String),
Value(PropValue),
Intercept(Box<Intercepted>),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Intercepted {
pub form: Option<crate::component::ComponentId>,
pub sender: Option<crate::component::ComponentId>,
pub event: String,
pub handler: String,
pub args: EventArgs,
}
impl EventArgs {
pub fn text(&self) -> Option<&str> {
match self {
EventArgs::Text(t) => Some(t),
EventArgs::Value(PropValue::String(t)) => Some(t),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PopupRequest {
pub at: crate::geometry::PointF,
pub source: crate::component::ComponentId,
}
pub struct EventCtx<'a> {
pub ui: &'a mut Ui,
pub id: ComponentId,
pub(crate) handled: bool,
}
impl<'a> EventCtx<'a> {
pub fn new(ui: &'a mut Ui, id: ComponentId) -> Self {
Self { ui, id, handled: false }
}
pub fn fire(&mut self, event: &str, args: EventArgs) {
self.ui.queue_event(self.id, event, args);
}
pub fn repaint(&mut self) {
self.ui.damage(self.id);
}
pub fn repaint_part(&mut self, rect: crate::RectF) {
self.ui.damage_part(self.id, rect);
}
pub fn scrolled(&mut self, rect: crate::RectF, delta: crate::PointF) {
self.ui.scroll_damage(self.id, rect, delta);
}
pub fn invalidate(&mut self) {
self.ui.invalidate_form(self.id);
}
pub fn request_focus(&mut self, id: ComponentId) {
self.ui.pending_focus = Some(id);
}
pub fn set_handled(&mut self) {
self.handled = true;
}
pub fn handled(&self) -> bool {
self.handled
}
}