teksilo-widgets 0.9.0

Widget library for Teksilo — over a hundred widgets and layout primitives, from Button to TreeTableView.
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! MessageBox — QMessageBox-style alert dialog.
//!
//! A higher-level surface built on top of [`ModalContainer`]
//! for the classic "tell the user something and ask for a response"
//! pattern: unsaved-changes prompts, error surfaces, confirmation
//! dialogs, and informational notices. Mirrors QMessageBox (Qt),
//! NSAlert (AppKit), and SwiftUI's `.alert(...)` while staying inside
//! Teksilo's idioms — closure result handlers, `Signal`/`Prop`
//! reactivity, `Intent`/`Action`/`Shortcut` routing for keyboard
//! defaults, and AccessKit `Role::AlertDialog` accessibility.
//!
//! ## Quick tour
//!
//! ```ignore
//! use teksilo::prelude::*;
//! use teksilo::widgets::{MessageBox, MessageBoxButtons, StandardButton};
//!
//! fn on_close(ctx: &mut EventContext) {
//!     MessageBox::question(lit!("Save changes?"))
//!         .text(lit!("You have unsaved changes in report.skrib."))
//!         .informative_text(lit!("Your changes will be lost if you don't save them."))
//!         .buttons(MessageBoxButtons::SaveDiscardCancel)
//!         .default_button(StandardButton::Save)
//!         .escape_button(StandardButton::Cancel)
//!         .on_result(|r, ctx| match r.button {
//!             StandardButton::Save => save_and_close(ctx),
//!             StandardButton::Discard => close(ctx),
//!             _ => {}
//!         })
//!         .present(ctx);
//! }
//! # fn save_and_close(_: &mut EventContext) {}
//! # fn close(_: &mut EventContext) {}
//! ```
//!
//! ## Severity
//!
//! [`MessageBoxSeverity`] controls the icon drawn beside the title and
//! its tint:
//!
//! - `Information` — info glyph, `status_info_fg` tint.
//! - `Question` — question mark glyph, `accent` tint.
//! - `Warning` — exclamation triangle, `status_warning_fg` tint.
//! - `Critical` — X-mark circle, `status_error_fg` tint. Also disables
//!   click-outside dismissal (Qt convention).
//! - `None` — no icon, no tint.
//!
//! Severity is conveyed through the icon + title + text. Per Teksilo's
//! Int UI baseline, buttons are **never** colored as "destructive":
//! destructive intent lives in the dialog's severity and wording, not
//! in the button. See [`crate::button`] for details.
//!
//! ## Default & escape buttons
//!
//! - `default_button` — activated by Enter (widget-scoped shortcut) and
//!   receives initial focus on open (via `ModalRequest::focus_target`
//!   plus `Widget::initial_focus_hint`). Styled with
//!   `ButtonVariant::Filled`.
//! - `escape_button` — activated by Escape. The fallback logic (for
//!   presets with no explicit `escape_button`) picks: explicit
//!   `escape_button` → first `Reject`-role button → `Cancel` → last
//!   button.
//!
//! ## Result reporting
//!
//! [`MessageBox::on_result`] takes `impl Fn(MessageBoxResult,
//! &mut EventContext) + 'static`. The callback fires exactly once — on
//! button activation or Escape dismissal — then the modal is closed by
//! the framework.
//!
//! ## Accessibility
//!
//! The widget exposes `Role::AlertDialog` (distinct from
//! `ModalContainer`'s `Role::Dialog`), with `set_modal()`,
//! `set_live(Live::Assertive)`, `set_name(title)`, and
//! `set_description(text + informative_text)` so screen readers
//! announce the dialog and its body on open.

use std::cell::{Cell, RefCell};
use std::rc::Rc;

use teksilo_canvas::{Rect, Size, SizeProposal};
use teksilo_core::accessibility::AccessNodeBuilder;
use teksilo_core::action::Action;
use teksilo_core::build_context::BuildContext;
use teksilo_core::event::{Key, Modifiers};
use teksilo_core::modal::{ModalCloseBehavior, ModalPresentation, ModalRequest};
use teksilo_core::shortcut::{KeyStroke, Shortcut};
use teksilo_core::signal::Signal;
use teksilo_core::widget::{EventContext, LayoutContext, PaintContext, Widget, WidgetPlacement};
use teksilo_core::widget_id::WidgetId;
use teksilo_i18n::LocalizedString;
use teksilo_tokens::VAlignment;

use crate::accordion::Accordion;
use crate::button::{Button, ButtonVariant};
use crate::checkbox::Checkbox;
use crate::dialog::ModalContainer;
use crate::primitives::{Expand, HStack, Spacer, TextWidget, VStack};
use crate::scroll_area::ScrollArea;
use crate::severity_badge::{SeverityBadge, SeverityIconKind};

// ── Severity ────────────────────────────────────────────────────────

/// Alert severity level. Drives the icon glyph + tint shown beside the
/// title, and (for `Critical`) whether click-outside dismiss is enabled.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MessageBoxSeverity {
    /// No icon. Use for plain notices where an icon would be noise.
    #[default]
    None,
    /// Informational notice — blue circle with "i" glyph.
    Information,
    /// Confirmation prompt — accent-tinted circle with "?" glyph.
    Question,
    /// Non-fatal warning — amber triangle with "!" glyph.
    Warning,
    /// Critical error — red circle with an "X" glyph. Click-outside
    /// dismissal is disabled (Escape still works).
    Critical,
}

// ── Standard button catalog ─────────────────────────────────────────

/// Semantic role of a message-box button. Used for fallback escape
/// resolution (`Reject` wins when no explicit escape button is set).
/// Teksilo deliberately does **not** render `Destructive` buttons with
/// a red fill — the dialog's severity icon and wording carry that
/// signal. See [`crate::button`] for the framework-level rationale.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonRole {
    /// Confirms / proceeds. Ok, Yes, Save, Open, Apply, Retry.
    Accept,
    /// Bails out. Cancel, Close, No, Abort.
    Reject,
    /// Data-loss action. Discard. (Same visuals as Regular — the
    /// severity of the surrounding MessageBox carries the warning.)
    Destructive,
    /// Side action. Help, Reset, RestoreDefaults, Ignore, and the
    /// "to all" variants.
    Action,
}

