Skip to main content

compose_rust/
input.rs

1//! What an event hands its handler, for the events whose payload is more than a value.
2//!
3//! These are the Host's types rather than an authoring layer's, because what they hold is
4//! decided by the wire: a key press and whether it was consumed, the paths a drop
5//! carried, the range a list asked for. Every runtime hands the same ones over.
6
7use crate::Key;
8use std::cell::Cell;
9use std::rc::Rc;
10
11/// A key-down event whose consumption state is shared with the Host boundary.
12#[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    /// Adds what the wire carries beyond the modifiers: whether a held key is repeating,
41    /// the physical key as a USB HID usage value, and the DOM `key` string.
42    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    /// True when the event comes from a held key repeating.
50    pub fn repeat(&self) -> bool {
51        self.repeat
52    }
53
54    /// The physical key as a USB HID usage value, the same on every layout.
55    pub fn code(&self) -> u32 {
56        self.code
57    }
58
59    /// The DOM `key` string, such as `"ArrowDown"` or `"a"`.
60    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/// The paths of the files a reader let go over a node.
94///
95/// Separated on the wire by a NUL, which is the one byte no path on any of the three
96/// desktops may contain. A newline would have been shorter to read and wrong: a file
97/// called "notes\nfor tuesday" is legal on two of them, and splitting on newlines would
98/// have turned one file into two.
99#[derive(Clone, Debug, PartialEq)]
100pub struct FileDrop {
101    paths: Vec<String>,
102}
103
104impl FileDrop {
105    /// Takes apart the one string the paths travelled in.
106    ///
107    /// Public because the separation is part of what this type promises, and a test that
108    /// could not build one could only check it through a window.
109    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    /// Every path that arrived, in the order the platform gave them.
120    pub fn paths(&self) -> &[String] {
121        &self.paths
122    }
123}
124
125/// The visible item range the Renderer asks the Host to materialise.
126#[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/// Which kinds of pointer event a node subscribes to, as the bit mask the wire carries.
147///
148/// The bit of a kind is `1 << (tag - 1)`, so `Move` is 1 and `ContextMenu` is 128. A mask
149/// of zero subscribes to nothing, and a node with no subscription gets no pointer input
150/// at all, which is what keeps a screen of plain containers free of it.
151#[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    /// Every kind.
165    pub const ALL: Self = Self(0xff);
166
167    /// The mask holding one kind.
168    pub const fn of(kind: crate::schema::PointerKind) -> Self {
169        Self(1 << (kind as u32 - 1))
170    }
171
172    /// This mask and `other` together.
173    pub const fn union(self, other: Self) -> Self {
174        Self(self.0 | other.0)
175    }
176
177    /// Whether `kind` is in the mask.
178    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
195/// The bit of each mouse button in `buttons` and `button`: the same numbers as the DOM's
196/// `buttons` and Compose's `PointerButtons`.
197pub 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/// One pointer event handed to a handler, with its consumption state shared with the
206/// Host boundary like [`KeyEvent`]'s.
207///
208/// Calling [`PointerEvent::consume`] keeps the event from reaching an ancestor's handler
209/// and from doing what the Renderer would otherwise do with it: a consumed `Wheel` does not
210/// scroll, a consumed `ContextMenu` shows no menu.
211#[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    /// Names one finger, pen or mouse across its events. A mouse is 1.
269    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    /// Distance from the node's left edge, in dp. Outside the node while it holds a capture.
278    pub fn x(&self) -> f32 {
279        self.x
280    }
281
282    /// Distance from the node's top edge, in dp.
283    pub fn y(&self) -> f32 {
284        self.y
285    }
286
287    /// The buttons held now, as a mask of [`pointer_button`] bits.
288    pub fn buttons(&self) -> u32 {
289        self.buttons
290    }
291
292    /// The button that caused this `Down` or `Up`, and 0 for any other kind.
293    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    /// Consecutive presses in the same place on a `Down` or `Up`: 1, 2, and so on.
314    pub fn click_count(&self) -> u32 {
315        self.click_count
316    }
317
318    /// Wheel travel, positive to the right. Zero for any other kind.
319    pub fn delta_x(&self) -> f32 {
320        self.delta_x
321    }
322
323    /// Wheel travel, positive downward.
324    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    /// When the OS produced the input, on the monotonic clock `render_frame` also uses.
333    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}