pane_ui 0.1.0

A RON-driven, hot-reloadable wgpu UI library with spring animations and consistent scaling
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
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
use serde::Deserialize;
use std::sync::mpsc;
use std::time::{Duration, SystemTime};

// ── Internal Message Bus ──────────────────────────────────────────────────────

#[derive(Debug)]
pub(crate) enum UiMsg {
    SwitchRoot(String),
    SwitchTabPage {
        tab_id: String,
        page: usize,
    },
    Quit,
    Custom(String),
    Reload,
    Slider(String, f32),
    TextChanged(String, String),
    TextSubmitted(String, String),
    Toggle(String, bool),
    Dropdown(String, usize, String),
    Radio(String, usize, String),
    /// Internal: spawns a toast widget into the active root. Fired by `PressAction::Toast`.
    Toast {
        message: String,
        duration: f32,
        x: f32,
        y: f32,
        width: f32,
        height: f32,
    },
}

// ── RON Data Structures ───────────────────────────────────────────────────────

#[derive(Deserialize)]
pub struct MenuDef {
    #[serde(default)]
    pub hot_reload: bool,
    #[serde(default)]
    pub headless_accessible: bool,
    #[serde(default)]
    pub shader_dirs: Vec<String>,
    #[serde(default)]
    pub style_dirs: Vec<String>,
    pub background: Option<String>,
    #[serde(default)]
    pub clear_color: Option<crate::draw::Color>,
    pub default_style: Option<String>,
    pub start_root: String,
    pub roots: Vec<RootDef>,
}

#[derive(Deserialize)]
pub struct RootDef {
    pub name: String,
    #[serde(default)]
    pub buttons: Vec<ButtonDef>,
    #[serde(default)]
    pub scroll_lists: Vec<ScrollListDef>,
    #[serde(default)]
    pub bars: Vec<BarDef>,
    #[serde(default)]
    pub popouts: Vec<PopoutDef>,
    #[serde(default)]
    pub toggles: Vec<ToggleDef>,
    #[serde(default)]
    pub sliders: Vec<SliderDef>,
    #[serde(default)]
    pub labels: Vec<FreeLabelDef>,
    #[serde(default)]
    pub dividers: Vec<DividerDef>,
    #[serde(default)]
    pub images: Vec<ImageDef>,
    #[serde(default)]
    pub text_boxes: Vec<TextBoxDef>,
    #[serde(default)]
    pub progress_bars: Vec<ProgressBarDef>,
    #[serde(default)]
    pub scroll_panes: Vec<ScrollPaneDef>,
    #[serde(default)]
    pub dropdowns: Vec<DropdownDef>,
    #[serde(default)]
    pub radio_groups: Vec<RadioGroupDef>,
    #[serde(default)]
    pub actors: Vec<ActorDef>,
    #[serde(default)]
    pub tabs: Vec<TabDef>,
}

#[derive(Deserialize)]
pub struct BarDef {
    pub id: String,
    #[serde(default)]
    pub edge: Option<BarEdgeDef>,
    pub thickness: f32,
    #[serde(default)]
    pub pad: f32,
    #[serde(default)]
    pub gap: f32,
    pub style: Option<String>,
    pub items: Vec<BarItemDef>,
    #[serde(default)]
    pub manual: bool,
    /// X position when `edge` is `None` or `Free` (screen-space, origin at center).
    #[serde(default)]
    pub x: f32,
    /// Y position when `edge` is `None` or `Free`.
    #[serde(default)]
    pub y: f32,
    /// Width override for free-positioned bars. If 0.0, uses `thickness` for vertical bars.
    #[serde(default)]
    pub width: f32,
    /// Height override for free-positioned bars. If 0.0, uses `thickness` for horizontal bars.
    #[serde(default)]
    pub height: f32,
}

#[derive(Deserialize, PartialEq, Eq)]
pub enum BarEdgeDef {
    Top,
    Bottom,
    Left,
    Right,
    Free,
}

#[derive(Deserialize)]
pub struct PopoutDef {
    pub id: String,
    #[serde(default)]
    pub closed_x: f32,
    #[serde(default)]
    pub closed_y: f32,
    #[serde(default)]
    pub open_x: f32,
    #[serde(default)]
    pub open_y: f32,
    pub width: f32,
    pub height: f32,
    pub toggle_id: String,
    pub style: Option<String>,
    pub edge: Option<PopoutEdgeDef>,
    #[serde(default = "default_true")]
    pub shadow: bool,
    #[serde(default)]
    pub horizontal: bool,
    #[serde(default)]
    pub gap: f32,
    #[serde(default)]
    pub full_span: bool,
    #[serde(default)]
    pub home_toggles: bool,
    #[serde(default)]
    pub items: Vec<PopoutItemDef>,
}