/// The Qt-modeled catalog of standard buttons. Each variant resolves
/// to a localized label, a semantic [`ButtonRole`], and a stable
/// intent-name string used internally for shortcut/action routing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StandardButton {
    /// Accept / confirm. `ButtonRole::Accept`.
    Ok,
    /// Cancel the operation. `ButtonRole::Reject`.
    Cancel,
    /// Close the dialog. `ButtonRole::Reject`.
    Close,
    /// Confirm with "Yes". `ButtonRole::Accept`.
    Yes,
    /// Decline with "No". `ButtonRole::Reject`.
    No,
    /// Confirm all remaining items. `ButtonRole::Accept`.
    YesToAll,
    /// Decline all remaining items. `ButtonRole::Reject`.
    NoToAll,
    /// Save changes. `ButtonRole::Accept`.
    Save,
    /// Save all open items. `ButtonRole::Accept`.
    SaveAll,
    /// Discard changes without saving. `ButtonRole::Destructive`.
    Discard,
    /// Apply changes without closing. `ButtonRole::Accept`.
    Apply,
    /// Reset to defaults. `ButtonRole::Action`.
    Reset,
    /// Restore factory defaults. `ButtonRole::Action`.
    RestoreDefaults,
    /// Abort the current operation. `ButtonRole::Reject`.
    Abort,
    /// Retry the failed operation. `ButtonRole::Accept`.
    Retry,
    /// Ignore the error and continue. `ButtonRole::Action`.
    Ignore,
    /// Open a file or resource. `ButtonRole::Accept`.
    Open,
    /// Show help. `ButtonRole::Action`.
    Help,
}

impl StandardButton {
    /// The button's semantic role — used internally by MessageBox's
    /// escape-button fallback resolution, and available to callers that
    /// want to inspect a `MessageBoxButton`'s role.
    pub fn role(self) -> ButtonRole {
        match self {
            Self::Ok
            | Self::Yes
            | Self::YesToAll
            | Self::Save
            | Self::SaveAll
            | Self::Apply
            | Self::Retry
            | Self::Open => ButtonRole::Accept,
            Self::Cancel | Self::Close | Self::No | Self::NoToAll | Self::Abort => {
                ButtonRole::Reject
            }
            Self::Discard => ButtonRole::Destructive,
            Self::Reset | Self::RestoreDefaults | Self::Ignore | Self::Help => ButtonRole::Action,
        }
    }

    /// Stable string id used as both the shortcut id and the intent
    /// name for routing default/escape key activations. Scoped to a
    /// MessageBox instance via widget-scoped shortcut registration, so
    /// the same id is safe to reuse across instances.
    pub fn intent_name(self) -> &'static str {
        match self {
            Self::Ok => "messagebox.btn.ok",
            Self::Cancel => "messagebox.btn.cancel",
            Self::Close => "messagebox.btn.close",
            Self::Yes => "messagebox.btn.yes",
            Self::No => "messagebox.btn.no",
            Self::YesToAll => "messagebox.btn.yes_to_all",
            Self::NoToAll => "messagebox.btn.no_to_all",
            Self::Save => "messagebox.btn.save",
            Self::SaveAll => "messagebox.btn.save_all",
            Self::Discard => "messagebox.btn.discard",
            Self::Apply => "messagebox.btn.apply",
            Self::Reset => "messagebox.btn.reset",
            Self::RestoreDefaults => "messagebox.btn.restore_defaults",
            Self::Abort => "messagebox.btn.abort",
            Self::Retry => "messagebox.btn.retry",
            Self::Ignore => "messagebox.btn.ignore",
            Self::Open => "messagebox.btn.open",
            Self::Help => "messagebox.btn.help",
        }
    }

    /// Default label for the button. Resolved through the Fluent
    /// catalog via `tr_widget!` so apps can override per-locale.
    pub fn default_label(self) -> LocalizedString {
        match self {
            Self::Ok => teksilo_i18n::tr_widget!(messagebox_btn_ok()),
            Self::Cancel => teksilo_i18n::tr_widget!(messagebox_btn_cancel()),
            Self::Close => teksilo_i18n::tr_widget!(messagebox_btn_close()),
            Self::Yes => teksilo_i18n::tr_widget!(messagebox_btn_yes()),
            Self::No => teksilo_i18n::tr_widget!(messagebox_btn_no()),
            Self::YesToAll => teksilo_i18n::tr_widget!(messagebox_btn_yes_to_all()),
            Self::NoToAll => teksilo_i18n::tr_widget!(messagebox_btn_no_to_all()),
            Self::Save => teksilo_i18n::tr_widget!(messagebox_btn_save()),
            Self::SaveAll => teksilo_i18n::tr_widget!(messagebox_btn_save_all()),
            Self::Discard => teksilo_i18n::tr_widget!(messagebox_btn_discard()),
            Self::Apply => teksilo_i18n::tr_widget!(messagebox_btn_apply()),
            Self::Reset => teksilo_i18n::tr_widget!(messagebox_btn_reset()),
            Self::RestoreDefaults => teksilo_i18n::tr_widget!(messagebox_btn_restore_defaults()),
            Self::Abort => teksilo_i18n::tr_widget!(messagebox_btn_abort()),
            Self::Retry => teksilo_i18n::tr_widget!(messagebox_btn_retry()),
            Self::Ignore => teksilo_i18n::tr_widget!(messagebox_btn_ignore()),
            Self::Open => teksilo_i18n::tr_widget!(messagebox_btn_open()),
            Self::Help => teksilo_i18n::tr_widget!(messagebox_btn_help()),
        }
    }
}

/// A single button placement inside a MessageBox, including an optional
/// per-instance label override. Callers usually build these via
/// [`From<StandardButton>`] (`StandardButton::Ok.into()`), or
/// construct them manually when `Custom` is needed.
#[derive(Debug, Clone)]
pub struct MessageBoxButton {
    /// Which standard button this is — drives role, intent name, and
    /// (when `label_override` is `None`) the label.
    pub kind: StandardButton,
    /// Optional explicit label that overrides `kind.default_label()`.
    /// Use sparingly: prefer translating the default via Fluent rather
    /// than hard-coding per-call labels.
    pub label_override: Option<LocalizedString>,
}

