oxiui-egui 0.1.0

egui/eframe adapter for OxiUI
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
#![forbid(unsafe_code)]
#![warn(missing_docs)]
//! `oxiui-egui` — egui/eframe adapter for OxiUI.
//!
//! Converts OxiUI [`Palette`] to [`egui::Visuals`], loads OxiFont bytes
//! into egui's font system, and provides [`EguiUiCtx`] which implements
//! [`UiCtx`] in terms of egui's `Ui` object.

use std::sync::Arc;

use oxiui_core::{
    ButtonResponse, CheckboxResponse, Color, DropdownResponse, Key, Modifiers, MouseButton,
    Palette, Size, SliderResponse, TextInputResponse, UiCtx, UiError, UiEvent, Widget,
    WidgetResponse,
};
use oxiui_theme::DesignTokens;

/// An [`UiCtx`] implementation backed by an egui [`egui::Ui`].
///
/// All heading/label/button calls are forwarded directly to egui. Extended
/// widget methods (`text_input`, `checkbox`, `slider`, `dropdown`, `image`,
/// `separator`, `spacer`, `scroll_area`, `tooltip`, `popup`, `modal`) are
/// also implemented and forward to their egui equivalents.
pub struct EguiUiCtx<'a> {
    ui: &'a mut egui::Ui,
    /// The egui response from the most recently rendered widget, if any.
    last_response: Option<egui::Response>,
    /// Monotonically incrementing sequence used to generate per-widget id salts.
    id_seq: usize,
}

impl<'a> EguiUiCtx<'a> {
    /// Wrap an egui `Ui` reference as an [`UiCtx`].
    pub fn new(ui: &'a mut egui::Ui) -> Self {
        Self {
            ui,
            last_response: None,
            id_seq: 0,
        }
    }

    /// Return a reference to the egui response produced by the most recently
    /// rendered widget, if one is stored.
    pub fn response(&self) -> Option<&egui::Response> {
        self.last_response.as_ref()
    }

    /// Advance the id sequence and return a fresh [`egui::Id`] salt value.
    fn next_salt(&mut self) -> egui::Id {
        let s = self.id_seq;
        self.id_seq += 1;
        egui::Id::new(("oxiui_widget", s))
    }
}

impl<'a> EguiUiCtx<'a> {
    /// Read the most-recently-copied text from egui's output command queue.
    ///
    /// Returns `Some(text)` if a [`egui::OutputCommand::CopyText`] command was
    /// queued in the current frame, otherwise `None`. Note that this reflects
    /// text *set* via egui's copy mechanism within the same frame; it does not
    /// read from the OS clipboard.
    pub fn clipboard_get(&self) -> Option<String> {
        self.ui.ctx().output(|o| {
            o.commands.iter().find_map(|cmd| {
                if let egui::OutputCommand::CopyText(text) = cmd {
                    if text.is_empty() {
                        None
                    } else {
                        Some(text.clone())
                    }
                } else {
                    None
                }
            })
        })
    }

    /// Queue a copy-to-clipboard command via egui.
    ///
    /// The `text` is enqueued as an [`egui::OutputCommand::CopyText`] and will
    /// be forwarded to the OS clipboard by the egui integration at the end of
    /// the frame.
    pub fn clipboard_set(&self, text: &str) {
        self.ui.ctx().copy_text(text.to_owned());
    }
}

impl<'a> UiCtx for EguiUiCtx<'a> {
    fn heading(&mut self, text: &str) {
        self.ui.heading(text);
    }

    fn label(&mut self, text: &str) {
        self.ui.label(text);
    }

    fn button(&mut self, label: &str) -> ButtonResponse {
        let resp = self.ui.button(label);
        ButtonResponse {
            clicked: resp.clicked(),
            hovered: resp.hovered(),
        }
    }

    fn text_input(&mut self, text: &str) -> TextInputResponse {
        let mut s = text.to_owned();
        let r = self.ui.add(egui::TextEdit::singleline(&mut s));
        let changed = r.changed();
        self.last_response = Some(r);
        TextInputResponse::supported(s, changed)
    }

    fn checkbox(&mut self, label: &str, checked: bool) -> CheckboxResponse {
        let mut c = checked;
        let r = self.ui.checkbox(&mut c, label);
        let changed = r.changed();
        self.last_response = Some(r);
        CheckboxResponse::supported(c, changed)
    }

    fn slider(&mut self, value: f64, range: std::ops::RangeInclusive<f64>) -> SliderResponse {
        let mut v = value;
        let r = self.ui.add(egui::Slider::new(&mut v, range));
        let changed = r.changed();
        self.last_response = Some(r);
        SliderResponse::supported(v, changed)
    }

    fn dropdown(&mut self, options: &[&str], selected: usize) -> DropdownResponse {
        let mut sel = selected.min(options.len().saturating_sub(1));
        let salt = self.next_salt();
        let r =
            egui::ComboBox::from_id_salt(salt)
                .show_index(self.ui, &mut sel, options.len(), |i| options[i]);
        let changed = r.changed();
        self.last_response = Some(r);
        DropdownResponse::supported(sel, changed)
    }