const fn default_true() -> bool {
    true
}

#[derive(Deserialize, Clone, Copy)]
pub enum PopoutEdgeDef {
    Left,
    Right,
    Top,
    Bottom,
}

#[derive(Deserialize)]
pub enum PopoutItemDef {
    Button(ButtonDef),
    ScrollList(ScrollListDef),
    Bar(BarDef),
    Popout(PopoutDef),
}

#[derive(Deserialize)]
pub enum BarItemDef {
    Button(ListButtonDef),
    Label(LabelDef),
    Spacer,
    ScrollList(ScrollListDef),
}

#[derive(Deserialize)]
pub struct LabelDef {
    pub id: String,
    pub text: String,
    #[serde(default = "default_label_size")]
    pub size: f32,
    pub color: Option<crate::draw::Color>,
    #[serde(default)]
    pub width: f32,
}

const fn default_label_size() -> f32 {
    24.0
}

#[derive(Deserialize)]
pub struct ButtonDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub text: String,
    pub tooltip: Option<String>,
    pub style: Option<String>,
    pub on_press: PressAction,
    #[serde(default)]
    pub nav_default: bool,
}

#[derive(Deserialize)]
pub struct ScrollListDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    #[serde(default)]
    pub pad_left: f32,
    #[serde(default)]
    pub pad_right: f32,
    #[serde(default)]
    pub pad_top: f32,
    #[serde(default)]
    pub pad_bottom: f32,
    #[serde(default)]
    pub gap: f32,
    pub style: Option<String>,
    #[serde(default)]
    pub horizontal: bool,
    #[serde(default)]
    pub full_span: bool,
    pub items: Vec<ListButtonDef>,
}

#[derive(Deserialize)]
pub struct ListButtonDef {
    pub id: String,
    pub height: f32,
    /// Width used when the parent [`ScrollListDef`] has `horizontal: true`.
    #[serde(default)]
    pub width: f32,
    pub text: String,
    pub tooltip: Option<String>,
    pub style: Option<String>,
    pub on_press: PressAction,
}

#[derive(Deserialize, Clone)]
pub enum PressAction {
    /// Switch the active root to the named root.
    SwitchRoot(String),
    /// Jump a tab widget to a specific page index.
    SwitchTabPage { tab_id: String, page: usize },
    /// Exit the application.
    Quit,
    /// Print a debug message to stdout when the button is pressed.
    Print(String),
    /// Emit a [`crate::api::PaneAction::Custom`] with the given tag string.
    Custom(String),
    /// Show a transient toast notification. All geometry is in the 1080-unit grid space.
    ///
    /// `duration`, `width`, and `height` are optional and default to `2.0`, `400.0`, and `60.0`.
    /// `x` and `y` default to `0.0` (screen centre).
    Toast {
        /// Text displayed inside the toast.
        message: String,
        /// How long the toast stays visible, in seconds. Defaults to `2.0`.
        #[serde(default = "default_toast_duration")]
        duration: f32,
        /// Horizontal centre of the toast in grid units. Defaults to `0.0`.
        #[serde(default)]
        x: f32,
        /// Vertical centre of the toast in grid units. Defaults to `0.0`.
        #[serde(default)]
        y: f32,
        /// Width of the toast in grid units. Defaults to `400.0`.
        #[serde(default = "default_toast_width")]
        width: f32,
        /// Height of the toast in grid units. Defaults to `60.0`.
        #[serde(default = "default_toast_height")]
        height: f32,
    },
    /// Internal: emitted by radio option buttons. Never appears in RON files.
    RadioSelect { group_id: String, index: usize },
    /// Internal: emitted by dropdown option buttons. Never appears in RON files.
    DropdownSelect { dropdown_id: String, index: usize },
}

const fn default_toast_duration() -> f32 {
    2.0
}
const fn default_toast_width() -> f32 {
    400.0
}
const fn default_toast_height() -> f32 {
    60.0
}

impl PressAction {
    /// True for actions that require post-press root mutation (`RadioSelect`, `DropdownSelect`).
    #[must_use]
    pub const fn is_internal(&self) -> bool {
        matches!(self, Self::RadioSelect { .. } | Self::DropdownSelect { .. })
    }
}

