aprender-present-widgets 0.39.0

Widget implementations for Presentar UI framework
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
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
//! Modal dialog widget for overlay content.
//!
//! The Modal widget displays content in a centered overlay with a backdrop,
//! supporting keyboard navigation, focus trap, and animation.

use presentar_core::{
    widget::{LayoutResult, TextStyle},
    Brick, BrickAssertion, BrickBudget, BrickVerification, Canvas, Color, Constraints, Event, Key,
    Point, Rect, Size, TypeId, Widget,
};
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::time::Duration;

/// Modal size variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum ModalSize {
    /// Small modal (300px)
    Small,
    /// Medium modal (500px)
    #[default]
    Medium,
    /// Large modal (800px)
    Large,
    /// Full width (with padding)
    FullWidth,
    /// Custom width
    Custom(u32),
}

impl ModalSize {
    /// Get the max width for this size.
    #[must_use]
    pub const fn max_width(&self) -> f32 {
        match self {
            Self::Small => 300.0,
            Self::Medium => 500.0,
            Self::Large => 800.0,
            Self::FullWidth => f32::MAX,
            Self::Custom(w) => *w as f32,
        }
    }
}

/// Modal backdrop behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum BackdropBehavior {
    /// Click backdrop to close modal
    #[default]
    CloseOnClick,
    /// Backdrop click does nothing (modal must be closed explicitly)
    Static,
    /// No backdrop shown
    None,
}

/// Modal dialog widget.
#[derive(Serialize, Deserialize)]
pub struct Modal {
    /// Whether modal is open
    pub open: bool,
    /// Modal size
    pub size: ModalSize,
    /// Backdrop behavior
    pub backdrop: BackdropBehavior,
    /// Close on escape key
    pub close_on_escape: bool,
    /// Optional title
    pub title: Option<String>,
    /// Show close button
    pub show_close_button: bool,
    /// Backdrop color
    pub backdrop_color: Color,
    /// Modal background color
    pub background_color: Color,
    /// Border radius
    pub border_radius: f32,
    /// Padding
    pub padding: f32,
    /// Test ID
    test_id_value: Option<String>,
    /// Cached bounds
    #[serde(skip)]
    bounds: Rect,
    /// Modal content bounds
    #[serde(skip)]
    content_bounds: Rect,
    /// Modal content
    #[serde(skip)]
    content: Option<Box<dyn Widget>>,
    /// Footer content
    #[serde(skip)]
    footer: Option<Box<dyn Widget>>,
    /// Animation progress (0.0 = closed, 1.0 = open)
    #[serde(skip)]
    animation_progress: f32,
}

impl Default for Modal {
    fn default() -> Self {
        Self {
            open: false,
            size: ModalSize::Medium,
            backdrop: BackdropBehavior::CloseOnClick,
            close_on_escape: true,
            title: None,
            show_close_button: true,
            backdrop_color: Color::rgba(0.0, 0.0, 0.0, 0.5),
            background_color: Color::WHITE,
            border_radius: 8.0,
            padding: 24.0,
            test_id_value: None,
            bounds: Rect::default(),
            content_bounds: Rect::default(),
            content: None,
            footer: None,
            animation_progress: 0.0,
        }
    }
}

impl Modal {
    /// Create a new modal dialog.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set modal open state.
    #[must_use]
    pub const fn open(mut self, open: bool) -> Self {
        self.open = open;
        self
    }

    /// Set modal size.
    #[must_use]
    pub const fn size(mut self, size: ModalSize) -> Self {
        self.size = size;
        self
    }

    /// Set backdrop behavior.
    #[must_use]
    pub const fn backdrop(mut self, behavior: BackdropBehavior) -> Self {
        self.backdrop = behavior;
        self
    }

    /// Set close on escape.
    #[must_use]
    pub const fn close_on_escape(mut self, enabled: bool) -> Self {
        self.close_on_escape = enabled;
        self
    }

    /// Set the title.
    #[must_use]
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set show close button.
    #[must_use]
    pub const fn show_close_button(mut self, show: bool) -> Self {
        self.show_close_button = show;
        self
    }

    /// Set backdrop color.
    #[must_use]
    pub const fn backdrop_color(mut self, color: Color) -> Self {
        self.backdrop_color = color;
        self
    }

    /// Set background color.
    #[must_use]
    pub const fn background_color(mut self, color: Color) -> Self {
        self.background_color = color;
        self
    }

