rust_widgets 2.7.0

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! FindReplaceDialog widget — a find/replace dialog (like VS Code/IDE find).
//!
//! Provides a two-row dialog with find and replace input fields, toggle
//! buttons for match case, whole word, regex, and highlight all, plus
//! action buttons for find next, find previous, replace, replace all,
//! and close. Emits typed signals when actions are triggered.

use crate::core::{Color, HorizontalAlignment, Rect, Size};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::Signal1;
use crate::widget::capability::coercion::{expect_bool, expect_string};
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::metrics::{dimensions, ControlMetrics};
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};

/// Padding inside the dialog.
const PADDING: i32 = 8;
/// Row height (input field area).
const ROW_HEIGHT: u32 = 28;
/// Small button size for toggle/action buttons.
const BTN_SIZE: u32 = 24;
/// The width of the trailing arrow buttons on the find row, and of the two action
/// buttons on the replace row.
///
/// Named once because the draw path and the hit test both step by it: the two used to
/// disagree (`24` at the draw site, a `28` stride the hit test measured with), so the
/// clickable area of Replace All sat one inset to the left of the button it named.
const ARROW_BTN: u32 = 24;
/// The width of the "Find:" / "Rpl:" label column.
///
/// Both rows reserve the same column, so the two entry fields start on the same x. The
/// draw path's `40` and the hit test's `46` were two different answers to the same
/// question, which is how a click could land in the gap between a label and its field.
const LABEL_WIDTH: u32 = 46;
/// Gap between adjacent widgets on a row.
const GAP: i32 = 6;

/// The four search flags the dialog exposes, as one value.
///
/// Grouped because they are always read together: a host running a search needs every one of them or
/// nobody's search is reproducible, and four separate payload fields would let a caller drop one on
/// the floor without noticing. [`Copy`] so a handler can stow it in a closure without a clone.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SearchOptions {
    /// Whether the match is case-sensitive.
    pub case_sensitive: bool,
    /// Whether a match must be a whole word.
    pub whole_word: bool,
    /// Whether the pattern is a regular expression.
    pub use_regex: bool,
    /// Whether reaching the end of the document continues from the top.
    ///
    /// Defaults to `true`, matching [`FindReplaceDialog::new`] -- a search that stops at the end
    /// without saying so is what "wrap around = off" already means, so the default is the permissive
    /// one and the host must opt into the stop.
    pub wrap_around: bool,
}

/// One find request: the text to look for plus the flags to look for it with.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchRequest {
    /// The text the user typed into the find field.
    pub text: String,
    /// The options in force when the button was pressed.
    pub options: SearchOptions,
}

/// FindReplaceDialog widget — a find/replace panel for text search and replace.
pub struct FindReplaceDialog {
    base: BaseWidget,
    /// Current find (search) text.
    find_text: String,
    /// Current replace text.
    replace_text: String,
    /// Match case toggle state.
    match_case: bool,
    /// Whole word toggle state.
    whole_word: bool,
    /// Use regex toggle state.
    use_regex: bool,
    /// Highlight all matches toggle state.
    highlight_all: bool,
    /// Whether a search that reaches the end of the document continues from the top.
    wrap_around: bool,
    /// Whether the dialog is visible.
    visible: bool,

    // -- Signals --
    /// Emitted when find next is requested, with the find text and the dialog's search options.
    ///
    /// The payload is [`SearchRequest`] rather than a bare `String` because the dialog **has no
    /// document to search**: it reports what the user asked for and the host runs it. That makes
    /// every option a piece of information the host needs and cannot derive -- and this dialog
    /// declares four of them (`case_sensitive`, `whole_word`, `use_regex`, `wrap_around`), of which
    /// the old payload carried none. A host could read them back off the getters, but only by
    /// reaching for the widget again from inside a signal handler that had just been handed
    /// everything else it needs.
    pub find_next_signal: Signal1<SearchRequest>,
    /// Emitted when find previous is requested, with the find text and the search options.
    pub find_previous_signal: Signal1<SearchRequest>,
    /// Emitted when replace is requested, with the replace text and the search options.
    pub replace_signal: Signal1<SearchRequest>,
    /// Emitted when replace all is requested, with (find_text, replace_text) and the options.
    pub replace_all_signal: Signal1<(String, String, SearchOptions)>,
    /// Emitted when the dialog is closed.
    pub close_signal: Signal1<()>,

    // Internal state tracking
    /// Which text field has focus: 0 = find, 1 = replace
    focus_field: u8,
    /// Cached row rectangles for hit-testing.
    find_row_rect: Rect,
    replace_row_rect: Rect,
}

