rust_widgets 2.9.3

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
438
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

use super::*;
use crate::compat::MiniToString;
use crate::event::Event;
#[test]
fn test_shortcut_parsing() {
    // `Ctrl` and `Cmd` both parse to PRIMARY: one declaration, every platform.
    let shortcut = Shortcut::from_string("Ctrl+A").unwrap();
    assert_eq!(shortcut.key, Key::A);
    assert!(shortcut.modifiers.contains(Modifiers::PRIMARY));
    assert!(!shortcut.modifiers.contains(Modifiers::ALT));
    assert_eq!(Shortcut::from_string("Cmd+A").unwrap(), shortcut);
    assert_eq!(Shortcut::from_string("Primary+A").unwrap(), shortcut);
    let shortcut = Shortcut::from_string("Alt+F4").unwrap();
    assert_eq!(shortcut.key, Key::F4);
    assert!(shortcut.modifiers.contains(Modifiers::ALT));
    let shortcut = Shortcut::from_string("Ctrl+Shift+S").unwrap();
    assert_eq!(shortcut.key, Key::S);
    assert!(shortcut.modifiers.contains(Modifiers::PRIMARY));
    assert!(shortcut.modifiers.contains(Modifiers::SHIFT));
    // "Meta"/"Win" stay a distinct, physical modifier.
    let shortcut = Shortcut::from_string("Meta+X").unwrap();
    assert!(shortcut.modifiers.contains(Modifiers::META));
    assert!(!shortcut.modifiers.contains(Modifiers::PRIMARY));
}
#[test]
fn test_shortcut_to_string() {
    // `format_shortcut` is the canonical, platform-independent form, and is an inverse of
    // `from_string`. A *physical* Control shortcut renders as `Control` so it is distinguishable
    // from the `Ctrl` input alias of PRIMARY (N-K-03).
    let shortcut = Shortcut::ctrl(Key::A);
    assert_eq!(shortcut.to_string(), "Control+A");
    let shortcut = Shortcut::new(Key::F1, Modifiers::CTRL | Modifiers::ALT);
    assert_eq!(shortcut.to_string(), "Control+Alt+F1");
    let shortcut = Shortcut::primary(Key::Z);
    assert_eq!(shortcut.to_string(), "Primary+Z");
}

/// Every representable key's canonical spelling must round-trip through
/// `from_string`. The page keys used to format as `PgUp`/`PgDn`, which the parser
/// did not accept, so a bound chord's own canonical spelling could not be parsed back.
#[test]
fn every_key_canonical_spelling_round_trips() {
    use Key::*;
    let keys = [
        None,
        A,
        B,
        C,
        D,
        E,
        F,
        G,
        H,
        I,
        J,
        K,
        L,
        M,
        N,
        O,
        P,
        Q,
        R,
        S,
        T,
        U,
        V,
        W,
        X,
        Y,
        Z,
        Num0,
        Num1,
        Num2,
        Num3,
        Num4,
        Num5,
        Num6,
        Num7,
        Num8,
        Num9,
        F1,
        F2,
        F3,
        F4,
        F5,
        F6,
        F7,
        F8,
        F9,
        F10,
        F11,
        F12,
        Escape,
        Tab,
        Enter,
        Space,
        Backspace,
        Delete,
        Insert,
        Home,
        End,
        PageUp,
        PageDown,
        Left,
        Right,
        Up,
        Down,
        Minus,
        Equals,
        LeftBracket,
        RightBracket,
        Semicolon,
        Quote,
        Comma,
        Period,
        Slash,
        Backslash,
    ];
    for key in keys {
        let spelling = key.format_key();
        assert_eq!(
            Key::from_string(spelling),
            Some(key),
            "canonical spelling {spelling:?} must round-trip",
        );
    }
}

/// Page keys format as `PageUp`/`PageDown` and parse back; the platform display
/// glyphs are display-only and are not accepted as canonical input.
#[test]
fn page_keys_round_trip_without_glyphs() {
    assert_eq!(Key::PageUp.format_key(), "PageUp");
    assert_eq!(Key::from_string("PageUp"), Some(Key::PageUp));
    assert_eq!(Key::PageDown.format_key(), "PageDown");
    assert_eq!(Key::from_string("PageDown"), Some(Key::PageDown));

    // A shortcut using a page key stringifies and parses back to itself.
    let shortcut = Shortcut::primary(Key::PageUp);
    assert_eq!(shortcut.to_string(), "Primary+PageUp");
    assert_eq!(Shortcut::from_string(&shortcut.to_string()), Some(shortcut));

    assert_eq!(Key::from_string("⇞"), None, "the page-up glyph is not canonical input");
    assert_eq!(Key::from_string("⇟"), None, "the page-down glyph is not canonical input");
}