impl MessageBoxButton {
    /// Build a button from a `StandardButton` with the default label.
    pub fn standard(kind: StandardButton) -> Self {
        Self {
            kind,
            label_override: None,
        }
    }

    /// Override the default translated label.
    pub fn label(mut self, label: impl Into<LocalizedString>) -> Self {
        self.label_override = Some(label.into());
        self
    }

    fn resolved_label(&self) -> LocalizedString {
        self.label_override
            .clone()
            .unwrap_or_else(|| self.kind.default_label())
    }
}

impl From<StandardButton> for MessageBoxButton {
    fn from(kind: StandardButton) -> Self {
        Self::standard(kind)
    }
}

/// Pre-built button bundles covering the common MessageBox shapes.
/// Custom combinations go through [`MessageBox::add_button`] or
/// [`MessageBoxButtons::Custom`].
#[derive(Debug, Clone)]
pub enum MessageBoxButtons {
    /// Just Ok.
    Ok,
    /// Ok + Cancel, Ok default, Cancel escape.
    OkCancel,
    /// Yes + No, Yes default, No escape.
    YesNo,
    /// Yes + No + Cancel, Yes default, Cancel escape.
    YesNoCancel,
    /// The unsaved-changes triad: Save + Discard + Cancel.
    SaveDiscardCancel,
    /// The error-recovery triad: Retry + Ignore + Abort.
    RetryIgnoreAbort,
    /// Explicit list. MessageBox preserves the order as the visual
    /// button order (leading Spacer pushes all buttons to the trailing
    /// edge; default button may appear anywhere).
    Custom(Vec<MessageBoxButton>),
}

impl MessageBoxButtons {
    fn into_buttons(self) -> Vec<MessageBoxButton> {
        match self {
            Self::Ok => vec![StandardButton::Ok.into()],
            Self::OkCancel => vec![StandardButton::Cancel.into(), StandardButton::Ok.into()],
            Self::YesNo => vec![StandardButton::No.into(), StandardButton::Yes.into()],
            Self::YesNoCancel => vec![
                StandardButton::Cancel.into(),
                StandardButton::No.into(),
                StandardButton::Yes.into(),
            ],
            Self::SaveDiscardCancel => vec![
                StandardButton::Discard.into(),
                StandardButton::Cancel.into(),
                StandardButton::Save.into(),
            ],
            Self::RetryIgnoreAbort => vec![
                StandardButton::Abort.into(),
                StandardButton::Ignore.into(),
                StandardButton::Retry.into(),
            ],
            Self::Custom(items) => items,
        }
    }

    /// Default button hint derived from the preset. Callers that want
    /// a different default override via `MessageBox::default_button`.
    fn preset_default(&self) -> Option<StandardButton> {
        match self {
            Self::Ok => Some(StandardButton::Ok),
            Self::OkCancel => Some(StandardButton::Ok),
            Self::YesNo => Some(StandardButton::Yes),
            Self::YesNoCancel => Some(StandardButton::Yes),
            Self::SaveDiscardCancel => Some(StandardButton::Save),
            Self::RetryIgnoreAbort => Some(StandardButton::Retry),
            Self::Custom(_) => None,
        }
    }

    /// Escape button hint derived from the preset.
    fn preset_escape(&self) -> Option<StandardButton> {
        match self {
            Self::Ok => Some(StandardButton::Ok),
            Self::OkCancel => Some(StandardButton::Cancel),
            Self::YesNo => Some(StandardButton::No),
            Self::YesNoCancel => Some(StandardButton::Cancel),
            Self::SaveDiscardCancel => Some(StandardButton::Cancel),
            Self::RetryIgnoreAbort => Some(StandardButton::Abort),
            Self::Custom(_) => None,
        }
    }
}

// ── Result ──────────────────────────────────────────────────────────

/// Report passed to [`MessageBox::on_result`] when the dialog closes.
#[derive(Debug, Clone, Copy)]
pub struct MessageBoxResult {
    /// Which button fired — either by click, Enter (default button),
    /// or Escape (escape button resolution).
    pub button: StandardButton,
    /// State of the "Don't show again" checkbox at dismiss time, when
    /// one was configured via [`MessageBox::show_again_checkbox`] or
    /// [`MessageBox::show_again_checkbox_state`]. `false` when no
    /// checkbox was attached.
    pub checkbox_checked: bool,
    /// `true` when the user dismissed via Escape (or scrim-click, when
    /// permitted) rather than clicking a button directly.
    pub dismissed_by_escape: bool,
}

const SEVERITY_ICON_SIZE: f32 = 48.0;

/// How tall the expanded "Show details" pane is allowed to grow before it scrolls.
///
/// `detailed_text` is where callers put the things that have no length bound — a stack
/// trace, an OS error dump, a list of every affected row — so without a cap the accordion
/// simply grew the dialog until it ran off the bottom of the screen, taking the buttons
/// with it. Tall enough for a dozen lines of `small` text, short enough that the dialog
/// still fits a laptop display beneath a header and a button row.
const DETAILS_MAX_HEIGHT: f32 = 220.0;

const DEFAULT_INTENT_NAME: &str = "messagebox.accept_default";
const ESCAPE_INTENT_NAME: &str = "messagebox.escape";

/// Map a `MessageBoxSeverity` onto the shared severity-badge kind.
/// `None` has no badge (the dialog reads as a plain notice).
fn severity_icon_kind(severity: MessageBoxSeverity) -> Option<SeverityIconKind> {
    match severity {
        MessageBoxSeverity::None => None,
        MessageBoxSeverity::Information => Some(SeverityIconKind::Info),
        MessageBoxSeverity::Question => Some(SeverityIconKind::Question),
        MessageBoxSeverity::Warning => Some(SeverityIconKind::Warning),
        MessageBoxSeverity::Critical => Some(SeverityIconKind::Error),
    }
}

// ── Internal runtime state ─────────────────────────────────────────