impl FindReplaceDialog {
    /// Creates a new FindReplaceDialog with the given geometry.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::FindReplaceDialog, geometry, "FindReplaceDialog"),
            find_text: String::new(),
            replace_text: String::new(),
            match_case: false,
            whole_word: false,
            use_regex: false,
            highlight_all: false,
            wrap_around: true,
            visible: false,
            find_next_signal: Signal1::new(),
            find_previous_signal: Signal1::new(),
            replace_signal: Signal1::new(),
            replace_all_signal: Signal1::new(),
            close_signal: Signal1::new(),
            focus_field: 0,
            find_row_rect: Rect::default(),
            replace_row_rect: Rect::default(),
        }
    }

    // ── Find text accessors ──

    /// Returns the current find text.
    pub fn find_text(&self) -> &str {
        &self.find_text
    }

    /// Sets the find text.
    pub fn set_find_text(&mut self, text: &str) {
        self.find_text = text.to_string();
        self.base.request_redraw();
    }

    /// Returns the current replace text.
    pub fn replace_text(&self) -> &str {
        &self.replace_text
    }

    /// Sets the replace text.
    pub fn set_replace_text(&mut self, text: &str) {
        self.replace_text = text.to_string();
        self.base.request_redraw();
    }

    // ── Match case ──

    /// Returns whether match case is enabled.
    pub fn is_match_case(&self) -> bool {
        self.match_case
    }

    /// Sets the match case toggle.
    pub fn set_match_case(&mut self, value: bool) {
        self.match_case = value;
        self.base.request_redraw();
    }

    // ── Whole word ──

    /// Returns whether whole word matching is enabled.
    pub fn is_whole_word(&self) -> bool {
        self.whole_word
    }

    /// Sets the whole word toggle.
    pub fn set_whole_word(&mut self, value: bool) {
        self.whole_word = value;
        self.base.request_redraw();
    }

    // ── Use regex ──

    /// Returns whether regex mode is enabled.
    pub fn is_use_regex(&self) -> bool {
        self.use_regex
    }

    /// Sets the regex toggle.
    pub fn set_use_regex(&mut self, value: bool) {
        self.use_regex = value;
        self.base.request_redraw();
    }

    // ── Highlight all ──

    /// Returns whether highlight all matches is enabled.
    pub fn is_highlight_all(&self) -> bool {
        self.highlight_all
    }

    /// Sets the highlight all toggle.
    pub fn set_highlight_all(&mut self, value: bool) {
        self.highlight_all = value;
        self.base.request_redraw();
    }

    // ── Wrap around ──

    /// Returns whether a search wraps around to the start of the document.
    pub fn is_wrap_around(&self) -> bool {
        self.wrap_around
    }

    /// The four search flags as one value, which is what the signals carry.
    ///
    /// Built here rather than at each of the four emit sites, so a fifth flag added later cannot be
    /// added to one payload and forgotten in the other three.
    fn search_options(&self) -> SearchOptions {
        SearchOptions {
            case_sensitive: self.match_case,
            whole_word: self.whole_word,
            use_regex: self.use_regex,
            wrap_around: self.wrap_around,
        }
    }

    /// Sets the wrap-around toggle.
    pub fn set_wrap_around(&mut self, value: bool) {
        self.wrap_around = value;
        self.base.request_redraw();
    }

    // ── Visibility ──

    /// Shows the dialog.
    pub fn show(&mut self) {
        self.visible = true;
        self.base.request_redraw();
    }

    /// Hides the dialog.
    pub fn hide(&mut self) {
        self.visible = false;
        self.base.request_redraw();
    }

    /// Returns whether the dialog is visible.
    pub fn is_visible(&self) -> bool {
        self.visible
    }

    // ── Actions ──

    /// Emits `find_next_signal` with the find text and the options in force.
    ///
    /// The options travel with the text because the host is the one that searches -- see the signal's
    /// own documentation for why a bare `String` was not enough.
    pub fn find_next(&mut self) {
        if !self.find_text.is_empty() {
            let request =
                SearchRequest { text: self.find_text.clone(), options: self.search_options() };
            self.find_next_signal.emit(request);
        }
    }

    /// Emits `find_previous_signal` with the find text and the options in force.
    pub fn find_previous(&mut self) {
        if !self.find_text.is_empty() {
            let request =
                SearchRequest { text: self.find_text.clone(), options: self.search_options() };
            self.find_previous_signal.emit(request);
        }
    }

    /// Emits `replace_signal` with the replacement text and the options in force.
    pub fn replace(&mut self) {
        if !self.find_text.is_empty() {
            let request =
                SearchRequest { text: self.replace_text.clone(), options: self.search_options() };
            self.replace_signal.emit(request);
        }
    }

    /// Emits `replace_all_signal` with (find_text, replace_text) and the options in force.
    pub fn replace_all(&mut self) {
        if !self.find_text.is_empty() {
            let options = self.search_options();
            self.replace_all_signal.emit((
                self.find_text.clone(),
                self.replace_text.clone(),
                options,
            ));
        }
    }

    /// Appends a character to the focused text field.
    fn append_to_focused(&mut self, ch: char) {
        match self.focus_field {
            0 => {
                self.find_text.push(ch);
                self.base.request_redraw();
            }
            1 => {
                self.replace_text.push(ch);
                self.base.request_redraw();
            }
            _ => {}
        }
    }

    /// Deletes the last character from the focused text field.
    fn backspace_focused(&mut self) {
        match self.focus_field {
            0 => {
                self.find_text.pop();
                self.base.request_redraw();
            }
            1 => {
                self.replace_text.pop();
                self.base.request_redraw();
            }
            _ => {}
        }
    }

    /// Computes layout rectangles for the two rows.
    ///
    /// Both rows are stored, so the draw path and the hit test step through the **same**
    /// boxes: they used to re-derive their own x offsets from four separate literals
    /// (`46` here, `40 + GAP` there), and the entry-field region the click test built was
    /// 6 px narrower than the field it was meant to name.
    fn compute_layout(&self) -> (Rect, Rect) {
        let geom = self.frame_rect();
        let row1 = Rect::new(
            geom.x + PADDING,
            geom.y + PADDING,
            geom.width.saturating_sub((PADDING as u32) * 2),
            ROW_HEIGHT,
        );
        let row2 = Rect::new(
            geom.x + PADDING,
            row1.y + row1.height as i32 + GAP,
            geom.width.saturating_sub((PADDING as u32) * 2),
            ROW_HEIGHT,
        );
        (row1, row2)
    }

    /// The bar this control actually paints: at most its intrinsic size, centred in the
    /// area it was given.
    ///
    /// # Why the bar is not the caller's rectangle
    ///
    /// `rect` is the area the find bar is **offered**. Painting it verbatim made the
    /// 240x120 census cell a 240x120 panel, and — because the two rows were then anchored
    /// to that panel's top edge — left three quarters of the bar empty with both rows
    /// crammed into the top eighth. [`ControlMetrics::painted_box`] caps the width at
    /// [`dimensions::DIALOG_MIN_WIDTH`] and the height at the two rows the control
    /// actually has, then centres the result, so the chrome is sized by what it contains
    /// rather than by whatever box the caller handed it.
    fn frame_rect(&self) -> Rect {
        // Two rows plus the padding above, between and below them: the bar's intrinsic
        // height is derived from its own parts so a row-height change cannot leave the
        // frame a row short.
        let intrinsic_height = PADDING as u32 * 2 + ROW_HEIGHT * 2 + GAP as u32;
        ControlMetrics::painted_box(
            self.geometry(),
            Size::new(dimensions::DIALOG_MIN_WIDTH, intrinsic_height),
        )
    }

    /// The x each trailing column starts at on a row of `row`, laid out from the row's
    /// **right edge** inwards.
    ///
    /// # Why the fixed columns are placed from the right
    ///
    /// The row's fixed budget on a 240 px control did not fit: the two arrow buttons were
    /// drawn at x 240 and x 270, entirely outside the frame, and at 400 px the Replace All
    /// button sat 30 px past the right edge. Anchoring every fixed-width control to the
    /// row's right edge makes the arrows — the affordances — the last thing to be placed
    /// and the one flexible column (the entry field) absorb the remainder, so no fixed
    /// column can leave the frame however narrow the caller's rectangle is.
    ///
    /// The arithmetic is floored at the row's own left edge, so even a row narrower than
    /// its trailing columns keeps every button inside the frame.
    fn trailing_x(row: Rect, trailing_width: i32) -> i32 {
        let right = row.x + row.width as i32 - trailing_width;
        right.max(row.x)
    }

    /// The line box, vertically centred in `cell`, that a label in `font` should occupy.
    ///
    /// Both rows of this panel draw each label from a hand-computed `y + height / 2 + 4`
    /// origin. The `+ 4` is not a centring term: the renderer's origin is the glyph's
    /// **top-left**, so the box painted downward from a point already below the cell's own
    /// middle and the bottom half of every label hung out of the row. Centring the measured
    /// line inside the cell is the correct placement, and naming it once keeps the entry
    /// fields, the toggles and the buttons aligned to the same rule.
    ///
    /// The arithmetic now lives in [`crate::render::text_line`], which this delegates to: the
    /// rule was correct here first and has since become the crate-wide primitive for it, so
    /// keeping a second copy would be the drift this file's own comment warns about.
    fn text_line(&self, cell: Rect, font: &crate::core::Font, context: &RenderContext) -> Rect {
        context.text_line(cell, font)
    }
}