/// N-K-02 / N-K-03: the canonical form round-trips for physical Control, and a second key token is
/// rejected rather than silently dropping the first.
#[test]
fn shortcut_canonical_form_round_trips_and_rejects_multiple_keys() {
    use crate::compat::format;
    use std::collections::HashMap;

    // Physical Control keeps its identity through format -> parse.
    for shortcut in [
        Shortcut::ctrl(Key::Z),
        Shortcut::ctrl_shift(Key::Z),
        Shortcut::ctrl_alt(Key::F1),
        Shortcut::primary(Key::Z),
        Shortcut::new(Key::A, Modifiers::CTRL | Modifiers::META),
    ] {
        let formatted = shortcut.to_string();
        let parsed = Shortcut::from_string(&formatted)
            .unwrap_or_else(|| panic!("{formatted:?} must parse back"));
        assert_eq!(parsed, shortcut, "{formatted:?} did not round-trip to an equal value");
        assert_eq!(
            format!("{parsed:?}"),
            format!("{shortcut:?}"),
            "Eq implies Hash, so Debug must match too"
        );
    }

    // The hash-identity regression is the point: a shortcut must find itself.
    let mut map: HashMap<Shortcut, u32> = HashMap::new();
    map.insert(Shortcut::ctrl(Key::Z), 1);
    let reparsed = Shortcut::from_string(&Shortcut::ctrl(Key::Z).to_string()).unwrap();
    assert_eq!(
        map.get(&reparsed).copied(),
        Some(1),
        "a reparsed shortcut must hash to the same key"
    );

    // A second key token is a malformed declaration, not a silent overwrite.
    assert_eq!(Shortcut::from_string("Ctrl+A+B"), None);
    assert_eq!(Shortcut::from_string("A+B"), None);
    // One key token still parses.
    assert!(Shortcut::from_string("Ctrl+A").is_some());
    assert!(Shortcut::from_string("Control+A").is_some());
    // `Ctrl` (input alias) and `Control` (physical) are distinct values.
    assert_ne!(Shortcut::from_string("Ctrl+A"), Shortcut::from_string("Control+A"));
}
#[test]
fn test_shortcut_manager_register() {
    let mut manager = ShortcutManager::new();
    assert!(manager.register("action1", Shortcut::ctrl(Key::A), "Action 1"));
    assert!(manager.has_action("action1"));
    assert!(manager.has_shortcut(&Shortcut::ctrl(Key::A)));
    assert!(manager.register("action1", Shortcut::ctrl(Key::A), "Action 1"));
    assert!(!manager.register("action2", Shortcut::ctrl(Key::A), "Action 2"));
}
#[test]
fn test_shortcut_manager_trigger() {
    let mut manager = ShortcutManager::new();
    manager.register("action1", Shortcut::ctrl(Key::A), "Action 1");
    assert!(manager.handle_key_event(Key::A, Modifiers::CTRL));
    assert!(!manager.handle_key_event(Key::B, Modifiers::CTRL));
    assert!(!manager.handle_key_event(Key::A, Modifiers::ALT));
}

#[test]
fn test_shortcut_manager_handles_framework_key_press_event() {
    let mut manager = ShortcutManager::new();
    manager.register("save", Shortcut::ctrl(Key::S), "Save");

    assert!(manager.handle_event(&Event::KeyPress { key: 83, modifiers: 2 }));
    assert!(manager.handle_event(&Event::KeyPress { key: 115, modifiers: 2 }));
    assert!(!manager.handle_event(&Event::KeyPress { key: 83, modifiers: 4 }));
    assert!(!manager.handle_event(&Event::KeyRelease { key: 83, modifiers: 2 }));
}

#[test]
fn test_shortcut_key_code_conversion_covers_navigation_keys() {
    assert_eq!(Key::from_key_code(37), Some(Key::Left));
    assert_eq!(Key::from_key_code(39), Some(Key::Right));
    assert_eq!(Key::from_key_code(13), Some(Key::Enter));
    assert_eq!(Key::from_key_code(9999), None);
}
#[test]
fn test_shortcut_manager_enable_disable() {
    let mut manager = ShortcutManager::new();
    manager.register("action1", Shortcut::ctrl(Key::A), "Action 1");
    assert!(manager.handle_key_event(Key::A, Modifiers::CTRL));
    manager.disable("action1");
    assert!(!manager.handle_key_event(Key::A, Modifiers::CTRL));
    manager.enable("action1");
    assert!(manager.handle_key_event(Key::A, Modifiers::CTRL));
}
#[test]
fn test_shortcut_conflict_detection() {
    let shortcut1 = Shortcut::ctrl(Key::A);
    let shortcut2 = Shortcut::ctrl(Key::A);
    let shortcut3 = Shortcut::alt(Key::A);
    assert!(shortcut1.conflicts_with(&shortcut2));
    assert!(!shortcut1.conflicts_with(&shortcut3));
}

