rust_widgets 0.9.6

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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Stub platform implementation for testing and demonstrations.
use crate::compat::HashMap;
use crate::compat::Mutex;
use crate::core::{ObjectId, PlatformFamily};
use crate::platform::state::BackendState;
use crate::platform::types::*;
#[cfg(not(feature = "mini"))]
use serde::{Deserialize, Serialize};

/// Handle kind discriminator for stub widget records.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(not(feature = "mini"), derive(Serialize, Deserialize))]
pub(crate) enum StubHandleKind {
    Window,
    Button,
    MenuBar,
    CheckBox,
    LineEdit,
    Label,
    RadioButton,
    Slider,
    ProgressBar,
    ComboBox,
    ListBox,
    Panel,
    Menu,
    MenuItem,
    ToolBar,
    StatusBar,
    MessageBox,
    FileDialog,
    ColorDialog,
    FontDialog,
    SpinBox,
    ListView,
    ScrollArea,
}

pub struct StubPlatform {
    backend: &'static str,
    family: PlatformFamily,
    state: BackendState<StubHandleKind>,
    menu_nodes: Mutex<HashMap<ObjectId, MenuNodeState>>,
    /// In-memory combo-box item storage by logical combo widget id.
    combo_box_items: Mutex<HashMap<ObjectId, Vec<String>>>,
    /// In-memory combo-box selected index by logical combo widget id.
    combo_box_selection: Mutex<HashMap<ObjectId, Option<usize>>>,
    /// In-memory list-box item storage by logical list widget id.
    list_box_items: Mutex<HashMap<ObjectId, Vec<String>>>,
    /// In-memory list-box selected index by logical list widget id.
    list_box_selection: Mutex<HashMap<ObjectId, Option<usize>>>,
}

impl StubPlatform {
    /// Creates a new in-memory stub backend for tests and demos.
    pub fn new(backend: &'static str, family: PlatformFamily) -> Self {
        Self {
            backend,
            family,
            state: BackendState::new(),
            menu_nodes: Mutex::new(HashMap::new()),
            combo_box_items: Mutex::new(HashMap::new()),
            combo_box_selection: Mutex::new(HashMap::new()),
            list_box_items: Mutex::new(HashMap::new()),
            list_box_selection: Mutex::new(HashMap::new()),
        }
    }

    fn is_embedded_profile(&self) -> bool {
        matches!(self.family, PlatformFamily::Embedded)
    }

    fn embedded_unsupported_id(&self, _name: &str) -> ObjectId {
        // Return a dummy id for unsupported features in embedded profile
        0
    }

    fn embedded_unsupported_bool(&self, _name: &str) -> bool {
        // Return false for unsupported features in embedded profile
        false
    }
}

impl Platform for StubPlatform {
    fn as_any(&self) -> &dyn core::any::Any {
        self
    }

