egui_styled 0.8.0

Tailwind-style utility styling for egui: per-widget hover/focus/active variants, design tokens, and composable style functions.
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
use egui::{Align, Color32, InnerResponse, Layout, Shape, Ui};

use crate::{
    impl_style_builders,
    state::PseudoState,
    style::shared_style::{
        SharedStyle, background_image_shape, bgimg_fade_alpha, border_gradient_mesh,
        gradient_shape, inner_glow_shape, justify_body_vertically, paint_shadows,
        paint_side_borders,
    },
};

/// A styled box that lives inside the current layout.
///
/// The in-layout counterpart to [`StyledArea`](crate::StyledArea): use
/// `StyledFrame` for a filled / bordered / padded panel inside the current
/// `Ui` tree, and [`StyledArea`](crate::StyledArea) for floating,
/// screen-anchored placement. Applies the [`SharedStyle`] box builders
/// (`bg`, `border`, `padding`, `corner_radius`, `margin`, `background_image`,
/// shadows) and optional [`align`](Self::align) / [`justify`](Self::justify) /
/// [`gap`](Self::gap) for its children.
///
/// Construct via [`Styled::frame`](crate::Styled::frame).
pub struct StyledFrame {
    pub style: SharedStyle,
    pub align: Option<Align>,
    pub justify: Option<Align>,
    pub gap: Option<f32>,
    pub fill_size: Option<egui::Vec2>,
}

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

impl StyledFrame {
    pub fn new() -> Self {
        Self {
            style: SharedStyle::default(),
            align: None,
            justify: None,
            gap: None,
            fill_size: None,
        }
    }

    /// Cross-axis (horizontal) alignment of the frame's children.
    pub fn align(mut self, align: Align) -> Self {
        self.align = Some(align);
        self
    }

    /// Main-axis (vertical) distribution of the frame's children. Treated as
    /// top-down inside the frame; see [`crate::StyledColumn::justify`] for details.
    pub fn justify(mut self, justify: Align) -> Self {
        self.justify = Some(justify);
        self
    }

    /// Spacing between children, applied to both axes (`item_spacing.x` and `.y`).
    pub fn gap(mut self, gap: f32) -> Self {
        self.gap = Some(gap);
        self
    }

