blinc_platform_web 0.5.1

Blinc Web platform — HtmlCanvasElement integration, browser event conversion, and fetch-based asset loading for wasm32-unknown-unknown
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Browser input → `blinc_platform::InputEvent` conversion
//!
//! Every function in this module is pure (no `web-sys` dep) so it
//! compiles and unit-tests on every host. The wasm-bindgen runner module
//! reads `web_sys::MouseEvent` / `KeyboardEvent` / `WheelEvent` /
//! `TouchEvent` fields, then hands the primitive values
//! (button index, key string, dx/dy, …) to the helpers here.
//!
//! Splitting the conversion this way mirrors how
//! `blinc_platform_desktop::input` keeps winit out of its
//! `convert_*` signatures by passing in primitives where possible.
//!
//! # Key string mapping
//!
//! The W3C `KeyboardEvent.key` value is a string like `"ArrowLeft"`,
//! `"Enter"`, `"a"`, or `"F1"`. This module's [`convert_key_from_dom`]
//! consumes that string directly. The full set of named keys is
//! specified at <https://www.w3.org/TR/uievents-key/#named-key-attribute-values>.

use blinc_platform::{
    InputEvent, Key, KeyState, KeyboardEvent, Modifiers, MouseButton, MouseEvent,
};

// ===========================================================================
// Pointer / mouse buttons
// ===========================================================================

/// Convert a W3C [`MouseEvent.button`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button)
/// value to a Blinc [`MouseButton`].
///
/// The W3C button index is:
/// - `0` — primary (usually left)
/// - `1` — auxiliary (usually middle / wheel click)
/// - `2` — secondary (usually right)
/// - `3` — fourth (often "back")
/// - `4` — fifth (often "forward")
pub fn convert_mouse_button(button: i16) -> MouseButton {
    match button {
        0 => MouseButton::Left,
        1 => MouseButton::Middle,
        2 => MouseButton::Right,
        3 => MouseButton::Back,
        4 => MouseButton::Forward,
        n if n >= 0 => MouseButton::Other(n as u16),
        // Negative buttons indicate "no button" in some browsers; treat
        // as left-click for the rare cases where this leaks into our
        // event path.
        _ => MouseButton::Left,
    }
}

/// Pointer events expose `pointer.button` as `i32` rather than `i16`.
/// Convenience wrapper around [`convert_mouse_button`].
pub fn convert_pointer_button(button: i32) -> MouseButton {
    convert_mouse_button(button.clamp(i16::MIN as i32, i16::MAX as i32) as i16)
}

// ===========================================================================
// Modifiers
// ===========================================================================

/// Build a [`Modifiers`] struct from the four boolean fields a
/// `web_sys::MouseEvent` exposes (`shiftKey`, `ctrlKey`, `altKey`,
/// `metaKey`). Kept primitive so it's testable without `web-sys`.
pub fn modifiers_from_mouse(shift: bool, ctrl: bool, alt: bool, meta: bool) -> Modifiers {
    Modifiers {
        shift,
        ctrl,
        alt,
        meta,
    }
}

/// Same as [`modifiers_from_mouse`] but spelled differently for
/// keyboard events. Browsers expose the identical four booleans on
/// both event types — we keep separate names purely for call-site
/// readability.
pub fn modifiers_from_keyboard(shift: bool, ctrl: bool, alt: bool, meta: bool) -> Modifiers {
    Modifiers {
        shift,
        ctrl,
        alt,
        meta,
    }
}

// ===========================================================================
// Key string → Key enum
// ===========================================================================