#[derive(Deserialize)]
pub struct ToggleDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub text: String,
    pub tooltip: Option<String>,
    #[serde(default)]
    pub checked: bool,
    pub style_off: String,
    pub style_on: String,
    pub on_change: ToggleAction,
}

#[derive(Deserialize, Clone)]
pub enum ToggleAction {
    Print,
    Custom(String),
}

// ── Load ──────────────────────────────────────────────────────────────────────

/// Parse a RON menu file, returning an error string on failure instead of panicking.
/// Used during hot-reload so a bad edit doesn't crash the running app.
pub(crate) fn load_menu_soft(path: &str) -> Result<MenuDef, String> {
    let src = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
    ron::Options::default()
        .with_default_extension(
            ron::extensions::Extensions::IMPLICIT_SOME
                | ron::extensions::Extensions::UNWRAP_NEWTYPES,
        )
        .from_str(&src)
        .map_err(|e| {
            eprintln!("[pane_ui] Reload parse error: {e}");
            e.to_string()
        })
}

/// Parse a RON menu file, panicking with a clear message on failure.
/// Used at startup where there is no sensible fallback.
pub(crate) fn load_menu(path: &str) -> MenuDef {
    load_menu_soft(path).unwrap_or_else(|e| panic!("[pane_ui] Failed to load '{path}': {e}"))
}

// ── Watcher ───────────────────────────────────────────────────────────────────

/// Spawn a background thread that polls the menu RON file, shader dirs, and style dirs
/// for modification-time changes every second, sending [`UiMsg::Reload`] on any change.
pub(crate) fn spawn_watcher(
    path: String,
    shader_dirs: Vec<String>,
    style_dirs: Vec<String>,
    tx: mpsc::Sender<UiMsg>,
) {
    std::thread::spawn(move || {
        let collect_stamps = || -> Vec<(String, Option<SystemTime>)> {
            let mut stamps = Vec::new();
            stamps.push((
                path.clone(),
                std::fs::metadata(&path).and_then(|m| m.modified()).ok(),
            ));
            for dir in &shader_dirs {
                if let Ok(entries) = std::fs::read_dir(dir) {
                    for entry in entries.filter_map(std::result::Result::ok) {
                        let p = entry.path();
                        if p.extension().is_some_and(|x| x == "wgsl") {
                            stamps.push((
                                p.to_string_lossy().to_string(),
                                std::fs::metadata(&p).and_then(|m| m.modified()).ok(),
                            ));
                        }
                    }
                }
            }
            for dir in &style_dirs {
                if let Ok(entries) = std::fs::read_dir(dir) {
                    for entry in entries.filter_map(std::result::Result::ok) {
                        let p = entry.path();
                        if p.extension().is_some_and(|x| x == "ron") {
                            stamps.push((
                                p.to_string_lossy().to_string(),
                                std::fs::metadata(&p).and_then(|m| m.modified()).ok(),
                            ));
                        }
                    }
                }
            }
            stamps
        };

        let mut last_stamps = collect_stamps();
        loop {
            std::thread::sleep(Duration::from_secs(1));
            let new_stamps = collect_stamps();
            if new_stamps != last_stamps {
                let _ = tx.send(UiMsg::Reload);
            }
            last_stamps = new_stamps;
        }
    });
}

// ── Slider ────────────────────────────────────────────────────────────────────

#[derive(Deserialize)]
pub struct SliderDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub min: f32,
    pub max: f32,
    pub value: f32,
    #[serde(default)]
    pub step: Option<f32>,
    pub tooltip: Option<String>,
    pub style_track: String,
    pub style_thumb: String,
    pub on_change: SliderAction,
}

// ── Static Display Items ──────────────────────────────────────────────────────

#[derive(Deserialize)]
pub struct FreeLabelDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub text: String,
    #[serde(default = "default_label_size")]
    pub size: f32,
    pub color: Option<crate::draw::Color>,
    #[serde(default)]
    pub width: f32,
}

#[derive(Deserialize)]
pub struct DividerDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub style: String,
    #[serde(default)]
    pub full_span: bool,
}

#[derive(Deserialize)]
pub struct ImageDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub path: String,
    #[serde(default)]
    pub gif_mode: Option<crate::textures::GifMode>,
}

#[derive(Deserialize)]
pub struct TextBoxDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    #[serde(default)]
    pub hint: String,
    #[serde(default)]
    pub max_len: Option<usize>,
    pub tooltip: Option<String>,
    pub style: String,
    #[serde(default)]
    pub style_focus: Option<String>,
    pub on_change: TextBoxAction,
    pub on_submit: TextBoxAction,
    #[serde(default)]
    pub password: bool,
    #[serde(default)]
    pub multiline: bool,
    #[serde(default)]
    pub rows: Option<u32>,
    #[serde(default)]
    pub font_size: Option<f32>,
}

