madori 0.1.0

Madori (間取り) — GPU application framework for pleme-io apps
Documentation
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use serde::{Deserialize, Serialize};

/// Top-level application event.
#[derive(Debug, Clone)]
pub enum AppEvent {
    /// Window resized to new physical dimensions.
    Resized { width: u32, height: u32 },
    /// Window close requested.
    CloseRequested,
    /// Window gained or lost focus.
    Focused(bool),
    /// Keyboard input.
    Key(KeyEvent),
    /// Mouse input.
    Mouse(MouseEvent),
    /// IME pre-edit (composition) text update.
    Ime(ImeEvent),
    /// Redraw requested (vsync tick or explicit).
    RedrawRequested,
}

/// Actions the event handler can request from the framework.
#[derive(Debug, Clone, Default)]
pub struct EventResponse {
    /// Whether the event was consumed (prevents default handling).
    pub consumed: bool,
    /// Request the event loop to exit.
    pub exit: bool,
    /// Request a window title change.
    pub set_title: Option<String>,
    /// Request fullscreen toggle.
    pub toggle_fullscreen: bool,
    /// Request cursor visibility change (None = no change).
    pub set_cursor_visible: Option<bool>,
}

impl EventResponse {
    /// Create a response that consumes the event.
    #[must_use]
    pub fn consumed() -> Self {
        Self {
            consumed: true,
            ..Default::default()
        }
    }

    /// Create a response that does not consume the event.
    #[must_use]
    pub fn ignored() -> Self {
        Self::default()
    }
}

impl From<bool> for EventResponse {
    fn from(consumed: bool) -> Self {
        Self {
            consumed,
            ..Default::default()
        }
    }
}

/// IME (Input Method Editor) event for CJK/compose input.
#[derive(Debug, Clone)]
pub enum ImeEvent {
    /// IME is enabled.
    Enabled,
    /// Pre-edit text while composing (with optional cursor position).
    Preedit(String, Option<(usize, usize)>),
    /// Final committed text.
    Commit(String),
    /// IME is disabled.
    Disabled,
}

/// Keyboard event (press or release).
#[derive(Debug, Clone)]
pub struct KeyEvent {
    pub key: KeyCode,
    pub pressed: bool,
    pub modifiers: Modifiers,
    /// Text input if this produced a character.
    pub text: Option<String>,
}

/// Mouse event.
#[derive(Debug, Clone)]
pub enum MouseEvent {
    Moved {
        x: f64,
        y: f64,
    },
    Button {
        button: MouseButton,
        pressed: bool,
        x: f64,
        y: f64,
        modifiers: Modifiers,
    },
    Scroll {
        dx: f64,
        dy: f64,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MouseButton {
    Left,
    Right,
    Middle,
}

/// Active modifier keys.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct Modifiers {
    pub shift: bool,
    pub ctrl: bool,
    pub alt: bool,
    pub meta: bool,
}

/// Platform-independent key codes for common keys.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyCode {
    Char(char),
    Enter,
    Escape,
    Backspace,
    Delete,
    Tab,
    Up,
    Down,
    Left,
    Right,
    Home,
    End,
    PageUp,
    PageDown,
    F(u8),
    Space,
    /// Key not mapped to any known code.
    Unknown,
}

/// Convert winit keyboard events to our KeyCode.
impl KeyCode {
    #[must_use]
    pub fn from_winit(key: &winit::keyboard::Key) -> Self {
        use winit::keyboard::{Key as WKey, NamedKey};
        match key {
            WKey::Named(named) => match named {
                NamedKey::Enter => Self::Enter,
                NamedKey::Escape => Self::Escape,
                NamedKey::Backspace => Self::Backspace,
                NamedKey::Delete => Self::Delete,
                NamedKey::Tab => Self::Tab,
                NamedKey::ArrowUp => Self::Up,
                NamedKey::ArrowDown => Self::Down,
                NamedKey::ArrowLeft => Self::Left,
                NamedKey::ArrowRight => Self::Right,
                NamedKey::Home => Self::Home,
                NamedKey::End => Self::End,
                NamedKey::PageUp => Self::PageUp,
                NamedKey::PageDown => Self::PageDown,
                NamedKey::Space => Self::Space,
                NamedKey::F1 => Self::F(1),
                NamedKey::F2 => Self::F(2),
                NamedKey::F3 => Self::F(3),
                NamedKey::F4 => Self::F(4),
                NamedKey::F5 => Self::F(5),
                NamedKey::F6 => Self::F(6),
                NamedKey::F7 => Self::F(7),
                NamedKey::F8 => Self::F(8),
                NamedKey::F9 => Self::F(9),
                NamedKey::F10 => Self::F(10),
                NamedKey::F11 => Self::F(11),
                NamedKey::F12 => Self::F(12),
                _ => Self::Unknown,
            },
            WKey::Character(c) => {
                let mut chars = c.chars();
                match (chars.next(), chars.next()) {
                    (Some(ch), None) => Self::Char(ch),
                    _ => Self::Unknown,
                }
            }
            _ => Self::Unknown,
        }
    }
}

