windjammer-ui 0.3.6

Cross-platform UI framework for Windjammer (Web, Desktop, Mobile)
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
/// Desktop renderer using egui + wgpu
/// Converts VNode tree to egui UI
#[cfg(not(target_arch = "wasm32"))]
use crate::simple_vnode::{VAttr, VNode};
#[cfg(not(target_arch = "wasm32"))]
use egui::{Color32, Context, Frame, Margin, RichText, Rounding, Stroke, Ui};
#[cfg(not(target_arch = "wasm32"))]
use std::cell::RefCell;
#[cfg(not(target_arch = "wasm32"))]
use std::rc::Rc;

#[cfg(not(target_arch = "wasm32"))]
pub struct DesktopRenderer {
    // Store event handlers for buttons
    #[allow(dead_code)]
    event_handlers: Rc<RefCell<Vec<crate::event_handler::EventHandler>>>,
}

#[cfg(not(target_arch = "wasm32"))]
impl Default for DesktopRenderer {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl DesktopRenderer {
    pub fn new() -> Self {
        Self {
            event_handlers: Rc::new(RefCell::new(Vec::new())),
        }
    }

    /// Render a VNode tree into egui
    pub fn render(&mut self, ctx: &Context, vnode: &VNode) {
        // Render the root VNode, which should be a Container
        // We need to handle it specially to fill the entire window
        egui::CentralPanel::default()
            .frame(Frame::none().fill(Color32::from_rgb(30, 30, 30)))
            .show(ctx, |ui| {
                // Get full screen size
                let screen_size = ctx.screen_rect().size();

                // Allocate the full screen for our content
                ui.allocate_ui_with_layout(
                    screen_size,
                    egui::Layout::top_down(egui::Align::Min),
                    |ui| {
                        self.render_vnode(ui, vnode);
                    },
                );
            });
    }

    pub fn render_vnode(&mut self, ui: &mut Ui, vnode: &VNode) {
        match vnode {
            VNode::Element {
                tag,
                attrs,
                children,
            } => {
                self.render_element(ui, tag, attrs, children);
            }
            VNode::Text(text) => {
                ui.label(text);
            }
        }
    }

    fn render_element(
        &mut self,
        ui: &mut Ui,
        tag: &str,
        attrs: &[(String, VAttr)],
        children: &[VNode],
    ) {
        // Extract classes and styles
        let classes = self.get_attr_value(attrs, "class");
        let style = self.get_attr_value(attrs, "style");

        // Check for special component classes
        if classes.contains("tree-view") {
            self.render_tree_view(ui, children);
        } else if classes.contains("tab-panel") {
            self.render_tab_panel(ui, children);
        } else if classes.contains("split-panel") {
            self.render_split_panel(ui, &classes, children);
        } else if classes.contains("advanced-code-editor") {
            self.render_advanced_code_editor(ui, children);
        } else {
            match tag {
                "button" => self.render_button(ui, attrs, children),
                "div" => self.render_div(ui, &classes, &style, attrs, children),
                "span" | "p" => self.render_text_container(ui, children),
                "input" => self.render_input(ui, attrs),
                "textarea" => self.render_textarea(ui, attrs),
                "pre" => self.render_code_block(ui, children),
                "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => self.render_heading(ui, tag, children),
                _ => {
                    // Generic container
                    ui.vertical(|ui| {
                        for child in children {
                            self.render_vnode(ui, child);
                        }
                    });
                }
            }
        }
    }

    fn render_button(&mut self, ui: &mut Ui, attrs: &[(String, VAttr)], children: &[VNode]) {
        let label = self.get_text_content(children);
        let disabled = self.get_attr_value(attrs, "disabled") == "true";
        let variant = self.get_variant_from_classes(attrs);

        // Style button based on variant
        let (bg_color, text_color) = match variant.as_str() {
            "primary" => (Color32::from_rgb(0, 122, 204), Color32::WHITE),
            "danger" => (Color32::from_rgb(220, 53, 69), Color32::WHITE),
            "success" => (Color32::from_rgb(40, 167, 69), Color32::WHITE),
            "warning" => (Color32::from_rgb(255, 193, 7), Color32::BLACK),
            _ => (Color32::from_rgb(108, 117, 125), Color32::WHITE),
        };

        let button = egui::Button::new(RichText::new(&label).color(text_color))
            .fill(bg_color)
            .rounding(Rounding::same(3.0));

        let response = ui.add_enabled(!disabled, button);

        // Handle click event
        if response.clicked() {
            println!("🔘 Button '{}' CLICKED!", label);
            if let Some(handler) = self.get_event_handler(attrs, "on_click") {
                handler.borrow_mut()();
            }
        }
    }

