aprender-present-widgets 0.31.2

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
//! Checkbox widget for boolean input.

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

/// Checkbox state (supports tri-state).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum CheckState {
    /// Not checked
    #[default]
    Unchecked,
    /// Checked
    Checked,
    /// Indeterminate (for partial selection in trees)
    Indeterminate,
}

impl CheckState {
    /// Toggle between checked and unchecked.
    #[must_use]
    pub const fn toggle(&self) -> Self {
        match self {
            Self::Unchecked => Self::Checked,
            Self::Checked | Self::Indeterminate => Self::Unchecked,
        }
    }

    /// Check if checked (true for Checked, false for others).
    #[must_use]
    pub const fn is_checked(&self) -> bool {
        matches!(self, Self::Checked)
    }

    /// Check if indeterminate.
    #[must_use]
    pub const fn is_indeterminate(&self) -> bool {
        matches!(self, Self::Indeterminate)
    }
}

/// Message emitted when checkbox state changes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CheckboxChanged {
    /// The new state
    pub state: CheckState,
}

/// Checkbox widget.
#[derive(Serialize, Deserialize)]
pub struct Checkbox {
    /// Current state
    state: CheckState,
    /// Whether disabled
    disabled: bool,
    /// Label text
    label: String,
    /// Box size
    box_size: f32,
    /// Spacing between box and label
    spacing: f32,
    /// Unchecked box color
    box_color: Color,
    /// Checked box color
    checked_color: Color,
    /// Check mark color
    check_color: Color,
    /// Label color
    label_color: Color,
    /// Disabled color
    disabled_color: Color,
    /// Test ID
    test_id_value: Option<String>,
    /// Accessible name
    accessible_name_value: Option<String>,
    /// Cached bounds
    #[serde(skip)]
    bounds: Rect,
    /// Whether hovered
    #[serde(skip)]
    hovered: bool,
}

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

impl Checkbox {
    /// Create a new checkbox.
    #[must_use]
    pub fn new() -> Self {
        Self {
            state: CheckState::Unchecked,
            disabled: false,
            label: String::new(),
            box_size: 18.0,
            spacing: 8.0,
            box_color: Color::new(0.8, 0.8, 0.8, 1.0),
            checked_color: Color::new(0.2, 0.47, 0.96, 1.0),
            check_color: Color::WHITE,
            label_color: Color::BLACK,
            disabled_color: Color::new(0.6, 0.6, 0.6, 1.0),
            test_id_value: None,
            accessible_name_value: None,
            bounds: Rect::default(),
            hovered: false,
        }
    }

    /// Set the checked state.
    #[must_use]
    pub const fn checked(mut self, checked: bool) -> Self {
        self.state = if checked {
            CheckState::Checked
        } else {
            CheckState::Unchecked
        };
        self
    }

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

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

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

    /// Set box size.
    #[must_use]
    pub fn box_size(mut self, size: f32) -> Self {
        self.box_size = size.max(8.0);
        self
    }

    /// Set spacing between box and label.
    #[must_use]
    pub fn spacing(mut self, spacing: f32) -> Self {
        self.spacing = spacing.max(0.0);
        self
    }

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

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

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

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

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

    /// Get current state.
    #[must_use]
    pub const fn get_state(&self) -> CheckState {
        self.state
    }

    /// Check if currently checked.
    #[must_use]
    pub const fn is_checked(&self) -> bool {
        self.state.is_checked()
    }

    /// Check if indeterminate.
    #[must_use]
    pub const fn is_indeterminate(&self) -> bool {
        self.state.is_indeterminate()
    }