impl Modifiers {
    #[must_use]
    pub fn from_winit(state: &winit::keyboard::ModifiersState) -> Self {
        Self {
            shift: state.shift_key(),
            ctrl: state.control_key(),
            alt: state.alt_key(),
            meta: state.super_key(),
        }
    }

    #[must_use]
    pub fn any(&self) -> bool {
        self.shift || self.ctrl || self.alt || self.meta
    }
}

/// Input event combining key + mouse for consumers.
#[derive(Debug, Clone)]
pub enum InputEvent {
    Key(KeyEvent),
    Mouse(MouseEvent),
}

#[cfg(test)]
mod tests {
    use super::*;
    use winit::keyboard::{Key as WKey, NamedKey};

    #[test]
    fn modifiers_any() {
        let none = Modifiers::default();
        assert!(!none.any());

        let shift = Modifiers {
            shift: true,
            ..Default::default()
        };
        assert!(shift.any());
    }

    #[test]
    fn modifiers_any_covers_every_field() {
        // `any()` must short-circuit on each of the four fields. If a
        // future refactor (e.g. bitflags) drops a field from the OR, that
        // modifier would silently stop gating hotkeys — hard to notice.
        for modifier in [
            Modifiers { shift: true, ..Default::default() },
            Modifiers { ctrl: true, ..Default::default() },
            Modifiers { alt: true, ..Default::default() },
            Modifiers { meta: true, ..Default::default() },
        ] {
            assert!(modifier.any(), "any() false for {modifier:?}");
        }
    }

    #[test]
    fn modifiers_any_with_combinations() {
        let all = Modifiers { shift: true, ctrl: true, alt: true, meta: true };
        assert!(all.any());
        let ctrl_alt = Modifiers { ctrl: true, alt: true, ..Default::default() };
        assert!(ctrl_alt.any());
    }

    #[test]
    fn modifiers_serde_roundtrip() {
        // Modifiers participates in config serialization (hotkeys stored
        // as YAML). A rename or reorder would silently break user
        // configs — pin the field set via JSON round-trip.
        let original = Modifiers { shift: true, ctrl: false, alt: true, meta: false };
        let json = serde_json::to_string(&original).unwrap();
        assert!(json.contains("\"shift\":true"));
        assert!(json.contains("\"alt\":true"));
        let back: Modifiers = serde_json::from_str(&json).unwrap();
        assert_eq!(back.shift, original.shift);
        assert_eq!(back.ctrl, original.ctrl);
        assert_eq!(back.alt, original.alt);
        assert_eq!(back.meta, original.meta);
    }

    #[test]
    fn key_code_char() {
        let k = KeyCode::Char('a');
        assert_eq!(k, KeyCode::Char('a'));
        assert_ne!(k, KeyCode::Char('b'));
    }

    #[test]
    fn key_code_from_winit_named_keys() {
        // Exhaustive table — if a NamedKey arm gets removed or
        // re-ordered silently (e.g. someone deletes ArrowUp thinking
        // it's unused), the paired app will stop responding to that key
        // and the user has no clue why.
        let cases: &[(WKey, KeyCode)] = &[
            (WKey::Named(NamedKey::Enter), KeyCode::Enter),
            (WKey::Named(NamedKey::Escape), KeyCode::Escape),
            (WKey::Named(NamedKey::Backspace), KeyCode::Backspace),
            (WKey::Named(NamedKey::Delete), KeyCode::Delete),
            (WKey::Named(NamedKey::Tab), KeyCode::Tab),
            (WKey::Named(NamedKey::ArrowUp), KeyCode::Up),
            (WKey::Named(NamedKey::ArrowDown), KeyCode::Down),
            (WKey::Named(NamedKey::ArrowLeft), KeyCode::Left),
            (WKey::Named(NamedKey::ArrowRight), KeyCode::Right),
            (WKey::Named(NamedKey::Home), KeyCode::Home),
            (WKey::Named(NamedKey::End), KeyCode::End),
            (WKey::Named(NamedKey::PageUp), KeyCode::PageUp),
            (WKey::Named(NamedKey::PageDown), KeyCode::PageDown),
            (WKey::Named(NamedKey::Space), KeyCode::Space),
        ];
        for (input, expected) in cases {
            assert_eq!(KeyCode::from_winit(input), *expected, "for {input:?}");
        }
    }

