rust_widgets 1.1.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 60+ 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Integration tests for the Wayland backend.
//!
//! These tests verify platform creation, basic widget lifecycle,
//! clipboard roundtrip, list data methods, dialog creation, and
//! extended control creation for the Wayland platform backend.

use crate::platform::wayland::WaylandPlatform;
use crate::platform::Platform;

/// With a live compositor on `WAYLAND_DISPLAY`, creating a window must take the
/// *native* path: connect to the compositor, bind `wl_compositor` and
/// `xdg_wm_base`, and create a real `xdg_toplevel` surface.
///
/// This is the automated half of the "Wayland compositor interaction" gap: a
/// state-only backend never populates `native_session`, so asserting that it is
/// `Some` after `create_window` proves the protocol path actually ran rather
/// than silently falling back.
///
/// The test is opt-in — it skips (with a notice) when no compositor is present,
/// so a headless CI runner without weston does not fail. To run it against a
/// real compositor use `tools/run_wayland_compositor_tests.sh`, which fetches
/// and/or installs weston and drives both this test and its negative control.
#[cfg(all(feature = "wayland-native", target_os = "linux"))]
#[test]
fn native_session_binds_live_compositor() {
    use crate::core::PlatformFamily;
    use crate::platform::Platform as _;

    // No compositor advertised → nothing to verify here; skip rather than fail.
    if !compositor_advertised() {
        eprintln!("skipping: WAYLAND_DISPLAY is not set (no compositor to bind)");
        return;
    }

    let backend = WaylandPlatform::new();
    backend.init();
    assert_eq!(backend.family(), PlatformFamily::Desktop);

    let window = backend.create_window("NativeProbe", 0, 0, 320, 200);
    assert!(window > 0, "window should be created");

    // The state handle alone does not prove the native path; the session does.
    let guard = backend.native_session.lock().expect("native_session mutex poisoned");
    let session = guard.as_ref().expect(
        "native_session must be Some after create_window with a live compositor \
         (state-only fallback would leave it None)",
    );

    // Both globals must have been bound during the registry roundtrip.
    assert!(
        session.state.compositor.is_some(),
        "wl_compositor must be bound from the live compositor"
    );
    assert!(
        session.state.xdg_wm_base.is_some(),
        "xdg_wm_base must be bound from the live compositor"
    );

    eprintln!(
        "bound live compositor: wl_compositor=yes xdg_wm_base=yes dpi_scale={}",
        session.state.dpi_scale
    );
}

/// Whether the environment advertises a usable compositor.
///
/// An *empty* `WAYLAND_DISPLAY` does not name a socket, so it is treated the
/// same as an unset one — this lets the negative control run under
/// `WAYLAND_DISPLAY=` instead of being silently skipped.
#[cfg(all(feature = "wayland-native", target_os = "linux"))]
fn compositor_advertised() -> bool {
    matches!(std::env::var("WAYLAND_DISPLAY"), Ok(v) if !v.is_empty())
}

/// Without a compositor the backend must degrade to state-only and leave
/// `native_session` empty — this is the negative control for the test above.
#[cfg(all(feature = "wayland-native", target_os = "linux"))]
#[test]
fn native_session_stays_empty_without_compositor() {
    use crate::platform::Platform as _;

    if compositor_advertised() {
        eprintln!("skipping: a compositor is advertised, cannot test the fallback");
        return;
    }

    let backend = WaylandPlatform::new();
    backend.init();
    let window = backend.create_window("Fallback", 0, 0, 200, 120);
    assert!(window > 0, "state-only window should still be created");

    let guard = backend.native_session.lock().expect("native_session mutex poisoned");
    assert!(guard.is_none(), "native_session must stay empty when there is no compositor");
}