    /// Get the label.
    #[must_use]
    pub fn get_label(&self) -> &str {
        &self.label
    }
}

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

    fn measure(&self, constraints: Constraints) -> Size {
        // Estimate label width (rough approximation)
        let label_width = if self.label.is_empty() {
            0.0
        } else {
            self.label.len() as f32 * 8.0 // ~8px per character
        };

        let total_width = self.box_size + self.spacing + label_width;
        let height = self.box_size;

        constraints.constrain(Size::new(total_width, height))
    }

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

    fn paint(&self, canvas: &mut dyn Canvas) {
        let box_rect = Rect::new(
            self.bounds.x,
            self.bounds.y + (self.bounds.height - self.box_size) / 2.0,
            self.box_size,
            self.box_size,
        );

        // Draw checkbox box
        let box_color = if self.disabled {
            self.disabled_color
        } else if self.state.is_checked() || self.state.is_indeterminate() {
            self.checked_color
        } else {
            self.box_color
        };

        canvas.fill_rect(box_rect, box_color);

        // Draw check mark or indeterminate line
        if !self.disabled {
            match self.state {
                CheckState::Checked => {
                    // Draw checkmark (simplified as a filled inner rect)
                    let inner = Rect::new(
                        self.box_size.mul_add(0.25, box_rect.x),
                        self.box_size.mul_add(0.25, box_rect.y),
                        self.box_size * 0.5,
                        self.box_size * 0.5,
                    );
                    canvas.fill_rect(inner, self.check_color);
                }
                CheckState::Indeterminate => {
                    // Draw horizontal line
                    let line = Rect::new(
                        self.box_size.mul_add(0.2, box_rect.x),
                        self.box_size.mul_add(0.4, box_rect.y),
                        self.box_size * 0.6,
                        self.box_size * 0.2,
                    );
                    canvas.fill_rect(line, self.check_color);
                }
                CheckState::Unchecked => {}
            }
        }

        // Draw label
        if !self.label.is_empty() {
            let label_x = self.bounds.x + self.box_size + self.spacing;
            let label_y = self.bounds.y + (self.bounds.height - 16.0) / 2.0;
            let label_color = if self.disabled {
                self.disabled_color
            } else {
                self.label_color
            };

            let style = presentar_core::widget::TextStyle {
                color: label_color,
                ..Default::default()
            };
            canvas.draw_text(
                &self.label,
                presentar_core::Point::new(label_x, label_y),
                &style,
            );
        }
    }

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

        match event {
            Event::MouseMove { position } => {
                self.hovered = self.bounds.contains_point(position);
            }
            Event::MouseDown {
                position,
                button: MouseButton::Left,
            } => {
                if self.bounds.contains_point(position) {
                    self.state = self.state.toggle();
                    return Some(Box::new(CheckboxChanged { state: self.state }));
                }
            }
            _ => {}
        }

        None
    }

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

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

    fn is_interactive(&self) -> bool {
        !self.disabled
    }

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

    fn accessible_name(&self) -> Option<&str> {
        self.accessible_name_value
            .as_deref()
            .or(if self.label.is_empty() {
                None
            } else {
                Some(self.label.as_str())
            })
    }

    fn accessible_role(&self) -> AccessibleRole {
        AccessibleRole::Checkbox
    }

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

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

    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 {
        let test_id = self.test_id_value.as_deref().unwrap_or("checkbox");
        let checked = if self.state.is_checked() {
            " checked"
        } else {
            ""
        };
        let disabled = if self.disabled { " disabled" } else { "" };
        format!(
            r#"<input type="checkbox" class="brick-checkbox" data-testid="{}" aria-label="{}"{}{}/>"#,
            test_id,
            self.accessible_name_value.as_deref().unwrap_or(&self.label),
            checked,
            disabled
        )
    }

    fn to_css(&self) -> String {
        ".brick-checkbox { display: inline-block; }".into()
    }

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

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::disallowed_methods)]
mod tests {
    use super::*;
    use presentar_core::Widget;

    // =========================================================================
    // CheckState Tests - TESTS FIRST
    // =========================================================================

    #[test]
    fn test_check_state_default() {
        assert_eq!(CheckState::default(), CheckState::Unchecked);
    }

    #[test]
    fn test_check_state_toggle() {
        assert_eq!(CheckState::Unchecked.toggle(), CheckState::Checked);
        assert_eq!(CheckState::Checked.toggle(), CheckState::Unchecked);
        assert_eq!(CheckState::Indeterminate.toggle(), CheckState::Unchecked);
    }

    #[test]
    fn test_check_state_is_checked() {
        assert!(!CheckState::Unchecked.is_checked());
        assert!(CheckState::Checked.is_checked());
        assert!(!CheckState::Indeterminate.is_checked());
    }

    #[test]
    fn test_check_state_is_indeterminate() {
        assert!(!CheckState::Unchecked.is_indeterminate());
        assert!(!CheckState::Checked.is_indeterminate());
        assert!(CheckState::Indeterminate.is_indeterminate());
    }

    // =========================================================================
    // CheckboxChanged Tests - TESTS FIRST
    // =========================================================================