/// `PRIMARY` renders as macOS glyphs on Mac and as spelled-out modifiers
/// elsewhere. Both branches are asserted on every host, so a change to either
/// convention is caught without a Windows or macOS machine.
#[test]
fn primary_shortcut_renders_per_platform() {
    use super::PlatformShortcutStyle::{Desktop, Mac};

    let undo = Shortcut::primary(Key::Z);
    assert_eq!(format_shortcut_for_platform(&undo, Mac), "⌘Z");
    assert_eq!(format_shortcut_for_platform(&undo, Desktop), "Ctrl+Z");

    let redo = Shortcut::primary_shift(Key::Z);
    assert_eq!(format_shortcut_for_platform(&redo, Mac), "⇧⌘Z");
    assert_eq!(format_shortcut_for_platform(&redo, Desktop), "Ctrl+Shift+Z");

    let save = Shortcut::primary(Key::S);
    assert_eq!(format_shortcut_for_platform(&save, Mac), "⌘S");
    assert_eq!(format_shortcut_for_platform(&save, Desktop), "Ctrl+S");
}

/// A physical-Control shortcut stays `Ctrl` on desktop and `⌃` on macOS, and is
/// not conflated with `PRIMARY`.
#[test]
fn physical_control_is_not_primary() {
    use super::PlatformShortcutStyle::{Desktop, Mac};

    let control_c = Shortcut::ctrl(Key::C);
    assert_eq!(format_shortcut_for_platform(&control_c, Desktop), "Ctrl+C");
    assert_eq!(format_shortcut_for_platform(&control_c, Mac), "⌃C");

    let primary_c = Shortcut::primary(Key::C);
    assert_ne!(primary_c, control_c);
}

/// macOS menus print arrow/enter keys as glyphs rather than words.
#[test]
fn mac_style_uses_glyphs_for_named_keys() {
    use super::PlatformShortcutStyle::{Desktop, Mac};

    let enter = Shortcut::new(Key::Enter, Modifiers::PRIMARY);
    assert_eq!(format_shortcut_for_platform(&enter, Mac), "⌘↩");
    assert_eq!(format_shortcut_for_platform(&enter, Desktop), "Ctrl+Enter");

    let up = Shortcut::new(Key::Up, Modifiers::ALT);
    assert_eq!(format_shortcut_for_platform(&up, Mac), "⌥↑");
    assert_eq!(format_shortcut_for_platform(&up, Desktop), "Alt+Up");
}

/// The style the library picks by default must match the host it is built for.
#[test]
fn current_style_matches_host() {
    // The compile-target mapping lives in `src/platform/` (principle #36); this
    // test only asserts that `current()` surfaces it faithfully.
    let expected = crate::platform::types::compile_target_shortcut_style();
    assert_eq!(PlatformShortcutStyle::current(), expected);
}

/// A `KeyPress` carrying the Meta/Command bit must match a `Primary` shortcut.
/// This is the link that makes typing Cmd+Z on macOS hit `Shortcut::primary`.
///
/// The Meta bit is the *macOS* primary accelerator. Under the desktop conventions
/// the primary accelerator is Control instead, and the lookup lifts a Control
/// press into `PRIMARY` to reach the same binding. The style is read at runtime
/// (principle #68: no `cfg(target_os)` outside `src/platform/`), so the expected
/// mapping is asserted per style rather than assuming one host's convention.
#[test]
fn command_modifier_bit_matches_primary_shortcut() {
    let mut manager = ShortcutManager::new();
    manager.register("undo", Shortcut::primary(Key::Z), "Undo");

    // Establish each link of the chain explicitly, so a failure names the step.
    assert_eq!(Key::from_key_code(90), Some(Key::Z));
    // Modifier bit 0b1000 is the Meta/Command slot. It lifts into PRIMARY so a
    // Command key event matches `Shortcut::primary`, and it also implies CTRL:
    // a widget that only looks at the control bit (as the editor's own key
    // handling does) still recognises `Cmd+C` as copy.
    let command = Modifiers::from_event_bits(0b1000);
    assert!(command.contains(Modifiers::PRIMARY));
    assert!(command.contains(Modifiers::CTRL));
    assert_eq!(command, Modifiers::PRIMARY | Modifiers::CTRL);

    // Shift+Command must not fire the unshifted command. This holds under both
    // conventions: the extra SHIFT bit survives normalisation either way.
    assert!(!manager.handle_event(&Event::KeyPress { key: 90, modifiers: 0b1001 }));

    match PlatformShortcutStyle::current() {
        // macOS: the Meta/Command bit *is* the primary accelerator.
        PlatformShortcutStyle::Mac => {
            assert!(manager.handle_event(&Event::KeyPress { key: 90, modifiers: 0b1000 }));
        }
        // Windows/Linux: Control is the primary accelerator, so a Control press is
        // lifted into PRIMARY and matches, while a bare Meta press is not this
        // platform's primary accelerator and must not fire the binding.
        PlatformShortcutStyle::Desktop => {
            assert!(manager.handle_event(&Event::KeyPress { key: 90, modifiers: 0b0010 }));
            assert!(!manager.handle_event(&Event::KeyPress { key: 90, modifiers: 0b1000 }));
        }
    }

    // A physical-Control key event always matches a `Shortcut::ctrl` binding.
    let mut control_manager = ShortcutManager::new();
    control_manager.register("physical", Shortcut::ctrl(Key::Z), "Control only");
    assert!(control_manager.handle_event(&Event::KeyPress { key: 90, modifiers: 0b0010 }));
}