/// Convert a W3C [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
/// string to a Blinc [`Key`].
///
/// The W3C `key` attribute returns either a "named key" (like `"Enter"`)
/// or a printable character (like `"a"`, `"A"`, `"!"`). Both forms are
/// handled. Unrecognised single characters fall through to
/// [`Key::Char`] so editor widgets can still observe them; unknown
/// named keys map to [`Key::Unknown`].
pub fn convert_key_from_dom(dom_key: &str) -> Key {
    // Fast path: empty string => Unknown.
    if dom_key.is_empty() {
        return Key::Unknown;
    }

    // Named keys come first because the named-key check is O(1) on
    // string comparison and we don't want "F1" misclassified as
    // `Key::Char('F')`.
    match dom_key {
        // Whitespace / control
        " " | "Spacebar" => Key::Space,
        "Enter" => Key::Enter,
        "Escape" | "Esc" => Key::Escape,
        "Backspace" => Key::Backspace,
        "Tab" => Key::Tab,
        "Delete" | "Del" => Key::Delete,
        "Insert" => Key::Insert,
        "Home" => Key::Home,
        "End" => Key::End,
        "PageUp" => Key::PageUp,
        "PageDown" => Key::PageDown,

        // Arrows
        "ArrowLeft" | "Left" => Key::Left,
        "ArrowRight" | "Right" => Key::Right,
        "ArrowUp" | "Up" => Key::Up,
        "ArrowDown" | "Down" => Key::Down,

        // Modifiers
        "Shift" => Key::Shift,
        "Control" => Key::Ctrl,
        "Alt" => Key::Alt,
        "Meta" | "OS" | "Hyper" | "Super" => Key::Meta,

        // Function keys
        "F1" => Key::F1,
        "F2" => Key::F2,
        "F3" => Key::F3,
        "F4" => Key::F4,
        "F5" => Key::F5,
        "F6" => Key::F6,
        "F7" => Key::F7,
        "F8" => Key::F8,
        "F9" => Key::F9,
        "F10" => Key::F10,
        "F11" => Key::F11,
        "F12" => Key::F12,

        // System
        "ContextMenu" => Key::Menu,
        "BrowserBack" | "GoBack" => Key::Back,

        // Single-character printable keys
        s => convert_printable_char(s),
    }
}

fn convert_printable_char(s: &str) -> Key {
    let mut chars = s.chars();
    let first = match chars.next() {
        Some(c) => c,
        None => return Key::Unknown,
    };
    // If there's a second character, this isn't a single-key event.
    // It could be:
    //   - a dead-key composition like "´a" — surface as Char(first)
    //     so editors can still observe something.
    //   - an unrecognised W3C named-key value like "Unidentified" or
    //     "AltGraph" — these are alphanumeric strings, NOT printable
    //     characters, and should map to Unknown rather than to
    //     Char of their leading letter.
    //
    // Heuristic: if the leading char is ASCII alphabetic, treat the
    // multi-char string as a missed named key. Anything else
    // (combining marks, IME compositions) we'll surface as the first
    // scalar.
    let extra = chars.next();
    if let Some(_extra_char) = extra {
        if first.is_ascii_alphabetic() {
            return Key::Unknown;
        }
        return Key::Char(first);
    }
    match first.to_ascii_uppercase() {
        'A' => Key::A,
        'B' => Key::B,
        'C' => Key::C,
        'D' => Key::D,
        'E' => Key::E,
        'F' => Key::F,
        'G' => Key::G,
        'H' => Key::H,
        'I' => Key::I,
        'J' => Key::J,
        'K' => Key::K,
        'L' => Key::L,
        'M' => Key::M,
        'N' => Key::N,
        'O' => Key::O,
        'P' => Key::P,
        'Q' => Key::Q,
        'R' => Key::R,
        'S' => Key::S,
        'T' => Key::T,
        'U' => Key::U,
        'V' => Key::V,
        'W' => Key::W,
        'X' => Key::X,
        'Y' => Key::Y,
        'Z' => Key::Z,
        '0' => Key::Num0,
        '1' => Key::Num1,
        '2' => Key::Num2,
        '3' => Key::Num3,
        '4' => Key::Num4,
        '5' => Key::Num5,
        '6' => Key::Num6,
        '7' => Key::Num7,
        '8' => Key::Num8,
        '9' => Key::Num9,
        '-' => Key::Minus,
        '=' => Key::Equals,
        '[' => Key::LeftBracket,
        ']' => Key::RightBracket,
        '\\' => Key::Backslash,
        ';' => Key::Semicolon,
        '\'' => Key::Quote,
        ',' => Key::Comma,
        '.' => Key::Period,
        '/' => Key::Slash,
        '`' => Key::Grave,
        _ => Key::Char(first),
    }
}

// ===========================================================================
// Builders for InputEvent — these are also pure
// ===========================================================================

