1use crate::Key;
8use std::cell::Cell;
9use std::rc::Rc;
10
11#[derive(Clone, Debug)]
13pub struct KeyEvent {
14 key: Key,
15 shift_key: bool,
16 ctrl_key: bool,
17 alt_key: bool,
18 meta_key: bool,
19 repeat: bool,
20 code: u32,
21 text: String,
22 consumed: Rc<Cell<bool>>,
23}
24
25impl KeyEvent {
26 pub fn new(key: Key, shift_key: bool, ctrl_key: bool, alt_key: bool, meta_key: bool) -> Self {
27 Self {
28 key,
29 shift_key,
30 ctrl_key,
31 alt_key,
32 meta_key,
33 repeat: false,
34 code: 0,
35 text: String::new(),
36 consumed: Rc::new(Cell::new(false)),
37 }
38 }
39
40 pub fn with_details(mut self, repeat: bool, code: u32, text: &str) -> Self {
43 self.repeat = repeat;
44 self.code = code;
45 self.text = text.to_owned();
46 self
47 }
48
49 pub fn repeat(&self) -> bool {
51 self.repeat
52 }
53
54 pub fn code(&self) -> u32 {
56 self.code
57 }
58
59 pub fn text(&self) -> &str {
61 &self.text
62 }
63
64 pub fn key(&self) -> Key {
65 self.key
66 }
67
68 pub fn shift_key(&self) -> bool {
69 self.shift_key
70 }
71
72 pub fn ctrl_key(&self) -> bool {
73 self.ctrl_key
74 }
75
76 pub fn alt_key(&self) -> bool {
77 self.alt_key
78 }
79
80 pub fn meta_key(&self) -> bool {
81 self.meta_key
82 }
83
84 pub fn consume(&self) {
85 self.consumed.set(true);
86 }
87
88 pub fn consumed(&self) -> bool {
89 self.consumed.get()
90 }
91}
92
93#[derive(Clone, Debug, PartialEq)]
100pub struct FileDrop {
101 paths: Vec<String>,
102}
103
104impl FileDrop {
105 pub fn new(joined: &str) -> Self {
110 Self {
111 paths: joined
112 .split('\0')
113 .filter(|path| !path.is_empty())
114 .map(str::to_owned)
115 .collect(),
116 }
117 }
118
119 pub fn paths(&self) -> &[String] {
121 &self.paths
122 }
123}
124
125#[derive(Clone, Copy, Debug, Eq, PartialEq)]
127pub struct RangeRequest {
128 start: u32,
129 count: u32,
130}
131
132impl RangeRequest {
133 pub fn new(start: u32, count: u32) -> Self {
134 Self { start, count }
135 }
136
137 pub fn start(&self) -> usize {
138 self.start as usize
139 }
140
141 pub fn count(&self) -> usize {
142 self.count as usize
143 }
144}
145
146#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
152pub struct PointerKinds(pub u32);
153
154impl PointerKinds {
155 pub const NONE: Self = Self(0);
156 pub const MOVE: Self = Self::of(crate::schema::PointerKind::Move);
157 pub const ENTER: Self = Self::of(crate::schema::PointerKind::Enter);
158 pub const LEAVE: Self = Self::of(crate::schema::PointerKind::Leave);
159 pub const DOWN: Self = Self::of(crate::schema::PointerKind::Down);
160 pub const UP: Self = Self::of(crate::schema::PointerKind::Up);
161 pub const CANCEL: Self = Self::of(crate::schema::PointerKind::Cancel);
162 pub const WHEEL: Self = Self::of(crate::schema::PointerKind::Wheel);
163 pub const CONTEXT_MENU: Self = Self::of(crate::schema::PointerKind::ContextMenu);
164 pub const ALL: Self = Self(0xff);
166
167 pub const fn of(kind: crate::schema::PointerKind) -> Self {
169 Self(1 << (kind as u32 - 1))
170 }
171
172 pub const fn union(self, other: Self) -> Self {
174 Self(self.0 | other.0)
175 }
176
177 pub const fn contains(self, kind: crate::schema::PointerKind) -> bool {
179 self.0 & Self::of(kind).0 != 0
180 }
181
182 pub const fn bits(self) -> u32 {
183 self.0
184 }
185}
186
187impl std::ops::BitOr for PointerKinds {
188 type Output = Self;
189
190 fn bitor(self, other: Self) -> Self {
191 self.union(other)
192 }
193}
194
195pub mod pointer_button {
198 pub const PRIMARY: u32 = 1;
199 pub const SECONDARY: u32 = 2;
200 pub const TERTIARY: u32 = 4;
201 pub const BACK: u32 = 8;
202 pub const FORWARD: u32 = 16;
203}
204
205#[derive(Clone, Debug)]
212pub struct PointerEvent {
213 kind: crate::schema::PointerKind,
214 pointer_id: u32,
215 pointer_type: crate::schema::PointerType,
216 x: f32,
217 y: f32,
218 buttons: u32,
219 button: u32,
220 modifiers: u32,
221 click_count: u32,
222 delta_x: f32,
223 delta_y: f32,
224 delta_mode: crate::schema::WheelDeltaMode,
225 timestamp_nanos: u64,
226 consumed: Rc<Cell<bool>>,
227}
228
229impl PointerEvent {
230 #[allow(clippy::too_many_arguments)]
231 pub fn new(
232 kind: crate::schema::PointerKind,
233 pointer_id: u32,
234 pointer_type: crate::schema::PointerType,
235 x: f32,
236 y: f32,
237 buttons: u32,
238 button: u32,
239 modifiers: u32,
240 click_count: u32,
241 delta_x: f32,
242 delta_y: f32,
243 delta_mode: crate::schema::WheelDeltaMode,
244 timestamp_nanos: u64,
245 ) -> Self {
246 Self {
247 kind,
248 pointer_id,
249 pointer_type,
250 x,
251 y,
252 buttons,
253 button,
254 modifiers,
255 click_count,
256 delta_x,
257 delta_y,
258 delta_mode,
259 timestamp_nanos,
260 consumed: Rc::new(Cell::new(false)),
261 }
262 }
263
264 pub fn kind(&self) -> crate::schema::PointerKind {
265 self.kind
266 }
267
268 pub fn pointer_id(&self) -> u32 {
270 self.pointer_id
271 }
272
273 pub fn pointer_type(&self) -> crate::schema::PointerType {
274 self.pointer_type
275 }
276
277 pub fn x(&self) -> f32 {
279 self.x
280 }
281
282 pub fn y(&self) -> f32 {
284 self.y
285 }
286
287 pub fn buttons(&self) -> u32 {
289 self.buttons
290 }
291
292 pub fn button(&self) -> u32 {
294 self.button
295 }
296
297 pub fn shift_key(&self) -> bool {
298 self.modifiers & 1 != 0
299 }
300
301 pub fn ctrl_key(&self) -> bool {
302 self.modifiers & 2 != 0
303 }
304
305 pub fn alt_key(&self) -> bool {
306 self.modifiers & 4 != 0
307 }
308
309 pub fn meta_key(&self) -> bool {
310 self.modifiers & 8 != 0
311 }
312
313 pub fn click_count(&self) -> u32 {
315 self.click_count
316 }
317
318 pub fn delta_x(&self) -> f32 {
320 self.delta_x
321 }
322
323 pub fn delta_y(&self) -> f32 {
325 self.delta_y
326 }
327
328 pub fn delta_mode(&self) -> crate::schema::WheelDeltaMode {
329 self.delta_mode
330 }
331
332 pub fn timestamp_nanos(&self) -> u64 {
334 self.timestamp_nanos
335 }
336
337 pub fn consume(&self) {
338 self.consumed.set(true);
339 }
340
341 pub fn consumed(&self) -> bool {
342 self.consumed.get()
343 }
344}