    fn render_div(
        &mut self,
        ui: &mut Ui,
        classes: &str,
        style: &str,
        _attrs: &[(String, VAttr)],
        children: &[VNode],
    ) {
        // Parse style for background color, width, height
        let bg_color = if style.contains("background-color:") {
            self.parse_color_from_style(style)
        } else {
            None
        };

        // Determine layout direction from style
        let is_flex_row = style.contains("flex-direction: row") || classes.contains("wj-flex");
        let is_flex_col =
            style.contains("flex-direction: column") || classes.contains("wj-container");

        // Determine if this is a panel
        let is_panel = classes.contains("wj-panel");

        if is_panel {
            self.render_panel(ui, children);
        } else if is_flex_row {
            // Horizontal flex with resizable panels
            let available_width = ui.available_width();
            let available_height = ui.available_height();

            let frame = if let Some(color) = bg_color {
                Frame::none().fill(color).inner_margin(Margin::same(8.0))
            } else {
                Frame::none()
            };

            frame.show(ui, |ui| {
                // Simple horizontal layout with equal distribution
                // TODO: Add proper resizable panels with egui_dock
                ui.horizontal(|ui| {
                    ui.spacing_mut().item_spacing.x = 4.0;

                    let child_width = (available_width - ((children.len() as f32 - 1.0) * 4.0))
                        / children.len() as f32;

                    for child in children {
                        ui.allocate_ui_with_layout(
                            egui::vec2(child_width, available_height),
                            egui::Layout::top_down(egui::Align::Min),
                            |ui| {
                                self.render_vnode(ui, child);
                            },
                        );
                    }
                });
            });
        } else if is_flex_col || classes.contains("wj-container") {
            // For containers, fill available space
            if classes.contains("wj-container") {
                // Container should fill all space and distribute to children
                ui.vertical(|ui| {
                    let total_height = ui.available_height();
                    let remaining_height = total_height;

                    // First pass: render fixed-height children (toolbar, status bar)
                    // Second pass: give remaining space to flexible children
                    let child_count = children.len();

                    for (i, child) in children.iter().enumerate() {
                        // Estimate height for each child
                        let height = if i == 0 {
                            // Toolbar - fixed height
                            50.0
                        } else if i == child_count - 1 {
                            // Status bar - fixed height
                            30.0
                        } else {
                            // Main content - use remaining space
                            remaining_height - 50.0 - 30.0
                        };

                        if height > 0.0 {
                            ui.allocate_ui(egui::vec2(ui.available_width(), height), |ui| {
                                self.render_vnode(ui, child);
                            });
                        } else {
                            self.render_vnode(ui, child);
                        }
                    }
                });
            } else if let Some(color) = bg_color {
                Frame::none().fill(color).show(ui, |ui| {
                    ui.vertical(|ui| {
                        for child in children {
                            self.render_vnode(ui, child);
                        }
                    });
                });
            } else {
                ui.vertical(|ui| {
                    for child in children {
                        self.render_vnode(ui, child);
                    }
                });
            }
        } else {
            // Default: vertical layout
            ui.vertical(|ui| {
                for child in children {
                    self.render_vnode(ui, child);
                }
            });
        }
    }