/// State shared between the MessageBox widget, its footer buttons, and
/// the Enter/Escape shortcut actions. Lives in an `Rc` so all three
/// dispatch paths can read/write the same checkbox state and fire the
/// same result callback exactly once per session.
struct State {
    on_result: RefCell<Option<Box<dyn Fn(MessageBoxResult, &mut EventContext)>>>,
    checkbox: Signal<bool>,
    escape_button: Cell<Option<StandardButton>>,
    default_button: Cell<Option<StandardButton>>,
    /// Fallback list of buttons in the order they were configured —
    /// consulted when neither `escape_button` nor a `Reject`-role
    /// button is set.
    buttons: RefCell<Vec<StandardButton>>,
    /// Guards against multiple result-callback invocations when a
    /// button click races the Escape shortcut.
    fired: Cell<bool>,
}

impl State {
    fn new(checkbox: Signal<bool>) -> Rc<Self> {
        Rc::new(Self {
            on_result: RefCell::new(None),
            checkbox,
            escape_button: Cell::new(None),
            default_button: Cell::new(None),
            buttons: RefCell::new(Vec::new()),
            fired: Cell::new(false),
        })
    }

    fn fire(&self, button: StandardButton, by_escape: bool, ctx: &mut EventContext) {
        if self.fired.replace(true) {
            return;
        }
        let result = MessageBoxResult {
            button,
            checkbox_checked: self.checkbox.get(),
            dismissed_by_escape: by_escape,
        };
        if let Some(handler) = self.on_result.borrow().as_ref() {
            handler(result, ctx);
        }
        ctx.dismiss_modal();
    }

    fn resolve_escape_button(&self) -> Option<StandardButton> {
        if let Some(btn) = self.escape_button.get() {
            return Some(btn);
        }
        let buttons = self.buttons.borrow();
        if let Some(btn) = buttons.iter().find(|b| b.role() == ButtonRole::Reject) {
            return Some(*btn);
        }
        if buttons.contains(&StandardButton::Cancel) {
            return Some(StandardButton::Cancel);
        }
        buttons.last().copied()
    }
}

// ── The MessageBox widget ──────────────────────────────────────────

/// A modal alert dialog that displays a severity icon, title, body text, and
/// one or more buttons.
///
/// Constructed via severity-named constructors ([`MessageBox::information`],
/// [`MessageBox::warning`], [`MessageBox::critical`], [`MessageBox::question`],
/// [`MessageBox::plain`]), configured fluently, and presented with
/// [`MessageBox::present`]. See the module documentation for the full guide.
pub struct MessageBox {
    severity: MessageBoxSeverity,
    title: LocalizedString,
    text: Option<LocalizedString>,
    informative_text: Option<LocalizedString>,
    detailed_text: Option<LocalizedString>,
    buttons_config: Option<MessageBoxButtons>,
    extra_buttons: Vec<MessageBoxButton>,
    default_button: Option<StandardButton>,
    escape_button: Option<StandardButton>,
    show_again_label: Option<LocalizedString>,
    show_again_state: Option<Signal<bool>>,
    on_result: Option<Box<dyn Fn(MessageBoxResult, &mut EventContext)>>,
    default_button_id: Cell<Option<WidgetId>>,
    root_child_id: Option<WidgetId>,
    state: Option<Rc<State>>,
}

impl std::fmt::Debug for MessageBox {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MessageBox")
            .field("severity", &self.severity)
            .field("title", &self.title)
            .field("text", &self.text)
            .field("informative_text", &self.informative_text)
            .field("detailed_text", &self.detailed_text)
            .field("default_button", &self.default_button)
            .field("escape_button", &self.escape_button)
            .finish()
    }
}

impl MessageBox {
    fn new_with_severity(severity: MessageBoxSeverity, title: impl Into<LocalizedString>) -> Self {
        let title = title.into();
        Self {
            severity,
            title,
            text: None,
            informative_text: None,
            detailed_text: None,
            buttons_config: None,
            extra_buttons: Vec::new(),
            default_button: None,
            escape_button: None,
            show_again_label: None,
            show_again_state: None,
            on_result: None,
            default_button_id: Cell::new(None),
            root_child_id: None,
            state: None,
        }
    }

    /// Construct an informational MessageBox (`Information` severity).
    pub fn information(title: impl Into<LocalizedString>) -> Self {
        Self::new_with_severity(MessageBoxSeverity::Information, title)
    }

    /// Construct a warning MessageBox (`Warning` severity).
    pub fn warning(title: impl Into<LocalizedString>) -> Self {
        Self::new_with_severity(MessageBoxSeverity::Warning, title)
    }

    /// Construct a critical-error MessageBox (`Critical` severity).
    /// Click-outside dismissal is disabled; use an explicit button or
    /// Escape to close.
    pub fn critical(title: impl Into<LocalizedString>) -> Self {
        Self::new_with_severity(MessageBoxSeverity::Critical, title)
    }

    /// Construct a confirmation / question MessageBox (`Question`
    /// severity).
    pub fn question(title: impl Into<LocalizedString>) -> Self {
        Self::new_with_severity(MessageBoxSeverity::Question, title)
    }

    /// Construct a plain MessageBox with no severity icon.
    pub fn plain(title: impl Into<LocalizedString>) -> Self {
        Self::new_with_severity(MessageBoxSeverity::None, title)
    }

    /// Primary message line, rendered in `typography.body` with
    /// `text_primary`. Prefer a short, self-contained sentence —
    /// details belong in `informative_text`.
    pub fn text(mut self, text: impl Into<LocalizedString>) -> Self {
        self.text = Some(text.into());
        self
    }

    /// Secondary, explanatory text rendered below the primary text in
    /// `typography.body` with `text_secondary`. Matches Qt's
    /// `setInformativeText`.
    pub fn informative_text(mut self, text: impl Into<LocalizedString>) -> Self {
        self.informative_text = Some(text.into());
        self
    }

    /// Detailed text hidden behind a "Show details" [`Accordion`] —
    /// for technical diagnostics (stack traces, error codes). Matches
    /// Qt's `setDetailedText`.
    pub fn detailed_text(mut self, text: impl Into<LocalizedString>) -> Self {
        self.detailed_text = Some(text.into());
        self
    }

    /// Apply a preset button bundle. Implicitly sets default and
    /// escape buttons for the preset (both can be overridden via
    /// [`MessageBox::default_button`] and
    /// [`MessageBox::escape_button`]).
    pub fn buttons(mut self, preset: MessageBoxButtons) -> Self {
        if self.default_button.is_none() {
            self.default_button = preset.preset_default();
        }
        if self.escape_button.is_none() {
            self.escape_button = preset.preset_escape();
        }
        self.buttons_config = Some(preset);
        self
    }