/// Build a `Mouse(Moved)` event from canvas-local coordinates.
pub fn mouse_moved(x: f32, y: f32) -> InputEvent {
    InputEvent::Mouse(MouseEvent::Moved { x, y })
}

/// Build a `Mouse(ButtonPressed)` event from canvas-local coordinates.
pub fn mouse_pressed(button: MouseButton, x: f32, y: f32) -> InputEvent {
    InputEvent::Mouse(MouseEvent::ButtonPressed { button, x, y })
}

/// Build a `Mouse(ButtonReleased)` event from canvas-local coordinates.
pub fn mouse_released(button: MouseButton, x: f32, y: f32) -> InputEvent {
    InputEvent::Mouse(MouseEvent::ButtonReleased { button, x, y })
}

/// Build a keyboard event with state + modifiers.
pub fn keyboard_event(key: Key, state: KeyState, modifiers: Modifiers) -> InputEvent {
    InputEvent::Keyboard(KeyboardEvent {
        key,
        state,
        modifiers,
    })
}

// ===========================================================================
// Tests — every assertion in this file runs on the host platform.
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mouse_button_indices_match_w3c() {
        assert_eq!(convert_mouse_button(0), MouseButton::Left);
        assert_eq!(convert_mouse_button(1), MouseButton::Middle);
        assert_eq!(convert_mouse_button(2), MouseButton::Right);
        assert_eq!(convert_mouse_button(3), MouseButton::Back);
        assert_eq!(convert_mouse_button(4), MouseButton::Forward);
        assert_eq!(convert_mouse_button(5), MouseButton::Other(5));
    }

    #[test]
    fn mouse_button_negative_falls_back_to_left() {
        // Some browsers report -1 when no button is associated with
        // the event (e.g. mousemove). We never expect that to reach
        // `mousedown` / `mouseup` handling, but be defensive.
        assert_eq!(convert_mouse_button(-1), MouseButton::Left);
    }

    #[test]
    fn pointer_button_clamps() {
        assert_eq!(convert_pointer_button(0), MouseButton::Left);
        assert_eq!(convert_pointer_button(99_999), MouseButton::Other(32767));
    }

    #[test]
    fn modifiers_round_trip() {
        let m = modifiers_from_mouse(true, false, true, false);
        assert!(m.shift && !m.ctrl && m.alt && !m.meta);
        assert!(!m.is_empty());
    }

    #[test]
    fn arrows_map_to_arrow_keys() {
        assert_eq!(convert_key_from_dom("ArrowLeft"), Key::Left);
        assert_eq!(convert_key_from_dom("ArrowRight"), Key::Right);
        assert_eq!(convert_key_from_dom("ArrowUp"), Key::Up);
        assert_eq!(convert_key_from_dom("ArrowDown"), Key::Down);
        // Legacy short names still supported
        assert_eq!(convert_key_from_dom("Left"), Key::Left);
        assert_eq!(convert_key_from_dom("Up"), Key::Up);
    }

    #[test]
    fn function_keys_map_individually() {
        assert_eq!(convert_key_from_dom("F1"), Key::F1);
        assert_eq!(convert_key_from_dom("F12"), Key::F12);
        // F1 must NOT be misclassified as Key::F + Key::Num1.
        assert_ne!(convert_key_from_dom("F1"), Key::F);
    }

    #[test]
    fn whitespace_and_named_keys() {
        assert_eq!(convert_key_from_dom(" "), Key::Space);
        assert_eq!(convert_key_from_dom("Enter"), Key::Enter);
        assert_eq!(convert_key_from_dom("Escape"), Key::Escape);
        assert_eq!(convert_key_from_dom("Esc"), Key::Escape);
        assert_eq!(convert_key_from_dom("Tab"), Key::Tab);
        assert_eq!(convert_key_from_dom("Backspace"), Key::Backspace);
        assert_eq!(convert_key_from_dom("Delete"), Key::Delete);
        assert_eq!(convert_key_from_dom("Del"), Key::Delete);
        assert_eq!(convert_key_from_dom("Home"), Key::Home);
        assert_eq!(convert_key_from_dom("End"), Key::End);
        assert_eq!(convert_key_from_dom("PageUp"), Key::PageUp);
        assert_eq!(convert_key_from_dom("PageDown"), Key::PageDown);
    }

    #[test]
    fn modifier_aliases() {
        assert_eq!(convert_key_from_dom("Meta"), Key::Meta);
        assert_eq!(convert_key_from_dom("OS"), Key::Meta);
        assert_eq!(convert_key_from_dom("Super"), Key::Meta);
        assert_eq!(convert_key_from_dom("Control"), Key::Ctrl);
    }

    #[test]
    fn printable_lowercase_and_uppercase_collapse() {
        assert_eq!(convert_key_from_dom("a"), Key::A);
        assert_eq!(convert_key_from_dom("A"), Key::A);
        assert_eq!(convert_key_from_dom("z"), Key::Z);
        assert_eq!(convert_key_from_dom("Z"), Key::Z);
    }

    #[test]
    fn printable_digits() {
        for (s, k) in [("0", Key::Num0), ("5", Key::Num5), ("9", Key::Num9)] {
            assert_eq!(convert_key_from_dom(s), k);
        }
    }

    #[test]
    fn printable_punctuation() {
        assert_eq!(convert_key_from_dom("-"), Key::Minus);
        assert_eq!(convert_key_from_dom("="), Key::Equals);
        assert_eq!(convert_key_from_dom("["), Key::LeftBracket);
        assert_eq!(convert_key_from_dom("]"), Key::RightBracket);
        assert_eq!(convert_key_from_dom("\\"), Key::Backslash);
        assert_eq!(convert_key_from_dom(";"), Key::Semicolon);
        assert_eq!(convert_key_from_dom("'"), Key::Quote);
        assert_eq!(convert_key_from_dom(","), Key::Comma);
        assert_eq!(convert_key_from_dom("."), Key::Period);
        assert_eq!(convert_key_from_dom("/"), Key::Slash);
        assert_eq!(convert_key_from_dom("`"), Key::Grave);
    }

    #[test]
    fn unknown_named_key_falls_back_to_unknown() {
        assert_eq!(convert_key_from_dom("Unidentified"), Key::Unknown);
        assert_eq!(convert_key_from_dom("Compose"), Key::Unknown);
    }

    #[test]
    fn unknown_single_char_falls_back_to_char() {
        // Latin-1 characters that aren't in our explicit table should
        // still surface as Char so editor widgets can observe them.
        assert_eq!(convert_key_from_dom("é"), Key::Char('é'));
        assert_eq!(convert_key_from_dom("ñ"), Key::Char('ñ'));
    }

    #[test]
    fn dead_key_composition_falls_back_to_first_char() {
        // Some browsers report multi-codepoint combos for dead keys —
        // surface the first scalar rather than dropping the event.
        assert_eq!(convert_key_from_dom("´a"), Key::Char('´'));
    }

    #[test]
    fn empty_string_is_unknown() {
        assert_eq!(convert_key_from_dom(""), Key::Unknown);
    }

    #[test]
    fn input_event_builders_round_trip() {
        let p = mouse_pressed(MouseButton::Left, 12.5, 7.0);
        match p {
            InputEvent::Mouse(MouseEvent::ButtonPressed { button, x, y }) => {
                assert_eq!(button, MouseButton::Left);
                assert_eq!(x, 12.5);
                assert_eq!(y, 7.0);
            }
            _ => panic!("expected ButtonPressed"),
        }

        let r = mouse_released(MouseButton::Right, 0.0, 0.0);
        matches!(r, InputEvent::Mouse(MouseEvent::ButtonReleased { .. }));

        let m = mouse_moved(1.0, 2.0);
        matches!(m, InputEvent::Mouse(MouseEvent::Moved { .. }));

        let k = keyboard_event(
            Key::Enter,
            KeyState::Pressed,
            modifiers_from_keyboard(false, false, false, true),
        );
        match k {
            InputEvent::Keyboard(KeyboardEvent {
                key,
                state,
                modifiers,
            }) => {
                assert_eq!(key, Key::Enter);
                assert_eq!(state, KeyState::Pressed);
                assert!(modifiers.meta_only());
            }
            _ => panic!("expected Keyboard"),
        }
    }
}