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    consumed: Rc<Cell<bool>>,
20}
21
22impl KeyEvent {
23    pub fn new(key: Key, shift_key: bool, ctrl_key: bool, alt_key: bool, meta_key: bool) -> Self {
24        Self {
25            key,
26            shift_key,
27            ctrl_key,
28            alt_key,
29            meta_key,
30            consumed: Rc::new(Cell::new(false)),
31        }
32    }
33
34    pub fn key(&self) -> Key {
35        self.key
36    }
37
38    pub fn shift_key(&self) -> bool {
39        self.shift_key
40    }
41
42    pub fn ctrl_key(&self) -> bool {
43        self.ctrl_key
44    }
45
46    pub fn alt_key(&self) -> bool {
47        self.alt_key
48    }
49
50    pub fn meta_key(&self) -> bool {
51        self.meta_key
52    }
53
54    pub fn consume(&self) {
55        self.consumed.set(true);
56    }
57
58    pub fn consumed(&self) -> bool {
59        self.consumed.get()
60    }
61}
62
63/// The paths of the files a reader let go over a node.
64///
65/// Separated on the wire by a NUL, which is the one byte no path on any of the three
66/// desktops may contain. A newline would have been shorter to read and wrong: a file
67/// called "notes\nfor tuesday" is legal on two of them, and splitting on newlines would
68/// have turned one file into two.
69#[derive(Clone, Debug, PartialEq)]
70pub struct FileDrop {
71    paths: Vec<String>,
72}
73
74impl FileDrop {
75    /// Takes apart the one string the paths travelled in.
76    ///
77    /// Public because the separation is part of what this type promises, and a test that
78    /// could not build one could only check it through a window.
79    pub fn new(joined: &str) -> Self {
80        Self {
81            paths: joined
82                .split('\0')
83                .filter(|path| !path.is_empty())
84                .map(str::to_owned)
85                .collect(),
86        }
87    }
88
89    /// Every path that arrived, in the order the platform gave them.
90    pub fn paths(&self) -> &[String] {
91        &self.paths
92    }
93}
94
95/// The visible item range the Renderer asks the Host to materialise.
96#[derive(Clone, Copy, Debug, Eq, PartialEq)]
97pub struct RangeRequest {
98    start: u32,
99    count: u32,
100}
101
102impl RangeRequest {
103    pub fn new(start: u32, count: u32) -> Self {
104        Self { start, count }
105    }
106
107    pub fn start(&self) -> usize {
108        self.start as usize
109    }
110
111    pub fn count(&self) -> usize {
112        self.count as usize
113    }
114}