    /// Append a single button. Use to augment a preset (rare) or to
    /// build a bespoke button row without going through
    /// [`MessageBoxButtons::Custom`].
    pub fn add_button(mut self, button: impl Into<MessageBoxButton>) -> Self {
        self.extra_buttons.push(button.into());
        self
    }

    /// Mark which button activates on Enter and receives initial
    /// focus. Must refer to one of the buttons configured via
    /// `buttons` / `add_button`.
    pub fn default_button(mut self, which: StandardButton) -> Self {
        self.default_button = Some(which);
        self
    }

    /// Mark which button activates on Escape (and scrim-click, when
    /// allowed). Must refer to one of the configured buttons.
    pub fn escape_button(mut self, which: StandardButton) -> Self {
        self.escape_button = Some(which);
        self
    }

    /// Attach a "Don't show again"-style checkbox below the body.
    /// Internally creates a `Signal<bool>` initialized to `false` and
    /// reports its state in [`MessageBoxResult::checkbox_checked`].
    /// For external observation, use
    /// [`MessageBox::show_again_checkbox_state`] instead.
    pub fn show_again_checkbox(mut self, label: impl Into<LocalizedString>) -> Self {
        self.show_again_label = Some(label.into());
        self
    }

    /// Like [`MessageBox::show_again_checkbox`], but with a
    /// caller-owned `Signal<bool>` so the checkbox state survives the
    /// dialog lifetime (useful for "remember my choice" persistence).
    pub fn show_again_checkbox_state(mut self, signal: Signal<bool>) -> Self {
        self.show_again_state = Some(signal);
        self
    }

    /// Register the result callback, invoked exactly once when a
    /// button fires (either by click or by Enter/Escape shortcut).
    pub fn on_result(mut self, f: impl Fn(MessageBoxResult, &mut EventContext) + 'static) -> Self {
        self.on_result = Some(Box::new(f));
        self
    }

    /// Present the MessageBox as a modal on top of `ctx`'s current
    /// tree. Consumes `self`; callers who need to present multiple
    /// dialogs with shared config should build a factory closure.
    pub fn present(self, ctx: &mut EventContext) {
        let title = self.title.clone();
        let close_behavior = if self.severity == MessageBoxSeverity::Critical {
            ModalCloseBehavior::EscapeKey
        } else {
            ModalCloseBehavior::EscapeOrClickOutside
        };

        let dialog_title = self.title.clone();
        let mut inner = Some(self);
        ctx.present_modal(
            ModalRequest::deferred(move |tree| {
                let mb = inner
                    .take()
                    .expect("MessageBox present closure called twice");
                tree.add(ModalContainer::new(mb).title(dialog_title.clone()))
            })
            .presentation(ModalPresentation::Auto)
            .close_behavior(close_behavior)
            .title(title)
            .size(460, 140),
        );
    }

    fn resolve_buttons(&mut self) -> Vec<MessageBoxButton> {
        let mut resolved = self
            .buttons_config
            .clone()
            .map(|b| b.into_buttons())
            .unwrap_or_default();
        resolved.extend(self.extra_buttons.iter().cloned());
        if resolved.is_empty() {
            resolved.push(StandardButton::Ok.into());
            if self.default_button.is_none() {
                self.default_button = Some(StandardButton::Ok);
            }
            if self.escape_button.is_none() {
                self.escape_button = Some(StandardButton::Ok);
            }
        }
        resolved
    }
}