    pub fn show<R>(self, ui: &mut Ui, body: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
        if self.style.visible == Some(false) {
            ui.set_invisible();
        }

        let shadow_idx = ui.painter().add(Shape::Noop);
        let corner_radius = self.style.corner_radius.unwrap_or_default();
        let shadows = self.style.shadows.clone();

        // A frame is non-interactive, so resolve its border against the base
        // (inactive) state. `Some` only when per-side overrides are set — that's
        // the signal to paint edges manually and skip egui's uniform stroke.
        let border_sides = {
            let resolved = self
                .style
                .resolve(PseudoState::default(), &ui.visuals().widgets.inactive);
            resolved
                .has_border_overrides
                .then_some(resolved.border_sides)
        };

        let has_bg_image = self.style.background_image.is_some();
        let has_bg_gradient = self.style.bg_gradient.is_some();
        let fade_id = has_bg_image.then(|| {
            ui.make_persistent_id(ui.next_auto_id())
                .with("__bgimg_fade_start")
        });

        let mut frame = egui::Frame::default();
        // When a background image or bg_gradient is set we paint fill + layers + border
        // ourselves so everything sits in the correct order. Without those, delegate to egui.
        if has_bg_image || has_bg_gradient {
            if let Some(r) = self.style.corner_radius {
                frame = frame.corner_radius(r);
            }
            if let Some(p) = self.style.padding {
                frame = frame.inner_margin(p);
            }
            if let Some(m) = self.style.margin {
                frame = frame.outer_margin(m);
            }
            // No .fill / .stroke — we'll draw those inside the bgimg shape.
        } else {
            if let Some(bg) = self.style.bg {
                frame = frame.fill(bg);
            }
            if let Some(r) = self.style.corner_radius {
                frame = frame.corner_radius(r);
            }
            if let Some(p) = self.style.padding {
                frame = frame.inner_margin(p);
            }
            if let Some(m) = self.style.margin {
                frame = frame.outer_margin(m);
            }
            // When per-side overrides are set we paint the border ourselves
            // after the frame renders; otherwise delegate the uniform stroke
            // to egui so rounded corners are handled for us.
            if border_sides.is_none()
                && let Some(b) = self.style.border
            {
                frame = frame.stroke(b);
            }
        }

        let full_width = self.style.full_width;
        let full_height = self.style.full_height;
        let min_width = self.style.min_width;
        let max_width = self.style.max_width;
        let min_height = self.style.min_height;
        let max_height = self.style.max_height;
        let style_for_pct = self.style.clone();
        let align = self.align;
        let justify = self.justify;
        let gap = self.gap;
        let fill_size = self.fill_size;

        // Stable id for caching the measured content height used by vertical
        // justify. Only allocated when justify is set (same lazy pattern as
        // fade_id — avoids burning an auto-id slot when not needed).
        let vjustify_id = justify.map(|_| {
            ui.make_persistent_id(ui.next_auto_id())
                .with("__vjustify_content_h")
        });

        // Content reveal: when set, the body fades in together with the
        // background image (same id + duration → in lockstep). `None` unless
        // `reveal_with_background_image` was used and an image is present.
        let reveal = if self.style.background_image_fade_content {
            match (
                self.style.background_image_fade_in,
                fade_id,
                self.style.background_image.clone(),
            ) {
                (Some(duration), Some(id), Some(image)) => Some((duration, id, image)),
                _ => None,
            }
        } else {
            None
        };

        // Reserve a slot for the background / gradient before children so it paints
        // behind them on the same layer. `bgimg_slot` is `None` when there is
        // neither a background image nor a gradient.
        let bgimg_slot = (has_bg_image || has_bg_gradient).then(|| ui.painter().add(Shape::Noop));

        let response = frame.show(ui, |ui| {
            // Fade the body in lockstep with the background image when a content
            // reveal is requested. Applied before any content is drawn; the bg
            // fill/image are painted on a separate slot, so the backdrop stays
            // opaque while image + content come up together.
            if let Some((duration, id, image)) = &reveal {
                let ready = matches!(
                    image.load_for_size(ui.ctx(), ui.available_size()),
                    Ok(egui::load::TexturePoll::Ready { .. })
                );
                ui.multiply_opacity(bgimg_fade_alpha(ui.ctx(), *id, *duration, ready));
            }
            // Pin to exactly `fill_size` (both min and max). `set_min_size` alone
            // only floors the area, so on a window *shrink* it stays at its
            // previous larger width and content centers on the stale midpoint.
            if let Some(size) = fill_size {
                ui.set_min_size(size);
                ui.set_max_size(size);
            }
            // Capture available before any sizing mutations for pct resolution.
            let avail_w = ui.available_width();
            let avail_h = ui.available_height();
            let pct_w = style_for_pct.resolved_width_pct(avail_w);
            let pct_h = style_for_pct.resolved_height_pct(avail_h);
            // Compute effective definite width for aspect ratio derivation.
            let effective_w = pct_w.or_else(|| {
                if full_width {
                    Some(max_width.map_or(avail_w, |m| avail_w.min(m)))
                } else {
                    None
                }
            });
            let aspect_h = effective_w.and_then(|w| style_for_pct.resolved_aspect_height(w));

            if let Some(w) = pct_w {
                // Definite width: pin both min and max; supersedes full_width.
                ui.set_min_width(w);
                ui.set_max_width(w);
            } else {
                // Apply max constraints first so full_width reads the capped
                // available size, and min constraints last so an explicit minimum
                // always wins over full_width.
                if let Some(w) = max_width {
                    ui.set_max_width(w);
                }
                if full_width {
                    ui.set_min_width(ui.available_width());
                }
                if let Some(w) = min_width {
                    ui.set_min_width(w);
                }
            }

            // Capture fill_height for vertical justify *before* set_min_height
            // changes the available size. Precedence: fill_size > height_pct > aspect > full_height.
            let fill_height_val: Option<f32> = fill_size
                .map(|s| s.y)
                .or(pct_h)
                .or(aspect_h)
                .or(if full_height { Some(avail_h) } else { None });

            if let Some(h) = pct_h {
                // Definite height: pin both min and max; supersedes full_height.
                ui.set_min_height(h);
                ui.set_max_height(h);
            } else if let Some(h) = aspect_h {
                ui.set_min_height(h);
                ui.set_max_height(h);
            } else {
                if let Some(h) = max_height {
                    ui.set_max_height(h);
                }
                if full_height {
                    ui.set_min_height(ui.available_height());
                }
                if let Some(h) = min_height {
                    ui.set_min_height(h);
                }
            }
            if let Some(g) = gap {
                ui.spacing_mut().item_spacing = egui::Vec2::splat(g);
            }
            // Vertical justify via top spacer when the frame has a determinate
            // height and justify is Center or Max. egui's `with_main_align` is a
            // no-op for top-down layouts, so we use the measured-spacer approach
            // instead. Horizontal alignment is applied as the cross-axis layout
            // inside the same body closure.
            let justify_factor = justify.map(|j| j.to_factor()).unwrap_or(0.0);
            match (fill_height_val, vjustify_id, justify_factor > 0.0) {
                (Some(fill_h), Some(vid), true) => {
                    justify_body_vertically(ui, fill_h, justify_factor, vid, |ui| {
                        if let Some(a) = align {
                            ui.with_layout(Layout::top_down(a), body).inner
                        } else {
                            body(ui)
                        }
                    })
                }
                _ => {
                    if align.is_some() || justify.is_some() {
                        let mut layout = Layout::top_down(align.unwrap_or(Align::Min));
                        if let Some(j) = justify {
                            layout = layout.with_main_align(j);
                        }
                        ui.with_layout(layout, body).inner
                    } else {
                        body(ui)
                    }
                }
            }
        });

        let rect = response.response.rect;

        // Paint background layers into the slot now that we know the rect.
        if let Some(slot) = bgimg_slot {
            let mut parts: Vec<Shape> = Vec::new();

            // Layer 1: solid bg fill.
            if let Some(bg) = self.style.bg {
                parts.push(Shape::rect_filled(rect, corner_radius, bg));
            }

            // Layer 2: gradient (on top of solid bg, under the image).
            if let Some(g) = &self.style.bg_gradient {
                parts.push(gradient_shape(ui.ctx(), rect, corner_radius, g));
            }

            // Layer 3: background image (on top of gradient).
            if let Some(ref image) = self.style.background_image {
                let tint = self.style.background_image_tint.unwrap_or(Color32::WHITE);
                let fit = self.style.background_image_fit;
                let fade = self
                    .style
                    .background_image_fade_in
                    .zip(fade_id)
                    .map(|(d, id)| (id, d));
                // Temporarily build the bg-image shape; reuse the existing helper
                // but without solid bg (we already emitted that above).
                if let Some(img_shape) = background_image_shape(
                    ui,
                    rect,
                    corner_radius,
                    image,
                    fit,
                    tint,
                    None, // bg already emitted above
                    if self.style.border_gradient.is_none() {
                        self.style.border
                    } else {
                        None
                    },
                    if self.style.border_gradient.is_none() {
                        border_sides
                    } else {
                        None
                    },
                    fade,
                ) {
                    match img_shape {
                        Shape::Vec(v) => parts.extend(v),
                        other => parts.push(other),
                    }
                }
            } else if self.style.border_gradient.is_none() {
                // No image path: add border shapes into the slot.
                if let Some(sides) = border_sides {
                    let mut push = |a, b, stroke: egui::Stroke| {
                        if stroke.width > 0.0 {
                            parts.push(Shape::line_segment([a, b], stroke));
                        }
                    };
                    push(rect.left_top(), rect.right_top(), sides.top);
                    push(rect.right_top(), rect.right_bottom(), sides.right);
                    push(rect.left_bottom(), rect.right_bottom(), sides.bottom);
                    push(rect.left_top(), rect.left_bottom(), sides.left);
                } else if let Some(b) = self.style.border {
                    parts.push(Shape::rect_stroke(
                        rect,
                        corner_radius,
                        b,
                        egui::StrokeKind::Outside,
                    ));
                }
            }

            if !parts.is_empty() {
                ui.painter().set(slot, Shape::Vec(parts));
            }
        }

        // Per-side borders for the non-bg-image / non-gradient path.
        if !has_bg_image
            && !has_bg_gradient
            && let Some(sides) = border_sides
        {
            paint_side_borders(ui.painter(), rect, sides);
        }

        // Border gradient (wins over per-side and uniform border) - see the
        // "Precedence rules" section on `SharedStyle` for the full chain.
        if let Some(bg) = self.style.border_gradient {
            ui.painter()
                .add(Shape::Mesh(border_gradient_mesh(rect, bg).into()));
        }

        // Inner glow (paints last, on top of everything).
        if let Some(glow) = self.style.inner_glow
            && let Some(shape) = inner_glow_shape(rect, corner_radius, glow)
        {
            ui.painter().add(shape);
        }

        paint_shadows(
            ui,
            shadow_idx,
            response.response.rect,
            corner_radius,
            &shadows,
        );

        response
    }
}