    #[test]
    fn test_checkbox_changed_message() {
        let msg = CheckboxChanged {
            state: CheckState::Checked,
        };
        assert_eq!(msg.state, CheckState::Checked);
    }

    // =========================================================================
    // Checkbox Construction Tests - TESTS FIRST
    // =========================================================================

    #[test]
    fn test_checkbox_new() {
        let cb = Checkbox::new();
        assert_eq!(cb.get_state(), CheckState::Unchecked);
        assert!(!cb.is_checked());
        assert!(!cb.disabled);
        assert!(cb.get_label().is_empty());
    }

    #[test]
    fn test_checkbox_default() {
        let cb = Checkbox::default();
        assert_eq!(cb.get_state(), CheckState::Unchecked);
    }

    #[test]
    fn test_checkbox_builder() {
        let cb = Checkbox::new()
            .checked(true)
            .label("Accept terms")
            .disabled(false)
            .box_size(20.0)
            .spacing(10.0)
            .with_test_id("terms-checkbox")
            .with_accessible_name("Terms and Conditions");

        assert!(cb.is_checked());
        assert_eq!(cb.get_label(), "Accept terms");
        assert!(!cb.disabled);
        assert_eq!(Widget::test_id(&cb), Some("terms-checkbox"));
        assert_eq!(cb.accessible_name(), Some("Terms and Conditions"));
    }

    #[test]
    fn test_checkbox_state_builder() {
        let cb = Checkbox::new().state(CheckState::Indeterminate);
        assert!(cb.is_indeterminate());
        assert!(!cb.is_checked());
    }

    // =========================================================================
    // Checkbox State Tests - TESTS FIRST
    // =========================================================================

    #[test]
    fn test_checkbox_checked_true() {
        let cb = Checkbox::new().checked(true);
        assert!(cb.is_checked());
        assert_eq!(cb.get_state(), CheckState::Checked);
    }

    #[test]
    fn test_checkbox_checked_false() {
        let cb = Checkbox::new().checked(false);
        assert!(!cb.is_checked());
        assert_eq!(cb.get_state(), CheckState::Unchecked);
    }

    #[test]
    fn test_checkbox_indeterminate() {
        let cb = Checkbox::new().state(CheckState::Indeterminate);
        assert!(cb.is_indeterminate());
        assert!(!cb.is_checked());
    }

    // =========================================================================
    // Checkbox Widget Trait Tests - TESTS FIRST
    // =========================================================================

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

    #[test]
    fn test_checkbox_measure_no_label() {
        let cb = Checkbox::new().box_size(18.0);
        let size = cb.measure(Constraints::loose(Size::new(200.0, 100.0)));
        assert_eq!(size.width, 18.0 + 8.0); // box + spacing
        assert_eq!(size.height, 18.0);
    }

    #[test]
    fn test_checkbox_measure_with_label() {
        let cb = Checkbox::new().box_size(18.0).spacing(8.0).label("Test");
        let size = cb.measure(Constraints::loose(Size::new(200.0, 100.0)));
        // 18 (box) + 8 (spacing) + 4*8 (label ~32px)
        assert!(size.width > 18.0);
    }

    #[test]
    fn test_checkbox_is_interactive() {
        let cb = Checkbox::new();
        assert!(cb.is_interactive());

        let cb = Checkbox::new().disabled(true);
        assert!(!cb.is_interactive());
    }

    #[test]
    fn test_checkbox_is_focusable() {
        let cb = Checkbox::new();
        assert!(cb.is_focusable());

        let cb = Checkbox::new().disabled(true);
        assert!(!cb.is_focusable());
    }

    #[test]
    fn test_checkbox_accessible_role() {
        let cb = Checkbox::new();
        assert_eq!(cb.accessible_role(), AccessibleRole::Checkbox);
    }

    #[test]
    fn test_checkbox_accessible_name_from_label() {
        let cb = Checkbox::new().label("My checkbox");
        assert_eq!(cb.accessible_name(), Some("My checkbox"));
    }

    #[test]
    fn test_checkbox_accessible_name_override() {
        let cb = Checkbox::new()
            .label("Short")
            .with_accessible_name("Full accessible name");
        assert_eq!(cb.accessible_name(), Some("Full accessible name"));
    }

    #[test]
    fn test_checkbox_children() {
        let cb = Checkbox::new();
        assert!(cb.children().is_empty());
    }

    // =========================================================================
    // Checkbox Color Tests - TESTS FIRST
    // =========================================================================