    #[test]
    fn key_code_from_winit_function_keys() {
        // F1..F12 all map to the same KeyCode::F(n) shape. Consumers
        // pattern-match `F(1)..=F(12)` so the numeric payload must be
        // exact; a typo like `NamedKey::F3 => Self::F(2)` would route
        // shortcuts to the wrong slot.
        let fkeys = [
            (NamedKey::F1, 1u8), (NamedKey::F2, 2), (NamedKey::F3, 3),
            (NamedKey::F4, 4), (NamedKey::F5, 5), (NamedKey::F6, 6),
            (NamedKey::F7, 7), (NamedKey::F8, 8), (NamedKey::F9, 9),
            (NamedKey::F10, 10), (NamedKey::F11, 11), (NamedKey::F12, 12),
        ];
        for (named, n) in fkeys {
            let got = KeyCode::from_winit(&WKey::Named(named));
            assert_eq!(got, KeyCode::F(n), "F-key mapped wrong: {named:?}");
        }
    }

    #[test]
    fn key_code_from_winit_character_single() {
        // `WKey::Character` with a single-code-point string becomes
        // `KeyCode::Char`. The parser takes two `chars.next()` calls to
        // assert there is no second char — regress this and we'd accept
        // multi-char strings as single chars.
        let k: winit::keyboard::SmolStr = "q".into();
        let got = KeyCode::from_winit(&WKey::Character(k));
        assert_eq!(got, KeyCode::Char('q'));
    }

    #[test]
    fn key_code_from_winit_character_multi_char() {
        // Dead-key compositions and some IME events produce
        // multi-char strings. We must map those to Unknown, not panic
        // and not take the first char silently (would lose input).
        let k: winit::keyboard::SmolStr = "ab".into();
        let got = KeyCode::from_winit(&WKey::Character(k));
        assert_eq!(got, KeyCode::Unknown);
    }

    #[test]
    fn key_code_from_winit_character_empty() {
        // Empty SmolStr — shouldn't occur in practice but the match arm
        // (None, None) needs to resolve to Unknown, not some default.
        let k: winit::keyboard::SmolStr = "".into();
        let got = KeyCode::from_winit(&WKey::Character(k));
        assert_eq!(got, KeyCode::Unknown);
    }

    #[test]
    fn key_code_from_winit_unmapped_named_is_unknown() {
        // Unmapped NamedKey variants fall into the catch-all `_ =>
        // Unknown` arm. CapsLock is intentionally unmapped — if someone
        // adds it without updating this test, we'll see the new mapping.
        let got = KeyCode::from_winit(&WKey::Named(NamedKey::CapsLock));
        assert_eq!(got, KeyCode::Unknown);
    }

    #[test]
    fn event_response_consumed_sets_only_consumed() {
        let r = EventResponse::consumed();
        assert!(r.consumed);
        assert!(!r.exit);
        assert!(r.set_title.is_none());
        assert!(!r.toggle_fullscreen);
        assert!(r.set_cursor_visible.is_none());
    }

    #[test]
    fn event_response_ignored_is_default() {
        let r = EventResponse::ignored();
        assert!(!r.consumed);
        assert!(!r.exit);
        assert!(r.set_title.is_none());
        assert!(!r.toggle_fullscreen);
        assert!(r.set_cursor_visible.is_none());
    }

    #[test]
    fn event_response_from_bool() {
        // `From<bool>` lets handlers write `return true.into()` or
        // similar. If someone drops the impl, ergonomic call sites
        // elsewhere stop compiling — this pins the shape.
        let consumed: EventResponse = true.into();
        assert!(consumed.consumed);
        assert!(!consumed.exit);

        let ignored: EventResponse = false.into();
        assert!(!ignored.consumed);
    }

    #[test]
    fn event_response_default_matches_ignored() {
        // Default and ignored() are intentionally identical — used at
        // thousands of call sites as `EventResponse::default()`. A
        // future refactor that changes one without the other would
        // produce subtly wrong handlers.
        let d = EventResponse::default();
        let i = EventResponse::ignored();
        assert_eq!(d.consumed, i.consumed);
        assert_eq!(d.exit, i.exit);
        assert_eq!(d.set_title, i.set_title);
        assert_eq!(d.toggle_fullscreen, i.toggle_fullscreen);
        assert_eq!(d.set_cursor_visible, i.set_cursor_visible);
    }

    #[test]
    fn modifiers_default_is_all_false() {
        // Modifiers::default() is used as the starting point for every
        // synthetic KeyEvent in tests + replay infra. All four fields
        // must start false or synthetic events arrive with phantom
        // modifiers set.
        let m = Modifiers::default();
        assert!(!m.shift);
        assert!(!m.ctrl);
        assert!(!m.alt);
        assert!(!m.meta);
    }
}