    fn render_panel(&mut self, ui: &mut Ui, children: &[VNode]) {
        // Find panel header and body
        let mut header_text = String::new();
        let mut body_children = Vec::new();

        for child in children {
            if let VNode::Element { tag, children, .. } = child {
                if tag == "div" {
                    // Check if this is header or body by looking at children
                    if let Some(VNode::Text(text)) = children.first() {
                        if header_text.is_empty() {
                            header_text = text.clone();
                        } else {
                            body_children.extend(children.clone());
                        }
                    } else {
                        body_children.extend(children.clone());
                    }
                }
            }
        }

        // Allocate all available space for the panel
        let available_size = ui.available_size();
        ui.allocate_ui(available_size, |ui| {
            Frame::group(ui.style())
                .fill(Color32::from_rgb(37, 37, 38))
                .stroke(Stroke::new(1.0, Color32::from_rgb(62, 62, 66)))
                .rounding(Rounding::same(5.0))
                .inner_margin(Margin::same(0.0))
                .show(ui, |ui| {
                    ui.vertical(|ui| {
                        // Collapsible header
                        if !header_text.is_empty() {
                            let header_id = ui.make_persistent_id(&header_text);
                            egui::collapsing_header::CollapsingState::load_with_default_open(
                                ui.ctx(),
                                header_id,
                                true,
                            )
                            .show_header(ui, |ui| {
                                Frame::none()
                                    .fill(Color32::from_rgb(45, 45, 48))
                                    .inner_margin(Margin::same(8.0))
                                    .show(ui, |ui| {
                                        ui.label(RichText::new(&header_text).strong());
                                    });
                            })
                            .body(|ui| {
                                // Body - fill remaining space
                                Frame::none()
                                    .inner_margin(Margin::same(8.0))
                                    .show(ui, |ui| {
                                        egui::ScrollArea::both().auto_shrink([false, false]).show(
                                            ui,
                                            |ui| {
                                                for child in &body_children {
                                                    self.render_vnode(ui, child);
                                                }
                                            },
                                        );
                                    });
                            });
                        } else {
                            // No header, just render body
                            Frame::none()
                                .inner_margin(Margin::same(8.0))
                                .show(ui, |ui| {
                                    egui::ScrollArea::both().auto_shrink([false, false]).show(
                                        ui,
                                        |ui| {
                                            for child in &body_children {
                                                self.render_vnode(ui, child);
                                            }
                                        },
                                    );
                                });
                        }
                    });
                });
        });
    }

    fn render_text_container(&mut self, ui: &mut Ui, children: &[VNode]) {
        let text = self.get_text_content(children);
        ui.label(text);
    }

    fn render_input(&mut self, ui: &mut Ui, attrs: &[(String, VAttr)]) {
        let _placeholder = self.get_attr_value(attrs, "placeholder");
        let value = self.get_attr_value(attrs, "value");

        let mut text = value;
        ui.text_edit_singleline(&mut text);
    }

    fn render_textarea(&mut self, ui: &mut Ui, attrs: &[(String, VAttr)]) {
        let value = self.get_attr_value(attrs, "value");
        let mut text = value;

        ui.add(
            egui::TextEdit::multiline(&mut text)
                .desired_width(f32::INFINITY)
                .desired_rows(10),
        );
    }

    fn render_heading(&mut self, ui: &mut Ui, tag: &str, children: &[VNode]) {
        let text = self.get_text_content(children);
        let size = match tag {
            "h1" => 32.0,
            "h2" => 24.0,
            "h3" => 20.0,
            "h4" => 18.0,
            "h5" => 16.0,
            "h6" => 14.0,
            _ => 16.0,
        };

        ui.heading(RichText::new(text).size(size));
    }

    // Helper methods

    fn get_attr_value(&self, attrs: &[(String, VAttr)], name: &str) -> String {
        attrs
            .iter()
            .find(|(k, _)| k == name)
            .and_then(|(_, v)| match v {
                VAttr::Static(s) | VAttr::Dynamic(s) => Some(s.clone()),
                _ => None,
            })
            .unwrap_or_default()
    }

    fn get_event_handler(
        &self,
        attrs: &[(String, VAttr)],
        name: &str,
    ) -> Option<Rc<RefCell<dyn FnMut()>>> {
        attrs
            .iter()
            .find(|(k, _)| k == name)
            .and_then(|(_, v)| match v {
                VAttr::Event(handler) => Some(handler.clone()),
                _ => None,
            })
    }