    /// Set border radius.
    #[must_use]
    pub const fn border_radius(mut self, radius: f32) -> Self {
        self.border_radius = radius;
        self
    }

    /// Set padding.
    #[must_use]
    pub const fn padding(mut self, padding: f32) -> Self {
        self.padding = padding;
        self
    }

    /// Set the content widget.
    pub fn content(mut self, widget: impl Widget + 'static) -> Self {
        self.content = Some(Box::new(widget));
        self
    }

    /// Set the footer widget.
    pub fn footer(mut self, widget: impl Widget + 'static) -> Self {
        self.footer = Some(Box::new(widget));
        self
    }

    /// Set the test ID.
    #[must_use]
    pub fn with_test_id(mut self, id: impl Into<String>) -> Self {
        self.test_id_value = Some(id.into());
        self
    }

    /// Open the modal.
    pub fn show(&mut self) {
        self.open = true;
    }

    /// Close the modal.
    pub fn hide(&mut self) {
        self.open = false;
    }

    /// Toggle the modal.
    pub fn toggle(&mut self) {
        self.open = !self.open;
    }

    /// Check if modal is open.
    #[must_use]
    pub const fn is_open(&self) -> bool {
        self.open
    }

    /// Get animation progress.
    #[must_use]
    pub const fn animation_progress(&self) -> f32 {
        self.animation_progress
    }

    /// Get content bounds.
    #[must_use]
    pub const fn content_bounds(&self) -> Rect {
        self.content_bounds
    }

    /// Calculate modal dimensions based on viewport.
    fn calculate_modal_bounds(&self, viewport: Rect) -> Rect {
        let max_width = self.size.max_width();
        let modal_width = max_width.min(viewport.width - 32.0); // 16px margin on each side

        // Estimate height based on content + header + footer
        let header_height = if self.title.is_some() { 56.0 } else { 0.0 };
        let footer_height = if self.footer.is_some() { 64.0 } else { 0.0 };
        let content_height = 200.0; // Placeholder, will be measured properly
        let total_height = self
            .padding
            .mul_add(2.0, header_height + content_height + footer_height);
        let modal_height = total_height.min(viewport.height - 64.0); // 32px margin top/bottom

        let x = viewport.x + (viewport.width - modal_width) / 2.0;
        let y = viewport.y + (viewport.height - modal_height) / 2.0;

        Rect::new(x, y, modal_width, modal_height)
    }
}

impl Widget for Modal {
    fn type_id(&self) -> TypeId {
        TypeId::of::<Self>()
    }

    fn measure(&self, constraints: Constraints) -> Size {
        // Modal overlays the entire viewport
        constraints.constrain(Size::new(constraints.max_width, constraints.max_height))
    }

    fn layout(&mut self, bounds: Rect) -> LayoutResult {
        self.bounds = bounds;

        if self.open {
            self.content_bounds = self.calculate_modal_bounds(bounds);

            // Layout content
            if let Some(ref mut content) = self.content {
                let header_height = if self.title.is_some() { 56.0 } else { 0.0 };
                let footer_height = if self.footer.is_some() { 64.0 } else { 0.0 };

                let content_rect = Rect::new(
                    self.content_bounds.x + self.padding,
                    self.content_bounds.y + header_height + self.padding,
                    self.padding.mul_add(-2.0, self.content_bounds.width),
                    self.padding.mul_add(
                        -2.0,
                        self.content_bounds.height - header_height - footer_height,
                    ),
                );
                content.layout(content_rect);
            }

            // Layout footer
            if let Some(ref mut footer) = self.footer {
                let footer_rect = Rect::new(
                    self.content_bounds.x + self.padding,
                    self.content_bounds.y + self.content_bounds.height - 64.0 - self.padding,
                    self.padding.mul_add(-2.0, self.content_bounds.width),
                    64.0,
                );
                footer.layout(footer_rect);
            }

            // Animate towards open
            self.animation_progress = (self.animation_progress + 0.15).min(1.0);
        } else {
            // Animate towards closed
            self.animation_progress = (self.animation_progress - 0.15).max(0.0);
        }

        LayoutResult {
            size: bounds.size(),
        }
    }