    #[test]
    fn test_checkbox_colors() {
        let cb = Checkbox::new()
            .checked_color(Color::RED)
            .check_color(Color::GREEN)
            .label_color(Color::BLUE);

        assert_eq!(cb.checked_color, Color::RED);
        assert_eq!(cb.check_color, Color::GREEN);
        assert_eq!(cb.label_color, Color::BLUE);
    }

    // =========================================================================
    // Checkbox Layout Tests - TESTS FIRST
    // =========================================================================

    #[test]
    fn test_checkbox_layout() {
        let mut cb = Checkbox::new();
        let bounds = Rect::new(10.0, 20.0, 100.0, 30.0);
        let result = cb.layout(bounds);
        assert_eq!(result.size, bounds.size());
        assert_eq!(cb.bounds, bounds);
    }

    // =========================================================================
    // Checkbox Size Tests - TESTS FIRST
    // =========================================================================

    #[test]
    fn test_checkbox_box_size_min() {
        let cb = Checkbox::new().box_size(2.0);
        assert_eq!(cb.box_size, 8.0); // Minimum is 8
    }

    #[test]
    fn test_checkbox_spacing_min() {
        let cb = Checkbox::new().spacing(-5.0);
        assert_eq!(cb.spacing, 0.0); // Minimum is 0
    }

    // =========================================================================
    // Paint Tests - TESTS FIRST
    // =========================================================================

    use presentar_core::draw::DrawCommand;
    use presentar_core::RecordingCanvas;

