1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum MouseButton {
#[default]
Left,
Right,
Middle,
/// The mouse's back button, the first of its side buttons: on its release lurq goes back
/// ([`NavigationCommand::Back`](super::NavigationCommand)).
Back,
/// The mouse's forward button: on its release lurq goes forward.
Forward,
/// Another button, by the number the platform gives it.
Other(u8),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, crate::DevtoolsInspectable)]
pub struct MouseButtonMask(u8);
impl MouseButtonMask {
pub const NONE: Self = Self(0);
pub const LEFT: Self = Self(1 << 0);
pub const RIGHT: Self = Self(1 << 1);
pub const MIDDLE: Self = Self(1 << 2);
pub const OTHER: Self = Self(1 << 3);
pub const ANY: Self = Self(Self::LEFT.0 | Self::RIGHT.0 | Self::MIDDLE.0 | Self::OTHER.0);
pub const LMB: Self = Self::LEFT;
pub const RMB: Self = Self::RIGHT;
pub const MMB: Self = Self::MIDDLE;
pub fn contains_button(self, button: MouseButton) -> bool {
let bit = match button {
MouseButton::Left => Self::LEFT.0,
MouseButton::Right => Self::RIGHT.0,
MouseButton::Middle => Self::MIDDLE.0,
MouseButton::Back | MouseButton::Forward | MouseButton::Other(_) => Self::OTHER.0,
};
self.0 & bit != 0
}
}
impl Default for MouseButtonMask {
fn default() -> Self {
Self::ANY
}
}
impl std::ops::BitOr for MouseButtonMask {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl std::ops::BitOrAssign for MouseButtonMask {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
use super::EventControl;
use crate::core::NodeId;
/// What pressed: what a mouse event came from, as a browser's `pointerType`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum PointerKind {
/// A mouse or a trackpad, a key that clicks, and input an app or a tool synthesizes as the mouse's.
#[default]
Mouse,
/// A finger on a touch screen. Its own events are `on_touch_start` and the rest; the mouse events are what it
/// stands for as the pointer, and an element that must tell the two apart can.
Touch,
/// A pen on a touch screen: it taps and clicks as a finger does, and is told apart.
Pen,
}
#[derive(Debug, Clone)]
pub struct MouseEvent {
pub x: f32,
pub y: f32,
pub button: MouseButton,
pub kind: MouseEventKind,
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
/// Cmd on macOS, the Windows key on Windows, Super elsewhere.
pub meta: bool,
/// A key made this click, not the pointer: Enter on a focused link, Enter
/// or Space on a focused button. `button` is `Left` and `x`, `y` are 0, as
/// for a browser's keyboard click; the modifiers are the key's.
pub from_keyboard: bool,
/// What pressed: a mouse (or a key, or input synthesized as the mouse's), a finger, or a pen. One `on_click`
/// serves them all; a handler that must tell them apart reads it.
pub pointer_kind: PointerKind,
pub target_id: NodeId,
/// When the window was given the event, on its clock for presses (`Tree::set_press_clock`, or the wall clock):
/// what a component that tells a second press from a first measures by.
pub(crate) time: crate::time::Instant,
pub(crate) control: EventControl,
}
impl MouseEvent {
pub fn prevent_default(&self) {
self.control.prevent_default();
}
pub fn default_prevented(&self) -> bool {
self.control.default_prevented()
}
pub fn stop_propagation(&self) {
self.control.stop_propagation();
}
pub fn propagation_stopped(&self) -> bool {
self.control.propagation_stopped()
}
pub fn stop_immediate_propagation(&self) {
self.control.stop_immediate_propagation();
}
pub fn immediate_propagation_stopped(&self) -> bool {
self.control.immediate_propagation_stopped()
}
/// Moves the focus, once this press's handlers have run and what they changed is rendered, to the element of the
/// [focus group](crate::node::FocusGroup) around the focused element whose key (`.key(..)`) is `key`, and into
/// view, with no ring: as [`KeyboardEvent::focus_key`](super::KeyboardEvent::focus_key) does for a key. For a
/// press that takes away the element it pressed: a calendar's day of the month beside, after which the day is in
/// the month then shown. Nothing moves where the group is not there after the render, or has no element of that
/// key that takes the focus. The press's default is not prevented. The last request of a press's handlers wins.
pub fn focus_key(&self, key: impl Into<std::sync::Arc<str>>) {
self.control.request_focus_key(key.into());
}
}
#[derive(Debug, Clone, Copy, Default)]
pub enum MouseEventKind {
#[default]
Click,
Move,
Up,
Down,
DoubleClick,
}