/// The event loop must observe `quit()` promptly even with no compositor: the
/// fd-based loop falls back to a bounded idle wait when there is no native
/// session, so `run()` cannot hang. This guards the quit-latency contract.
#[test]
fn run_exits_promptly_on_quit_without_compositor() {
    use std::sync::Arc;
    use std::time::{Duration, Instant};

    let backend = Arc::new(WaylandPlatform::new());
    backend.init();

    let runner = Arc::clone(&backend);
    let handle = std::thread::spawn(move || runner.run());

    // Let the loop enter its wait state, then quit from this thread.
    std::thread::sleep(Duration::from_millis(50));
    let started = Instant::now();
    backend.quit();

    // The loop re-checks the flag at least once per idle timeout, so joining
    // must complete well within a generous bound.
    let mut finished = false;
    for _ in 0..100 {
        if handle.is_finished() {
            finished = true;
            break;
        }
        std::thread::sleep(Duration::from_millis(20));
    }
    let elapsed = started.elapsed();
    if !finished {
        // Avoid a hung test process if the contract regressed.
        panic!("run() did not exit within 2s of quit() (elapsed {elapsed:?})");
    }
    handle.join().expect("event loop thread panicked");
    assert!(elapsed < Duration::from_millis(500), "quit() took too long: {elapsed:?}");
}

#[test]
fn platform_creates_and_runs() {
    let backend = WaylandPlatform::new();
    backend.init();
    assert_eq!(backend.backend_name(), "wayland");

    // Create a window and basic widgets.
    let window = backend.create_window("TestWindow", 50, 50, 400, 300);
    assert!(window > 0, "Window should be created");

    let button = backend.create_button(window, "Click", 10, 10, 80, 24);
    assert!(button > 0, "Button should be created");

    let label = backend.create_label(window, "Hello", 10, 40, 80, 24);
    assert!(label > 0, "Label should be created");

    let checkbox = backend.create_checkbox(window, "Check", 10, 70, 80, 24);
    assert!(checkbox > 0, "Checkbox should be created");

    let line_edit = backend.create_line_edit(window, "edit", 10, 100, 160, 24);
    assert!(line_edit > 0, "LineEdit should be created");

    let radio = backend.create_radio_button(window, "Radio", 10, 130, 80, 24);
    assert!(radio > 0, "RadioButton should be created");

    let slider = backend.create_slider(window, 10, 160, 200, 24);
    assert!(slider > 0, "Slider should be created");

    let progress = backend.create_progress_bar(window, 10, 190, 200, 24);
    assert!(progress > 0, "ProgressBar should be created");

    let combo = backend.create_combo_box(window, 10, 220, 140, 24);
    assert!(combo > 0, "ComboBox should be created");

    let list_box = backend.create_list_box(window, 10, 250, 140, 80);
    assert!(list_box > 0, "ListBox should be created");
}

#[test]
fn widget_lifecycle() {
    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("Lifecycle", 0, 0, 200, 120);
    assert!(window > 0, "Window should be created");

    let button = backend.create_button(window, "btn", 10, 10, 80, 24);
    assert!(button > 0, "Button should be created");

    // Text roundtrip.
    backend.set_widget_text(button, "updated");
    assert_eq!(backend.get_widget_text(button), "updated");

    // Show/hide roundtrip.
    backend.show_widget(button);
    assert!(backend.is_widget_visible(button), "Button should be visible after show");

    backend.hide_widget(button);
    assert!(!backend.is_widget_visible(button), "Button should be hidden after hide");

    // Enable/disable roundtrip.
    backend.set_widget_enabled(button, false);
    assert!(!backend.is_widget_enabled(button), "Button should be disabled");

    backend.set_widget_enabled(button, true);
    assert!(backend.is_widget_enabled(button), "Button should be enabled");

    // Geometry update.
    backend.set_widget_geometry(button, 20, 20, 120, 32);

    // Window-level lifecycle.
    backend.set_widget_text(window, "UpdatedTitle");
    assert_eq!(backend.get_widget_text(window), "UpdatedTitle");

    backend.show_widget(window);
    assert!(backend.is_widget_visible(window), "Window should be visible");

    backend.hide_widget(window);
    assert!(!backend.is_widget_visible(window), "Window should be hidden");
}

