aumate 0.2.8

Cross-platform desktop automation library with GUI support
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//! Widget renderer - renders WidgetDef to egui
//!
//! This module provides the rendering logic that converts widget definitions
//! into actual egui UI elements.

use super::definition::{WidgetDef, WidgetProps};
use super::events::{WidgetEvent, WidgetId};
use super::fonts::FontManager;
use super::style::{TextAlign, WidgetStyle};
use egui::{Color32, FontId, RichText, Ui, Vec2};
use std::collections::HashMap;

/// Mutable state for widgets that need it (text inputs, checkboxes, etc.)
#[derive(Debug, Clone, Default)]
pub struct WidgetState {
    /// Text value for text inputs
    pub text: String,
    /// Boolean value for checkboxes
    pub checked: bool,
    /// Numeric value for sliders
    pub value: f32,
    /// Selected index for dropdowns, radio groups, and tabs
    pub selected: usize,
}

/// Widget renderer context
pub struct WidgetRenderer {
    /// Mutable state for interactive widgets, keyed by widget ID
    pub state: HashMap<WidgetId, WidgetState>,
    /// Font manager for loading system fonts
    font_manager: FontManager,
}

impl Default for WidgetRenderer {
    fn default() -> Self {
        Self::new()
    }
}

impl WidgetRenderer {
    /// Create a new widget renderer
    pub fn new() -> Self {
        Self { state: HashMap::new(), font_manager: FontManager::new() }
    }

    /// Render a widget definition to egui, collecting events
    pub fn render(&mut self, ui: &mut Ui, widget: &WidgetDef, events: &mut Vec<WidgetEvent>) {
        let props = widget.props();

        // Skip invisible widgets
        if !props.visible {
            return;
        }

        // Apply style constraints
        self.apply_style_constraints(ui, &props.style);

        match widget {
            WidgetDef::Label { text, props } => {
                self.render_label(ui, text, props);
            }
            WidgetDef::Button { text, props } => {
                self.render_button(ui, text, props, events);
            }
            WidgetDef::TextInput { value, placeholder, password, props } => {
                self.render_text_input(ui, value, placeholder.as_deref(), *password, props, events);
            }
            WidgetDef::Checkbox { checked, label, props } => {
                self.render_checkbox(ui, *checked, label, props, events);
            }
            WidgetDef::Slider { value, min, max, step, props } => {
                self.render_slider(ui, *value, *min, *max, *step, props, events);
            }
            WidgetDef::ProgressBar { value, show_percentage, props } => {
                self.render_progress_bar(ui, *value, *show_percentage, props);
            }
            WidgetDef::Image { data, width, height, props } => {
                self.render_image(ui, data, *width, *height, props);
            }
            WidgetDef::Separator { .. } => {
                ui.separator();
            }
            WidgetDef::Spacer { size, .. } => {
                ui.add_space(*size);
            }
            WidgetDef::HBox { children, spacing, props } => {
                self.render_hbox(ui, children, *spacing, props, events);
            }
            WidgetDef::VBox { children, spacing, props } => {
                self.render_vbox(ui, children, *spacing, props, events);
            }
            WidgetDef::Grid { children, row_spacing, col_spacing, props } => {
                self.render_grid(ui, children, *row_spacing, *col_spacing, props, events);
            }
            WidgetDef::Panel { child, props } => {
                self.render_panel(ui, child, props, events);
            }
            WidgetDef::ScrollArea { child, max_height, props } => {
                self.render_scroll_area(ui, child, *max_height, props, events);
            }
            WidgetDef::Group { title, child, collapsed, props } => {
                self.render_group(ui, title.as_deref(), child, *collapsed, props, events);
            }
            WidgetDef::Dropdown { options, selected, placeholder, props } => {
                self.render_dropdown(ui, options, *selected, placeholder.as_deref(), props, events);
            }
            WidgetDef::RadioGroup { options, selected, horizontal, props } => {
                self.render_radio_group(ui, options, *selected, *horizontal, props, events);
            }
            WidgetDef::TextArea { value, placeholder, rows, props } => {
                self.render_text_area(ui, value, placeholder.as_deref(), *rows, props, events);
            }

            WidgetDef::Tabs { tabs, active, props } => {
                self.render_tabs(ui, tabs, *active, props, events);
            }
            WidgetDef::Link { text, props } => {
                self.render_link(ui, text, props, events);
            }
            WidgetDef::SelectableLabel { text, selected, props } => {
                self.render_selectable_label(ui, text, *selected, props, events);
            }
            WidgetDef::DragValue { value, min, max, speed, prefix, suffix, decimals, props } => {
                self.render_drag_value(
                    ui,
                    *value,
                    *min,
                    *max,
                    *speed,
                    prefix.as_deref(),
                    suffix.as_deref(),
                    *decimals,
                    props,
                    events,
                );
            }
            WidgetDef::ColorPicker { color, alpha, props } => {
                self.render_color_picker(ui, *color, *alpha, props, events);
            }
            WidgetDef::Hyperlink { text, url, new_tab, props } => {
                self.render_hyperlink(ui, text, url, *new_tab, props, events);
            }
            WidgetDef::ImageButton { data, width, height, frame, selected, tint, props } => {
                self.render_image_button(
                    ui, data, *width, *height, *frame, *selected, *tint, props, events,
                );
            }
        }
    }