    #[test]
    fn test_checkbox_paint_unchecked_draws_box() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Should draw box rect
        assert!(canvas.command_count() >= 1);
        match &canvas.commands()[0] {
            DrawCommand::Rect { bounds, style, .. } => {
                assert_eq!(bounds.width, 18.0);
                assert_eq!(bounds.height, 18.0);
                assert!(style.fill.is_some());
            }
            _ => panic!("Expected Rect command for checkbox box"),
        }
    }

    #[test]
    fn test_checkbox_paint_unchecked_no_checkmark() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Only box, no checkmark
        assert_eq!(canvas.command_count(), 1);
    }

    #[test]
    fn test_checkbox_paint_checked_draws_checkmark() {
        let mut cb = Checkbox::new().box_size(18.0).checked(true);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Should draw box + checkmark
        assert_eq!(canvas.command_count(), 2);

        // Second rect is the checkmark (inner rect)
        match &canvas.commands()[1] {
            DrawCommand::Rect { bounds, .. } => {
                // Checkmark is 50% of box size, centered
                assert!((bounds.width - 9.0).abs() < 0.1);
                assert!((bounds.height - 9.0).abs() < 0.1);
            }
            _ => panic!("Expected Rect command for checkmark"),
        }
    }

    #[test]
    fn test_checkbox_paint_indeterminate_draws_line() {
        let mut cb = Checkbox::new()
            .box_size(18.0)
            .state(CheckState::Indeterminate);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Should draw box + indeterminate line
        assert_eq!(canvas.command_count(), 2);

        // Second rect is the indeterminate line
        match &canvas.commands()[1] {
            DrawCommand::Rect { bounds, .. } => {
                // Line is 60% width, 20% height
                assert!((bounds.width - 10.8).abs() < 0.1);
                assert!((bounds.height - 3.6).abs() < 0.1);
            }
            _ => panic!("Expected Rect command for indeterminate line"),
        }
    }

    #[test]
    fn test_checkbox_paint_with_label() {
        let mut cb = Checkbox::new().box_size(18.0).label("Test label");
        cb.layout(Rect::new(0.0, 0.0, 200.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Should draw box + label text
        assert_eq!(canvas.command_count(), 2);

        // Second command is the label
        match &canvas.commands()[1] {
            DrawCommand::Text { content, .. } => {
                assert_eq!(content, "Test label");
            }
            _ => panic!("Expected Text command for label"),
        }
    }

    #[test]
    fn test_checkbox_paint_checked_with_label() {
        let mut cb = Checkbox::new().box_size(18.0).checked(true).label("Accept");
        cb.layout(Rect::new(0.0, 0.0, 200.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Should draw box + checkmark + label
        assert_eq!(canvas.command_count(), 3);

        // Third command is the label
        match &canvas.commands()[2] {
            DrawCommand::Text { content, .. } => {
                assert_eq!(content, "Accept");
            }
            _ => panic!("Expected Text command for label"),
        }
    }

    #[test]
    fn test_checkbox_paint_uses_checked_color() {
        let mut cb = Checkbox::new()
            .box_size(18.0)
            .checked(true)
            .checked_color(Color::RED);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Box should use checked color
        match &canvas.commands()[0] {
            DrawCommand::Rect { style, .. } => {
                assert_eq!(style.fill, Some(Color::RED));
            }
            _ => panic!("Expected Rect command"),
        }
    }

    #[test]
    fn test_checkbox_paint_uses_check_color() {
        let mut cb = Checkbox::new()
            .box_size(18.0)
            .checked(true)
            .check_color(Color::GREEN);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Checkmark should use check color
        match &canvas.commands()[1] {
            DrawCommand::Rect { style, .. } => {
                assert_eq!(style.fill, Some(Color::GREEN));
            }
            _ => panic!("Expected Rect command for checkmark"),
        }
    }

    #[test]
    fn test_checkbox_paint_disabled_no_checkmark() {
        let mut cb = Checkbox::new().box_size(18.0).checked(true).disabled(true);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Disabled checkbox doesn't draw checkmark
        assert_eq!(canvas.command_count(), 1);
    }

    #[test]
    fn test_checkbox_paint_disabled_uses_disabled_color() {
        let mut cb = Checkbox::new()
            .box_size(18.0)
            .disabled(true)
            .label("Disabled");
        let disabled_color = cb.disabled_color;
        cb.layout(Rect::new(0.0, 0.0, 200.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Box should use disabled color
        match &canvas.commands()[0] {
            DrawCommand::Rect { style, .. } => {
                assert_eq!(style.fill, Some(disabled_color));
            }
            _ => panic!("Expected Rect command"),
        }
    }

    #[test]
    fn test_checkbox_paint_label_position() {
        let mut cb = Checkbox::new().box_size(18.0).spacing(8.0).label("Label");
        cb.layout(Rect::new(10.0, 20.0, 200.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Label should be positioned after box + spacing
        match &canvas.commands()[1] {
            DrawCommand::Text { position, .. } => {
                // label_x = bounds.x + box_size + spacing = 10 + 18 + 8 = 36
                assert_eq!(position.x, 36.0);
            }
            _ => panic!("Expected Text command"),
        }
    }

    #[test]
    fn test_checkbox_paint_box_position_from_layout() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(50.0, 100.0, 100.0, 18.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        match &canvas.commands()[0] {
            DrawCommand::Rect { bounds, .. } => {
                assert_eq!(bounds.x, 50.0);
            }
            _ => panic!("Expected Rect command"),
        }
    }

    #[test]
    fn test_checkbox_paint_custom_box_size() {
        let mut cb = Checkbox::new().box_size(24.0).checked(true);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 24.0));

        let mut canvas = RecordingCanvas::new();
        cb.paint(&mut canvas);

        // Box should be 24x24
        match &canvas.commands()[0] {
            DrawCommand::Rect { bounds, .. } => {
                assert_eq!(bounds.width, 24.0);
                assert_eq!(bounds.height, 24.0);
            }
            _ => panic!("Expected Rect command"),
        }

        // Checkmark should be 50% = 12x12
        match &canvas.commands()[1] {
            DrawCommand::Rect { bounds, .. } => {
                assert_eq!(bounds.width, 12.0);
                assert_eq!(bounds.height, 12.0);
            }
            _ => panic!("Expected Rect command for checkmark"),
        }
    }

    // =========================================================================
    // Event Handling Tests - TESTS FIRST
    // =========================================================================

    use presentar_core::{MouseButton, Point};

    #[test]
    fn test_checkbox_event_click_toggles_unchecked_to_checked() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        assert!(!cb.is_checked());
        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Left,
        });
        assert!(cb.is_checked());
        assert!(result.is_some());
    }

    #[test]
    fn test_checkbox_event_click_toggles_checked_to_unchecked() {
        let mut cb = Checkbox::new().box_size(18.0).checked(true);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        assert!(cb.is_checked());
        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Left,
        });
        assert!(!cb.is_checked());
        assert!(result.is_some());
    }

    #[test]
    fn test_checkbox_event_click_indeterminate_to_unchecked() {
        let mut cb = Checkbox::new()
            .box_size(18.0)
            .state(CheckState::Indeterminate);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        assert!(cb.is_indeterminate());
        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Left,
        });
        // Indeterminate -> Unchecked per toggle() logic
        assert!(!cb.is_checked());
        assert!(!cb.is_indeterminate());
        let msg = result.unwrap().downcast::<CheckboxChanged>().unwrap();
        assert_eq!(msg.state, CheckState::Unchecked);
    }

    #[test]
    fn test_checkbox_event_emits_checkbox_changed() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Left,
        });

        let msg = result.unwrap().downcast::<CheckboxChanged>().unwrap();
        assert_eq!(msg.state, CheckState::Checked);
    }

    #[test]
    fn test_checkbox_event_message_reflects_new_state() {
        let mut cb = Checkbox::new().box_size(18.0).checked(true);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Left,
        });

        let msg = result.unwrap().downcast::<CheckboxChanged>().unwrap();
        assert_eq!(msg.state, CheckState::Unchecked);
    }

    #[test]
    fn test_checkbox_event_click_outside_bounds_no_toggle() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let result = cb.event(&Event::MouseDown {
            position: Point::new(200.0, 9.0),
            button: MouseButton::Left,
        });
        assert!(!cb.is_checked());
        assert!(result.is_none());
    }

    #[test]
    fn test_checkbox_event_right_click_no_toggle() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Right,
        });
        assert!(!cb.is_checked());
        assert!(result.is_none());
    }

    #[test]
    fn test_checkbox_event_mouse_move_sets_hover() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        assert!(!cb.hovered);
        cb.event(&Event::MouseMove {
            position: Point::new(50.0, 9.0),
        });
        assert!(cb.hovered);
    }

    #[test]
    fn test_checkbox_event_mouse_move_clears_hover() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));
        cb.hovered = true;

        cb.event(&Event::MouseMove {
            position: Point::new(200.0, 200.0),
        });
        assert!(!cb.hovered);
    }

    #[test]
    fn test_checkbox_event_disabled_blocks_click() {
        let mut cb = Checkbox::new().box_size(18.0).disabled(true);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Left,
        });
        assert!(!cb.is_checked());
        assert!(result.is_none());
    }

    #[test]
    fn test_checkbox_event_disabled_blocks_hover() {
        let mut cb = Checkbox::new().box_size(18.0).disabled(true);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        cb.event(&Event::MouseMove {
            position: Point::new(50.0, 9.0),
        });
        assert!(!cb.hovered);
    }

    #[test]
    fn test_checkbox_event_click_on_label_area_toggles() {
        let mut cb = Checkbox::new().box_size(18.0).label("Accept terms");
        cb.layout(Rect::new(0.0, 0.0, 150.0, 18.0));

        // Click on label area (past box)
        let result = cb.event(&Event::MouseDown {
            position: Point::new(100.0, 9.0),
            button: MouseButton::Left,
        });
        assert!(cb.is_checked());
        assert!(result.is_some());
    }

    #[test]
    fn test_checkbox_event_full_interaction_flow() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(0.0, 0.0, 100.0, 18.0));

        // 1. Start unchecked
        assert!(!cb.is_checked());
        assert!(!cb.hovered);

        // 2. Hover
        cb.event(&Event::MouseMove {
            position: Point::new(50.0, 9.0),
        });
        assert!(cb.hovered);

        // 3. Click to check
        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Left,
        });
        assert!(cb.is_checked());
        let msg = result.unwrap().downcast::<CheckboxChanged>().unwrap();
        assert_eq!(msg.state, CheckState::Checked);

        // 4. Click again to uncheck
        let result = cb.event(&Event::MouseDown {
            position: Point::new(9.0, 9.0),
            button: MouseButton::Left,
        });
        assert!(!cb.is_checked());
        let msg = result.unwrap().downcast::<CheckboxChanged>().unwrap();
        assert_eq!(msg.state, CheckState::Unchecked);

        // 5. Move out
        cb.event(&Event::MouseMove {
            position: Point::new(200.0, 200.0),
        });
        assert!(!cb.hovered);
    }

    #[test]
    fn test_checkbox_event_with_offset_bounds() {
        let mut cb = Checkbox::new().box_size(18.0);
        cb.layout(Rect::new(50.0, 100.0, 100.0, 18.0));

        // Click inside bounds (relative to offset)
        let result = cb.event(&Event::MouseDown {
            position: Point::new(100.0, 109.0),
            button: MouseButton::Left,
        });
        assert!(cb.is_checked());
        assert!(result.is_some());
    }
}