    fn backend_name(&self) -> &'static str {
        self.backend
    }

    fn family(&self) -> PlatformFamily {
        self.family
    }

    fn init(&self) {
        log::info!("[stub] StubPlatform init (testing backend)");
    }

    fn run(&self) {
        log::info!("[stub] StubPlatform run (testing backend)");
    }

    fn quit(&self) {
        log::info!("[stub] StubPlatform quit");
    }

    fn create_window(&self, title: &str, x: i32, y: i32, width: u32, height: u32) -> ObjectId {
        self.state.create_widget(StubHandleKind::Window, title, x, y, width, height)
    }

    fn create_button(
        &self,
        _parent: ObjectId,
        text: &str,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::Button, text, x, y, width, height)
    }

    fn create_menu_bar(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        if self.is_embedded_profile() {
            return self.embedded_unsupported_id("create_menu_bar");
        }
        let id = self.state.create_widget(StubHandleKind::MenuBar, "MenuBar", x, y, width, height);
        self.menu_nodes
            .lock()
            .expect("platform lock poisoned")
            .insert(id, MenuNodeState { text: "MenuBar".to_string() });
        id
    }

    fn create_checkbox(
        &self,
        _parent: ObjectId,
        text: &str,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::CheckBox, text, x, y, width, height)
    }

    fn create_line_edit(
        &self,
        _parent: ObjectId,
        text: &str,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::LineEdit, text, x, y, width, height)
    }

    fn create_label(
        &self,
        _parent: ObjectId,
        text: &str,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::Label, text, x, y, width, height)
    }

    fn create_radio_button(
        &self,
        _parent: ObjectId,
        text: &str,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::RadioButton, text, x, y, width, height)
    }

    fn create_slider(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::Slider, "Slider", x, y, width, height)
    }

    fn create_progress_bar(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::ProgressBar, "ProgressBar", x, y, width, height)
    }

    fn create_combo_box(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        let id =
            self.state.create_widget(StubHandleKind::ComboBox, "ComboBox", x, y, width, height);
        self.combo_box_items.lock().expect("platform lock poisoned").insert(id, Vec::new());
        self.combo_box_selection.lock().expect("platform lock poisoned").insert(id, None);
        id
    }

    fn combo_box_add_item(&self, combo_box: ObjectId, _text: &str) -> bool {
        let mut items = self.combo_box_items.lock().expect("platform lock poisoned");
        let list = match items.get_mut(&combo_box) {
            Some(list) => list,
            None => return false,
        };
        list.push(_text.to_string());
        true
    }

    fn combo_box_clear_items(&self, combo_box: ObjectId) -> bool {
        {
            let mut items = self.combo_box_items.lock().expect("platform lock poisoned");
            if let Some(list) = items.get_mut(&combo_box) {
                list.clear();
            } else {
                return false;
            }
        }
        self.combo_box_selection.lock().expect("platform lock poisoned").insert(combo_box, None);
        true
    }

    fn combo_box_set_current_index(&self, combo_box: ObjectId, index: usize) -> bool {
        let items = self.combo_box_items.lock().expect("platform lock poisoned");
        let len = match items.get(&combo_box) {
            Some(list) => list.len(),
            None => return false,
        };
        if index >= len {
            return false;
        }
        drop(items);
        self.combo_box_selection
            .lock()
            .expect("platform lock poisoned")
            .insert(combo_box, Some(index));
        true
    }

    fn combo_box_current_index(&self, combo_box: ObjectId) -> Option<usize> {
        self.combo_box_selection
            .lock()
            .expect("platform lock poisoned")
            .get(&combo_box)
            .and_then(|index| *index)
    }

    fn combo_box_item_count(&self, combo_box: ObjectId) -> usize {
        self.combo_box_items
            .lock()
            .expect("platform lock poisoned")
            .get(&combo_box)
            .map(|items| items.len())
            .unwrap_or(0)
    }

    fn combo_box_item_text(&self, combo_box: ObjectId, index: usize) -> Option<String> {
        self.combo_box_items
            .lock()
            .expect("platform lock poisoned")
            .get(&combo_box)
            .and_then(|items| items.get(index).cloned())
    }

    fn create_list_box(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        let id = self.state.create_widget(StubHandleKind::ListBox, "ListBox", x, y, width, height);
        self.list_box_items.lock().expect("platform lock poisoned").insert(id, Vec::new());
        self.list_box_selection.lock().expect("platform lock poisoned").insert(id, None);
        id
    }

    fn list_box_add_item(&self, list_box: ObjectId, text: &str) -> bool {
        let mut items = self.list_box_items.lock().expect("platform lock poisoned");
        let list = match items.get_mut(&list_box) {
            Some(list) => list,
            None => return false,
        };
        list.push(text.to_string());
        true
    }

    fn list_box_remove_item(&self, list_box: ObjectId, index: usize) -> bool {
        let len;
        {
            let mut items = self.list_box_items.lock().expect("platform lock poisoned");
            let list = match items.get_mut(&list_box) {
                Some(list) => list,
                None => return false,
            };
            if index >= list.len() {
                return false;
            }
            list.remove(index);
            len = list.len();
        }
        let mut selection = self.list_box_selection.lock().expect("platform lock poisoned");
        if let Some(current) = selection.get(&list_box).and_then(|value| *value) {
            if current == index {
                selection.insert(list_box, None);
            } else if current > index && len > 0 {
                selection.insert(list_box, Some((current - 1).min(len - 1)));
            }
        }
        true
    }

    fn list_box_clear_items(&self, list_box: ObjectId) -> bool {
        {
            let mut items = self.list_box_items.lock().expect("platform lock poisoned");
            if let Some(list) = items.get_mut(&list_box) {
                list.clear();
            } else {
                return false;
            }
        }
        self.list_box_selection.lock().expect("platform lock poisoned").insert(list_box, None);
        true
    }

    fn list_box_set_current_index(&self, list_box: ObjectId, index: usize) -> bool {
        let items = self.list_box_items.lock().expect("platform lock poisoned");
        let len = match items.get(&list_box) {
            Some(list) => list.len(),
            None => return false,
        };
        if index >= len {
            return false;
        }
        drop(items);
        self.list_box_selection
            .lock()
            .expect("platform lock poisoned")
            .insert(list_box, Some(index));
        true
    }

    fn list_box_current_index(&self, list_box: ObjectId) -> Option<usize> {
        self.list_box_selection
            .lock()
            .expect("platform lock poisoned")
            .get(&list_box)
            .and_then(|index| *index)
    }

    fn list_box_item_count(&self, list_box: ObjectId) -> usize {
        self.list_box_items
            .lock()
            .expect("platform lock poisoned")
            .get(&list_box)
            .map(|items| items.len())
            .unwrap_or(0)
    }

    fn list_box_item_text(&self, list_box: ObjectId, index: usize) -> Option<String> {
        self.list_box_items
            .lock()
            .expect("platform lock poisoned")
            .get(&list_box)
            .and_then(|items| items.get(index).cloned())
    }

    fn create_panel(&self, _parent: ObjectId, x: i32, y: i32, width: u32, height: u32) -> ObjectId {
        self.state.create_widget(StubHandleKind::Panel, "Panel", x, y, width, height)
    }

    fn create_menu(
        &self,
        _parent: ObjectId,
        text: &str,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        if self.is_embedded_profile() {
            return self.embedded_unsupported_id("create_menu");
        }
        let id = self.state.create_widget(StubHandleKind::Menu, text, x, y, width, height);
        self.menu_nodes
            .lock()
            .expect("platform lock poisoned")
            .insert(id, MenuNodeState { text: text.to_string() });
        id
    }

    fn create_tool_bar(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        if self.is_embedded_profile() {
            return self.embedded_unsupported_id("create_tool_bar");
        }
        self.state.create_widget(StubHandleKind::ToolBar, "ToolBar", x, y, width, height)
    }

    fn create_status_bar(
        &self,
        _parent: ObjectId,
        text: &str,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        if self.is_embedded_profile() {
            return self.embedded_unsupported_id("create_status_bar");
        }
        self.state.create_widget(StubHandleKind::StatusBar, text, x, y, width, height)
    }

    fn create_message_box(
        &self,
        _parent: ObjectId,
        _title: &str,
        _text: &str,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::MessageBox, "MessageBox", x, y, width, height)
    }

    fn create_file_dialog(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::FileDialog, "FileDialog", x, y, width, height)
    }

    fn create_color_dialog(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::ColorDialog, "ColorDialog", x, y, width, height)
    }

    fn create_font_dialog(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::FontDialog, "FontDialog", x, y, width, height)
    }

    fn create_spin_box(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::SpinBox, "SpinBox", x, y, width, height)
    }

    fn create_list_view(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::ListView, "ListView", x, y, width, height)
    }

    fn create_scroll_area(
        &self,
        _parent: ObjectId,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
    ) -> ObjectId {
        self.state.create_widget(StubHandleKind::ScrollArea, "ScrollArea", x, y, width, height)
    }

    fn attach_menu_bar_to_window(&self, window: ObjectId, menu_bar: ObjectId) -> bool {
        if self.is_embedded_profile() {
            return self.embedded_unsupported_bool("attach_menu_bar_to_window");
        }
        self.state.contains_widget(window) && self.state.contains_widget(menu_bar)
    }

    fn menu_add_item(
        &self,
        _parent_menu: ObjectId,
        text: &str,
        shortcut: Option<&str>,
    ) -> ObjectId {
        if self.is_embedded_profile() {
            return self.embedded_unsupported_id("menu_add_item");
        }
        let id = self.state.create_widget(StubHandleKind::MenuItem, text, 0, 0, 0, 0);
        self.menu_nodes
            .lock()
            .expect("platform lock poisoned")
            .insert(id, MenuNodeState { text: text.to_string() });
        let _ = shortcut;
        id
    }

    fn poll_menu_triggered(&self) -> Option<ObjectId> {
        self.state.pop_menu_event()
    }

    fn inject_menu_trigger(&self, menu_item_id: ObjectId) -> bool {
        if self.is_embedded_profile() {
            return self.embedded_unsupported_bool("inject_menu_trigger");
        }
        // Accept only known menu ids to avoid emitting orphan events.
        if !self.menu_nodes.lock().expect("platform lock poisoned").contains_key(&menu_item_id) {
            return false;
        }
        self.state.push_menu_event(menu_item_id);
        true
    }

    fn poll_widget_triggered(&self) -> Option<ObjectId> {
        self.poll_widget_trigger_event().map(|event| event.widget_id)
    }

    fn poll_widget_trigger_event(&self) -> Option<WidgetTriggerEvent> {
        self.state.pop_widget_event()
    }

    fn inject_widget_trigger_event(&self, widget_id: ObjectId, kind: WidgetTriggerKind) -> bool {
        // Accept only known widget ids to keep queue semantics deterministic.
        if !self.state.contains_widget(widget_id) {
            return false;
        }
        self.state.push_widget_event(WidgetTriggerEvent { widget_id, kind });
        true
    }

    fn show_widget(&self, widget_id: ObjectId) {
        self.state.set_visible(widget_id, true);
    }

    fn hide_widget(&self, widget_id: ObjectId) {
        self.state.set_visible(widget_id, false);
    }

    fn set_widget_geometry(&self, widget_id: ObjectId, x: i32, y: i32, width: u32, height: u32) {
        self.state.set_geometry(widget_id, x, y, width, height);
    }

    fn set_widget_text(&self, widget_id: ObjectId, text: &str) {
        self.state.set_text(widget_id, text);
        if let Some(node) =
            self.menu_nodes.lock().expect("platform lock poisoned").get_mut(&widget_id)
        {
            node.text = text.to_string();
        }
    }

    fn get_widget_text(&self, widget_id: ObjectId) -> String {
        self.state.text(widget_id)
    }

    fn set_widget_enabled(&self, widget_id: ObjectId, enabled: bool) {
        self.state.set_enabled(widget_id, enabled);
    }

    fn is_widget_enabled(&self, widget_id: ObjectId) -> bool {
        self.state.enabled(widget_id)
    }

    fn set_widget_visible(&self, widget_id: ObjectId, visible: bool) {
        self.state.set_visible(widget_id, visible);
    }

    fn is_widget_visible(&self, widget_id: ObjectId) -> bool {
        self.state.visible(widget_id)
    }

    fn set_widget_ime_enabled(&self, widget_id: ObjectId, enabled: bool) -> bool {
        self.state.set_ime_enabled(widget_id, enabled)
    }

    fn is_widget_ime_enabled(&self, widget_id: ObjectId) -> bool {
        self.state.ime_enabled(widget_id)
    }

    fn set_widget_accessibility_name(&self, widget_id: ObjectId, name: &str) -> bool {
        self.state.set_accessibility_name(widget_id, name)
    }

    fn get_widget_accessibility_name(&self, widget_id: ObjectId) -> String {
        self.state.accessibility_name(widget_id)
    }

    fn set_clipboard_text(&self, text: &str) -> bool {
        self.state.set_clipboard_text(text)
    }

    fn get_clipboard_text(&self) -> String {
        self.state.clipboard_text()
    }

    fn begin_drag(&self, source_widget_id: ObjectId, mime: &str, payload: &[u8]) -> bool {
        self.state.begin_drag(source_widget_id, mime, payload)
    }

    fn poll_drop_event(&self) -> Option<DropEvent> {
        self.state.pop_drop_event()
    }

    fn inject_drop_event(&self, event: DropEvent) -> bool {
        self.state.inject_drop_event(event)
    }
}