#[derive(Deserialize)]
pub struct ProgressBarDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub value: f32,
    pub style_track: String,
    pub style_fill: String,
}

#[derive(Deserialize, Clone)]
pub enum TextBoxAction {
    Print,
    Custom(String),
    None,
}

#[derive(Deserialize, Clone)]
pub enum SliderAction {
    Print,
    Custom(String),
}

// ── ScrollPane ────────────────────────────────────────────────────────────────

#[derive(Deserialize)]
pub struct ScrollPaneDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    #[serde(default)]
    pub pad_left: f32,
    #[serde(default)]
    pub pad_right: f32,
    #[serde(default)]
    pub pad_top: f32,
    #[serde(default)]
    pub pad_bottom: f32,
    #[serde(default)]
    pub gap: f32,
    pub style: Option<String>,
    #[serde(default)]
    pub horizontal: bool,
    #[serde(default)]
    pub full_span: bool,
    #[serde(default)]
    pub manual: bool,
    pub items: Vec<ContainerItemDef>,
}

#[derive(Deserialize)]
pub enum ContainerItemDef {
    Button(ButtonDef),
    Toggle(ToggleDef),
    Slider(SliderDef),
    TextBox(TextBoxDef),
    Label(FreeLabelDef),
    Divider(DividerDef),
    Image(ImageDef),
    ProgressBar(ProgressBarDef),
    ScrollPane(ScrollPaneDef),
    ScrollList(ScrollListDef),
    Tab(TabDef),
}

// ── Tab ───────────────────────────────────────────────────────────────────────

#[derive(Deserialize)]
pub struct TabPageDef {
    pub label: String,
    pub items: Vec<ContainerItemDef>,
}

#[derive(Deserialize)]
pub struct TabDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    #[serde(default)]
    pub pad_left: f32,
    #[serde(default)]
    pub pad_right: f32,
    #[serde(default)]
    pub pad_top: f32,
    #[serde(default)]
    pub pad_bottom: f32,
    #[serde(default)]
    pub gap: f32,
    pub style: Option<String>,
    pub pages: Vec<TabPageDef>,
    #[serde(default)]
    pub full_span: bool,
}

// ── Dropdown ──────────────────────────────────────────────────────────────────

#[derive(Deserialize)]
pub struct DropdownDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub options: Vec<String>,
    #[serde(default)]
    pub selected: usize,
    pub tooltip: Option<String>,
    pub style: Option<String>,
    pub style_list: Option<String>,
    pub style_item: Option<String>,
    pub on_change: DropdownAction,
}

#[derive(Deserialize, Clone)]
pub enum DropdownAction {
    Print,
    Custom(String),
}

// ── Actor ─────────────────────────────────────────────────────────────────────

#[derive(Deserialize)]
pub struct ActorDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub style: Option<String>,
    /// Base gif path — always loops automatically. Optional.
    pub gif: Option<String>,
    #[serde(default)]
    pub z_front: bool,
    #[serde(default = "default_true")]
    pub return_on_end: bool,
    #[serde(default)]
    pub behaviours: Vec<BehaviourDef>,
}

#[derive(Deserialize)]
pub struct BehaviourDef {
    pub trigger: TriggerDef,
    pub action: ActionDef,
}

#[derive(Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum TriggerDef {
    Always,
    OnHoverSelf,
    OnPressSelf,
    OnClickSelf,
    OnClickAnywhere,
}

#[derive(Deserialize, Clone)]
pub enum ActionDef {
    FollowCursor {
        speed: f32,
        #[serde(default)]
        trail: f32,
    },
    MoveTo {
        x: f32,
        y: f32,
        speed: f32,
    },
    /// Swap to a different gif while this trigger is active. Reverts when trigger ends.
    SwapGif {
        path: String,
    },
}

// ── RadioGroup ────────────────────────────────────────────────────────────────

#[derive(Deserialize)]
pub struct RadioGroupDef {
    pub id: String,
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub options: Vec<String>,
    #[serde(default)]
    pub selected: usize,
    #[serde(default)]
    pub gap: f32,
    pub tooltip: Option<String>,
    pub style_idle: Option<String>,
    pub style_selected: Option<String>,
    pub on_change: RadioAction,
}

#[derive(Deserialize, Clone)]
pub enum RadioAction {
    Print,
    Custom(String),
}