impl Widget for FindReplaceDialog {
    fn base(&self) -> &BaseWidget {
        &self.base
    }

    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn size_hint(&self) -> Size {
        crate::core::Size::new(350, 200)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
}

/// `FindReplaceDialog`'s property contract.
///
/// Read/write semantics are carried over unchanged from the centralised
/// `access_read_dialog.in.rs` / `access_write_dialog.in.rs` dispatch, so callers see
/// the same coercions and the same errors as before.
impl WidgetProperties for FindReplaceDialog {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "find_text" => Ok(CapabilityValue::String(self.find_text().to_string())),
            "replace_text" => Ok(CapabilityValue::String(self.replace_text().to_string())),
            "match_case" => Ok(CapabilityValue::Bool(self.is_match_case())),
            "wrap_around" => Ok(CapabilityValue::Bool(self.is_wrap_around())),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "find_text" => {
                self.set_find_text(&expect_string(value)?);
                Ok(())
            }
            "replace_text" => {
                self.set_replace_text(&expect_string(value)?);
                Ok(())
            }
            "match_case" => {
                self.set_match_case(expect_bool(value)?);
                Ok(())
            }
            "wrap_around" => {
                self.set_wrap_around(expect_bool(value)?);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "find_text",
            "replace_text",
            "match_case",
            "wrap_around",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl Draw for FindReplaceDialog {
    fn draw(&mut self, context: &mut RenderContext) {
        if !self.visible {
            return;
        }

        // The **bar**, not the control's rectangle: see `frame_rect`. Every band below —
        // the two rows, their labels, their fields and their buttons — is derived from
        // this one rect, so the ink and the hit test cannot be anchored to different boxes.
        let geom = self.frame_rect();

        // Chrome colours resolve explicit style first, then the theme's resolved style
        // for this control, and only then fall back to a literal. Every colour below used
        // to be one of the `Color::BACKGROUND` / `FOREGROUND` / `PRIMARY` constants, which
        // do not move when the appearance switches — the rendering census reported the
        // control as theme-blind.
        //
        // The theme read is a separate manager lock, taken and released inside
        // `resolved_theme_style`, so it is not held across the draw — the global
        // manager's mutex is not re-entrant.
        let style = self.base.style().clone();
        let theme = crate::style::resolved_theme_style("find_replace_dialog");
        let themed_ink = theme.as_ref().and_then(|t| t.text_color);
        let themed_border = theme.as_ref().and_then(|t| t.border_color);

        let ink = style.text_color.or(themed_ink).unwrap_or(Color::FOREGROUND);
        let surface = style
            .background_color
            .or_else(|| theme.as_ref().and_then(|t| t.background_color))
            .unwrap_or(Color::BACKGROUND);
        let border = style
            .border_color
            .or(themed_border)
            .filter(|resolved| *resolved != surface)
            .unwrap_or_else(|| surface.blend(&ink, 0.25));
        // The entry fields and the accent are read as their own lock acquisition and copied
        // out as values, so the guard is dropped before anything else touches the theme.
        let (window_fill, accent, muted) = {
            let manager = crate::style::theme_manager();
            match manager.current_theme() {
                Some(active) => {
                    (active.colors.background, active.colors.primary, active.colors.secondary)
                }
                None => (Color::BACKGROUND, Color::PRIMARY, Color::SECONDARY),
            }
        };
        // The two entry fields are editable interiors: one step *away* from the bar's own
        // surface, so on a light bar they are lighter and on a dark one darker, rather
        // than the forced white they used to be.
        let field = if surface.is_dark() {
            surface.blend(&Color::WHITE, 0.06)
        } else {
            surface.blend(&Color::BLACK, 0.04)
        };
        let accent_ink = accent.contrast_color();
        let muted_ink = muted.contrast_color();

        // ── Background ──
        // The bar floats over the surface it searches, so its fill is derived from its own
        // resolved surface and nudged away from the window fill.
        let bar = if surface == window_fill { surface.blend(&ink, 0.06) } else { surface };
        context.fill_rect(geom, bar);
        context.draw_rect_stroke(geom, border, 1);

        let (find_row, replace_row) = self.compute_layout();
        self.find_row_rect = find_row;
        self.replace_row_rect = replace_row;

        // ── Find row: label + input + toggles + buttons ──
        //
        // The four toggles and the two arrows are a **fixed** trailing group; only the
        // entry field flexes. The group's width is named once so the field can take
        // exactly the remainder and no fixed column can be pushed past the row's right
        // edge — see `trailing_x` for the defect that caused.
        let trailing = GAP * 6 + BTN_SIZE as i32 * 4 + ARROW_BTN as i32 * 2;
        let label_col = LABEL_WIDTH.min(find_row.width);
        let input_width = (find_row.width as i32 - label_col as i32 - GAP - trailing).max(0) as u32;
        let mut x = find_row.x;

        // "Find:" label. Bounded by the label column, so it truncates there rather than
        // running into the input it names.
        let label_rect = Rect::new(x, find_row.y, label_col, find_row.height);
        let font = crate::core::Font::simple("sans-serif", 12.0);
        let label_line = self.text_line(label_rect, &font, context);
        context.draw_text_fitted(label_line, "Find:", &font, ink, HorizontalAlignment::Left);
        x += label_col as i32 + GAP;

        // Find text input background. The field is only drawn when the row genuinely
        // leaves room for it: on a bar narrower than its own trailing columns the
        // remainder floors to zero, and a zero-width field is an element the SVG backend
        // emits and the rasteriser skips — a silent divergence. Dropping it keeps the two
        // backends in agreement.
        if input_width > 0 {
            let input_rect = Rect::new(x, find_row.y, input_width, find_row.height);
            context.fill_rect(input_rect, field);
            context.draw_rect_stroke(input_rect, border, 1);
            // Guarded on the text being non-empty: an unguarded `draw_text_fitted` emits
            // `<text …></text>`, an empty element that the rasteriser never produces.
            let display_text = if self.find_text.is_empty() { "" } else { &self.find_text };
            if !display_text.is_empty() {
                let input_line = self.text_line(input_rect, &font, context);
                context.draw_text_fitted(
                    input_line,
                    display_text,
                    &font,
                    ink,
                    HorizontalAlignment::Left,
                );
            }
        }
        x = Self::trailing_x(find_row, trailing);

        // Match Case toggle
        let mc_rect = Rect::new(x, find_row.y, BTN_SIZE, find_row.height);
        let mc_color = if self.match_case { accent } else { field.blend(&ink, 0.15) };
        context.fill_rect(mc_rect, mc_color);
        let mc_line = self.text_line(mc_rect, &font, context);
        context.draw_text_fitted(
            mc_line,
            "Aa",
            &font,
            if self.match_case { accent_ink } else { ink },
            HorizontalAlignment::Left,
        );
        x += BTN_SIZE as i32 + GAP;

        // Whole Word toggle
        let ww_rect = Rect::new(x, find_row.y, BTN_SIZE, find_row.height);
        let ww_color = if self.whole_word { accent } else { field.blend(&ink, 0.15) };
        context.fill_rect(ww_rect, ww_color);
        let ww_line = self.text_line(ww_rect, &font, context);
        context.draw_text_fitted(
            ww_line,
            "W",
            &font,
            if self.whole_word { accent_ink } else { ink },
            HorizontalAlignment::Left,
        );
        x += BTN_SIZE as i32 + GAP;

        // Regex toggle
        let rx_rect = Rect::new(x, find_row.y, BTN_SIZE, find_row.height);
        let rx_color = if self.use_regex { accent } else { field.blend(&ink, 0.15) };
        context.fill_rect(rx_rect, rx_color);
        let rx_line = self.text_line(rx_rect, &font, context);
        context.draw_text_fitted(
            rx_line,
            ".*",
            &font,
            if self.use_regex { accent_ink } else { ink },
            HorizontalAlignment::Left,
        );
        x += BTN_SIZE as i32 + GAP;

        // Highlight All toggle
        let ha_rect = Rect::new(x, find_row.y, BTN_SIZE, find_row.height);
        let ha_color = if self.highlight_all { accent } else { field.blend(&ink, 0.15) };
        context.fill_rect(ha_rect, ha_color);
        let ha_line = self.text_line(ha_rect, &font, context);
        context.draw_text_fitted(
            ha_line,
            "H",
            &font,
            if self.highlight_all { accent_ink } else { ink },
            HorizontalAlignment::Left,
        );
        x += BTN_SIZE as i32 + GAP;

        // Find Previous button
        let fp_rect = Rect::new(x, find_row.y, ARROW_BTN, find_row.height);
        context.fill_rect(fp_rect, muted);
        let fp_line = self.text_line(fp_rect, &font, context);
        context.draw_text_fitted(fp_line, "\u{25B2}", &font, muted_ink, HorizontalAlignment::Left);
        x += ARROW_BTN as i32 + GAP;

        // Find Next button
        let fn_rect = Rect::new(x, find_row.y, ARROW_BTN, find_row.height);
        context.fill_rect(fn_rect, accent);
        let fn_line = self.text_line(fn_rect, &font, context);
        context.draw_text_fitted(fn_line, "\u{25BC}", &font, accent_ink, HorizontalAlignment::Left);

        // ── Replace row: label + input + buttons ──
        //
        // The replace row's trailing group is the two action buttons and the two GAPs
        // around them, laid out from the right edge by the same helper as the find row, so
        // the Replace All button sits inside the frame instead of 30 px past it.
        let r_trailing = GAP * 2 + ARROW_BTN as i32 * 2;
        let r_input_width =
            (replace_row.width as i32 - label_col as i32 - GAP - r_trailing).max(0) as u32;
        let mut x2 = replace_row.x;

        // "Replace:" label
        let rl_rect = Rect::new(x2, replace_row.y, label_col, replace_row.height);
        let rl_line = self.text_line(rl_rect, &font, context);
        context.draw_text_fitted(rl_line, "Rpl:", &font, ink, HorizontalAlignment::Left);
        x2 += label_col as i32 + GAP;

        // Replace text input background. Drawn only when there is room, for the same
        // zero-width reason the find field is guarded above.
        if r_input_width > 0 {
            let r_input_rect = Rect::new(x2, replace_row.y, r_input_width, replace_row.height);
            context.fill_rect(r_input_rect, field);
            context.draw_rect_stroke(r_input_rect, border, 1);
            let r_text = if self.replace_text.is_empty() { "" } else { &self.replace_text };
            if !r_text.is_empty() {
                let r_input_line = self.text_line(r_input_rect, &font, context);
                context.draw_text_fitted(
                    r_input_line,
                    r_text,
                    &font,
                    ink,
                    HorizontalAlignment::Left,
                );
            }
        }
        x2 = Self::trailing_x(replace_row, r_trailing);

        // Replace button
        let rep_rect = Rect::new(x2, replace_row.y, ARROW_BTN, replace_row.height);
        context.fill_rect(rep_rect, muted);
        let rep_line = self.text_line(rep_rect, &font, context);
        context.draw_text_fitted(rep_line, "R", &font, muted_ink, HorizontalAlignment::Left);
        x2 += ARROW_BTN as i32 + GAP;

        // Replace All button
        let ra_rect = Rect::new(x2, replace_row.y, ARROW_BTN, replace_row.height);
        context.fill_rect(ra_rect, muted);
        let ra_line = self.text_line(ra_rect, &font, context);
        context.draw_text_fitted(ra_line, "RA", &font, muted_ink, HorizontalAlignment::Left);
    }
}

impl EventHandler for FindReplaceDialog {
    fn handle_event(&mut self, event: &Event) {
        if !self.visible {
            self.base.handle_event(event);
            return;
        }

        // A disabled dialog must not act on input: no focus changes, no find/replace
        // actions, no close. Only `visible` was checked here, so `set_enabled(false)`
        // left the whole dialog live.
        if !self.base.is_enabled() {
            self.base.handle_event(event);
            return;
        }

        match event {
            Event::MousePress { pos, button } => {
                if *button == 1 {
                    // Check clicks on find row buttons. The regions come from the same
                    // `compute_layout` + trailing-column arithmetic the draw path uses, so
                    // a button's clickable area cannot drift from its ink. They used to be
                    // re-derived here from their own literals — `46` for the label column
                    // against the draw path's `40 + GAP`, and a `28` stride between the two
                    // replace buttons against the `24` they were drawn at — which put the
                    // Replace All hit region one inset to the left of the button it named.
                    let (find_row, replace_row) = self.compute_layout();
                    let label_col = LABEL_WIDTH.min(find_row.width);
                    let trailing = GAP * 6 + BTN_SIZE as i32 * 4 + ARROW_BTN as i32 * 2;

                    // Focus find field on click. The region is exactly the drawn field:
                    // when the row leaves no room for it (`input_width == 0`) there is no
                    // field to click, so it is not reported as one.
                    let find_input_w =
                        (find_row.width as i32 - label_col as i32 - GAP - trailing).max(0);
                    if find_input_w > 0 {
                        let find_input_region = Rect::new(
                            find_row.x + label_col as i32 + GAP,
                            find_row.y,
                            find_input_w as u32,
                            find_row.height,
                        );
                        if find_input_region.contains_point(*pos) {
                            self.focus_field = 0;
                            self.base.request_redraw();
                            return;
                        }
                    }

                    // Click on replace input
                    let r_trailing = GAP * 2 + ARROW_BTN as i32 * 2;
                    let r_label_col = LABEL_WIDTH.min(replace_row.width);
                    let r_input_width =
                        (replace_row.width as i32 - r_label_col as i32 - GAP - r_trailing).max(0);
                    if r_input_width > 0 {
                        let r_input_region = Rect::new(
                            replace_row.x + r_label_col as i32 + GAP,
                            replace_row.y,
                            r_input_width as u32,
                            replace_row.height,
                        );
                        if r_input_region.contains_point(*pos) {
                            self.focus_field = 1;
                            self.base.request_redraw();
                            return;
                        }
                    }

                    // Toggle buttons on find row. Placed from the row's right edge by the
                    // same helper the draw path uses, so the two agree by construction.
                    let button_start_x = Self::trailing_x(find_row, trailing);

                    // Match Case
                    let mc_rect = Rect::new(button_start_x, find_row.y, BTN_SIZE, find_row.height);
                    if mc_rect.contains_point(*pos) {
                        self.match_case = !self.match_case;
                        self.base.request_redraw();
                        return;
                    }

                    // Whole Word
                    let ww_rect = Rect::new(
                        button_start_x + BTN_SIZE as i32 + GAP,
                        find_row.y,
                        BTN_SIZE,
                        find_row.height,
                    );
                    if ww_rect.contains_point(*pos) {
                        self.whole_word = !self.whole_word;
                        self.base.request_redraw();
                        return;
                    }

                    // Regex
                    let rx_rect = Rect::new(
                        button_start_x + (BTN_SIZE as i32 + GAP) * 2,
                        find_row.y,
                        BTN_SIZE,
                        find_row.height,
                    );
                    if rx_rect.contains_point(*pos) {
                        self.use_regex = !self.use_regex;
                        self.base.request_redraw();
                        return;
                    }

                    // Highlight All
                    let ha_rect = Rect::new(
                        button_start_x + (BTN_SIZE as i32 + GAP) * 3,
                        find_row.y,
                        BTN_SIZE,
                        find_row.height,
                    );
                    if ha_rect.contains_point(*pos) {
                        self.highlight_all = !self.highlight_all;
                        self.base.request_redraw();
                        return;
                    }

                    // Find Previous button
                    let fp_x = button_start_x + (BTN_SIZE as i32 + GAP) * 4;
                    let fp_rect = Rect::new(fp_x, find_row.y, ARROW_BTN, find_row.height);
                    if fp_rect.contains_point(*pos) {
                        self.find_previous();
                        return;
                    }

                    // Find Next button
                    let fn_rect = Rect::new(
                        fp_x + ARROW_BTN as i32 + GAP,
                        find_row.y,
                        ARROW_BTN,
                        find_row.height,
                    );
                    if fn_rect.contains_point(*pos) {
                        self.find_next();
                        return;
                    }

                    // Replace button. Anchored to the row's right edge by the same helper
                    // the draw path uses, so the hit region is the button.
                    let r_btn_x = Self::trailing_x(replace_row, r_trailing);
                    let rep_rect = Rect::new(r_btn_x, replace_row.y, ARROW_BTN, replace_row.height);
                    if rep_rect.contains_point(*pos) {
                        self.replace();
                        return;
                    }

                    // Replace All button
                    let ra_rect = Rect::new(
                        r_btn_x + ARROW_BTN as i32 + GAP,
                        replace_row.y,
                        ARROW_BTN,
                        replace_row.height,
                    );
                    if ra_rect.contains_point(*pos) {
                        self.replace_all();
                    }
                }
            }
            Event::KeyPress { key, modifiers: _ } => {
                match *key {
                    27 => {
                        // Escape
                        self.visible = false;
                        self.close_signal.emit(());
                        self.base.request_redraw();
                    }
                    13 => {
                        // Enter — trigger find next
                        self.find_next();
                    }
                    8 => {
                        // Backspace
                        self.backspace_focused();
                    }
                    9 => {
                        // Tab — switch focus between find and replace
                        self.focus_field = 1 - self.focus_field;
                        self.base.request_redraw();
                    }
                    _ => {
                        // Printable character range (rough check)
                        if *key >= 32 && *key <= 126 {
                            if let Some(ch) = char::from_u32(*key) {
                                self.append_to_focused(ch);
                            }
                        }
                    }
                }
            }
            _ => {
                self.base.handle_event(event);
            }
        }
    }
}

// These tests drive the **theme**, which only exists in a build with a device profile
// (see `crate::lib`: `pub mod theme` is gated on `device_profile`). Without this gate the
// `mini` and `embedded` profiles fail to compile their test targets, because the test code
// names a module that those builds compile out — the production code is profile-clean and
// only the fixture was not.
#[cfg(all(test, full_widgets))]
mod tests {
    use super::*;
    use crate::widget::svg::render_to_svg;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    #[test]
    fn find_replace_dialog_default_creation() {
        let dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        assert_eq!(dialog.kind(), WidgetKind::FindReplaceDialog);
        assert!(!dialog.is_visible());
        assert!(dialog.find_text().is_empty());
        assert!(dialog.replace_text().is_empty());
        assert!(!dialog.is_match_case());
        assert!(!dialog.is_whole_word());
        assert!(!dialog.is_use_regex());
        assert!(!dialog.is_highlight_all());
    }

    #[test]
    fn find_replace_dialog_set_get_text() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        dialog.set_find_text("hello");
        assert_eq!(dialog.find_text(), "hello");

        dialog.set_replace_text("world");
        assert_eq!(dialog.replace_text(), "world");

        dialog.set_find_text("");
        assert!(dialog.find_text().is_empty());
    }

    #[test]
    fn find_replace_dialog_toggle_options() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));