    fn get_text_content(&self, children: &[VNode]) -> String {
        children
            .iter()
            .filter_map(|child| match child {
                VNode::Text(text) => Some(text.clone()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join("")
    }

    fn get_variant_from_classes(&self, attrs: &[(String, VAttr)]) -> String {
        let classes = self.get_attr_value(attrs, "class");
        if classes.contains("wj-button-primary") {
            "primary".to_string()
        } else if classes.contains("wj-button-danger") {
            "danger".to_string()
        } else if classes.contains("wj-button-success") {
            "success".to_string()
        } else if classes.contains("wj-button-warning") {
            "warning".to_string()
        } else {
            "secondary".to_string()
        }
    }

    fn parse_color_from_style(&self, style: &str) -> Option<Color32> {
        // Simple parser for "background-color: #RRGGBB"
        if let Some(start) = style.find("background-color:") {
            let rest = &style[start + 17..].trim();
            if let Some(end) = rest.find(';').or(Some(rest.len())) {
                let color_str = rest[..end].trim();
                if color_str.starts_with('#') && color_str.len() == 7 {
                    if let Ok(r) = u8::from_str_radix(&color_str[1..3], 16) {
                        if let Ok(g) = u8::from_str_radix(&color_str[3..5], 16) {
                            if let Ok(b) = u8::from_str_radix(&color_str[5..7], 16) {
                                return Some(Color32::from_rgb(r, g, b));
                            }
                        }
                    }
                }
            }
        }
        None
    }

    // Advanced component renderers

    fn render_tree_view(&mut self, ui: &mut Ui, children: &[VNode]) {
        egui::ScrollArea::vertical()
            .id_salt("tree_view_scroll")
            .show(ui, |ui| {
                for child in children {
                    if let VNode::Element {
                        attrs,
                        children: item_children,
                        ..
                    } = child
                    {
                        if self.get_attr_value(attrs, "class").contains("tree-item") {
                            self.render_tree_item(ui, item_children, attrs);
                        }
                    }
                }
            });
    }

    fn render_tree_item(&mut self, ui: &mut Ui, children: &[VNode], attrs: &[(String, VAttr)]) {
        let text = self.get_text_content(children);

        let response = ui.selectable_label(false, RichText::new(text).monospace());

        if response.clicked() {
            if let Some(handler) = self.get_event_handler(attrs, "on_click") {
                handler.borrow_mut()();
            }
        }
    }

    fn render_tab_panel(&mut self, ui: &mut Ui, children: &[VNode]) {
        // Find tab bar and content
        for child in children {
            if let VNode::Element {
                tag: _tag,
                attrs,
                children: tab_children,
            } = child
            {
                let classes = self.get_attr_value(attrs, "class");

                if classes.contains("tab-bar") {
                    // Render tab buttons horizontally
                    ui.horizontal(|ui| {
                        for tab_child in tab_children {
                            if let VNode::Element {
                                tag,
                                children: btn_children,
                                attrs: btn_attrs,
                            } = tab_child
                            {
                                if tag == "button" {
                                    let label = self.get_text_content(btn_children);
                                    let is_active =
                                        self.get_attr_value(btn_attrs, "class").contains("active");

                                    let button = if is_active {
                                        egui::Button::new(RichText::new(label).strong())
                                            .fill(Color32::from_rgb(0, 122, 204))
                                    } else {
                                        egui::Button::new(label).fill(Color32::from_rgb(60, 60, 60))
                                    };

                                    ui.add(button);
                                }
                            }
                        }
                    });
                } else {
                    // Render content
                    self.render_vnode(ui, child);
                }
            }
        }
    }

    fn render_split_panel(&mut self, ui: &mut Ui, classes: &str, children: &[VNode]) {
        let is_horizontal = classes.contains("horizontal");

        if is_horizontal {
            // Horizontal split - use egui's horizontal layout with proper sizing
            ui.horizontal(|ui| {
                // Force the horizontal container to use full width
                ui.set_min_width(ui.available_width());

                let total_width = ui.available_width();
                let height = ui.available_height();

                for (i, child) in children.iter().enumerate() {
                    if i > 0 {
                        ui.separator();
                    }

                    // Calculate width for each panel
                    let width = if i == 0 || i == children.len() - 1 {
                        total_width * 0.20 // Left or Right: 20%
                    } else {
                        total_width * 0.60 // Middle: 60%
                    };

                    // Use a fixed-size container
                    ui.allocate_ui_with_layout(
                        egui::vec2(width, height),
                        egui::Layout::top_down(egui::Align::Min),
                        |ui| {
                            ui.set_min_size(egui::vec2(width, height));
                            self.render_vnode(ui, child);
                        },
                    );
                }
            });
        } else {
            // Vertical split
            ui.vertical(|ui| {
                ui.set_min_width(ui.available_width());

                let width = ui.available_width();
                let total_height = ui.available_height();

                for (i, child) in children.iter().enumerate() {
                    if i > 0 {
                        ui.separator();
                    }

                    let height = if i == 0 {
                        total_height * 0.60 // Top: 60%
                    } else {
                        total_height * 0.40 // Bottom: 40%
                    };

                    ui.allocate_ui_with_layout(
                        egui::vec2(width, height),
                        egui::Layout::top_down(egui::Align::Min),
                        |ui| {
                            ui.set_min_size(egui::vec2(width, height));
                            self.render_vnode(ui, child);
                        },
                    );
                }
            });
        }
    }

    fn render_advanced_code_editor(&mut self, ui: &mut Ui, children: &[VNode]) {
        egui::ScrollArea::both()
            .id_salt("code_editor_scroll")
            .show(ui, |ui| {
                Frame::none()
                    .fill(Color32::from_rgb(30, 30, 30))
                    .inner_margin(Margin::same(10.0))
                    .show(ui, |ui| {
                        ui.horizontal(|ui| {
                            // Find line numbers and code content
                            for child in children {
                                if let VNode::Element {
                                    attrs,
                                    children: content_children,
                                    ..
                                } = child
                                {
                                    let classes = self.get_attr_value(attrs, "class");

                                    if classes.contains("line-numbers") {
                                        ui.vertical(|ui| {
                                            ui.style_mut().override_text_style =
                                                Some(egui::TextStyle::Monospace);
                                            for line_child in content_children {
                                                if let VNode::Text(text) = line_child {
                                                    ui.label(
                                                        RichText::new(text).color(
                                                            Color32::from_rgb(100, 100, 100),
                                                        ),
                                                    );
                                                }
                                            }
                                        });
                                    } else if classes.contains("code-content") {
                                        ui.vertical(|ui| {
                                            ui.style_mut().override_text_style =
                                                Some(egui::TextStyle::Monospace);
                                            for code_child in content_children {
                                                self.render_code_with_highlighting(ui, code_child);
                                            }
                                        });
                                    }
                                }
                            }
                        });
                    });
            });
    }

    fn render_code_with_highlighting(&mut self, ui: &mut Ui, vnode: &VNode) {
        match vnode {
            VNode::Element {
                tag,
                children,
                attrs,
            } => {
                if tag == "pre" {
                    for child in children {
                        Self::render_code_span(ui, child);
                    }
                } else if tag == "span" {
                    let classes = self.get_attr_value(attrs, "class");
                    let color = match classes.as_str() {
                        "keyword" => Color32::from_rgb(86, 156, 214), // Blue for keywords
                        "type" => Color32::from_rgb(78, 201, 176),    // Teal for types
                        "string" => Color32::from_rgb(206, 145, 120), // Orange for strings
                        "comment" => Color32::from_rgb(106, 153, 85), // Green for comments
                        _ => Color32::from_rgb(212, 212, 212),        // Default white
                    };

                    let text = self.get_text_content(children);
                    ui.label(RichText::new(text).color(color));
                }
            }
            VNode::Text(text) => {
                ui.label(RichText::new(text).color(Color32::from_rgb(212, 212, 212)));
            }
        }
    }

    fn render_code_span(ui: &mut Ui, vnode: &VNode) {
        match vnode {
            VNode::Element { children, .. } => {
                for child in children {
                    Self::render_code_span(ui, child);
                }
            }
            VNode::Text(text) => {
                ui.label(RichText::new(text).color(Color32::from_rgb(212, 212, 212)));
            }
        }
    }

    fn render_code_block(&mut self, ui: &mut Ui, children: &[VNode]) {
        Frame::none()
            .fill(Color32::from_rgb(30, 30, 30))
            .inner_margin(Margin::same(10.0))
            .show(ui, |ui| {
                ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
                for child in children {
                    Self::render_code_span(ui, child);
                }
            });
    }
}