#[test]
fn clipboard_roundtrip() {
    let backend = WaylandPlatform::new();
    backend.init();

    // Create a window so the backend is fully initialised.
    let _window = backend.create_window("ClipTest", 0, 0, 200, 120);

    // Clipboard set/get roundtrip.
    assert!(backend.set_clipboard_text("wayland_clip_test"), "Should set clipboard text");
    assert_eq!(backend.get_clipboard_text(), "wayland_clip_test", "Clipboard text should match");

    // Overwrite with new content.
    assert!(backend.set_clipboard_text("updated_clip"), "Should overwrite clipboard");
    assert_eq!(backend.get_clipboard_text(), "updated_clip", "Updated clipboard text should match");

    // Empty string.
    assert!(backend.set_clipboard_text(""), "Should set empty clipboard");
    assert_eq!(backend.get_clipboard_text(), "", "Empty clipboard should match");
}

#[test]
fn combo_box_data_methods() {
    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("ComboTest", 0, 0, 200, 120);
    let combo = backend.create_combo_box(window, 10, 10, 140, 24);
    assert!(combo > 0, "ComboBox should be created");

    // Add items.
    assert!(backend.combo_box_add_item(combo, "Item A"), "Should add Item A");
    assert!(backend.combo_box_add_item(combo, "Item B"), "Should add Item B");
    assert!(backend.combo_box_add_item(combo, "Item C"), "Should add Item C");

    assert_eq!(backend.combo_box_item_count(combo), 3);

    // Item text retrieval.
    assert_eq!(backend.combo_box_item_text(combo, 0), Some("Item A".to_string()));
    assert_eq!(backend.combo_box_item_text(combo, 1), Some("Item B".to_string()));
    assert_eq!(backend.combo_box_item_text(combo, 2), Some("Item C".to_string()));
    assert_eq!(backend.combo_box_item_text(combo, 5), None);

    // Set and get current index.
    assert!(backend.combo_box_set_current_index(combo, 1), "Should set index 1");
    assert_eq!(backend.combo_box_current_index(combo), Some(1));

    // Clear items.
    assert!(backend.combo_box_clear_items(combo), "Should clear items");
    assert_eq!(backend.combo_box_item_count(combo), 0);
    assert_eq!(backend.combo_box_current_index(combo), None);
}

#[test]
fn list_box_data_methods() {
    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("ListBoxTest", 0, 0, 200, 200);
    let list_box = backend.create_list_box(window, 10, 10, 140, 80);
    assert!(list_box > 0, "ListBox should be created");

    // Add items.
    assert!(backend.list_box_add_item(list_box, "Item 1"), "Should add Item 1");
    assert!(backend.list_box_add_item(list_box, "Item 2"), "Should add Item 2");
    assert!(backend.list_box_add_item(list_box, "Item 3"), "Should add Item 3");

    assert_eq!(backend.list_box_item_count(list_box), 3);

    // Item text retrieval.
    assert_eq!(backend.list_box_item_text(list_box, 0), Some("Item 1".to_string()));
    assert_eq!(backend.list_box_item_text(list_box, 1), Some("Item 2".to_string()));
    assert_eq!(backend.list_box_item_text(list_box, 2), Some("Item 3".to_string()));

    // Set and get current index.
    assert!(backend.list_box_set_current_index(list_box, 1), "Should set index 1");
    assert_eq!(backend.list_box_current_index(list_box), Some(1));

    // Remove item at index 0 — remaining: [Item 2, Item 3], current should shift.
    assert!(backend.list_box_remove_item(list_box, 0), "Should remove Item 1");
    assert_eq!(backend.list_box_item_count(list_box), 2);
    assert_eq!(backend.list_box_item_text(list_box, 0), Some("Item 2".to_string()));
    // Current index adjusts from 1 to 0 after removal.
    assert_eq!(
        backend.list_box_current_index(list_box),
        Some(0),
        "Current index should adjust after removal"
    );

    // Clear items.
    assert!(backend.list_box_clear_items(list_box), "Should clear items");
    assert_eq!(backend.list_box_item_count(list_box), 0);
    assert_eq!(backend.list_box_current_index(list_box), None);
}