impl Widget for MessageBox {
    fn build(&mut self, ctx: &mut BuildContext) -> Vec<WidgetId> {
        let theme = ctx.theme().clone();

        let checkbox_signal = self
            .show_again_state
            .clone()
            .unwrap_or_else(|| ctx.signal(false));
        let state = State::new(checkbox_signal.clone());
        *state.on_result.borrow_mut() = self.on_result.take();

        let buttons = self.resolve_buttons();
        *state.buttons.borrow_mut() = buttons.iter().map(|b| b.kind).collect();
        state.default_button.set(self.default_button);
        state.escape_button.set(self.escape_button);

        let mut header_text_stack = VStack::new().spacing(6.0);
        header_text_stack = header_text_stack.child(
            TextWidget::new(self.title.clone())
                .style(theme.typography.body_bold.clone())
                .color(theme.colors.text_primary),
        );
        if let Some(text) = self.text.clone() {
            header_text_stack = header_text_stack.child(
                TextWidget::new(text)
                    .style(theme.typography.body.clone())
                    .color(theme.colors.text_primary),
            );
        }
        if let Some(info) = self.informative_text.clone() {
            header_text_stack = header_text_stack.child(
                TextWidget::new(info)
                    .style(theme.typography.body.clone())
                    .color(theme.colors.text_secondary),
            );
        }

        let header: Box<dyn Widget> = if let Some(kind) = severity_icon_kind(self.severity) {
            // Wrap the text stack in `Expand::horizontal()` so HStack
            // distributes its width slack to the text column. Without
            // this the HStack measures the text stack with width=None
            // (single-line), which makes the body / informative text
            // overflow the dialog instead of wrapping.
            Box::new(
                HStack::new()
                    .spacing(16.0)
                    .alignment(VAlignment::Top)
                    .child(SeverityBadge::new(kind, SEVERITY_ICON_SIZE))
                    .child(Expand::horizontal().child(header_text_stack)),
            )
        } else {
            Box::new(header_text_stack)
        };

        let detailed_child: Option<Box<dyn Widget>> = self.detailed_text.clone().map(|text| {
            let expanded = ctx.signal(false);
            let label: LocalizedString = teksilo_i18n::tr_widget!(messagebox_show_details());
            let body = TextWidget::new(text)
                .style(theme.typography.small.clone())
                .color(theme.colors.text_secondary);
            // Capped and scrollable rather than free-growing: see `DETAILS_MAX_HEIGHT`.
            // `preferred_height` caps the height only when the parent proposes an
            // unconstrained one — which a hugging dialog does — while leaving the width to
            // follow the content, so short details still size to themselves and never
            // acquire a scroll bar they do not need.
            let scroller = ScrollArea::new()
                .child(body)
                .preferred_height(DETAILS_MAX_HEIGHT);
            let accordion: Box<dyn Widget> =
                Box::new(Accordion::new(label, expanded).content(scroller));
            accordion
        });

        let checkbox_child: Option<Box<dyn Widget>> = self.show_again_label.clone().map(|label| {
            let cb: Box<dyn Widget> = Box::new(Checkbox::new(checkbox_signal.clone()).label(label));
            cb
        });

        let mut footer = HStack::new().spacing(8.0).child(Spacer::new());
        for button_cfg in &buttons {
            let kind = button_cfg.kind;
            let label = button_cfg.resolved_label();
            let variant = if Some(kind) == self.default_button {
                ButtonVariant::Filled
            } else {
                ButtonVariant::Plain
            };
            let state_for_btn = state.clone();
            let btn_id = ctx.add(
                Button::new(label)
                    .variant(variant)
                    .on_activate_fn(move |ctx| {
                        state_for_btn.fire(kind, false, ctx);
                    }),
            );
            if Some(kind) == self.default_button {
                self.default_button_id.set(Some(btn_id));
            }
            footer = footer.add_child(btn_id);
        }

        let mut stack = VStack::new().spacing(16.0);
        stack = stack.add_child(ctx.add_boxed(header));
        if let Some(det) = detailed_child {
            stack = stack.add_child(ctx.add_boxed(det));
        }
        if let Some(cb) = checkbox_child {
            stack = stack.add_child(ctx.add_boxed(cb));
        }
        // Push the footer to the bottom of the dialog by absorbing any
        // vertical slack with a Spacer (flex=1).
        stack = stack.add_child(ctx.add(Spacer::new()));
        let footer_id = ctx.add(footer);
        stack = stack.add_child(footer_id);

        let root = ctx.add(stack);
        self.root_child_id = Some(root);

        {
            let state_enter = state.clone();
            ctx.register_action(
                Action::new(DEFAULT_INTENT_NAME).on_invoke(move |_intent, ctx| {
                    if let Some(kind) = state_enter.default_button.get() {
                        state_enter.fire(kind, false, ctx);
                    }
                }),
            );
            ctx.register_shortcut(
                Shortcut::new(DEFAULT_INTENT_NAME)
                    .primary(KeyStroke::new(Key::Enter, Modifiers::NONE))
                    .build(),
            );
        }
        {
            let state_escape = state.clone();
            ctx.register_action(
                Action::new(ESCAPE_INTENT_NAME).on_invoke(move |_intent, ctx| {
                    if let Some(kind) = state_escape.resolve_escape_button() {
                        state_escape.fire(kind, true, ctx);
                    } else {
                        ctx.dismiss_modal();
                    }
                }),
            );
            ctx.register_shortcut(
                Shortcut::new(ESCAPE_INTENT_NAME)
                    .primary(KeyStroke::new(Key::Escape, Modifiers::NONE))
                    .build(),
            );
        }

        self.state = Some(state);
        vec![root]
    }

    fn layout_response(
        &self,
        proposal: SizeProposal,
        ctx: &LayoutContext,
    ) -> teksilo_core::widget::LayoutResponse {
        // Enforce a 460×140 floor without clamping the forwarded
        // proposal: the overlay's intrinsic-measurement pass calls us
        // with (None, None), and a Spacer-bearing VStack child would
        // otherwise collapse to the proposal height. Letting the child
        // see the unmodified proposal lets it report its real natural
        // size — required so the overlay grows when the "Show details"
        // accordion expands.
        let child = self
            .root_child_id
            .and_then(|id| ctx.child_size(id, proposal))
            .unwrap_or_else(|| proposal.resolve(0.0, 0.0));
        Size::new(child.width.max(460.0), child.height.max(140.0)).into()
    }

    fn place_children(
        &self,
        bounds: Rect,
        _proposal: SizeProposal,
        children: &mut [WidgetPlacement],
        _ctx: &LayoutContext,
    ) {
        for child in children.iter_mut() {
            child.origin = bounds.origin();
            child.size = bounds.size();
        }
    }

    fn paint(&self, _bounds: Rect, _canvas: &mut teksilo_canvas::Canvas, _ctx: &PaintContext) {}

    fn accessibility(&self, builder: &mut AccessNodeBuilder) {
        builder.set_role(teksilo_core::accesskit::Role::AlertDialog);
        builder.set_name(self.title.clone());
        if let Some(description) = self.accessible_description() {
            builder.set_description(description);
        }
        builder.set_modal();
        builder.set_live(teksilo_core::accesskit::Live::Assertive);
        builder.add_action(teksilo_core::accesskit::Action::Focus);
    }

    fn accessible_title_hint(&self) -> Option<String> {
        Some(self.title.resolve_now())
    }

    fn initial_focus_hint(&self) -> Option<WidgetId> {
        self.default_button_id.get()
    }

    fn children(&self) -> Vec<WidgetId> {
        self.root_child_id.into_iter().collect()
    }
}

impl MessageBox {
    fn accessible_description(&self) -> Option<String> {
        match (
            self.text.as_ref().map(|t| t.resolve_now()),
            self.informative_text.as_ref().map(|i| i.resolve_now()),
        ) {
            (None, None) => None,
            (Some(t), None) => Some(t),
            (None, Some(i)) => Some(i),
            (Some(t), Some(i)) => Some(format!("{t}\n{i}")),
        }
    }
}

/// Extension trait on [`EventContext`] for ergonomic MessageBox
/// presentation. Mirrors `ctx.present_modal(...)` for the general
/// case.
pub trait EventContextMessageBoxExt {
    /// Present `mb` as a modal. Equivalent to `mb.present(self)`.
    fn present_message_box(&mut self, mb: MessageBox);
}