    fn paint(&self, canvas: &mut dyn Canvas) {
        if self.animation_progress <= 0.0 {
            return;
        }

        let opacity = self.animation_progress;

        // Draw backdrop
        if self.backdrop != BackdropBehavior::None {
            let backdrop_color = Color::rgba(
                self.backdrop_color.r,
                self.backdrop_color.g,
                self.backdrop_color.b,
                self.backdrop_color.a * opacity,
            );
            canvas.fill_rect(self.bounds, backdrop_color);
        }

        // Draw modal container with slight animation offset
        let y_offset = (1.0 - opacity) * 20.0;
        let animated_bounds = Rect::new(
            self.content_bounds.x,
            self.content_bounds.y + y_offset,
            self.content_bounds.width,
            self.content_bounds.height,
        );

        // Draw shadow (simplified) - draw first so it's behind
        let shadow_color = Color::rgba(0.0, 0.0, 0.0, 0.1 * opacity);
        let shadow_bounds = Rect::new(
            animated_bounds.x + 4.0,
            animated_bounds.y + 4.0,
            animated_bounds.width,
            animated_bounds.height,
        );
        canvas.fill_rect(shadow_bounds, shadow_color);

        // Modal background
        canvas.fill_rect(animated_bounds, self.background_color);

        // Draw title
        if let Some(ref title) = self.title {
            let title_pos = Point::new(
                animated_bounds.x + self.padding,
                animated_bounds.y + self.padding + 16.0, // Baseline offset
            );
            let title_style = TextStyle {
                size: 18.0,
                color: Color::BLACK,
                ..Default::default()
            };
            canvas.draw_text(title, title_pos, &title_style);
        }

        // Draw close button
        if self.show_close_button {
            let close_x = animated_bounds.x + animated_bounds.width - 40.0 - self.padding;
            let close_y = animated_bounds.y + self.padding + 16.0;
            let close_style = TextStyle {
                size: 24.0,
                color: Color::rgb(0.5, 0.5, 0.5),
                ..Default::default()
            };
            canvas.draw_text("×", Point::new(close_x, close_y), &close_style);
        }

        // Draw content
        if let Some(ref content) = self.content {
            content.paint(canvas);
        }

        // Draw footer
        if let Some(ref footer) = self.footer {
            footer.paint(canvas);
        }
    }

    fn event(&mut self, event: &Event) -> Option<Box<dyn Any + Send>> {
        if !self.open {
            return None;
        }

        match event {
            Event::KeyDown {
                key: Key::Escape, ..
            } if self.close_on_escape => {
                self.hide();
                return Some(Box::new(ModalClosed {
                    reason: CloseReason::Escape,
                }));
            }
            Event::MouseDown { position, .. } => {
                // Check if click is on backdrop
                if self.backdrop == BackdropBehavior::CloseOnClick {
                    let in_modal = position.x >= self.content_bounds.x
                        && position.x <= self.content_bounds.x + self.content_bounds.width
                        && position.y >= self.content_bounds.y
                        && position.y <= self.content_bounds.y + self.content_bounds.height;

                    if !in_modal {
                        self.hide();
                        return Some(Box::new(ModalClosed {
                            reason: CloseReason::Backdrop,
                        }));
                    }
                }

                // Check if click is on close button
                if self.show_close_button {
                    let close_x =
                        self.content_bounds.x + self.content_bounds.width - 40.0 - self.padding;
                    let close_y = self.content_bounds.y + self.padding;
                    let on_close_btn = position.x >= close_x
                        && position.x <= close_x + 24.0
                        && position.y >= close_y
                        && position.y <= close_y + 24.0;

                    if on_close_btn {
                        self.hide();
                        return Some(Box::new(ModalClosed {
                            reason: CloseReason::CloseButton,
                        }));
                    }
                }

                // Forward to content
                if let Some(ref mut content) = self.content {
                    if let Some(msg) = content.event(event) {
                        return Some(msg);
                    }
                }

                // Forward to footer
                if let Some(ref mut footer) = self.footer {
                    if let Some(msg) = footer.event(event) {
                        return Some(msg);
                    }
                }
            }
            _ => {
                // Forward other events to content
                if let Some(ref mut content) = self.content {
                    if let Some(msg) = content.event(event) {
                        return Some(msg);
                    }
                }

                if let Some(ref mut footer) = self.footer {
                    if let Some(msg) = footer.event(event) {
                        return Some(msg);
                    }
                }
            }
        }

        None
    }