    fn apply_style_constraints(&self, ui: &mut Ui, style: &WidgetStyle) {
        if let Some(min_width) = style.min_width {
            ui.set_min_width(min_width);
        }
        if let Some(min_height) = style.min_height {
            ui.set_min_height(min_height);
        }
        if let Some(max_width) = style.max_width {
            ui.set_max_width(max_width);
        }
        if let Some(max_height) = style.max_height {
            ui.set_max_height(max_height);
        }
    }

    fn render_label(&self, ui: &mut Ui, text: &str, props: &WidgetProps) {
        let mut rich_text = RichText::new(text);

        // Apply text color
        if let Some([r, g, b, a]) = props.style.text_color {
            rich_text = rich_text.color(Color32::from_rgba_unmultiplied(r, g, b, a));
        }

        // Apply font family and size
        let size = props.style.font_size.unwrap_or(14.0);
        if let Some(ref family_name) = props.style.font_family {
            let family = self.font_manager.ensure_font_loaded(ui.ctx(), family_name);
            rich_text = rich_text.font(FontId::new(size, family));
        } else if props.style.font_size.is_some() {
            rich_text = rich_text.size(size);
        }

        // Create label with alignment
        let label = egui::Label::new(rich_text);

        // Handle text alignment
        match props.style.text_align {
            Some(TextAlign::Center) => {
                ui.centered_and_justified(|ui| {
                    ui.add(label);
                });
            }
            Some(TextAlign::Right) => {
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    ui.add(label);
                });
            }
            _ => {
                ui.add(label);
            }
        }

        // Note: Labels don't support hover tooltips directly in egui
        // Tooltips for labels would require wrapping in a sense area
        let _ = &props.tooltip; // Acknowledge but don't use for labels
    }

    fn render_button(
        &self,
        ui: &mut Ui,
        text: &str,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let mut button = egui::Button::new(text);

        // Apply min size from style
        if props.style.min_width.is_some() || props.style.min_height.is_some() {
            let min_size = Vec2::new(
                props.style.min_width.unwrap_or(0.0),
                props.style.min_height.unwrap_or(0.0),
            );
            button = button.min_size(min_size);
        }

        let response = ui.add_enabled(props.enabled, button);

        if response.clicked() {
            if let Some(id) = &props.id {
                events.push(WidgetEvent::ButtonClick { id: id.clone() });
            }
        }

        // Tooltip
        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_text_input(
        &mut self,
        ui: &mut Ui,
        initial_value: &str,
        placeholder: Option<&str>,
        password: bool,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        // Get or initialize state
        let id = props.id.clone().unwrap_or_default();
        let state = self.state.entry(id.clone()).or_insert_with(|| WidgetState {
            text: initial_value.to_string(),
            ..Default::default()
        });

        let mut text_edit = egui::TextEdit::singleline(&mut state.text);

        if let Some(hint) = placeholder {
            text_edit = text_edit.hint_text(hint);
        }

        if password {
            text_edit = text_edit.password(true);
        }

        // Apply min width
        if let Some(min_width) = props.style.min_width {
            text_edit = text_edit.desired_width(min_width);
        }

        let response = ui.add_enabled(props.enabled, text_edit);

        // Emit text changed event
        if response.changed() && !id.is_empty() {
            events.push(WidgetEvent::TextChanged { id: id.clone(), value: state.text.clone() });
        }

        // Emit submit event on Enter
        if response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) && !id.is_empty()
        {
            events.push(WidgetEvent::TextSubmit { id: id.clone(), value: state.text.clone() });
        }

        // Tooltip
        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_checkbox(
        &mut self,
        ui: &mut Ui,
        initial_checked: bool,
        label: &str,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();
        let state = self
            .state
            .entry(id.clone())
            .or_insert_with(|| WidgetState { checked: initial_checked, ..Default::default() });

        let response =
            ui.add_enabled(props.enabled, egui::Checkbox::new(&mut state.checked, label));

        if response.changed() && !id.is_empty() {
            events.push(WidgetEvent::CheckboxChanged { id: id.clone(), checked: state.checked });
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn render_slider(
        &mut self,
        ui: &mut Ui,
        initial_value: f32,
        min: f32,
        max: f32,
        step: Option<f32>,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();
        let state = self
            .state
            .entry(id.clone())
            .or_insert_with(|| WidgetState { value: initial_value, ..Default::default() });

        let mut slider = egui::Slider::new(&mut state.value, min..=max);

        if let Some(s) = step {
            slider = slider.step_by(s as f64);
        }

        let response = ui.add_enabled(props.enabled, slider);

        if response.changed() && !id.is_empty() {
            events.push(WidgetEvent::SliderChanged { id: id.clone(), value: state.value });
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_progress_bar(
        &self,
        ui: &mut Ui,
        value: f32,
        show_percentage: bool,
        props: &WidgetProps,
    ) {
        let progress = value.clamp(0.0, 1.0);
        let mut bar = egui::ProgressBar::new(progress);

        if show_percentage {
            bar = bar.show_percentage();
        }

        let response = ui.add(bar);

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_image(&self, ui: &mut Ui, data: &[u8], width: u32, height: u32, props: &WidgetProps) {
        // Create texture from RGBA data
        let size = [width as usize, height as usize];
        let image = egui::ColorImage::from_rgba_unmultiplied(size, data);
        let texture = ui.ctx().load_texture(
            format!("widget_image_{}", props.id.as_deref().unwrap_or("default")),
            image,
            egui::TextureOptions::default(),
        );

        let display_size = Vec2::new(width as f32, height as f32);
        let response = ui.add(egui::Image::new(&texture).fit_to_exact_size(display_size));

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_hbox(
        &mut self,
        ui: &mut Ui,
        children: &[WidgetDef],
        spacing: f32,
        _props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        ui.horizontal(|ui| {
            ui.spacing_mut().item_spacing.x = spacing;
            for child in children {
                self.render(ui, child, events);
            }
        });
    }

    fn render_vbox(
        &mut self,
        ui: &mut Ui,
        children: &[WidgetDef],
        spacing: f32,
        _props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        ui.vertical(|ui| {
            ui.spacing_mut().item_spacing.y = spacing;
            for child in children {
                self.render(ui, child, events);
            }
        });
    }

    fn render_grid(
        &mut self,
        ui: &mut Ui,
        children: &[Vec<WidgetDef>],
        row_spacing: f32,
        col_spacing: f32,
        _props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        egui::Grid::new(ui.id().with("grid")).spacing(Vec2::new(col_spacing, row_spacing)).show(
            ui,
            |ui| {
                for row in children {
                    for cell in row {
                        self.render(ui, cell, events);
                    }
                    ui.end_row();
                }
            },
        );
    }

    fn render_panel(
        &mut self,
        ui: &mut Ui,
        child: &WidgetDef,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let padding = props.style.padding.left;
        let frame = if let Some([r, g, b, a]) = props.style.background_color {
            egui::Frame::default()
                .fill(Color32::from_rgba_unmultiplied(r, g, b, a))
                .inner_margin(padding)
        } else {
            egui::Frame::default().inner_margin(padding)
        };

        frame.show(ui, |ui| {
            self.render(ui, child, events);
        });
    }

    fn render_scroll_area(
        &mut self,
        ui: &mut Ui,
        child: &WidgetDef,
        max_height: Option<f32>,
        _props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let mut scroll = egui::ScrollArea::vertical();

        if let Some(height) = max_height {
            scroll = scroll.max_height(height);
        }

        scroll.show(ui, |ui| {
            self.render(ui, child, events);
        });
    }

    fn render_group(
        &mut self,
        ui: &mut Ui,
        title: Option<&str>,
        child: &WidgetDef,
        collapsed: bool,
        _props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        if let Some(title) = title {
            egui::CollapsingHeader::new(title).default_open(!collapsed).show(ui, |ui| {
                self.render(ui, child, events);
            });
        } else {
            ui.group(|ui| {
                self.render(ui, child, events);
            });
        }
    }

    fn render_dropdown(
        &mut self,
        ui: &mut Ui,
        options: &[String],
        initial_selected: Option<usize>,
        placeholder: Option<&str>,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();
        let state = self.state.entry(id.clone()).or_insert_with(|| WidgetState {
            value: initial_selected.map(|i| i as f32).unwrap_or(-1.0),
            ..Default::default()
        });

        let selected_idx = if state.value >= 0.0 { Some(state.value as usize) } else { None };
        let display_text =
            selected_idx.and_then(|i| options.get(i)).cloned().unwrap_or_else(|| {
                placeholder.map(|s| s.to_string()).unwrap_or_else(|| "Select...".to_string())
            });

        let response = egui::ComboBox::from_id_salt(ui.id().with(&id))
            .selected_text(display_text)
            .show_ui(ui, |ui| {
                for (idx, option) in options.iter().enumerate() {
                    let is_selected = selected_idx == Some(idx);
                    if ui.selectable_label(is_selected, option).clicked() {
                        state.value = idx as f32;
                        if !id.is_empty() {
                            events.push(WidgetEvent::SelectionChanged {
                                id: id.clone(),
                                index: idx,
                                value: option.clone(),
                            });
                        }
                    }
                }
            });

        if let Some(tooltip) = &props.tooltip {
            response.response.on_hover_text(tooltip);
        }
    }

    fn render_radio_group(
        &mut self,
        ui: &mut Ui,
        options: &[String],
        initial_selected: Option<usize>,
        horizontal: bool,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();
        let state = self.state.entry(id.clone()).or_insert_with(|| WidgetState {
            value: initial_selected.map(|i| i as f32).unwrap_or(-1.0),
            ..Default::default()
        });

        let selected_idx = if state.value >= 0.0 { Some(state.value as usize) } else { None };

        let render_options = |ui: &mut Ui| {
            for (idx, option) in options.iter().enumerate() {
                let is_selected = selected_idx == Some(idx);
                if ui.radio(is_selected, option).clicked() {
                    state.value = idx as f32;
                    if !id.is_empty() {
                        events.push(WidgetEvent::RadioChanged {
                            id: id.clone(),
                            index: idx,
                            value: option.clone(),
                        });
                    }
                }
            }
        };

        if horizontal {
            ui.horizontal(render_options);
        } else {
            ui.vertical(render_options);
        }
    }

    fn render_text_area(
        &mut self,
        ui: &mut Ui,
        initial_value: &str,
        placeholder: Option<&str>,
        rows: u32,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();
        let state = self.state.entry(id.clone()).or_insert_with(|| WidgetState {
            text: initial_value.to_string(),
            ..Default::default()
        });

        let mut text_edit = egui::TextEdit::multiline(&mut state.text).desired_rows(rows as usize);

        if let Some(hint) = placeholder {
            text_edit = text_edit.hint_text(hint);
        }

        if let Some(min_width) = props.style.min_width {
            text_edit = text_edit.desired_width(min_width);
        }

        let response = ui.add_enabled(props.enabled, text_edit);

        if response.changed() && !id.is_empty() {
            events.push(WidgetEvent::TextChanged { id: id.clone(), value: state.text.clone() });
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_tabs(
        &mut self,
        ui: &mut Ui,
        tabs: &[(String, Box<WidgetDef>)],
        initial_active: usize,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();
        let state = self
            .state
            .entry(id.clone())
            .or_insert_with(|| WidgetState { selected: initial_active, ..Default::default() });

        // Render tab bar
        ui.horizontal(|ui| {
            for (i, (label, _)) in tabs.iter().enumerate() {
                let is_active = state.selected == i;
                let tab_text = if is_active {
                    egui::RichText::new(label).strong()
                } else {
                    egui::RichText::new(label)
                };

                if ui.selectable_label(is_active, tab_text).clicked() && !is_active {
                    state.selected = i;
                    if !id.is_empty() {
                        events.push(WidgetEvent::TabChanged {
                            id: id.clone(),
                            index: i,
                            label: label.clone(),
                        });
                    }
                }
            }
        });

        ui.separator();

        // Render active tab content
        if let Some((_, content)) = tabs.get(state.selected) {
            self.render(ui, content, events);
        }
    }

    fn render_link(
        &self,
        ui: &mut Ui,
        text: &str,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let response = ui.add_enabled(props.enabled, egui::Link::new(text));

        if response.clicked() {
            if let Some(id) = &props.id {
                events.push(WidgetEvent::LinkClicked { id: id.clone() });
            }
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_selectable_label(
        &mut self,
        ui: &mut Ui,
        text: &str,
        initial_selected: bool,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();
        let state = self
            .state
            .entry(id.clone())
            .or_insert_with(|| WidgetState { checked: initial_selected, ..Default::default() });

        // Use Button::selected() instead of deprecated SelectableLabel
        let button = egui::Button::new(text).selected(state.checked);
        let response = ui.add_enabled(props.enabled, button);

        if response.clicked() {
            state.checked = !state.checked;
            if !id.is_empty() {
                events.push(WidgetEvent::SelectableLabelChanged {
                    id: id.clone(),
                    selected: state.checked,
                });
            }
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn render_drag_value(
        &mut self,
        ui: &mut Ui,
        initial_value: f64,
        min: Option<f64>,
        max: Option<f64>,
        speed: f64,
        prefix: Option<&str>,
        suffix: Option<&str>,
        decimals: Option<usize>,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();
        let state = self
            .state
            .entry(id.clone())
            .or_insert_with(|| WidgetState { value: initial_value as f32, ..Default::default() });

        // Store as f64 for precision
        let mut value = state.value as f64;
        let mut drag = egui::DragValue::new(&mut value).speed(speed);

        if let Some(min_val) = min {
            drag = drag.range(min_val..=max.unwrap_or(f64::MAX));
        } else if let Some(max_val) = max {
            drag = drag.range(f64::MIN..=max_val);
        }

        if let Some(prefix_str) = prefix {
            drag = drag.prefix(prefix_str);
        }

        if let Some(suffix_str) = suffix {
            drag = drag.suffix(suffix_str);
        }

        if let Some(dec) = decimals {
            drag = drag.max_decimals(dec);
        }

        let response = ui.add_enabled(props.enabled, drag);

        if response.changed() {
            state.value = value as f32;
            if !id.is_empty() {
                events.push(WidgetEvent::DragValueChanged { id: id.clone(), value });
            }
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_color_picker(
        &mut self,
        ui: &mut Ui,
        initial_color: [u8; 4],
        alpha: bool,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let id = props.id.clone().unwrap_or_default();

        // Store color in state - we need to use a separate storage approach
        // since WidgetState doesn't have a color field
        let state = self.state.entry(id.clone()).or_insert_with(|| {
            // Encode color in value field as 32-bit float
            let color_u32 = u32::from_le_bytes(initial_color);
            WidgetState { value: f32::from_bits(color_u32), ..Default::default() }
        });

        // Decode color from value
        let color_u32 = state.value.to_bits();
        let color_bytes = color_u32.to_le_bytes();
        let mut color = egui::Color32::from_rgba_unmultiplied(
            color_bytes[0],
            color_bytes[1],
            color_bytes[2],
            color_bytes[3],
        );

        let response = if alpha {
            ui.color_edit_button_srgba(&mut color)
        } else {
            // For no-alpha, use RGB only
            let mut rgb = [color.r(), color.g(), color.b()];
            let response = ui.color_edit_button_srgb(&mut rgb);
            color = egui::Color32::from_rgb(rgb[0], rgb[1], rgb[2]);
            response
        };

        if response.changed() {
            let new_color = [color.r(), color.g(), color.b(), color.a()];
            let new_color_u32 = u32::from_le_bytes(new_color);
            state.value = f32::from_bits(new_color_u32);

            if !id.is_empty() {
                events.push(WidgetEvent::ColorChanged { id: id.clone(), color: new_color });
            }
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    fn render_hyperlink(
        &self,
        ui: &mut Ui,
        text: &str,
        url: &str,
        _new_tab: bool,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        let response =
            ui.add_enabled(props.enabled, egui::Hyperlink::from_label_and_url(text, url));

        // Note: egui's Hyperlink automatically opens the URL on click
        // We emit an event for tracking purposes
        if response.clicked() {
            if let Some(id) = &props.id {
                events.push(WidgetEvent::HyperlinkClicked { id: id.clone(), url: url.to_string() });
            }
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn render_image_button(
        &self,
        ui: &mut Ui,
        data: &[u8],
        width: u32,
        height: u32,
        frame: bool,
        selected: bool,
        tint: Option<[u8; 4]>,
        props: &WidgetProps,
        events: &mut Vec<WidgetEvent>,
    ) {
        // Create texture from RGBA data
        let size = [width as usize, height as usize];
        let image_data = egui::ColorImage::from_rgba_unmultiplied(size, data);
        let texture = ui.ctx().load_texture(
            format!("image_button_{}", props.id.as_deref().unwrap_or("default")),
            image_data,
            egui::TextureOptions::default(),
        );

        let display_size = Vec2::new(width as f32, height as f32);
        let mut img = egui::Image::new(&texture).fit_to_exact_size(display_size);

        if let Some([r, g, b, a]) = tint {
            img = img.tint(Color32::from_rgba_unmultiplied(r, g, b, a));
        }

        // Use Button::image() instead of deprecated ImageButton
        let mut button = egui::Button::image(img).frame(frame).selected(selected);

        // For image buttons without frame, use minimal styling
        if !frame {
            button = button.fill(Color32::TRANSPARENT);
        }

        let response = ui.add_enabled(props.enabled, button);

        if response.clicked() {
            if let Some(id) = &props.id {
                events.push(WidgetEvent::ButtonClick { id: id.clone() });
            }
        }

        if let Some(tooltip) = &props.tooltip {
            response.on_hover_text(tooltip);
        }
    }

    /// Update widget state from external source (e.g., command)
    pub fn update_state(&mut self, id: &WidgetId, update: WidgetStateUpdate) {
        if let Some(state) = self.state.get_mut(id) {
            match update {
                WidgetStateUpdate::SetText(text) => state.text = text,
                WidgetStateUpdate::SetChecked(checked) => state.checked = checked,
                WidgetStateUpdate::SetValue(value) => state.value = value,
            }
        }
    }

    /// Clear all widget state
    pub fn clear_state(&mut self) {
        self.state.clear();
    }
}

/// Updates that can be applied to widget state
#[derive(Debug, Clone)]
pub enum WidgetStateUpdate {
    SetText(String),
    SetChecked(bool),
    SetValue(f32),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_renderer_creation() {
        let renderer = WidgetRenderer::new();
        assert!(renderer.state.is_empty());
    }

    #[test]
    fn test_state_update() {
        let mut renderer = WidgetRenderer::new();
        renderer.state.insert(
            "test".to_string(),
            WidgetState { text: "initial".to_string(), ..Default::default() },
        );

        renderer
            .update_state(&"test".to_string(), WidgetStateUpdate::SetText("updated".to_string()));

        assert_eq!(renderer.state.get("test").unwrap().text, "updated");
    }
}