impl EventContextMessageBoxExt for EventContext<'_> {
    fn present_message_box(&mut self, mb: MessageBox) {
        mb.present(self);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use teksilo_core::ModalContent;
    use teksilo_core::event::WidgetEvent;
    use teksilo_core::widget_tree::WidgetTree;
    use teksilo_i18n::lit;

    /// Mirrors the focus resolution `teksilo_app::present_in_tree_modal_request`
    /// applies after the modal content subtree is built. Reproduced here
    /// because `teksilo-widgets` can't depend on `teksilo-app`.
    fn present_and_lay_out(tree: &mut WidgetTree, mb: MessageBox) -> WidgetId {
        use crate::button::Button as Btn;
        let mb_cell: Rc<RefCell<Option<MessageBox>>> = Rc::new(RefCell::new(Some(mb)));
        let mb_for_closure = mb_cell.clone();
        let trigger = tree.add(Btn::new(lit!("Open")).on_activate_fn(move |ctx| {
            if let Some(mb) = mb_for_closure.borrow_mut().take() {
                mb.present(ctx);
            }
        }));
        tree.layout(SizeProposal::exact(800.0, 600.0));
        tree.dispatch_event(WidgetEvent::AccessAction {
            action: teksilo_core::accesskit::Action::Click,
            target: Some(trigger),
            target_node: teksilo_core::accessibility::root_node_id(),
            data: None,
        });
        let request = tree.drain_pending_modal_requests().pop().unwrap().request;
        let content_id = match request.content {
            ModalContent::Deferred(builder) => builder(tree),
            ModalContent::ExistingWidget(_) => panic!("MessageBox must use deferred content"),
        };
        tree.layout(SizeProposal::exact(800.0, 600.0));
        let focus_target = request
            .focus_target
            .filter(|id| tree.is_active(*id) && tree.is_descendant_of(*id, content_id))
            .or_else(|| tree.widget_initial_focus_hint(content_id))
            .or_else(|| tree.first_focusable_descendant(content_id));
        if let Some(id) = focus_target {
            tree.focus(id);
        }
        content_id
    }

    #[test]
    fn present_queues_modal_request() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let mb = MessageBox::information(lit!("t"))
            .text(lit!("x"))
            .buttons(MessageBoxButtons::Ok);
        let _content = present_and_lay_out(&mut tree, mb);
        assert!(tree.find_by_label("t").is_some());
    }

    #[test]
    fn critical_uses_escape_only_close_behavior() {
        use crate::button::Button as Btn;
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let mb_cell: Rc<RefCell<Option<MessageBox>>> = Rc::new(RefCell::new(Some(
            MessageBox::critical(lit!("Fatal"))
                .text(lit!("Boom"))
                .buttons(MessageBoxButtons::Ok),
        )));
        let mb_for_closure = mb_cell.clone();
        let trigger = tree.add(Btn::new(lit!("Open")).on_activate_fn(move |ctx| {
            if let Some(mb) = mb_for_closure.borrow_mut().take() {
                mb.present(ctx);
            }
        }));
        tree.layout(SizeProposal::exact(800.0, 600.0));
        tree.dispatch_event(WidgetEvent::AccessAction {
            action: teksilo_core::accesskit::Action::Click,
            target: Some(trigger),
            target_node: teksilo_core::accessibility::root_node_id(),
            data: None,
        });
        let request = tree.drain_pending_modal_requests().pop().unwrap().request;
        assert_eq!(request.close_behavior, ModalCloseBehavior::EscapeKey);
    }

    #[test]
    fn alert_dialog_role_exposed() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let mb = MessageBox::warning(lit!("Title"))
            .text(lit!("Body"))
            .buttons(MessageBoxButtons::Ok);
        let content = present_and_lay_out(&mut tree, mb);
        // `content` is the `ModalContainer`; its child is now the
        // `DialogStyle` panel frame, and the `MessageBox` sits one
        // level below that.
        let panel = tree.children(content).first().copied().unwrap();
        let mb_id = tree.children(panel).first().copied().unwrap();
        let info = tree.accessibility_node(mb_id);
        assert_eq!(info.role(), teksilo_core::accesskit::Role::AlertDialog);
        assert_eq!(info.name(), Some("Title"));
    }

    #[test]
    fn ok_button_fires_result_with_correct_kind() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let captured: Rc<RefCell<Option<MessageBoxResult>>> = Rc::new(RefCell::new(None));
        let captured_for_handler = captured.clone();
        let mb = MessageBox::information(lit!("t"))
            .text(lit!("x"))
            .buttons(MessageBoxButtons::Ok)
            .on_result(move |r, _ctx| {
                *captured_for_handler.borrow_mut() = Some(r);
            });
        let _content = present_and_lay_out(&mut tree, mb);
        let ok_id = tree
            .find_by_label(&StandardButton::Ok.default_label().resolve_now())
            .unwrap();
        tree.dispatch_event(WidgetEvent::AccessAction {
            action: teksilo_core::accesskit::Action::Click,
            target: Some(ok_id),
            target_node: teksilo_core::accessibility::root_node_id(),
            data: None,
        });
        let result = captured.borrow().expect("result must be captured");
        assert_eq!(result.button, StandardButton::Ok);
        assert!(!result.checkbox_checked);
        assert!(!result.dismissed_by_escape);
    }

    #[test]
    fn default_button_is_focused_on_open() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let mb = MessageBox::question(lit!("t"))
            .text(lit!("x"))
            .buttons(MessageBoxButtons::YesNoCancel)
            .default_button(StandardButton::No);
        let _content = present_and_lay_out(&mut tree, mb);
        let no_id = tree
            .find_by_label(&StandardButton::No.default_label().resolve_now())
            .unwrap();
        assert_eq!(tree.focused(), Some(no_id));
    }

    #[test]
    fn enter_fires_default_button_from_any_focus() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let captured: Rc<RefCell<Option<MessageBoxResult>>> = Rc::new(RefCell::new(None));
        let captured_for_handler = captured.clone();
        let mb = MessageBox::question(lit!("t"))
            .text(lit!("x"))
            .buttons(MessageBoxButtons::OkCancel)
            .on_result(move |r, _ctx| {
                *captured_for_handler.borrow_mut() = Some(r);
            });
        let _content = present_and_lay_out(&mut tree, mb);
        let cancel_id = tree
            .find_by_label(&StandardButton::Cancel.default_label().resolve_now())
            .unwrap();
        tree.focus(cancel_id);
        tree.press_key(Key::Enter, Modifiers::NONE);
        let result = captured.borrow().expect("result must be captured");
        assert_eq!(result.button, StandardButton::Ok);
        assert!(!result.dismissed_by_escape);
    }

    #[test]
    fn escape_fires_escape_button_and_marks_dismissed() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let captured: Rc<RefCell<Option<MessageBoxResult>>> = Rc::new(RefCell::new(None));
        let captured_for_handler = captured.clone();
        let mb = MessageBox::question(lit!("t"))
            .text(lit!("x"))
            .buttons(MessageBoxButtons::YesNoCancel)
            .on_result(move |r, _ctx| {
                *captured_for_handler.borrow_mut() = Some(r);
            });
        let _content = present_and_lay_out(&mut tree, mb);
        tree.press_key(Key::Escape, Modifiers::NONE);
        let result = captured.borrow().expect("result must be captured");
        assert_eq!(result.button, StandardButton::Cancel);
        assert!(result.dismissed_by_escape);
    }

    #[test]
    fn checkbox_state_reported_in_result() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let shared_state = Signal::new(false);
        let captured: Rc<RefCell<Option<MessageBoxResult>>> = Rc::new(RefCell::new(None));
        let captured_for_handler = captured.clone();
        let mb = MessageBox::information(lit!("t"))
            .text(lit!("x"))
            .buttons(MessageBoxButtons::Ok)
            .show_again_checkbox_state(shared_state.clone())
            .show_again_checkbox(lit!("Don't show again"))
            .on_result(move |r, _ctx| {
                *captured_for_handler.borrow_mut() = Some(r);
            });
        let _content = present_and_lay_out(&mut tree, mb);
        shared_state.set(true);
        let ok_id = tree
            .find_by_label(&StandardButton::Ok.default_label().resolve_now())
            .unwrap();
        tree.dispatch_event(WidgetEvent::AccessAction {
            action: teksilo_core::accesskit::Action::Click,
            target: Some(ok_id),
            target_node: teksilo_core::accessibility::root_node_id(),
            data: None,
        });
        assert!(captured.borrow().unwrap().checkbox_checked);
    }

    #[test]
    fn accessible_title_hint_propagates_to_container() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let mb = MessageBox::information(lit!("Title propagation test"))
            .text(lit!("Body"))
            .buttons(MessageBoxButtons::Ok);
        let content = present_and_lay_out(&mut tree, mb);
        let info = tree.accessibility_node(content);
        assert_eq!(info.role(), teksilo_core::accesskit::Role::Dialog);
        assert_eq!(info.name(), Some("Title propagation test"));
    }

    #[test]
    fn standard_button_roles_classify_correctly() {
        assert_eq!(StandardButton::Ok.role(), ButtonRole::Accept);
        assert_eq!(StandardButton::Yes.role(), ButtonRole::Accept);
        assert_eq!(StandardButton::Save.role(), ButtonRole::Accept);
        assert_eq!(StandardButton::Cancel.role(), ButtonRole::Reject);
        assert_eq!(StandardButton::No.role(), ButtonRole::Reject);
        assert_eq!(StandardButton::Abort.role(), ButtonRole::Reject);
        assert_eq!(StandardButton::Discard.role(), ButtonRole::Destructive);
        assert_eq!(StandardButton::Help.role(), ButtonRole::Action);
        assert_eq!(StandardButton::Ignore.role(), ButtonRole::Action);
    }

    /// A long `detailed_text` expands, stays reachable, and lays out without blowing up.
    ///
    /// The pane is where callers put the things with no length bound — a stack trace, an OS
    /// error dump, a list of every affected row — and it used to be a bare `TextWidget` in
    /// the accordion, so expanding it grew the dialog until the button row went off the
    /// bottom of the screen. It is capped and scrollable now (`DETAILS_MAX_HEIGHT`).
    ///
    /// This pins the reachable half: a hundred lines of detail still build, expand and
    /// re-lay out with the buttons intact. The *height* cap itself is not asserted here —
    /// `WidgetTree` exposes no laid-out geometry to widget tests, so there is nothing to
    /// measure against; that half is the `ScrollArea::preferred_height` contract, which
    /// `scroll_area` owns and tests.
    #[test]
    fn a_long_details_pane_expands_and_keeps_the_dialog_intact() {
        let mut tree = WidgetTree::new().with_theme(teksilo_core::presets::intui::light());
        let long: String = (1..=100)
            .map(|i| format!("line {i} of a very long detail dump\n"))
            .collect();
        let mb = MessageBox::critical(lit!("Could not open file"))
            .text(lit!("It went wrong."))
            .detailed_text(lit!(long))
            .buttons(MessageBoxButtons::Ok);
        let _content = present_and_lay_out(&mut tree, mb);

        let toggle = tree
            .find_by_label(&teksilo_i18n::tr_widget!(messagebox_show_details()).resolve_now())
            .expect("the Show details toggle");
        tree.dispatch_event(WidgetEvent::AccessAction {
            action: teksilo_core::accesskit::Action::Click,
            target: Some(toggle),
            target_node: teksilo_core::accessibility::root_node_id(),
            data: None,
        });
        tree.layout(SizeProposal::exact(800.0, 600.0));

        // The dialog is still whole: its title and its button both survived the expansion.
        assert!(tree.find_by_label("Could not open file").is_some());
        assert!(
            tree.find_by_label(&StandardButton::Ok.default_label().resolve_now())
                .is_some(),
            "the button row must survive an expanded details pane"
        );
    }

    #[test]
    fn escape_resolution_prefers_explicit_escape_button() {
        let state = State::new(Signal::new(false));
        *state.buttons.borrow_mut() = vec![StandardButton::Save, StandardButton::Discard];
        state.escape_button.set(Some(StandardButton::Discard));
        assert_eq!(state.resolve_escape_button(), Some(StandardButton::Discard));
    }

    #[test]
    fn escape_resolution_falls_back_to_first_reject() {
        let state = State::new(Signal::new(false));
        *state.buttons.borrow_mut() = vec![
            StandardButton::Retry,
            StandardButton::Ignore,
            StandardButton::Abort,
        ];
        state.escape_button.set(None);
        assert_eq!(state.resolve_escape_button(), Some(StandardButton::Abort));
    }

    #[test]
    fn escape_resolution_falls_back_to_last_when_no_reject() {
        let state = State::new(Signal::new(false));
        *state.buttons.borrow_mut() = vec![StandardButton::Ok, StandardButton::Help];
        state.escape_button.set(None);
        assert_eq!(state.resolve_escape_button(), Some(StandardButton::Help));
    }
}