    fn image(&mut self, uri: &str, size: Option<Size>) -> WidgetResponse {
        let mut img = egui::Image::from_uri(uri.to_owned());
        if let Some(s) = size {
            img = img.fit_to_exact_size(egui::vec2(s.width, s.height));
        }
        let r = self.ui.add(img);
        self.last_response = Some(r);
        WidgetResponse::supported()
    }

    fn separator(&mut self) -> WidgetResponse {
        let r = self.ui.separator();
        self.last_response = Some(r);
        WidgetResponse::supported()
    }

    fn spacer(&mut self, size: f32) -> WidgetResponse {
        self.ui.add_space(size);
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn scroll_area(&mut self, content: &mut dyn FnMut(&mut dyn UiCtx)) -> WidgetResponse {
        egui::ScrollArea::vertical().show(self.ui, |ui| {
            let mut child = EguiUiCtx::new(ui);
            content(&mut child);
        });
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn tooltip(&mut self, text: &str) -> WidgetResponse {
        if let Some(r) = self.last_response.take() {
            self.last_response = Some(r.on_hover_text(text));
            WidgetResponse::supported()
        } else {
            WidgetResponse::unsupported()
        }
    }

    fn popup(&mut self, content: &mut dyn FnMut(&mut dyn UiCtx)) -> WidgetResponse {
        let ctx = self.ui.ctx().clone();
        let salt = self.next_salt();
        egui::Window::new("")
            .id(egui::Id::new(("oxiui_popup", salt)))
            .title_bar(false)
            .resizable(false)
            .show(&ctx, |ui| {
                let mut child = EguiUiCtx::new(ui);
                content(&mut child);
            });
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn modal(&mut self, title: &str, content: &mut dyn FnMut(&mut dyn UiCtx)) -> WidgetResponse {
        let ctx = self.ui.ctx().clone();
        let salt = self.next_salt();
        let title = title.to_owned();
        egui::Modal::new(egui::Id::new(("oxiui_modal", salt))).show(&ctx, |ui| {
            ui.heading(&title);
            let mut child = EguiUiCtx::new(ui);
            content(&mut child);
        });
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn horizontal(&mut self, content: &mut dyn FnMut(&mut dyn UiCtx)) -> WidgetResponse {
        self.ui.horizontal(|ui| {
            let mut child = EguiUiCtx::new(ui);
            content(&mut child);
        });
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn vertical(&mut self, content: &mut dyn FnMut(&mut dyn UiCtx)) -> WidgetResponse {
        self.ui.vertical(|ui| {
            let mut child = EguiUiCtx::new(ui);
            content(&mut child);
        });
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn grid(&mut self, cols: usize, content: &mut dyn FnMut(&mut dyn UiCtx)) -> WidgetResponse {
        let salt = self.next_salt();
        egui::Grid::new(salt).num_columns(cols).show(self.ui, |ui| {
            let mut child = EguiUiCtx::new(ui);
            content(&mut child);
        });
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn menu_bar(&mut self, content: &mut dyn FnMut(&mut dyn UiCtx)) -> WidgetResponse {
        egui::MenuBar::new().ui(self.ui, |ui| {
            let mut child = EguiUiCtx::new(ui);
            content(&mut child);
        });
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn rich_text(&mut self, spans: &[oxiui_core::RichTextSpan]) -> WidgetResponse {
        use egui::text::{LayoutJob, TextFormat};
        let mut job = LayoutJob::default();
        for span in spans {
            let color = egui::Color32::from_rgba_unmultiplied(
                span.color[0],
                span.color[1],
                span.color[2],
                span.color[3],
            );
            // `TextFormat` in egui 0.34 has no `bold` field; bold spans are
            // rendered using the same font weight as non-bold (deviation noted).
            // `font_family` override is not applied when `TextFormat` is built
            // this way; it would require a named `FontFamily` registered in the
            // egui `FontDefinitions` (deviation noted).
            let font_id = egui::FontId::proportional(span.font_size);
            job.append(
                &span.text,
                0.0,
                TextFormat {
                    color,
                    font_id,
                    italics: span.italic,
                    ..Default::default()
                },
            );
        }
        let r = self.ui.label(job);
        self.last_response = Some(r);
        WidgetResponse::supported()
    }

    fn drag_source(&mut self, id: u64, content: &mut dyn FnMut(&mut dyn UiCtx)) -> WidgetResponse {
        // `dnd_drag_source` returns `InnerResponse<R>` (not a tuple).
        self.ui.dnd_drag_source(egui::Id::new(id), id, |ui| {
            let mut child = EguiUiCtx::new(ui);
            content(&mut child);
        });
        self.last_response = None;
        WidgetResponse::supported()
    }

    fn drop_target(
        &mut self,
        accept_ids: &[u64],
        content: &mut dyn FnMut(&mut dyn UiCtx),
    ) -> WidgetResponse {
        // `dnd_drop_zone` returns `(InnerResponse<R>, Option<Arc<Payload>>)`.
        // `WidgetResponse` has no `drag_dropped` field; accept-id filtering
        // cannot be surfaced to the caller through the current response type
        // (deviation noted). The payload is checked so egui performs the correct
        // drop-zone highlighting, but the result is not returned.
        let (_inner, payload) = self
            .ui
            .dnd_drop_zone::<u64, ()>(egui::Frame::default(), |ui| {
                let mut child = EguiUiCtx::new(ui);
                content(&mut child);
            });
        // Evaluate acceptance purely to suppress the unused-variable warning.
        let _accepted = payload
            .as_deref()
            .map(|p| accept_ids.contains(p))
            .unwrap_or(false);
        self.last_response = None;
        WidgetResponse::supported()
    }
}

// ── Event helpers ────────────────────────────────────────────────────────────

/// Map an OxiUI [`Key`] to the closest [`egui::Key`].
///
/// For any variant without an exact egui equivalent the fallback is
/// [`egui::Key::F12`] (which is noted in the deviation log).
///
/// Deviation note: `Key::Character(String)` and `Key::Named(String)` are
/// forwarded via [`egui::Key::from_name`]; if the name is not recognised by
/// egui, `F12` is used as the fallback.
fn map_key(key: &Key) -> egui::Key {
    match key {
        Key::Enter => egui::Key::Enter,
        Key::Tab => egui::Key::Tab,
        Key::Space => egui::Key::Space,
        Key::Backspace => egui::Key::Backspace,
        Key::Delete => egui::Key::Delete,
        Key::Escape => egui::Key::Escape,
        Key::ArrowLeft => egui::Key::ArrowLeft,
        Key::ArrowRight => egui::Key::ArrowRight,
        Key::ArrowUp => egui::Key::ArrowUp,
        Key::ArrowDown => egui::Key::ArrowDown,
        Key::Home => egui::Key::Home,
        Key::End => egui::Key::End,
        Key::PageUp => egui::Key::PageUp,
        Key::PageDown => egui::Key::PageDown,
        Key::Function(n) => map_function_key(*n),
        Key::Character(s) => egui::Key::from_name(s.as_str()).unwrap_or(egui::Key::F12),
        Key::Named(s) => egui::Key::from_name(s.as_str()).unwrap_or(egui::Key::F12),
        // Non-exhaustive: any future Key variants fall back to F12.
        _ => egui::Key::F12,
    }
}

/// Map a function-key number (1-based) to the corresponding [`egui::Key`].
///
/// egui supports F1–F35. Numbers above 35 fall back to [`egui::Key::F12`].
fn map_function_key(n: u8) -> egui::Key {
    match n {
        1 => egui::Key::F1,
        2 => egui::Key::F2,
        3 => egui::Key::F3,
        4 => egui::Key::F4,
        5 => egui::Key::F5,
        6 => egui::Key::F6,
        7 => egui::Key::F7,
        8 => egui::Key::F8,
        9 => egui::Key::F9,
        10 => egui::Key::F10,
        11 => egui::Key::F11,
        12 => egui::Key::F12,
        13 => egui::Key::F13,
        14 => egui::Key::F14,
        15 => egui::Key::F15,
        16 => egui::Key::F16,
        17 => egui::Key::F17,
        18 => egui::Key::F18,
        19 => egui::Key::F19,
        20 => egui::Key::F20,
        21 => egui::Key::F21,
        22 => egui::Key::F22,
        23 => egui::Key::F23,
        24 => egui::Key::F24,
        25 => egui::Key::F25,
        26 => egui::Key::F26,
        27 => egui::Key::F27,
        28 => egui::Key::F28,
        29 => egui::Key::F29,
        30 => egui::Key::F30,
        31 => egui::Key::F31,
        32 => egui::Key::F32,
        33 => egui::Key::F33,
        34 => egui::Key::F34,
        35 => egui::Key::F35,
        _ => egui::Key::F12,
    }
}

/// Map OxiUI [`Modifiers`] to [`egui::Modifiers`].
fn map_modifiers(m: &Modifiers) -> egui::Modifiers {
    egui::Modifiers {
        alt: m.alt,
        ctrl: m.ctrl,
        shift: m.shift,
        mac_cmd: false,
        command: m.ctrl || m.meta,
    }
}

/// Map an OxiUI [`MouseButton`] to an [`egui::PointerButton`].
///
/// `MouseButton::Other(_)` has no egui counterpart and is mapped to
/// `PointerButton::Extra1` as the closest approximation.
fn map_mouse_button(b: &MouseButton) -> egui::PointerButton {
    match b {
        MouseButton::Left => egui::PointerButton::Primary,
        MouseButton::Right => egui::PointerButton::Secondary,
        MouseButton::Middle => egui::PointerButton::Middle,
        MouseButton::Other(_) => egui::PointerButton::Extra1,
    }
}

// ── palette_to_egui_visuals (unchanged signature — DO NOT modify) ─────────────

/// Convert an OxiUI [`Palette`] into an [`egui::Visuals`] colour scheme.
///
/// Maps semantic OxiUI colours to their closest egui counterparts:
/// - `palette.text`       → `visuals.override_text_color`
/// - `palette.background` → `visuals.panel_fill`
/// - `palette.surface`    → `visuals.window_fill`
/// - `palette.primary`    → `visuals.selection.bg_fill` + `visuals.hyperlink_color`
///
/// Deviation note: `oxiui_core::Palette` has no `error`/`warning`/`success`
/// fields (those live on `oxiui_theme::ExtendedPalette`). Therefore
/// `warn_fg_color` and `error_fg_color` are left at their egui defaults.
pub fn palette_to_egui_visuals(palette: &Palette) -> egui::Visuals {
    fn c(col: &Color) -> egui::Color32 {
        egui::Color32::from_rgba_unmultiplied(col.0, col.1, col.2, col.3)
    }
    let mut v = egui::Visuals::dark();
    v.override_text_color = Some(c(&palette.text));
    v.panel_fill = c(&palette.background);
    v.window_fill = c(&palette.surface);
    v.selection.bg_fill = c(&palette.primary);
    v.hyperlink_color = c(&palette.primary);
    v
}

/// Map a [`Palette`] and [`DesignTokens`] to a full [`egui::Style`].
///
/// Calls [`palette_to_egui_visuals`] for the colour visuals, then additionally
/// maps design-token spacing and border-radius values into the egui style:
///
/// - `tokens.spacing(SpacingStep::Sm)` → `style.spacing.item_spacing`
/// - `tokens.radius(RadiusStep::Md)`   → `style.visuals.menu_corner_radius`
///   and `style.visuals.window_corner_radius`
///
/// The result is a fully configured [`egui::Style`] that can be applied with
/// [`egui::Context::set_style`].
pub fn palette_to_egui_visuals_with_tokens(
    palette: &Palette,
    tokens: &DesignTokens,
) -> egui::Style {
    use oxiui_theme::{RadiusStep, SpacingStep};

    // Build the visuals first so we can apply token overrides before
    // assembling the style struct.
    let mut visuals = palette_to_egui_visuals(palette);

    // Map border-radius: use Md (4 px by default) for menus and windows.
    // CornerRadius::same takes a u8; clamp f32 to [0, 255] to avoid overflow.
    let radius_val = tokens.radius(RadiusStep::Md).round().clamp(0.0, 255.0) as u8;
    let corner_radius = egui::CornerRadius::same(radius_val);
    visuals.menu_corner_radius = corner_radius;
    visuals.window_corner_radius = corner_radius;

    // Map spacing: use Sm (8 px by default) for widget item spacing.
    let spacing_val = tokens.spacing(SpacingStep::Sm);
    let spacing = egui::style::Spacing {
        item_spacing: egui::vec2(spacing_val, spacing_val),
        ..egui::style::Spacing::default()
    };

    egui::Style {
        visuals,
        spacing,
        ..egui::Style::default()
    }
}

/// Map [`DesignTokens`] and [`oxiui_theme::TypographyScale`] to a complete [`egui::Style`].
///
/// This function provides the full design-token-to-egui-style translation,
/// combining spacing, border-radius, and typography into a single style:
///
/// **Spacing** (`DesignTokens.spacing`):
/// - `Xs` → `style.spacing.button_padding` (tight vertical padding)
/// - `Sm` → `style.spacing.item_spacing` (gap between widgets)
///
/// **Border radius** (`DesignTokens.radius`) applied to all widget states
/// (`noninteractive`, `inactive`, `active`):
/// - `Sm` → `noninteractive` + `inactive` corner radius (subtle rounding)
/// - `Md` → `active` corner radius (slightly more rounded when pressed)
/// - `Md` → `visuals.menu_corner_radius` and `visuals.window_corner_radius`
///
/// **Typography** (`TypographyScale`) mapped to egui's five [`egui::TextStyle`]
/// variants using the `size` field of each typographic role:
/// - `Heading` ← `typography.headline.size`
/// - `Body`    ← `typography.body.size`
/// - `Button`  ← `typography.body.size` (same as body for consistency)
/// - `Monospace` ← `typography.body.size` (monospace uses the body scale)
/// - `Small`   ← `typography.caption.size`
///
/// # Merging with palette colours
///
/// The returned [`egui::Style`] carries default visuals. To apply full theming
/// (colours + tokens), replace the visuals before calling
/// [`egui::Context::set_style`]:
///
/// ```rust,ignore
/// use oxiui_egui::{tokens_to_egui_style, palette_to_egui_visuals};
/// let mut style = tokens_to_egui_style(&tokens, &typography);
/// style.visuals = palette_to_egui_visuals(&palette);
/// ctx.set_style(style);
/// ```
pub fn tokens_to_egui_style(
    tokens: &oxiui_theme::DesignTokens,
    typography: &oxiui_theme::TypographyScale,
) -> egui::Style {
    use egui::{FontFamily, FontId, TextStyle};
    use oxiui_theme::{RadiusStep, SpacingStep};

    let mut style = egui::Style::default();

    // ── Spacing ───────────────────────────────────────────────────────────────
    let spacing_xs = tokens.spacing(SpacingStep::Xs);
    let spacing_sm = tokens.spacing(SpacingStep::Sm);

    style.spacing.item_spacing = egui::vec2(spacing_sm, spacing_xs);
    style.spacing.button_padding = egui::vec2(spacing_sm, spacing_xs / 2.0);

    // ── Border radius ─────────────────────────────────────────────────────────
    // CornerRadius::same takes a u8; clamp f32 to [0, 255] to avoid overflow.
    let radius_sm =
        egui::CornerRadius::same(tokens.radius(RadiusStep::Sm).round().clamp(0.0, 255.0) as u8);
    let radius_md =
        egui::CornerRadius::same(tokens.radius(RadiusStep::Md).round().clamp(0.0, 255.0) as u8);

    style.visuals.widgets.noninteractive.corner_radius = radius_sm;
    style.visuals.widgets.inactive.corner_radius = radius_sm;
    style.visuals.widgets.active.corner_radius = radius_md;
    style.visuals.menu_corner_radius = radius_md;
    style.visuals.window_corner_radius = radius_md;

    // ── Typography ────────────────────────────────────────────────────────────
    // Map typographic roles to egui's five text-style variants.
    // `FontFamily::Proportional` is the fallback that every egui app has.
    style.text_styles.insert(
        TextStyle::Heading,
        FontId::new(typography.headline.size, FontFamily::Proportional),
    );
    style.text_styles.insert(
        TextStyle::Body,
        FontId::new(typography.body.size, FontFamily::Proportional),
    );
    style.text_styles.insert(
        TextStyle::Button,
        FontId::new(typography.body.size, FontFamily::Proportional),
    );
    style.text_styles.insert(
        TextStyle::Monospace,
        FontId::new(typography.body.size, FontFamily::Monospace),
    );
    style.text_styles.insert(
        TextStyle::Small,
        FontId::new(typography.caption.size, FontFamily::Proportional),
    );

    style
}

// ── forward_event_to_egui (extended, signature preserved) ────────────────────

/// Forward an OxiUI [`UiEvent`] to an egui [`egui::Context`]'s input queue.
///
/// Handles the following event families:
///
/// **IME events:**
/// - [`UiEvent::ImePreedit`] → [`egui::Event::Ime`](`egui::ImeEvent::Preedit`)
///   (the cursor range is dropped; egui 0.34 does not carry it).
/// - [`UiEvent::ImeCommit`] → [`egui::Event::Ime`](`egui::ImeEvent::Commit`)
///
/// **Keyboard events (extended):**
/// - [`UiEvent::KeyDown`] → [`egui::Event::Key`] with `pressed = true`.
/// - [`UiEvent::KeyUp`]   → [`egui::Event::Key`] with `pressed = false`.
/// - [`UiEvent::KeyPress`] — a bare `String` key name; forwarded via
///   [`egui::Key::from_name`] if the name is recognisable, silently ignored
///   otherwise. Maps to a pressed=true / repeat=false event.
///
/// **Pointer events (extended):**
/// - [`UiEvent::MouseMove`] → [`egui::Event::PointerMoved`].
/// - [`UiEvent::Mouse`]     → [`egui::Event::PointerMoved`] (position only).
/// - [`UiEvent::MouseDown`] → [`egui::Event::PointerButton`] with `pressed = true`.
/// - [`UiEvent::MouseUp`]   → [`egui::Event::PointerButton`] with `pressed = false`.
///
/// **Resize (extended):**
/// - [`UiEvent::Resize`] — recorded as a no-op comment. egui viewport resizing
///   is handled via the integration's `RawInput.screen_rect`; there is no
///   direct `egui::Event` for it, so only the intent is noted here.
///
/// **Deviation note:** The plan referenced fictional variants
/// `KeyPress { key, modifiers, pressed }`, `MouseButton { .. }`, and
/// `Resize { width, height }`. The actual `oxiui_core::UiEvent` has
/// `KeyPress(String)`, `MouseDown`/`MouseUp` (separate), `Mouse { x, y }`,
/// and `Resize(u32, u32)`. The mapping above uses the real variants.
///
/// All other event variants are silently ignored (the enum is
/// `#[non_exhaustive]`).
pub fn forward_event_to_egui(ctx: &egui::Context, event: &UiEvent) {
    match event {
        UiEvent::ImePreedit { text, cursor: _ } => {
            // cursor is intentionally dropped: egui 0.34 ImeEvent::Preedit(String)
            // does not carry a cursor position.
            ctx.input_mut(|i| {
                i.events
                    .push(egui::Event::Ime(egui::ImeEvent::Preedit(text.clone())));
            });
        }
        UiEvent::ImeCommit(text) => {
            ctx.input_mut(|i| {
                i.events
                    .push(egui::Event::Ime(egui::ImeEvent::Commit(text.clone())));
            });
        }
        UiEvent::KeyDown {
            key,
            modifiers,
            repeat,
        } => {
            ctx.input_mut(|i| {
                i.events.push(egui::Event::Key {
                    key: map_key(key),
                    physical_key: None,
                    pressed: true,
                    repeat: *repeat,
                    modifiers: map_modifiers(modifiers),
                });
            });
        }
        UiEvent::KeyUp { key, modifiers } => {
            ctx.input_mut(|i| {
                i.events.push(egui::Event::Key {
                    key: map_key(key),
                    physical_key: None,
                    pressed: false,
                    repeat: false,
                    modifiers: map_modifiers(modifiers),
                });
            });
        }
        UiEvent::KeyPress(name) => {
            // Legacy string-based key: forward only if recognisable.
            if let Some(egui_key) = egui::Key::from_name(name.as_str()) {
                ctx.input_mut(|i| {
                    i.events.push(egui::Event::Key {
                        key: egui_key,
                        physical_key: None,
                        pressed: true,
                        repeat: false,
                        modifiers: egui::Modifiers::default(),
                    });
                });
            }
        }
        UiEvent::MouseMove { x, y } => {
            ctx.input_mut(|i| {
                i.events.push(egui::Event::PointerMoved(egui::pos2(*x, *y)));
            });
        }
        UiEvent::Mouse { x, y } => {
            ctx.input_mut(|i| {
                i.events.push(egui::Event::PointerMoved(egui::pos2(*x, *y)));
            });
        }
        UiEvent::MouseDown {
            button,
            x,
            y,
            modifiers,
        } => {
            ctx.input_mut(|i| {
                i.events.push(egui::Event::PointerButton {
                    pos: egui::pos2(*x, *y),
                    button: map_mouse_button(button),
                    pressed: true,
                    modifiers: map_modifiers(modifiers),
                });
            });
        }
        UiEvent::MouseUp {
            button,
            x,
            y,
            modifiers,
        } => {
            ctx.input_mut(|i| {
                i.events.push(egui::Event::PointerButton {
                    pos: egui::pos2(*x, *y),
                    button: map_mouse_button(button),
                    pressed: false,
                    modifiers: map_modifiers(modifiers),
                });
            });
        }
        UiEvent::Resize(_w, _h) => {
            // egui viewport resize is driven by RawInput.screen_rect from the
            // integration layer; there is no egui::Event for it.
        }
        // CloseRequested, Wheel, and any future variants are not forwarded.
        _ => {}
    }
}

// ── OxiWidget — bridge from oxiui_core::Widget to egui::Widget ───────────────

/// Wraps a mutable [`Widget`] reference so it can be placed in an egui layout.
///
/// ```rust,ignore
/// use oxiui_egui::OxiWidget;
/// ui.add(OxiWidget::new(&mut my_widget));
/// ```
pub struct OxiWidget<'a> {
    widget: &'a mut dyn Widget,
}

impl<'a> OxiWidget<'a> {
    /// Create a new [`OxiWidget`] wrapping the given [`Widget`].
    pub fn new(widget: &'a mut dyn Widget) -> Self {
        Self { widget }
    }
}

impl<'a> egui::Widget for OxiWidget<'a> {
    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
        let mut ctx = EguiUiCtx::new(ui);
        self.widget.render(&mut ctx);
        // Extract the response before `ctx` (which borrows `ui`) is dropped.
        let maybe_resp = ctx.last_response.take();
        drop(ctx);
        maybe_resp.unwrap_or_else(|| ui.allocate_response(egui::Vec2::ZERO, egui::Sense::hover()))
    }
}

// ── load_font_into_egui (unchanged) ──────────────────────────────────────────

/// Load OxiFont bytes into the egui context as the "OxiFont" family.
///
/// Validates the font bytes via `oxiui_text::TextPipeline::from_bytes` before
/// inserting into egui's font system. Returns an error if the bytes are empty
/// or cannot be parsed as a valid font.
///
/// On success, inserts the font at position 0 of the Proportional family, so
/// it takes precedence over egui's default fonts for all body text.
///
/// # Errors
///
/// Returns [`UiError::Render`] if `font_bytes` is empty or not a valid TTF/OTF
/// file.
pub fn load_font_into_egui(ctx: &egui::Context, font_bytes: Vec<u8>) -> Result<(), UiError> {
    // Validate the font bytes before handing off to egui.
    oxiui_text::TextPipeline::from_bytes(&font_bytes)
        .map_err(|e| UiError::Render(format!("invalid font bytes: {e}")))?;

    let mut fonts = egui::FontDefinitions::default();
    fonts.font_data.insert(
        "OxiFont".to_owned(),
        Arc::new(egui::FontData::from_owned(font_bytes)),
    );
    fonts
        .families
        .entry(egui::FontFamily::Proportional)
        .or_default()
        .insert(0, "OxiFont".to_owned());
    fonts
        .families
        .entry(egui::FontFamily::Monospace)
        .or_default()
        .push("OxiFont".to_owned());
    ctx.set_fonts(fonts);
    Ok(())
}

// ── load_fonts_into_egui — multi-family font loading ─────────────────────────

/// Load multiple font families into egui's font definitions.
///
/// Each entry in `family_map` is a `(family_name, font_bytes)` pair. Fonts are
/// validated via [`oxiui_text::TextPipeline::from_bytes`] before being
/// registered. On success the font is inserted into:
/// - A named family `"OxiFont-<family_name>"`.
///
/// Starts from [`egui::FontDefinitions::default`], so any previously loaded
/// `OxiFont-*` families are replaced.
///
/// # Errors
///
/// Returns [`UiError::Render`] if any entry's bytes are invalid.
pub fn load_fonts_into_egui(
    family_map: &[(&str, Vec<u8>)],
    ctx: &egui::Context,
) -> Result<(), UiError> {
    let mut fonts = egui::FontDefinitions::default();
    for (family, bytes) in family_map {
        oxiui_text::TextPipeline::from_bytes(bytes)
            .map_err(|e| UiError::Render(format!("invalid font bytes for '{family}': {e}")))?;
        let name = format!("OxiFont-{family}");
        fonts.font_data.insert(
            name.clone(),
            Arc::new(egui::FontData::from_owned(bytes.clone())),
        );
        fonts
            .families
            .entry(egui::FontFamily::Name(name.clone().into()))
            .or_default()
            .push(name);
    }
    ctx.set_fonts(fonts);
    Ok(())
}

// ── EguiAdapter builder (unchanged) ──────────────────────────────────────────

/// Builder for configuring and applying an OxiUI egui adapter to an
/// [`egui::Context`].
///
/// Use [`EguiAdapter::new`] to start building, optionally call
/// [`EguiAdapter::with_palette`] to inject a colour palette, then call
/// [`EguiAdapter::build`] to obtain a configuration closure suitable for
/// passing to `eframe`'s `setup_callback` or calling directly on an
/// [`egui::Context`].
pub struct EguiAdapter {
    palette: Option<Palette>,
}

impl EguiAdapter {
    /// Create a new [`EguiAdapter`] builder with no palette set.
    pub fn new() -> Self {
        Self { palette: None }
    }

    /// Set the [`Palette`] that will be applied to the egui [`egui::Context`]
    /// when [`EguiAdapter::build`] is called.
    pub fn with_palette(mut self, p: Palette) -> Self {
        self.palette = Some(p);
        self
    }

    /// Build the adapter configuration closure.
    ///
    /// Returns a `Fn(&egui::Context)` that, when called, applies the configured
    /// palette (if any) to the context's visuals.
    pub fn build(self) -> impl Fn(&egui::Context) {
        let palette = self.palette;
        move |ctx: &egui::Context| {
            if let Some(p) = &palette {
                ctx.set_visuals(palette_to_egui_visuals(p));
            }
        }
    }
}

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

// ── StatefulEguiAdapter — per-frame caching adapter ───────────────────────────

/// Returns `true` if two [`Palette`] values are equal field-by-field.
///
/// Used by [`StatefulEguiAdapter`] to detect theme changes without requiring
/// [`PartialEq`] on [`Palette`] itself.
fn palettes_equal(a: &Palette, b: &Palette) -> bool {
    a.background == b.background
        && a.surface == b.surface
        && a.primary == b.primary
        && a.on_primary == b.on_primary
        && a.text == b.text
        && a.muted == b.muted
}

/// A stateful egui adapter that caches expensive operations across frames.
///
/// Unlike [`EguiAdapter`] (which returns a stateless `Fn`), `StatefulEguiAdapter`
/// keeps internal state between calls to [`StatefulEguiAdapter::apply`]:
///
/// - **Visuals caching** — [`palette_to_egui_visuals`] is called and
///   [`egui::Context::set_visuals`] is invoked *only* when the palette has
///   changed since the last frame.  When the theme is stable this saves the
///   visuals recomputation and the redundant egui context lock on every frame.
///
/// - **Font definition caching** — [`egui::Context::set_fonts`] is called at
///   most *once*: the first time [`apply`](StatefulEguiAdapter::apply) is
///   invoked.  Subsequent frames skip the call entirely.  Font bytes are owned
///   by the adapter and released after the first load to avoid keeping a
///   duplicate copy in memory.
///
/// - **Design-token style** — when [`StatefulEguiAdapter::with_design_tokens`] is called, the
///   adapter additionally calls [`tokens_to_egui_style`] and
///   [`egui::Context::set_style`] on the first frame (tokens are static once
///   configured). Palette colours are merged into the style so they are not
///   lost when both palette and tokens are present.
///
/// # Usage
///
/// ```rust,ignore
/// let mut adapter = StatefulEguiAdapter::new()
///     .with_palette(my_palette)
///     .with_design_tokens(my_tokens, my_typography)
///     .with_font_bytes(font_data);
///
/// // In your eframe::App::update():
/// adapter.apply(ctx);   // cheap after the first frame if theme is unchanged
/// ```
///
/// # Instrumentation
///
/// Two public counters are always available for testing and monitoring:
/// - [`visuals_recompute_count`](StatefulEguiAdapter::visuals_recompute_count)
///   — number of times visuals were recomputed.
/// - [`fonts_load_count`](StatefulEguiAdapter::fonts_load_count)
///   — number of times `set_fonts` was called (at most 1 in normal use).
pub struct StatefulEguiAdapter {
    palette: Option<Palette>,
    /// Cached `(last_palette, computed_visuals)`.  Recomputed only on change.
    cached_visuals: Option<(Palette, egui::Visuals)>,
    /// Font bytes to load on the first [`apply`](StatefulEguiAdapter::apply) call.
    /// Moved into egui and set to `None` after the first successful load.
    pending_font_bytes: Option<Vec<u8>>,
    /// `true` once the font-load attempt has been made; prevents repeated loads.
    fonts_loaded: bool,
    /// Design tokens to apply once at startup (spacing, radius, typography).
    design_tokens: Option<oxiui_theme::DesignTokens>,
    /// Typography scale paired with design tokens.
    typography: Option<oxiui_theme::TypographyScale>,
    /// `true` once the design-token style has been applied.
    tokens_applied: bool,
    /// Number of times visuals were recomputed (palette changed or first frame).
    ///
    /// Useful for testing that caching is working correctly: after the first
    /// frame with a given palette this counter should not increase until the
    /// palette is changed via [`set_palette`](StatefulEguiAdapter::set_palette).
    pub visuals_recompute_count: usize,
    /// Number of times `set_fonts` was attempted (should be ≤ 1 in normal use).
    ///
    /// A value of 1 means fonts were loaded once on the first frame.
    /// A value of 0 means no font bytes were supplied via
    /// [`with_font_bytes`](StatefulEguiAdapter::with_font_bytes).
    pub fonts_load_count: usize,
}

impl StatefulEguiAdapter {
    /// Create a new [`StatefulEguiAdapter`] with no palette or fonts.
    pub fn new() -> Self {
        Self {
            palette: None,
            cached_visuals: None,
            pending_font_bytes: None,
            fonts_loaded: false,
            design_tokens: None,
            typography: None,
            tokens_applied: false,
            visuals_recompute_count: 0,
            fonts_load_count: 0,
        }
    }

    /// Set the [`Palette`] to be applied each frame (cached — only recomputed on change).
    pub fn with_palette(mut self, p: Palette) -> Self {
        self.palette = Some(p);
        self
    }

    /// Attach [`oxiui_theme::DesignTokens`] and [`oxiui_theme::TypographyScale`] to be
    /// applied *once* on the first [`StatefulEguiAdapter::apply`] call via [`tokens_to_egui_style`].
    ///
    /// When a palette is also configured, palette colours are merged into the
    /// style so neither colour nor token information is lost.  If only tokens are
    /// set (no palette), the style is applied with egui's default dark visuals.
    pub fn with_design_tokens(
        mut self,
        tokens: oxiui_theme::DesignTokens,
        typography: oxiui_theme::TypographyScale,
    ) -> Self {
        self.design_tokens = Some(tokens);
        self.typography = Some(typography);
        self
    }

    /// Attach font bytes to be loaded *once* on the first [`StatefulEguiAdapter::apply`] call.
    ///
    /// The bytes are validated and forwarded to egui via
    /// [`load_font_into_egui`].  If validation fails the error is silently
    /// discarded (the adapter falls back to egui's built-in fonts).  Call
    /// this method before the first [`StatefulEguiAdapter::apply`] call.
    pub fn with_font_bytes(mut self, bytes: Vec<u8>) -> Self {
        self.pending_font_bytes = Some(bytes);
        self
    }

    /// Update the live palette.
    ///
    /// If the adapter already has a cached visuals entry, this marks it stale
    /// so the next [`StatefulEguiAdapter::apply`] call recomputes the visuals.
    pub fn set_palette(&mut self, p: Palette) {
        self.palette = Some(p);
    }

    /// Apply the adapter state to `ctx` for one frame.
    ///
    /// - Loads fonts exactly once (the first call).
    /// - Applies design-token style exactly once (the first call, if tokens were set).
    ///   When palette colours are also present, they are merged into the token style.
    /// - Recomputes and applies visuals only when the palette has changed
    ///   (skipped when design-token style is active — tokens mode uses `set_style`).
    pub fn apply(&mut self, ctx: &egui::Context) {
        // ── font loading (at most once) ───────────────────────────────────────
        if !self.fonts_loaded {
            if let Some(bytes) = self.pending_font_bytes.take() {
                // Silently ignore validation errors; egui falls back to defaults.
                let _ = load_font_into_egui(ctx, bytes);
                self.fonts_load_count += 1;
            }
            self.fonts_loaded = true;
        }

        // ── design-token style (at most once) ─────────────────────────────────
        if !self.tokens_applied {
            if let (Some(ref tok), Some(ref typo)) = (&self.design_tokens, &self.typography) {
                let mut style = tokens_to_egui_style(tok, typo);
                // Merge palette colours so they are not lost.
                if let Some(ref p) = self.palette {
                    style.visuals = palette_to_egui_visuals(p);
                    // Re-apply token radius overrides on top of palette visuals.
                    use oxiui_theme::RadiusStep;
                    let radius_sm = egui::CornerRadius::same(
                        tok.radius(RadiusStep::Sm).round().clamp(0.0, 255.0) as u8,
                    );
                    let radius_md = egui::CornerRadius::same(
                        tok.radius(RadiusStep::Md).round().clamp(0.0, 255.0) as u8,
                    );
                    style.visuals.widgets.noninteractive.corner_radius = radius_sm;
                    style.visuals.widgets.inactive.corner_radius = radius_sm;
                    style.visuals.widgets.active.corner_radius = radius_md;
                    style.visuals.menu_corner_radius = radius_md;
                    style.visuals.window_corner_radius = radius_md;

                    // Mark visuals as cached so the palette-only path below is
                    // skipped for this frame (colours already applied in style).
                    let visuals = style.visuals.clone();
                    self.cached_visuals = Some((p.clone(), visuals));
                    self.visuals_recompute_count += 1;
                }
                ctx.set_global_style(style);
                self.tokens_applied = true;
                return;
            }
        }

        // ── visuals caching (palette-only path) ───────────────────────────────
        if let Some(ref current) = self.palette {
            let needs_update = match &self.cached_visuals {
                None => true,
                Some((ref last, _)) => !palettes_equal(last, current),
            };

            if needs_update {
                let visuals = palette_to_egui_visuals(current);
                ctx.set_visuals(visuals.clone());
                self.cached_visuals = Some((current.clone(), visuals));
                self.visuals_recompute_count += 1;
            }
        }
    }
}

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