/// W-201: a scoped binding shadows the global binding on the same chord while its scope is active,
/// and the two do not conflict at registration time.
#[test]
fn a_scoped_binding_shadows_the_global_one() {
    let mut manager = ShortcutManager::new();
    assert!(manager.register("global_save", Shortcut::primary(Key::S), "Global save"));
    assert!(
        manager.register_in("dialog", "dialog_save", Shortcut::primary(Key::S), "Dialog save"),
        "the same chord is allowed in a different scope"
    );

    assert_eq!(
        manager.action_for_key(Key::S, Modifiers::PRIMARY).as_deref(),
        Some("global_save"),
        "with no scope active only the global binding resolves"
    );

    manager.push_scope("dialog");
    assert_eq!(
        manager.action_for_key(Key::S, Modifiers::PRIMARY).as_deref(),
        Some("dialog_save"),
        "the innermost scope wins"
    );
    assert!(manager.has_scoped_shortcut("dialog", &Shortcut::primary(Key::S)));
    assert!(manager.has_shortcut(&Shortcut::primary(Key::S)), "the global view is untouched");

    manager.pop_scope();
    assert_eq!(
        manager.action_for_key(Key::S, Modifiers::PRIMARY).as_deref(),
        Some("global_save"),
        "popping the scope restores the global binding"
    );
    assert!(manager.unregister("dialog_save"));
    assert!(!manager.has_scoped_shortcut("dialog", &Shortcut::primary(Key::S)));
}

/// W-201: a modal scope shields every layer below it, including global bindings it does not
/// shadow itself.
#[test]
fn a_modal_scope_shields_the_layers_below() {
    let mut manager = ShortcutManager::new();
    assert!(manager.register("global_save", Shortcut::primary(Key::S), "Global save"));
    manager.push_modal_scope("dialog");

    assert_eq!(
        manager.action_for_key(Key::S, Modifiers::PRIMARY),
        None,
        "the modal dialog did not bind Ctrl+S, and it must not reach the global binding"
    );

    assert!(manager.register_in("dialog", "dialog_ok", Shortcut::primary(Key::Enter), "OK"));
    assert_eq!(
        manager.action_for_key(Key::Enter, Modifiers::PRIMARY).as_deref(),
        Some("dialog_ok")
    );

    manager.pop_scope();
    assert_eq!(
        manager.action_for_key(Key::S, Modifiers::PRIMARY).as_deref(),
        Some("global_save"),
        "closing the modal restores the layer below"
    );
}

/// W-201: conflicts are judged inside a scope, so two scopes may bind one chord to different
/// actions while a duplicate within one scope is still refused.
#[test]
fn shortcut_conflicts_are_scoped() {
    let mut manager = ShortcutManager::new();
    assert!(manager.register_in("window_a", "a_save", Shortcut::primary(Key::S), "A"));
    assert!(manager.register_in("window_b", "b_save", Shortcut::primary(Key::S), "B"));
    assert!(
        !manager.register_in("window_a", "a_other", Shortcut::primary(Key::S), "Other"),
        "a second action for one chord in the same scope is still a conflict"
    );
    assert_eq!(
        manager.action_for_key(Key::S, Modifiers::PRIMARY),
        None,
        "neither window is active, so no scoped binding resolves"
    );
    manager.push_scope("window_b");
    assert_eq!(manager.action_for_key(Key::S, Modifiers::PRIMARY).as_deref(), Some("b_save"));
}