    fn children(&self) -> &[Box<dyn Widget>] {
        &[]
    }

    fn children_mut(&mut self) -> &mut [Box<dyn Widget>] {
        &mut []
    }

    fn is_focusable(&self) -> bool {
        self.open
    }

    fn test_id(&self) -> Option<&str> {
        self.test_id_value.as_deref()
    }

    fn bounds(&self) -> Rect {
        self.bounds
    }
}

// PROBAR-SPEC-009: Brick Architecture - Tests define interface
impl Brick for Modal {
    fn brick_name(&self) -> &'static str {
        "Modal"
    }

    fn assertions(&self) -> &[BrickAssertion] {
        &[BrickAssertion::MaxLatencyMs(16)]
    }

    fn budget(&self) -> BrickBudget {
        BrickBudget::uniform(16)
    }

    fn verify(&self) -> BrickVerification {
        BrickVerification {
            passed: self.assertions().to_vec(),
            failed: vec![],
            verification_time: Duration::from_micros(10),
        }
    }

    fn to_html(&self) -> String {
        r#"<div class="brick-modal"></div>"#.to_string()
    }

    fn to_css(&self) -> String {
        ".brick-modal { display: block; position: fixed; }".to_string()
    }

    fn test_id(&self) -> Option<&str> {
        self.test_id_value.as_deref()
    }
}

/// Reason the modal was closed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CloseReason {
    /// Closed via escape key
    Escape,
    /// Closed via backdrop click
    Backdrop,
    /// Closed via close button
    CloseButton,
    /// Closed programmatically
    Programmatic,
}

/// Message emitted when modal is closed.
#[derive(Debug, Clone)]
pub struct ModalClosed {
    /// Reason for closure
    pub reason: CloseReason,
}