#[test]
fn menu_system() {
    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("MenuTest", 0, 0, 400, 300);
    let menu_bar = backend.create_menu_bar(window, 0, 0, 400, 24);
    assert!(menu_bar > 0, "MenuBar should be created");

    let file_menu = backend.create_menu(menu_bar, "File", 0, 24, 100, 24);
    assert!(file_menu > 0, "File menu should be created");

    let new_item = backend.menu_add_item(file_menu, "New", Some("Ctrl+N"));
    assert!(new_item > 0, "New menu item should be created");

    let open_item = backend.menu_add_item(file_menu, "Open", Some("Ctrl+O"));
    assert!(open_item > 0, "Open menu item should be created");

    let quit_item = backend.menu_add_item(file_menu, "Quit", Some("Ctrl+Q"));
    assert!(quit_item > 0, "Quit menu item should be created");

    assert!(
        backend.attach_menu_bar_to_window(window, menu_bar),
        "Should attach menu bar to window"
    );

    // Inject and poll menu trigger.
    assert!(backend.inject_menu_trigger(open_item), "Should inject menu trigger");
    assert_eq!(backend.poll_menu_triggered(), Some(open_item), "Should poll open item");

    // Second trigger.
    assert!(backend.inject_menu_trigger(quit_item), "Should inject quit trigger");
    assert_eq!(backend.poll_menu_triggered(), Some(quit_item), "Should poll quit item");
}

#[test]
fn widget_trigger_events() {
    use crate::platform::WidgetTriggerKind;

    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("TriggerTest", 0, 0, 200, 120);
    let button = backend.create_button(window, "Click", 10, 10, 80, 24);

    assert!(
        backend.inject_widget_trigger_event(button, WidgetTriggerKind::Clicked),
        "Should inject click event"
    );

    let event = backend.poll_widget_trigger_event();
    assert!(event.is_some(), "Should poll trigger event");
    assert_eq!(event.unwrap().widget_id, button);
    assert_eq!(event.unwrap().kind, WidgetTriggerKind::Clicked, "Should match Clicked kind");
}

#[test]
fn dialog_and_extended_controls() {
    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("DialogTest", 0, 0, 500, 400);

    // Dialogs.
    let msg_box = backend.create_message_box(window, "Title", "Hello", 10, 10, 300, 150);
    assert!(msg_box > 0, "MessageBox should be created");

    let file_dlg = backend.create_file_dialog(window, 10, 170, 400, 300);
    assert!(file_dlg > 0, "FileDialog should be created");

    let color_dlg = backend.create_color_dialog(window, 10, 10, 300, 300);
    assert!(color_dlg > 0, "ColorDialog should be created");

    let font_dlg = backend.create_font_dialog(window, 10, 10, 300, 300);
    assert!(font_dlg > 0, "FontDialog should be created");

    // Extended controls.
    let spin = backend.create_spin_box(window, 10, 10, 80, 24);
    assert!(spin > 0, "SpinBox should be created");

    let list_view = backend.create_list_view(window, 10, 40, 200, 150);
    assert!(list_view > 0, "ListView should be created");

    let scroll = backend.create_scroll_area(window, 10, 200, 200, 150);
    assert!(scroll > 0, "ScrollArea should be created");
}