        assert!(!dialog.is_match_case());
        dialog.set_match_case(true);
        assert!(dialog.is_match_case());
        dialog.set_match_case(false);
        assert!(!dialog.is_match_case());

        assert!(!dialog.is_whole_word());
        dialog.set_whole_word(true);
        assert!(dialog.is_whole_word());

        assert!(!dialog.is_use_regex());
        dialog.set_use_regex(true);
        assert!(dialog.is_use_regex());

        assert!(!dialog.is_highlight_all());
        dialog.set_highlight_all(true);
        assert!(dialog.is_highlight_all());
    }

    #[test]
    fn find_replace_dialog_show_hide() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        assert!(!dialog.is_visible());

        dialog.show();
        assert!(dialog.is_visible());

        dialog.hide();
        assert!(!dialog.is_visible());
    }

    #[test]
    fn find_replace_dialog_signals_find_next() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        dialog.set_find_text("search");
        dialog.show();

        let fired = Arc::new(AtomicBool::new(false));
        let fired_clone = Arc::clone(&fired);
        dialog.find_next_signal.connect(move |request: Arc<SearchRequest>| {
            if request.text == "search" {
                fired_clone.store(true, Ordering::SeqCst);
            }
        });

        dialog.find_next();
        assert!(fired.load(Ordering::SeqCst), "find_next_signal should fire with the find text");
    }

    /// The find signal carries the **options**, not only the text.
    ///
    /// # The defect this pins
    ///
    /// The dialog declares four search flags and its signals carried none of them, so a host could
    /// not run the search the user asked for: it received a bare string and had to reach back into
    /// the widget for the mode. `wrap_around` was the sharpest case -- it was stored, published, and
    /// **read by nothing at all**, not even by the dialog, because the dialog does not search.
    ///
    /// The assertion is on the payload's *contents*, and it flips a flag first, because a payload
    /// that always carried the defaults would satisfy "the options arrived" while being useless.
    #[test]
    fn a_find_request_carries_the_options_in_force() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        dialog.set_find_text("needle");
        dialog.set_wrap_around(false);
        dialog.show();

        let seen = Arc::new(std::sync::Mutex::new(Vec::<SearchOptions>::new()));
        let sink = Arc::clone(&seen);
        dialog.find_next_signal.connect(move |request: Arc<SearchRequest>| {
            sink.lock().expect("sink poisoned").push(request.options);
        });

        dialog.find_next();
        let captured = seen.lock().expect("lock").clone();
        assert_eq!(captured.len(), 1, "the signal fired once");
        let options = captured[0];
        assert!(
            !options.wrap_around,
            "the wrap-around toggle must travel with the request, or the host cannot honour it: \
             {options:?}"
        );
        // The other three travel with it too -- one grouped value is the point of `SearchOptions`.
        assert_eq!(
            options,
            SearchOptions {
                case_sensitive: dialog.is_match_case(),
                whole_word: dialog.is_whole_word(),
                use_regex: dialog.is_use_regex(),
                wrap_around: false,
            },
            "every flag must match the dialog's own state"
        );
    }

    #[test]
    fn find_replace_dialog_signals_replace_all() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        dialog.set_find_text("foo");
        dialog.set_replace_text("bar");
        dialog.show();

        let fired = Arc::new(AtomicBool::new(false));
        let fired_clone = Arc::clone(&fired);
        dialog.replace_all_signal.connect(move |payload: Arc<(String, String, SearchOptions)>| {
            if payload.0 == "foo" && payload.1 == "bar" {
                fired_clone.store(true, Ordering::SeqCst);
            }
        });

        dialog.replace_all();
        assert!(
            fired.load(Ordering::SeqCst),
            "replace_all_signal should fire with (find, replace)"
        );
    }

    /// A **shown** find bar paints differently in the two appearances.
    ///
    /// The rendering census measures this control hidden, which is correct — a find bar
    /// is opened on demand, so `visible: false` is its designed initial state and the
    /// census therefore counts zero ink. That makes the census silent about whether the
    /// bar responds to a theme switch, so the property is asserted here, through the real
    /// `Draw` path, by rendering a shown instance in each appearance and comparing the
    /// output.
    ///
    /// # Why the guard covers the whole test, not just each render
    ///
    /// The appearance is process-wide, and `theme_test_guard` is the mutex that serialises
    /// tests against it. This test used to take the guard *inside* its `frame` helper, release
    /// it, and then restore the appearance after the assertion — outside any guard. The window
    /// between the helper returning and that final write was unguarded, so a test in another
    /// module that reads the global theme while rendering (for instance `meter`'s render
    /// assertions) could observe a half-switched state and fail intermittently. Holding one
    /// guard for the whole test is what makes "restore what I changed" atomic with respect to
    /// every other reader.
    #[test]
    fn a_shown_find_bar_renders_differently_in_light_and_dark() {
        let _guard = crate::style::theme_test_guard();
        {
            let mut manager = crate::style::theme_manager();
            manager.register_theme(crate::theme::Theme::default());
            manager.register_theme(crate::theme::Theme::dark());
        }

        fn frame(appearance: crate::style::AppearanceMode) -> String {
            crate::style::theme_manager().set_appearance(appearance);
            let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 240, 120));
            dialog.set_find_text("Sample");
            dialog.show();
            crate::theme::apply_active_theme(&mut dialog);
            crate::widget::svg::render_to_svg(&mut dialog)
        }

        let light = frame(crate::style::AppearanceMode::Light);
        let dark = frame(crate::style::AppearanceMode::Dark);
        assert_ne!(
            light, dark,
            "a theme switch must change what a shown find bar paints; identical output \
             means the chrome is hardcoded"
        );
        // Restored while the guard is still held, so no other reader can observe the light
        // appearance this test selected.
        crate::style::theme_manager().set_appearance(crate::style::AppearanceMode::Light);
    }

    #[test]
    fn find_replace_dialog_close_on_escape() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        dialog.show();
        assert!(dialog.is_visible());

        let fired = Arc::new(AtomicBool::new(false));
        let fired_clone = Arc::clone(&fired);
        dialog.close_signal.connect(move |_: Arc<()>| {
            fired_clone.store(true, Ordering::SeqCst);
        });

        dialog.handle_event(&Event::KeyPress { key: 27, modifiers: 0 });
        assert!(!dialog.is_visible());
        assert!(fired.load(Ordering::SeqCst), "close_signal should fire on Escape");
    }

    /// A disabled dialog must neither close nor act on typed input.
    ///
    /// `handle_event` gated on `visible` only, so `set_enabled(false)` left the dialog
    /// fully interactive — a suspended dialog could still be closed with Escape and
    /// still ran find/replace on Enter.
    #[test]
    fn find_replace_dialog_disabled_ignores_input() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        dialog.show();
        dialog.set_enabled(false);

        let fired = Arc::new(AtomicBool::new(false));
        let fired_clone = Arc::clone(&fired);
        dialog.close_signal.connect(move |_: Arc<()>| {
            fired_clone.store(true, Ordering::SeqCst);
        });

        dialog.handle_event(&Event::KeyPress { key: 27, modifiers: 0 });
        assert!(dialog.is_visible(), "a disabled dialog must not close on Escape");
        assert!(!fired.load(Ordering::SeqCst), "a disabled dialog must not emit close_signal");

        // The find action must not run either.
        let find_fired = Arc::new(AtomicBool::new(false));
        let ff = Arc::clone(&find_fired);
        dialog.find_next_signal.connect(move |_: Arc<SearchRequest>| {
            ff.store(true, Ordering::SeqCst);
        });
        dialog.handle_event(&Event::KeyPress { key: 13, modifiers: 0 });
        assert!(!find_fired.load(Ordering::SeqCst), "a disabled dialog must not run find");

        // Re-enabling restores the behaviour, so the gate suspends rather than locks.
        dialog.set_enabled(true);
        dialog.handle_event(&Event::KeyPress { key: 27, modifiers: 0 });
        assert!(!dialog.is_visible(), "re-enabling must restore Escape-to-close");
    }

    #[test]
    fn find_replace_dialog_svg_output() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 400, 80));
        dialog.set_find_text("find_me");
        dialog.set_replace_text("replace_with");
        dialog.set_match_case(true);
        dialog.show();

        let svg = render_to_svg(&mut dialog);
        assert!(svg.starts_with("<svg"), "SVG should start with <svg, got: {svg:.60}");
        assert!(svg.ends_with("</svg>"), "SVG should end with </svg>");
    }

    /// The census geometry must emit no zero-extent rect and no empty text element, and no
    /// element may leave the 240 px frame.
    ///
    /// This pins the three named defects of the old layout together, because they share one
    /// cause — the rows were laid out from the control's raw rectangle with four independent
    /// literals:
    ///
    /// * `find_replace_dialog.svg` carried `<rect x="52" y="29" width="0" height="28">`
    ///   (the find field collapsed to zero width). A zero-width rect is emitted by the SVG
    ///   backend and skipped by the rasteriser, so the two backends disagreed about the frame.
    /// * each entry field emitted `<text …></text>` when its value was empty — again an element
    ///   the rasteriser never produces.
    /// * the two arrow buttons were drawn at x 240 and x 270, past the 240 px frame.
    #[test]
    fn the_find_bar_emits_no_zero_extent_or_out_of_frame_ink_at_the_census_geometry() {
        let mut dialog = FindReplaceDialog::new(Rect::new(0, 0, 240, 120));
        dialog.show();
        let svg = render_to_svg(&mut dialog);

        assert!(
            !svg.contains("width=\"0\"") && !svg.contains("height=\"0\""),
            "a zero-extent rect is emitted by the SVG backend and skipped by the rasteriser: {svg}"
        );
        assert!(
            !svg.contains("></text>"),
            "an empty <text> element is a silent divergence between the two backends: {svg}"
        );
        // Every x is a placement or a line endpoint; none may start or end past the frame.
        for token in
            svg.split(|c: char| !(c.is_ascii_digit() || c == '=' || c.is_ascii_alphabetic()))
        {
            if let Some(rest) = token.strip_prefix("x=") {
                if let Ok(value) = rest.parse::<i32>() {
                    assert!(value <= 240, "ink at x={value} is outside the 240 px frame: {svg}");
                }
            }
        }
    }
}