/// Message emitted when modal is opened.
#[derive(Debug, Clone)]
pub struct ModalOpened;

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

    // =========================================================================
    // ModalSize Tests
    // =========================================================================

    #[test]
    fn test_modal_size_default() {
        assert_eq!(ModalSize::default(), ModalSize::Medium);
    }

    #[test]
    fn test_modal_size_max_width() {
        assert_eq!(ModalSize::Small.max_width(), 300.0);
        assert_eq!(ModalSize::Medium.max_width(), 500.0);
        assert_eq!(ModalSize::Large.max_width(), 800.0);
        assert_eq!(ModalSize::FullWidth.max_width(), f32::MAX);
        assert_eq!(ModalSize::Custom(600).max_width(), 600.0);
    }

    // =========================================================================
    // BackdropBehavior Tests
    // =========================================================================

    #[test]
    fn test_backdrop_behavior_default() {
        assert_eq!(BackdropBehavior::default(), BackdropBehavior::CloseOnClick);
    }

    // =========================================================================
    // Modal Tests
    // =========================================================================

    #[test]
    fn test_modal_new() {
        let modal = Modal::new();
        assert!(!modal.open);
        assert_eq!(modal.size, ModalSize::Medium);
        assert_eq!(modal.backdrop, BackdropBehavior::CloseOnClick);
        assert!(modal.close_on_escape);
        assert!(modal.title.is_none());
        assert!(modal.show_close_button);
    }

    #[test]
    fn test_modal_builder() {
        let modal = Modal::new()
            .open(true)
            .size(ModalSize::Large)
            .backdrop(BackdropBehavior::Static)
            .close_on_escape(false)
            .title("Test Modal")
            .show_close_button(false)
            .border_radius(16.0)
            .padding(32.0);

        assert!(modal.open);
        assert_eq!(modal.size, ModalSize::Large);
        assert_eq!(modal.backdrop, BackdropBehavior::Static);
        assert!(!modal.close_on_escape);
        assert_eq!(modal.title, Some("Test Modal".to_string()));
        assert!(!modal.show_close_button);
        assert_eq!(modal.border_radius, 16.0);
        assert_eq!(modal.padding, 32.0);
    }

    #[test]
    fn test_modal_show_hide() {
        let mut modal = Modal::new();
        assert!(!modal.is_open());

        modal.show();
        assert!(modal.is_open());

        modal.hide();
        assert!(!modal.is_open());
    }

    #[test]
    fn test_modal_toggle() {
        let mut modal = Modal::new();
        assert!(!modal.is_open());

        modal.toggle();
        assert!(modal.is_open());

        modal.toggle();
        assert!(!modal.is_open());
    }

    #[test]
    fn test_modal_measure() {
        let modal = Modal::new();
        let size = modal.measure(Constraints::loose(Size::new(1024.0, 768.0)));
        assert_eq!(size, Size::new(1024.0, 768.0));
    }

    #[test]
    fn test_modal_layout_closed() {
        let mut modal = Modal::new();
        let result = modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        assert_eq!(result.size, Size::new(1024.0, 768.0));
        assert_eq!(modal.animation_progress, 0.0);
    }

    #[test]
    fn test_modal_layout_open() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        assert!(modal.animation_progress > 0.0);
    }

    #[test]
    fn test_modal_calculate_bounds() {
        let modal = Modal::new().size(ModalSize::Medium);
        let viewport = Rect::new(0.0, 0.0, 1024.0, 768.0);
        let bounds = modal.calculate_modal_bounds(viewport);

        // Modal should be centered
        assert!(bounds.x > 0.0);
        assert!(bounds.y > 0.0);
        assert!(bounds.width <= 500.0);
    }

    #[test]
    fn test_modal_type_id() {
        let modal = Modal::new();
        assert_eq!(Widget::type_id(&modal), TypeId::of::<Modal>());
    }

    #[test]
    fn test_modal_is_focusable() {
        let modal = Modal::new();
        assert!(!modal.is_focusable()); // Not focusable when closed

        let modal_open = Modal::new().open(true);
        assert!(modal_open.is_focusable()); // Focusable when open
    }

    #[test]
    fn test_modal_test_id() {
        let modal = Modal::new().with_test_id("my-modal");
        assert_eq!(Widget::test_id(&modal), Some("my-modal"));
    }

    #[test]
    fn test_modal_children_empty() {
        let modal = Modal::new();
        assert!(modal.children().is_empty());
    }

    #[test]
    fn test_modal_bounds() {
        let mut modal = Modal::new();
        modal.layout(Rect::new(10.0, 20.0, 1024.0, 768.0));
        assert_eq!(modal.bounds(), Rect::new(10.0, 20.0, 1024.0, 768.0));
    }

    #[test]
    fn test_modal_backdrop_color() {
        let modal = Modal::new().backdrop_color(Color::rgba(0.0, 0.0, 0.0, 0.7));
        assert_eq!(modal.backdrop_color.a, 0.7);
    }

    #[test]
    fn test_modal_background_color() {
        let modal = Modal::new().background_color(Color::rgb(0.9, 0.9, 0.9));
        assert_eq!(modal.background_color.r, 0.9);
    }

    #[test]
    fn test_modal_escape_closes() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));

        let result = modal.event(&Event::key_down(Key::Escape));
        assert!(result.is_some());
        assert!(!modal.is_open());
    }

    #[test]
    fn test_modal_escape_disabled() {
        let mut modal = Modal::new().open(true).close_on_escape(false);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));

        let result = modal.event(&Event::key_down(Key::Escape));
        assert!(result.is_none());
        assert!(modal.is_open());
    }

    #[test]
    fn test_modal_animation_progress() {
        let modal = Modal::new();
        assert_eq!(modal.animation_progress(), 0.0);
    }

    #[test]
    fn test_modal_content_bounds() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        let content_bounds = modal.content_bounds();
        assert!(content_bounds.width > 0.0);
        assert!(content_bounds.height > 0.0);
    }

    // =========================================================================
    // CloseReason Tests
    // =========================================================================

    #[test]
    fn test_close_reason_eq() {
        assert_eq!(CloseReason::Escape, CloseReason::Escape);
        assert_ne!(CloseReason::Escape, CloseReason::Backdrop);
    }

    // =========================================================================
    // Message Tests
    // =========================================================================

    #[test]
    fn test_modal_closed_message() {
        let msg = ModalClosed {
            reason: CloseReason::CloseButton,
        };
        assert_eq!(msg.reason, CloseReason::CloseButton);
    }

    #[test]
    fn test_modal_opened_message() {
        let msg = ModalOpened;
        assert_eq!(format!("{msg:?}"), "ModalOpened");
    }

    // =========================================================================
    // Additional Coverage Tests
    // =========================================================================

    #[test]
    fn test_modal_backdrop_none() {
        let modal = Modal::new().backdrop(BackdropBehavior::None);
        assert_eq!(modal.backdrop, BackdropBehavior::None);
    }

    #[test]
    fn test_modal_backdrop_static() {
        let modal = Modal::new().backdrop(BackdropBehavior::Static);
        assert_eq!(modal.backdrop, BackdropBehavior::Static);
    }

    #[test]
    fn test_modal_size_small() {
        assert_eq!(ModalSize::Small.max_width(), 300.0);
    }

    #[test]
    fn test_modal_size_full_width() {
        assert_eq!(ModalSize::FullWidth.max_width(), f32::MAX);
    }

    #[test]
    fn test_modal_children_mut_empty() {
        let mut modal = Modal::new();
        assert!(modal.children_mut().is_empty());
    }

    #[test]
    fn test_modal_calculate_bounds_with_title() {
        let modal = Modal::new().title("Test Title");
        let viewport = Rect::new(0.0, 0.0, 1024.0, 768.0);
        let bounds = modal.calculate_modal_bounds(viewport);
        assert!(bounds.height > 0.0);
    }

    #[test]
    fn test_modal_layout_animation_closes() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        // Progress should increase
        let prog1 = modal.animation_progress;
        modal.open = false;
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        // Progress should decrease
        assert!(modal.animation_progress < prog1);
    }

    #[test]
    fn test_modal_event_not_open_returns_none() {
        let mut modal = Modal::new();
        let result = modal.event(&Event::key_down(Key::Escape));
        assert!(result.is_none());
    }

    #[test]
    fn test_modal_other_key_does_nothing() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        let result = modal.event(&Event::key_down(Key::Tab));
        assert!(result.is_none());
        assert!(modal.is_open());
    }

    #[test]
    fn test_close_reason_programmatic() {
        let reason = CloseReason::Programmatic;
        assert_eq!(reason, CloseReason::Programmatic);
    }

    #[test]
    fn test_close_reason_close_button() {
        let reason = CloseReason::CloseButton;
        assert_eq!(reason, CloseReason::CloseButton);
    }

    #[test]
    fn test_modal_size_custom_value() {
        let size = ModalSize::Custom(750);
        assert_eq!(size.max_width(), 750.0);
    }

    #[test]
    fn test_modal_backdrop_eq() {
        assert_eq!(
            BackdropBehavior::CloseOnClick,
            BackdropBehavior::CloseOnClick
        );
        assert_ne!(BackdropBehavior::CloseOnClick, BackdropBehavior::Static);
    }

    #[test]
    fn test_modal_size_eq() {
        assert_eq!(ModalSize::Medium, ModalSize::Medium);
        assert_ne!(ModalSize::Small, ModalSize::Large);
    }

    // =========================================================================
    // Brick Trait Tests
    // =========================================================================

    #[test]
    fn test_modal_brick_name() {
        let modal = Modal::new();
        assert_eq!(modal.brick_name(), "Modal");
    }

    #[test]
    fn test_modal_brick_assertions() {
        let modal = Modal::new();
        let assertions = modal.assertions();
        assert!(!assertions.is_empty());
        assert!(matches!(assertions[0], BrickAssertion::MaxLatencyMs(16)));
    }

    #[test]
    fn test_modal_brick_budget() {
        let modal = Modal::new();
        let budget = modal.budget();
        // Verify budget has reasonable values
        assert!(budget.layout_ms > 0);
        assert!(budget.paint_ms > 0);
    }

    #[test]
    fn test_modal_brick_verify() {
        let modal = Modal::new();
        let verification = modal.verify();
        assert!(!verification.passed.is_empty());
        assert!(verification.failed.is_empty());
    }

    #[test]
    fn test_modal_brick_to_html() {
        let modal = Modal::new();
        let html = modal.to_html();
        assert!(html.contains("brick-modal"));
    }

    #[test]
    fn test_modal_brick_to_css() {
        let modal = Modal::new();
        let css = modal.to_css();
        assert!(css.contains(".brick-modal"));
        assert!(css.contains("display: block"));
        assert!(css.contains("position: fixed"));
    }

    #[test]
    fn test_modal_brick_test_id() {
        let modal = Modal::new().with_test_id("my-modal");
        assert_eq!(Brick::test_id(&modal), Some("my-modal"));
    }

    #[test]
    fn test_modal_brick_test_id_none() {
        let modal = Modal::new();
        assert!(Brick::test_id(&modal).is_none());
    }

    // =========================================================================
    // Backdrop Click Tests
    // =========================================================================

    #[test]
    fn test_modal_backdrop_click_closes() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));

        // Click outside the modal content (on backdrop)
        let result = modal.event(&Event::MouseDown {
            position: Point::new(10.0, 10.0),
            button: presentar_core::MouseButton::Left,
        });

        assert!(result.is_some());
        assert!(!modal.is_open());
    }

    #[test]
    fn test_modal_backdrop_static_no_close() {
        let mut modal = Modal::new().open(true).backdrop(BackdropBehavior::Static);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));

        // Click outside the modal content
        let result = modal.event(&Event::MouseDown {
            position: Point::new(10.0, 10.0),
            button: presentar_core::MouseButton::Left,
        });

        assert!(result.is_none());
        assert!(modal.is_open());
    }

    #[test]
    fn test_modal_click_inside_does_not_close() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));

        // Click inside the modal content
        let center_x = modal.content_bounds.x + modal.content_bounds.width / 2.0;
        let center_y = modal.content_bounds.y + modal.content_bounds.height / 2.0;

        let result = modal.event(&Event::MouseDown {
            position: Point::new(center_x, center_y),
            button: presentar_core::MouseButton::Left,
        });

        // No close message, modal stays open
        assert!(result.is_none());
        assert!(modal.is_open());
    }

    // =========================================================================
    // Close Button Tests
    // =========================================================================

    #[test]
    fn test_modal_close_button_click() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));

        // Calculate close button position
        let close_x = modal.content_bounds.x + modal.content_bounds.width - 40.0 - modal.padding;
        let close_y = modal.content_bounds.y + modal.padding;

        let result = modal.event(&Event::MouseDown {
            position: Point::new(close_x + 10.0, close_y + 10.0),
            button: presentar_core::MouseButton::Left,
        });

        assert!(result.is_some());
        assert!(!modal.is_open());
    }

    #[test]
    fn test_modal_close_button_hidden() {
        let mut modal = Modal::new().open(true).show_close_button(false);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));

        // Click where close button would be
        let close_x = modal.content_bounds.x + modal.content_bounds.width - 40.0 - modal.padding;
        let close_y = modal.content_bounds.y + modal.padding;

        let result = modal.event(&Event::MouseDown {
            position: Point::new(close_x + 10.0, close_y + 10.0),
            button: presentar_core::MouseButton::Left,
        });

        // Should not close because button is hidden
        assert!(result.is_none());
        assert!(modal.is_open());
    }

    // =========================================================================
    // Animation Tests
    // =========================================================================

    #[test]
    fn test_modal_animation_opens() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        assert!(modal.animation_progress > 0.0);

        // Another layout call should increase progress further
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        assert!(modal.animation_progress >= 0.15);
    }

    #[test]
    fn test_modal_animation_caps_at_one() {
        let mut modal = Modal::new().open(true);
        // Run layout multiple times
        for _ in 0..20 {
            modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        }
        assert!((modal.animation_progress - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_modal_animation_closes_to_zero() {
        let mut modal = Modal::new().open(true);
        // Open fully
        for _ in 0..20 {
            modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        }

        modal.open = false;
        // Close animation
        for _ in 0..20 {
            modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));
        }
        assert!(modal.animation_progress < 0.01);
    }

    // =========================================================================
    // Calculate Bounds Tests
    // =========================================================================

    #[test]
    fn test_modal_calculate_bounds_centered() {
        let modal = Modal::new().size(ModalSize::Medium);
        let viewport = Rect::new(0.0, 0.0, 1024.0, 768.0);
        let bounds = modal.calculate_modal_bounds(viewport);

        // Modal should be horizontally centered
        let expected_x = (1024.0 - bounds.width) / 2.0;
        assert!((bounds.x - expected_x).abs() < 1.0);
    }

    #[test]
    fn test_modal_calculate_bounds_small_viewport() {
        let modal = Modal::new().size(ModalSize::Large); // 800px wide
        let viewport = Rect::new(0.0, 0.0, 400.0, 300.0); // Smaller than modal
        let bounds = modal.calculate_modal_bounds(viewport);

        // Modal should be constrained to viewport minus margins
        assert!(bounds.width <= 400.0 - 32.0);
    }

    #[test]
    fn test_modal_calculate_bounds_with_footer() {
        let modal = Modal::new().title("Test");
        let viewport = Rect::new(0.0, 0.0, 1024.0, 768.0);
        let bounds = modal.calculate_modal_bounds(viewport);

        // Height should include header
        assert!(bounds.height > 0.0);
    }

    // =========================================================================
    // Size Variant Tests
    // =========================================================================

    #[test]
    fn test_modal_size_large() {
        assert_eq!(ModalSize::Large.max_width(), 800.0);
    }

    #[test]
    fn test_modal_size_custom_zero() {
        // Custom size of 0 should still work
        assert_eq!(ModalSize::Custom(0).max_width(), 0.0);
    }

    // =========================================================================
    // CloseReason Tests
    // =========================================================================

    #[test]
    fn test_close_reason_copy() {
        let reason = CloseReason::Escape;
        let copied: CloseReason = reason;
        assert_eq!(copied, CloseReason::Escape);
    }

    #[test]
    fn test_close_reason_all_variants() {
        let reasons = [
            CloseReason::Escape,
            CloseReason::Backdrop,
            CloseReason::CloseButton,
            CloseReason::Programmatic,
        ];
        assert_eq!(reasons.len(), 4);
    }

    // =========================================================================
    // Message Tests
    // =========================================================================

    #[test]
    fn test_modal_closed_clone() {
        let msg = ModalClosed {
            reason: CloseReason::Escape,
        };
        let cloned = msg;
        assert_eq!(cloned.reason, CloseReason::Escape);
    }

    #[test]
    fn test_modal_opened_clone() {
        let msg = ModalOpened;
        let cloned = msg;
        assert_eq!(format!("{cloned:?}"), "ModalOpened");
    }

    #[test]
    fn test_modal_closed_debug() {
        let msg = ModalClosed {
            reason: CloseReason::Backdrop,
        };
        let debug_str = format!("{msg:?}");
        assert!(debug_str.contains("Backdrop"));
    }

    #[test]
    fn test_modal_opened_debug() {
        let msg = ModalOpened;
        let debug_str = format!("{msg:?}");
        assert!(debug_str.contains("ModalOpened"));
    }

    // =========================================================================
    // Default Trait Tests
    // =========================================================================

    #[test]
    fn test_modal_default_values() {
        let modal = Modal::default();
        assert!(!modal.open);
        assert_eq!(modal.size, ModalSize::Medium);
        assert_eq!(modal.backdrop, BackdropBehavior::CloseOnClick);
        assert!(modal.close_on_escape);
        assert!(modal.title.is_none());
        assert!(modal.show_close_button);
        assert_eq!(modal.border_radius, 8.0);
        assert_eq!(modal.padding, 24.0);
    }

    // =========================================================================
    // Widget Trait Edge Cases
    // =========================================================================

    #[test]
    fn test_modal_measure_constraints() {
        let modal = Modal::new();
        let size = modal.measure(Constraints::tight(Size::new(800.0, 600.0)));
        assert_eq!(size.width, 800.0);
        assert_eq!(size.height, 600.0);
    }

    #[test]
    fn test_modal_children_mut() {
        let mut modal = Modal::new();
        assert!(modal.children_mut().is_empty());
    }

    #[test]
    fn test_modal_mouse_move_does_nothing() {
        let mut modal = Modal::new().open(true);
        modal.layout(Rect::new(0.0, 0.0, 1024.0, 768.0));

        let result = modal.event(&Event::MouseMove {
            position: Point::new(100.0, 100.0),
        });
        assert!(result.is_none());
    }

    #[test]
    fn test_modal_title_setter() {
        let modal = Modal::new().title("Test Modal");
        // Modal doesn't derive Debug, just test it exists
        let _ = modal;
    }

    #[test]
    fn test_backdrop_behavior_copy() {
        let behavior = BackdropBehavior::Static;
        let copied: BackdropBehavior = behavior;
        assert_eq!(copied, BackdropBehavior::Static);
    }

    #[test]
    fn test_modal_size_copy() {
        let size = ModalSize::Large;
        let copied: ModalSize = size;
        assert_eq!(copied, ModalSize::Large);
    }

    #[test]
    fn test_close_reason_debug() {
        let reason = CloseReason::CloseButton;
        let debug_str = format!("{reason:?}");
        assert!(debug_str.contains("CloseButton"));
    }
}