#[test]
fn invalid_parent_and_kind_validation() {
    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("Validation", 0, 0, 400, 300);
    assert!(window > 0);

    let bogus = 9999;
    // Parented creators reject an unknown parent instead of orphaning widgets.
    assert_eq!(backend.create_button(bogus, "b", 0, 0, 10, 10), 0);
    assert_eq!(backend.create_label(bogus, "l", 0, 0, 10, 10), 0);
    assert_eq!(backend.create_combo_box(bogus, 0, 0, 10, 10), 0);
    assert_eq!(backend.create_list_box(bogus, 0, 0, 10, 10), 0);
    assert_eq!(backend.create_message_box(bogus, "t", "m", 0, 0, 10, 10), 0);
    assert_eq!(backend.create_file_dialog(bogus, 0, 0, 10, 10), 0);
    assert_eq!(backend.create_spin_box(bogus, 0, 0, 10, 10), 0);
    assert_eq!(backend.create_scroll_area(bogus, 0, 0, 10, 10), 0);

    // A menu must hang off a menu bar (or another menu), not a window.
    assert_eq!(backend.create_menu(window, "File", 0, 0, 10, 10), 0);

    let menu_bar = backend.create_menu_bar(window, 0, 0, 400, 24);
    assert!(menu_bar > 0);
    let menu = backend.create_menu(menu_bar, "File", 0, 0, 10, 10);
    assert!(menu > 0, "menu under a menu bar must succeed");

    // Menu item requires a menu parent.
    assert_eq!(backend.menu_add_item(window, "Bad", None), 0);
    let item = backend.menu_add_item(menu, "Open", None);
    assert!(item > 0);

    // Only a menu item may be injected as a menu trigger.
    assert!(!backend.inject_menu_trigger(window));
    assert!(!backend.inject_menu_trigger(menu_bar));
    assert!(backend.inject_menu_trigger(item));

    // attach_menu_bar_to_window validates both ids and their kinds.
    assert!(!backend.attach_menu_bar_to_window(bogus, menu_bar));
    assert!(!backend.attach_menu_bar_to_window(window, bogus));
    assert!(!backend.attach_menu_bar_to_window(window, item));
    assert!(backend.attach_menu_bar_to_window(window, menu_bar));

    // inject_widget_trigger_event rejects unknown ids.
    use crate::platform::WidgetTriggerKind;
    assert!(!backend.inject_widget_trigger_event(bogus, WidgetTriggerKind::Clicked));
    assert!(backend.inject_widget_trigger_event(window, WidgetTriggerKind::Clicked));
}

#[test]
fn drag_and_drop() {
    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("DragTest", 0, 0, 400, 300);
    let source = backend.create_button(window, "Drag", 10, 10, 80, 24);

    // Begin drag from source.
    assert!(backend.begin_drag(source, "text/plain", b"drag payload"), "Should begin drag");

    // Poll drop event.
    let drop = backend.poll_drop_event();
    assert!(drop.is_some(), "Should poll drop event");
    let drop = drop.unwrap();
    assert_eq!(drop.source_widget_id, source);
    assert_eq!(drop.mime, "text/plain");
    assert_eq!(drop.payload, b"drag payload");
}

#[test]
fn ime_and_accessibility() {
    let backend = WaylandPlatform::new();
    backend.init();

    let window = backend.create_window("IMETest", 0, 0, 400, 300);
    let line_edit = backend.create_line_edit(window, "input", 10, 10, 160, 24);

    // IME roundtrip.
    assert!(backend.set_widget_ime_enabled(line_edit, true), "Should enable IME");
    assert!(backend.is_widget_ime_enabled(line_edit), "IME should be enabled");

    assert!(backend.set_widget_ime_enabled(line_edit, false), "Should disable IME");
    assert!(!backend.is_widget_ime_enabled(line_edit), "IME should be disabled");

    // Accessibility name roundtrip.
    assert!(
        backend.set_widget_accessibility_name(line_edit, "InputField"),
        "Should set accessibility name"
    );
    assert_eq!(
        backend.get_widget_accessibility_name(line_edit),
        "InputField",
        "Accessibility name should match"
    );
}