impl_style_builders!(StyledFrame);
crate::impl_styled_container!(StyledFrame);

#[cfg(test)]
mod tests {
    use super::*;
    use egui::{Color32, epaint::Shape};

    fn load_ready_texture(ctx: &egui::Context) -> egui::Image<'static> {
        let handle = ctx.load_texture(
            "fade_test",
            egui::ColorImage::new([4, 4], vec![Color32::WHITE; 16]),
            Default::default(),
        );
        let sized = egui::load::SizedTexture::from_handle(&handle);
        egui::Image::from_texture(sized)
    }

    /// Walk a shape tree and return the fill alpha of the first textured rect.
    fn textured_rect_alpha(shapes: &[Shape]) -> Option<u8> {
        for shape in shapes {
            match shape {
                Shape::Vec(inner) => {
                    if let Some(a) = textured_rect_alpha(inner) {
                        return Some(a);
                    }
                }
                Shape::Rect(rs) if rs.brush.is_some() => {
                    return Some(rs.fill.a());
                }
                _ => {}
            }
        }
        None
    }

    fn run_frame_at(
        ctx: &egui::Context,
        img: egui::Image<'static>,
        fade_secs: Option<f32>,
        time: f64,
    ) -> Vec<Shape> {
        let raw = egui::RawInput {
            time: Some(time),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            let mut frame = StyledFrame::new().background_image(img.clone());
            if let Some(d) = fade_secs {
                frame = frame.background_image_fade_in(d);
            }
            frame.show(ui, |_ui| {});
        });
        output.shapes.into_iter().map(|cs| cs.shape).collect()
    }

    #[test]
    fn fade_in_alpha_increases_and_reaches_full() {
        let ctx = egui::Context::default();
        let img = load_ready_texture(&ctx);

        // Frame at t=0: start is stamped, alpha = 0/0.5 = 0 → WHITE multiplied = transparent
        let shapes0 = run_frame_at(&ctx, img.clone(), Some(0.5), 0.0);
        let alpha0 = textured_rect_alpha(&shapes0).expect("textured rect present");

        // Frame at t=0.25: alpha = 0.25/0.5 = 0.5 → 128
        let shapes1 = run_frame_at(&ctx, img.clone(), Some(0.5), 0.25);
        let alpha1 = textured_rect_alpha(&shapes1).expect("textured rect present");

        // Frame at t=0.6: alpha >= 1.0 → full
        let shapes2 = run_frame_at(&ctx, img.clone(), Some(0.5), 0.6);
        let alpha2 = textured_rect_alpha(&shapes2).expect("textured rect present");

        assert!(
            alpha0 < alpha1,
            "alpha should increase: {alpha0} < {alpha1}"
        );
        assert_eq!(alpha2, 255, "alpha should be full at >= duration");
    }

    #[test]
    fn no_fade_paints_full_opacity_on_first_ready_frame() {
        let ctx = egui::Context::default();
        let img = load_ready_texture(&ctx);

        let shapes = run_frame_at(&ctx, img, None, 0.0);
        let alpha = textured_rect_alpha(&shapes).expect("textured rect present");
        assert_eq!(alpha, 255, "no-fade path must paint at full opacity");
    }

    /// Alpha of the body content rect: a non-textured rect with non-zero colour.
    /// Skips the textured background image (`brush.is_some()`) and egui::Frame's
    /// own fully-transparent `(0,0,0,0)` fill rect.
    fn content_rect_alpha(shapes: &[Shape]) -> Option<u8> {
        for shape in shapes {
            match shape {
                Shape::Vec(inner) => {
                    if let Some(a) = content_rect_alpha(inner) {
                        return Some(a);
                    }
                }
                Shape::Rect(rs)
                    if rs.brush.is_none()
                        && (rs.fill.r() > 0 || rs.fill.g() > 0 || rs.fill.b() > 0) =>
                {
                    return Some(rs.fill.a());
                }
                _ => {}
            }
        }
        None
    }

    /// Render a frame with an opaque content rect; `reveal` toggles
    /// `reveal_with_background_image` vs image-only `background_image_fade_in`.
    fn run_with_content(
        ctx: &egui::Context,
        img: egui::Image<'static>,
        secs: f32,
        reveal: bool,
        time: f64,
    ) -> Vec<Shape> {
        let raw = egui::RawInput {
            time: Some(time),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            let mut frame = StyledFrame::new().background_image(img.clone());
            frame = if reveal {
                frame.reveal_with_background_image(secs)
            } else {
                frame.background_image_fade_in(secs)
            };
            frame.show(ui, |ui| {
                let rect = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(10.0, 10.0));
                ui.painter()
                    .rect_filled(rect, 0.0, Color32::from_rgb(40, 50, 60));
            });
        });
        output.shapes.into_iter().map(|cs| cs.shape).collect()
    }

    #[test]
    fn reveal_fades_content_in_with_image() {
        let ctx = egui::Context::default();
        let img = load_ready_texture(&ctx);

        // t=0: opacity 0 → content recorded as Shape::Noop, no solid rect.
        let s0 = run_with_content(&ctx, img.clone(), 0.5, true, 0.0);
        assert!(
            content_rect_alpha(&s0).is_none(),
            "content should be invisible at the fade start"
        );

        // t=0.25: half-way through the fade.
        let s1 = run_with_content(&ctx, img.clone(), 0.5, true, 0.25);
        let a1 = content_rect_alpha(&s1).expect("content present mid-fade");

        // t=0.6: past the duration → full opacity.
        let s2 = run_with_content(&ctx, img.clone(), 0.5, true, 0.6);
        let a2 = content_rect_alpha(&s2).expect("content present after fade");

        assert!(
            a1 > 0 && a1 < 255,
            "content mid-fade alpha should be partial: {a1}"
        );
        assert_eq!(a2, 255, "content should be full opacity after the duration");
    }

    #[test]
    fn image_only_fade_does_not_fade_content() {
        let ctx = egui::Context::default();
        let img = load_ready_texture(&ctx);

        // background_image_fade_in (no content reveal): content full opacity at t=0.
        let shapes = run_with_content(&ctx, img, 0.5, false, 0.0);
        assert_eq!(
            content_rect_alpha(&shapes),
            Some(255),
            "image-only fade must leave content at full opacity"
        );
    }

    /// True if `shapes` contains a solid (non-textured) rect filled with `color`.
    fn has_solid_rect(shapes: &[Shape], color: Color32) -> bool {
        for shape in shapes {
            match shape {
                Shape::Vec(inner) if has_solid_rect(inner, color) => return true,
                Shape::Rect(rs) if rs.brush.is_none() && rs.fill == color => return true,
                _ => {}
            }
        }
        false
    }

    fn run_frame_with_bg(
        ctx: &egui::Context,
        img: egui::Image<'static>,
        bg: Color32,
    ) -> Vec<Shape> {
        let raw = egui::RawInput::default();
        let output = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .bg(bg)
                .background_image(img.clone())
                .show(ui, |_ui| {});
        });
        output.shapes.into_iter().map(|cs| cs.shape).collect()
    }

    /// A pending image source — uses `from_bytes` with no loader installed, so
    /// `load_for_size` returns `Pending` every frame.
    fn pending_image() -> egui::Image<'static> {
        egui::Image::from_bytes("bytes://pending_test_image", vec![0u8; 4])
    }

    #[test]
    fn bg_set_pending_texture_paints_bg_fill_only() {
        let ctx = egui::Context::default();
        let img = pending_image();
        let bg_color = Color32::from_rgb(200, 100, 50);

        let shapes = run_frame_with_bg(&ctx, img, bg_color);

        assert!(
            has_solid_rect(&shapes, bg_color),
            "bg fill must paint while texture is pending"
        );
        assert!(
            textured_rect_alpha(&shapes).is_none(),
            "no textured rect should be present while pending"
        );
    }

    #[test]
    fn bg_set_ready_texture_paints_both() {
        let ctx = egui::Context::default();
        let img = load_ready_texture(&ctx);
        let bg_color = Color32::from_rgb(200, 100, 50);

        let shapes = run_frame_with_bg(&ctx, img, bg_color);

        assert!(
            has_solid_rect(&shapes, bg_color),
            "bg fill must be present when texture is ready"
        );
        assert!(
            textured_rect_alpha(&shapes).is_some(),
            "textured rect must be present when texture is ready"
        );
    }

    #[test]
    fn no_bg_pending_texture_paints_nothing() {
        let ctx = egui::Context::default();
        let img = pending_image();

        let raw = egui::RawInput::default();
        let output = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .background_image(img.clone())
                .show(ui, |_ui| {});
        });
        let shapes: Vec<Shape> = output.shapes.into_iter().map(|cs| cs.shape).collect();

        // No bg fill and no textured rect — the slot stays Noop.
        let bg_color = Color32::from_rgb(200, 100, 50);
        assert!(
            !has_solid_rect(&shapes, bg_color),
            "no solid rect expected with no bg set"
        );
        assert!(
            textured_rect_alpha(&shapes).is_none(),
            "no textured rect should appear while pending and no bg set"
        );
    }

    /// Return the bounding rect of the first non-transparent, non-textured filled rect.
    fn first_solid_rect(shapes: &[Shape]) -> Option<egui::Rect> {
        for shape in shapes {
            match shape {
                Shape::Vec(inner) => {
                    if let Some(r) = first_solid_rect(inner) {
                        return Some(r);
                    }
                }
                Shape::Rect(rs) if rs.brush.is_none() && rs.fill.a() > 0 => {
                    return Some(rs.rect);
                }
                _ => {}
            }
        }
        None
    }

    #[test]
    fn max_width_constrains_frame() {
        let ctx = egui::Context::default();
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, 400.0),
            )),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            // full_width makes the frame try to expand to available (400px), but
            // max_width caps it at 100px. max is applied first so full_width reads
            // the capped available_width.
            StyledFrame::new()
                .bg(Color32::RED)
                .max_width(100.0)
                .full_width()
                .show(ui, |_ui| {});
        });
        let shapes: Vec<Shape> = output.shapes.into_iter().map(|cs| cs.shape).collect();
        let rect = first_solid_rect(&shapes).expect("bg fill rect present");
        assert!(
            rect.width() <= 100.0 + 1.0,
            "frame width {} should be <= max_width 100",
            rect.width()
        );
    }

    #[test]
    fn min_height_expands_frame() {
        let ctx = egui::Context::default();
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, 400.0),
            )),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .bg(Color32::RED)
                .min_height(80.0)
                .show(ui, |_ui| {});
        });
        let shapes: Vec<Shape> = output.shapes.into_iter().map(|cs| cs.shape).collect();
        let rect = first_solid_rect(&shapes).expect("bg fill rect present");
        assert!(
            rect.height() >= 80.0 - 1.0,
            "frame height {} should be >= min_height 80",
            rect.height()
        );
    }

    #[test]
    fn full_height_fills_parent() {
        let ctx = egui::Context::default();
        let parent_height = 200.0;
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, parent_height),
            )),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .bg(Color32::RED)
                .full_height()
                .show(ui, |_ui| {});
        });
        let shapes: Vec<Shape> = output.shapes.into_iter().map(|cs| cs.shape).collect();
        let rect = first_solid_rect(&shapes).expect("bg fill rect present");
        assert!(
            rect.height() >= parent_height - 1.0,
            "frame height {} should fill parent height {}",
            rect.height(),
            parent_height
        );
    }

    #[test]
    fn no_size_constraints_leaves_frame_natural() {
        let ctx = egui::Context::default();
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, 400.0),
            )),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            StyledFrame::new().bg(Color32::RED).show(ui, |ui| {
                // Some content so the frame has non-zero natural size.
                ui.set_min_width(50.0);
                ui.set_min_height(20.0);
            });
        });
        let shapes: Vec<Shape> = output.shapes.into_iter().map(|cs| cs.shape).collect();
        let rect = first_solid_rect(&shapes).expect("bg fill rect present");
        // Without any size constraints the frame should be exactly content-sized.
        assert!(
            rect.width() >= 50.0 - 1.0 && rect.height() >= 20.0 - 1.0,
            "unconstrained frame should match content size, got {:?}",
            rect
        );
    }

    /// Run a full_height + justify frame at a fixed screen size and return
    /// (content_visible, content_center_y, screen_center_y).
    fn run_full_height_justify(
        ctx: &egui::Context,
        justify: egui::Align,
        screen_h: f32,
    ) -> (bool, f32, f32) {
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, screen_h),
            )),
            ..Default::default()
        };
        let mut content_visible = true;
        let mut content_center_y = 0.0f32;
        let screen_center_y = screen_h / 2.0;
        let _ = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .bg(Color32::RED)
                .full_height()
                .full_width()
                .justify(justify)
                .show(ui, |ui| {
                    content_visible = ui.is_visible();
                    let resp = ui.label("hello");
                    content_center_y = resp.rect.center().y;
                });
        });
        (content_visible, content_center_y, screen_center_y)
    }

    #[test]
    fn full_height_justify_center_hides_on_first_frame() {
        let ctx = egui::Context::default();
        let (visible, _, _) = run_full_height_justify(&ctx, egui::Align::Center, 300.0);
        assert!(
            !visible,
            "content should be invisible on the first frame while height is measured"
        );
    }

    #[test]
    fn full_height_justify_center_centers_on_second_frame() {
        let ctx = egui::Context::default();
        // Frame 1: measurement frame (invisible).
        run_full_height_justify(&ctx, egui::Align::Center, 300.0);
        // Frame 2: content renders centered.
        let (visible, cy, screen_cy) = run_full_height_justify(&ctx, egui::Align::Center, 300.0);
        assert!(visible, "content should be visible on the second frame");
        assert!(
            (cy - screen_cy).abs() < 2.0,
            "content center y={cy} should be near screen center y={screen_cy}"
        );
    }

    #[test]
    fn full_height_justify_max_bottom_aligns_on_second_frame() {
        let ctx = egui::Context::default();
        let screen_h = 300.0;
        run_full_height_justify(&ctx, egui::Align::Max, screen_h);
        let (visible, cy, _) = run_full_height_justify(&ctx, egui::Align::Max, screen_h);
        assert!(visible);
        // Bottom-aligned: content center should be near the bottom quarter.
        assert!(
            cy > screen_h * 0.6,
            "bottom-aligned content center y={cy} should be in the lower portion of {screen_h}"
        );
    }

    #[test]
    fn no_full_height_justify_stays_top_aligned() {
        let ctx = egui::Context::default();
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, 300.0),
            )),
            ..Default::default()
        };
        let mut content_center_y = 0.0f32;
        let _ = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .bg(Color32::RED)
                // No full_height — no determinate height, so justify is a no-op.
                .justify(egui::Align::Center)
                .show(ui, |ui| {
                    let resp = ui.label("hello");
                    content_center_y = resp.rect.center().y;
                });
        });
        // Content should sit near the top, not centered in the 300px screen.
        assert!(
            content_center_y < 50.0,
            "without full_height, content should be top-aligned, got center_y={content_center_y}"
        );
    }

    #[test]
    fn width_pct_50_is_half_screen() {
        let ctx = egui::Context::default();
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, 400.0),
            )),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .bg(Color32::RED)
                .width_pct(50.0)
                .show(ui, |_ui| {});
        });
        let shapes: Vec<Shape> = output.shapes.into_iter().map(|cs| cs.shape).collect();
        let rect = first_solid_rect(&shapes).expect("bg fill rect present");
        assert!(
            (rect.width() - 200.0).abs() < 2.0,
            "width_pct(50) on 400px screen should be ~200px, got {}",
            rect.width()
        );
    }

    #[test]
    fn width_pct_clamped_by_max_width() {
        let ctx = egui::Context::default();
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, 400.0),
            )),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .bg(Color32::RED)
                .width_pct(50.0)
                .max_width(120.0)
                .show(ui, |_ui| {});
        });
        let shapes: Vec<Shape> = output.shapes.into_iter().map(|cs| cs.shape).collect();
        let rect = first_solid_rect(&shapes).expect("bg fill rect present");
        assert!(
            rect.width() <= 121.0,
            "width_pct(50).max_width(120) on 400px should cap at 120px, got {}",
            rect.width()
        );
    }

    #[test]
    fn height_pct_50_is_half_screen() {
        let ctx = egui::Context::default();
        let raw = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, 300.0),
            )),
            ..Default::default()
        };
        let output = ctx.run_ui(raw, |ui| {
            StyledFrame::new()
                .bg(Color32::RED)
                .height_pct(50.0)
                .show(ui, |_ui| {});
        });
        let shapes: Vec<Shape> = output.shapes.into_iter().map(|cs| cs.shape).collect();
        let rect = first_solid_rect(&shapes).expect("bg fill rect present");
        assert!(
            (rect.height() - 150.0).abs() < 2.0,
            "height_pct(50) on 300px screen should be ~150px, got {}",
            rect.height()
        );
    }
}