rust_widgets 2.9.2

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
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! SearchBar widget — iOS-style search bar with search icon, clear button,
//! and optional cancel button when active.
//!
//! Unlike the existing SearchBox, this widget is designed for mobile-style
//! search UX with active state management and a cancel button.

use crate::core::{Color, HorizontalAlignment, Point, Rect};
use crate::event::key_codes;
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::shortcut::Key;
use crate::signal::{GenericSignal, Signal1};
use crate::undo::{UndoStack, ValueCommand};
use crate::widget::capability::coercion::{
    expect_horizontal_alignment, expect_string, horizontal_alignment_to_str,
};
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::input_widgets::shortcut_editor::primary_shortcut_pressed;
use crate::widget::metrics::effective_font;
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};
use std::cell::RefCell;
use std::rc::Rc;

/// The number of recent searches the bar remembers.
const MAX_RECENT_SEARCHES: usize = 5;
/// Height of one recent-search row in the menu below the field.
const RECENT_ROW_HEIGHT: u32 = 24;

/// iOS-style search bar widget.
///
/// Displays a rounded search field with a search icon on the left, optional
/// clear button when text is present, and a cancel button on the right when
/// the search bar is active.
pub struct SearchBar {
    base: BaseWidget,
    text: String,
    placeholder: String,
    is_active: bool,
    cancel_button_visible: bool,
    /// How the typed value is aligned within its own box.
    ///
    /// Horizontal only: the value is centred vertically in the field band as a matter of the
    /// field's own layout, so a `top`/`bottom` value would be one this control could never honour —
    /// [`crate::widget::capability::coercion::expect_horizontal_alignment`] refuses those rather
    /// than accepting a write that does nothing. Defaults to left, so a caller that never asks
    /// behaves exactly as it did.
    alignment: crate::core::Alignment,
    /// Emitted when the text content changes.
    pub text_changed: Signal1<String>,
    /// Emitted when the user submits a search (Enter key).
    pub search_submitted: Signal1<String>,
    /// Emitted when the user taps the cancel button.
    pub canceled: GenericSignal,
    undo_stack: UndoStack,
    history_target: Rc<RefCell<String>>,
    restoring_history: bool,
    /// Whether the field owns keyboard focus. Key and text events only edit the value while
    /// this is true; before it existed, any keystroke anywhere in the window rewrote the query.
    focused: bool,
    /// The insertion point, in bytes from the start of the value.
    cursor: usize,
    /// The other end of a selection, in bytes. `None` means no selection.
    selection_anchor: Option<usize>,
    /// The caret's blink state, shared with the crate's other carets.
    cursor_blink: crate::style::CursorBlink,
    /// Most recent submitted queries, newest first.
    recent_searches: Vec<String>,
    /// Whether the recent-search menu is open.
    show_recent: bool,
    /// When true, the value is drawn as one chip per whitespace-separated term.
    tokenizing: bool,
    /// Memoised token list keyed by the input string (I-P64).
    tokens_cache: RefCell<Option<(String, Rc<Vec<String>>)>>,
    /// Memoised cancel-button width keyed by the resolved font (I-P63).
    cancel_width_cache: RefCell<Option<(crate::core::Font, u32)>>,
    /// The IME composition string painted inline before commit (I-111).
    preedit: String,
    /// First recent-search row the menu shows (I-111).
    recent_scroll: usize,
}

impl SearchBar {
    /// Creates a new SearchBar widget with the given geometry.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::SearchBar, geometry, "SearchBar"),
            text: String::new(),
            placeholder: "Search".to_string(),
            is_active: false,
            cancel_button_visible: true,
            alignment: crate::core::Alignment::Left,
            text_changed: Signal1::new(),
            search_submitted: Signal1::new(),
            canceled: GenericSignal::new(),
            undo_stack: UndoStack::new(),
            history_target: Rc::new(RefCell::new(String::new())),
            restoring_history: false,
            focused: false,
            cursor: 0,
            selection_anchor: None,
            cursor_blink: crate::style::CursorBlink::new(),
            recent_searches: Vec::new(),
            show_recent: false,
            tokenizing: false,
            tokens_cache: RefCell::new(None),
            cancel_width_cache: RefCell::new(None),
            preedit: String::new(),
            recent_scroll: 0,
        }
    }

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

    /// Compatibility spelling of [`Self::canceled`] (W-19): the same emitted signal.
    pub fn cancelled(&self) -> &GenericSignal {
        &self.canceled
    }

    /// How the typed value is aligned within its own box.
    pub fn alignment(&self) -> crate::core::Alignment {
        self.alignment
    }

    /// Sets how the typed value is aligned within its own box.
    ///
    /// Horizontal only. A `top`/`bottom` alignment is **ignored**, because the value is centred
    /// vertically in the field band by the field's own layout — the property route refuses it
    /// through [`crate::widget::capability::coercion::expect_horizontal_alignment`], and this setter
    /// matching that keeps the two entry points from disagreeing.
    pub fn set_alignment(&mut self, alignment: crate::core::Alignment) {
        if alignment.to_horizontal().is_none() || self.alignment == alignment {
            return;
        }
        self.alignment = alignment;
        self.base.request_redraw();
    }

    /// Sets the search text. Emits `text_changed` signal.
    pub fn set_text(&mut self, text: impl Into<String>) {
        let new_text = text.into();
        if self.text != new_text {
            let before = self.text.clone();
            self.text = new_text.clone();
            if !self.restoring_history {
                *self.history_target.borrow_mut() = self.text.clone();
                self.undo_stack.push(Box::new(ValueCommand::new(
                    self.history_target.clone(),
                    before,
                    self.text.clone(),
                    "Edit text",
                    "search_bar_text",
                )));
            }
            self.cursor = self.text.len();
            self.selection_anchor = None;
            self.text_changed.emit(new_text);
            self.base.request_redraw();
        }
    }

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

    /// Sets the placeholder text.
    pub fn set_placeholder(&mut self, placeholder: impl Into<String>) {
        let placeholder = placeholder.into();
        if self.placeholder == placeholder {
            return;
        }
        self.placeholder = placeholder;
        self.base.request_redraw();
    }

    /// Sets whether the search bar is active (showing cancel button).
    pub fn set_active(&mut self, active: bool) {
        if self.is_active != active {
            self.is_active = active;
            self.base.request_redraw();
        }
    }

    /// Returns whether the search bar is active.
    pub fn is_active(&self) -> bool {
        self.is_active
    }

    /// Whether the field owns keyboard focus.
    pub fn is_focused(&self) -> bool {
        self.focused
    }

    /// Sets the focused state. Losing focus closes the recent-search menu.
    pub fn set_focused(&mut self, focused: bool) {
        if self.focused == focused {
            return;
        }
        self.focused = focused;
        if focused {
            self.cursor_blink.start();
        } else {
            self.cursor_blink.stop();
            self.selection_anchor = None;
            self.show_recent = false;
        }
        self.base.request_redraw();
    }

    /// The caret's byte offset in the value.
    pub fn cursor_position(&self) -> usize {
        self.cursor
    }

    /// Moves the caret, clearing the selection.
    pub fn set_cursor_position(&mut self, position: usize) {
        let position = crate::widget::text_utils::floor_char_boundary(
            &self.text,
            position.min(self.text.len()),
        );
        if self.cursor == position && self.selection_anchor.is_none() {
            return;
        }
        self.cursor = position;
        self.selection_anchor = None;
        self.base.request_redraw();
    }

    /// The selected byte range, or `None` when nothing is selected.
    pub fn selection_range(&self) -> Option<(usize, usize)> {
        let anchor = self.selection_anchor?;
        let (start, end) =
            if anchor <= self.cursor { (anchor, self.cursor) } else { (self.cursor, anchor) };
        (start != end).then_some((start, end))
    }

    /// The selected text, or an empty string when nothing is selected.
    pub fn selected_text(&self) -> String {
        self.selection_range()
            .map_or_else(String::new, |(start, end)| self.text[start..end].to_string())
    }

    /// Selects the whole value.
    pub fn select_all(&mut self) {
        if !self.text.is_empty() {
            self.selection_anchor = Some(0);
            self.cursor = self.text.len();
            self.base.request_redraw();
        }
    }

    /// Advances the caret blink. Returns whether it owes another frame.
    pub fn tick(&mut self, delta_ms: u32) -> bool {
        if !self.focused {
            return false;
        }
        let running = self.cursor_blink.tick(delta_ms);
        if running {
            self.base.request_redraw();
        }
        running
    }

    /// The remembered recent searches, newest first.
    pub fn recent_searches(&self) -> &[String] {
        &self.recent_searches
    }

    /// Replaces the recent-search list. The list is capped at five entries.
    pub fn set_recent_searches(&mut self, searches: Vec<String>) {
        let mut next: Vec<String> = Vec::new();
        for search in searches {
            let search = search.trim().to_string();
            if search.is_empty() || next.contains(&search) {
                continue;
            }
            next.push(search);
            if next.len() == MAX_RECENT_SEARCHES {
                break;
            }
        }
        if self.recent_searches == next {
            return;
        }
        self.recent_searches = next;
        self.base.request_redraw();
    }

    /// Remembers `search` as the newest recent search.
    pub fn remember_search(&mut self, search: &str) {
        let search = search.trim();
        if search.is_empty() {
            return;
        }
        self.recent_searches.retain(|existing| existing != search);
        self.recent_searches.insert(0, search.to_string());
        self.recent_searches.truncate(MAX_RECENT_SEARCHES);
        self.base.request_redraw();
    }

    /// Clears the recent-search list and closes its menu.
    pub fn clear_recent_searches(&mut self) {
        if self.recent_searches.is_empty() && !self.show_recent {
            return;
        }
        self.recent_searches.clear();
        self.show_recent = false;
        self.base.request_redraw();
    }

    /// Whether the recent-search menu is open.
    pub fn is_recent_menu_open(&self) -> bool {
        self.show_recent
    }

    /// Opens or closes the recent-search menu.
    pub fn set_recent_menu_open(&mut self, open: bool) {
        let open = open && !self.recent_searches.is_empty();
        if self.show_recent == open {
            return;
        }
        self.show_recent = open;
        self.base.request_redraw();
    }

    /// Whether the value is drawn as one chip per term.
    pub fn is_tokenizing(&self) -> bool {
        self.tokenizing
    }

    /// Sets whether the value is drawn as chips.
    ///
    /// Tokenizing is presentation only: the stored value and the submitted string stay the
    /// space-separated text the user typed, so a host never has to rejoin chips.
    pub fn set_tokenizing(&mut self, tokenizing: bool) {
        if self.tokenizing == tokenizing {
            return;
        }
        self.tokenizing = tokenizing;
        self.base.request_redraw();
    }

    /// The value split into its terms, or an empty list when the value is blank.
    pub fn tokens(&self) -> Vec<String> {
        self.tokens_cached().as_ref().clone()
    }

    /// The token list, rebuilt only when the input string changes (I-P64).
    fn tokens_cached(&self) -> Rc<Vec<String>> {
        if let Some((text, tokens)) = self.tokens_cache.borrow().as_ref() {
            if *text == self.text {
                return Rc::clone(tokens);
            }
        }
        let tokens = Rc::new(
            self.text.split_whitespace().map(|term| term.to_string()).collect::<Vec<String>>(),
        );
        *self.tokens_cache.borrow_mut() = Some((self.text.clone(), Rc::clone(&tokens)));
        tokens
    }

    /// The width of the cancel button, measured from its label rather than assumed.
    ///
    /// The field reserves this much room when active, so a translated or larger-font label
    /// cannot be clipped by a literal 60 px reservation. The measurement is cached by the
    /// resolved font, because draw and the press test each asked for it every frame (I-P63).
    fn cancel_button_width(&self) -> u32 {
        let font = effective_font(self.style());
        if let Some((cached_font, width)) = self.cancel_width_cache.borrow().as_ref() {
            if cached_font == font {
                return *width;
            }
        }
        let mut measurement =
            crate::render::SoftwarePaintBackend::new(crate::core::Size::new(0, 0), 1.0);
        let context = RenderContext::new(&mut measurement);
        let measured = context.measure_text("Cancel", font).width;
        let width = (measured + 16).max(44);
        *self.cancel_width_cache.borrow_mut() = Some((font.clone(), width));
        width
    }

    /// Sets whether the cancel button is visible when active.
    pub fn set_cancel_button_visible(&mut self, visible: bool) {
        if self.cancel_button_visible == visible {
            return;
        }
        self.cancel_button_visible = visible;
        self.base.request_redraw();
    }

    /// Returns whether the cancel button is visible when active.
    pub fn cancel_button_visible(&self) -> bool {
        self.cancel_button_visible
    }

    /// Clears the search text. Emits `text_changed` signal.
    pub fn clear(&mut self) {
        if !self.text.is_empty() {
            self.set_text(String::new());
        }
    }

    /// Reverts the most recent edit in this search bar.
    ///
    /// Returns `false` and changes nothing when there is nothing to undo;
    /// otherwise emits `text_changed` with the restored text.
    pub fn undo(&mut self) -> bool {
        if self.undo_stack.undo().is_err() {
            return false;
        }
        self.restore_history_text();
        true
    }

    /// Reapplies the most recently undone edit.
    ///
    /// Returns `false` and changes nothing when there is nothing to redo;
    /// otherwise emits `text_changed` with the restored text.
    pub fn redo(&mut self) -> bool {
        if self.undo_stack.redo().is_err() {
            return false;
        }
        self.restore_history_text();
        true
    }

    /// Returns whether there is an edit to undo.
    pub fn can_undo(&self) -> bool {
        self.undo_stack.can_undo()
    }

    /// Returns whether there is an undone edit to reapply.
    pub fn can_redo(&self) -> bool {
        self.undo_stack.can_redo()
    }

    fn restore_history_text(&mut self) {
        self.restoring_history = true;
        self.text = self.history_target.borrow().clone();
        self.restoring_history = false;
        self.cursor = self.text.len();
        self.selection_anchor = None;
        self.text_changed.emit(self.text.clone());
        self.base.request_redraw();
    }

    /// Matches a printable or IME-committed string at the caret, replacing any selection.
    fn insert_text(&mut self, text: &str) {
        let filtered: String = text.chars().filter(|ch| !ch.is_control()).collect();
        if filtered.is_empty() {
            return;
        }
        let (start, end) = self.selection_range().unwrap_or((self.cursor, self.cursor));
        let mut next = self.text.clone();
        next.replace_range(start..end, &filtered);
        self.set_text(next);
        self.cursor = start + filtered.len();
        self.selection_anchor = None;
        self.base.request_redraw();
    }

    /// Removes the selection, or the character before the caret when there is none.
    fn backspace(&mut self) {
        let (start, end) = self.selection_range().unwrap_or_else(|| {
            let start = crate::widget::text_utils::previous_char_boundary(&self.text, self.cursor);
            (start, self.cursor)
        });
        if start == end {
            return;
        }
        let mut next = self.text.clone();
        next.replace_range(start..end, "");
        self.set_text(next);
        self.cursor = start;
        self.selection_anchor = None;
    }

    /// Moves the caret by `delta` characters, extending the selection when `extend` is set.
    fn move_caret(&mut self, delta: i32, extend: bool) {
        let mut target = self.cursor;
        if delta < 0 {
            for _ in 0..delta.unsigned_abs() {
                target = crate::widget::text_utils::previous_char_boundary(&self.text, target);
            }
        } else {
            for _ in 0..delta.unsigned_abs() {
                target = crate::widget::text_utils::next_char_boundary(&self.text, target);
            }
        }
        let target = crate::widget::text_utils::floor_char_boundary(&self.text, target);
        if extend {
            if self.selection_anchor.is_none() {
                self.selection_anchor = Some(self.cursor);
            }
        } else {
            self.selection_anchor = None;
        }
        if self.cursor != target {
            self.cursor = target;
            self.base.request_redraw();
        }
    }

    /// The recent-search row a point falls on, if the menu is open.
    fn recent_row_at(&self, pos: Point) -> Option<usize> {
        if !self.show_recent {
            return None;
        }
        let rect = self.geometry();
        let menu_top = rect.y + rect.height as i32;
        if pos.x < rect.x || pos.x >= rect.x + rect.width as i32 || pos.y < menu_top {
            return None;
        }
        let row = ((pos.y - menu_top) / RECENT_ROW_HEIGHT as i32) as usize;
        (row < self.recent_searches.len()).then_some(row)
    }

    /// Submits the current search text.
    fn submit(&mut self) {
        let text = self.text.clone();
        if !text.is_empty() {
            self.remember_search(&text);
            self.search_submitted.emit(text);
        }
    }
}

impl Widget for SearchBar {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn set_state_theme_hook(&mut self) {
        crate::style::reapply_active_theme_state(self);
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn size_hint(&self) -> crate::core::Size {
        crate::core::Size::new(200, 36)
    }

    /// Advances the caret blink so an on-demand host keeps the animation alive (I-P64).
    fn tick(&mut self, delta_ms: u32) -> bool {
        SearchBar::tick(self, delta_ms)
    }

    /// A focused field has a blinking caret and therefore owes frames (I-P64).
    fn is_animating(&self) -> bool {
        self.focused
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
    /// Resolves a published event name to the control's own signal.
    ///
    /// # Why this is explicit per control
    ///
    /// `connect_event` validates a name against the capability table and registers a hub slot; only
    /// `event_signal_dyn` joins that name to the signal the control actually emits. Without it a name
    /// is valid and inert, which is the silent failure `tools/check_event_signal_dyn.sh` exists to
    /// make impossible. The arm set is checked against the capability's published names, so this
    /// list cannot drift from what the control advertises.
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        #[allow(unused_imports)]
        use crate::widget::capability::CapabilityValue;
        match name {
            "text_changed" => {
                Some(EventSignalRef::mapped("text_changed", &self.text_changed, |v| {
                    CapabilityValue::String(v.clone())
                }))
            }
            "search_submitted" => {
                Some(EventSignalRef::mapped("search_submitted", &self.search_submitted, |v| {
                    CapabilityValue::String(v.clone())
                }))
            }
            "canceled" => Some(EventSignalRef::unit("canceled", &self.canceled)),
            _ => None,
        }
    }
}

/// `SearchBar`'s property contract.
impl WidgetProperties for SearchBar {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "text" => Ok(CapabilityValue::String(self.text().to_string())),
            "placeholder" => Ok(CapabilityValue::String(self.placeholder().to_string())),
            "alignment" => Ok(CapabilityValue::String(
                horizontal_alignment_to_str(self.alignment()).to_string(),
            )),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "text" => {
                self.set_text(expect_string(value)?);
                Ok(())
            }
            "placeholder" => {
                self.set_placeholder(expect_string(value)?);
                Ok(())
            }
            "alignment" => {
                self.set_alignment(expect_horizontal_alignment(value)?);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        // Mirrors `SEARCH_BAR_PROPERTIES`.
        property_names_of!["text", "placeholder", "alignment", BASE_PROPERTY_NAMES]
    }

    /// Runs one of the commands `search_bar` publishes.
    ///
    /// Both names assign state and need a payload, so both are answered through the
    /// property route. The capability publishes no zero-argument action for this
    /// control, which is why there is no `Ok(())` arm here.
    fn command(&mut self, name: &str) -> Result<(), CapabilityAccessError> {
        match name {
            "set_text" | "set_placeholder" => Err(CapabilityAccessError::OutOfRange),
            _ => Err(CapabilityAccessError::UnknownCommand),
        }
    }
}

impl Draw for SearchBar {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        let is_enabled = self.base.is_enabled();
        // Resolved once for the whole draw: the field, the icon and the cancel label all read
        // it, and re-resolving would take the theme lock once per read. Precedence is the
        // crate-wide one — an explicit style wins, then the theme, then the literal.
        let style = self.style();
        let themed = crate::style::resolved_theme_style_shared("search_bar");
        let themed_bg = themed.as_ref().and_then(|resolved| resolved.background_color);
        let accent = style
            .border_color
            .or_else(|| themed.as_ref().and_then(|resolved| resolved.border_color));
        let cancel_width: u32 = if self.is_active && self.cancel_button_visible {
            self.cancel_button_width()
        } else {
            0
        };
        let field_width = rect.width.saturating_sub(cancel_width + 4);

        // Cancel button area
        let cancel_x = rect.x + rect.width as i32 - cancel_width as i32;
        // The **effective font** — the resolved theme/caller font — for the cancel label, the
        // placeholder and the value (D09-STYLE-01).
        let font = effective_font(style);
        if self.is_active && self.cancel_button_visible {
            let _cancel_rect = Rect::new(cancel_x, rect.y, cancel_width, rect.height);
            let cancel_font = font;
            let cancel_text = "Cancel";
            let metrics = context.measure_text(cancel_text, cancel_font);
            let text_x = cancel_x + (cancel_width as i32 - metrics.width as i32) / 2;
            // Centre the label on the cancel area. The origin is the glyph box's top edge, so
            // half the *line box* is the correct offset; the `+ ascent` began the glyph box
            // half a line below the middle.
            let text_y = rect.y + (rect.height as i32 - metrics.height as i32) / 2;
            context.draw_text(
                Point::new(text_x, text_y),
                cancel_text,
                cancel_font,
                // The cancel label is the control's accent — an actionable affordance — so it
                // follows the resolved border colour rather than a fixed blue.
                accent.unwrap_or(Color::rgba(52, 120, 246, 255)),
                HorizontalAlignment::Left,
            );
        }

        // Search field background
        //
        // Resolved from the style so a theme switch reaches this control; the two states are
        // *derived* from the resolved colour, so disabled reads as the same field with the
        // energy taken out and the control follows the theme.
        let field_rect = Rect::new(rect.x, rect.y, field_width, rect.height);
        let base_field =
            style.background_color.or(themed_bg).unwrap_or(Color::rgba(200, 200, 205, 200));
        // The page this field sits in, read once: a disabled fill recedes toward it.
        let window_fill = crate::style::theme_manager()
            .current_theme()
            .map(|active| active.colors.background)
            .unwrap_or(Color::WHITE);
        // # Why the field acknowledges the pointer
        //
        // Disabled is handled (faded toward white); hovered was not, so a pointer over the
        // search field painted the resting fill and the field read as static. The weight comes
        // from `StateOverlay::fill_blend`, which is the same source the preset theme's
        // `"<kind>:hover"` key derives from — a private constant here would drift from it.
        let hover = if is_enabled {
            let overlay = crate::style::StateOverlay::from_base(
                self.base.is_hovered(),
                self.base.is_pressed(),
                false,
            );
            overlay.fill_blend()
        } else {
            0.0
        };
        let field_color = if !is_enabled {
            // Stepped toward the field's own contrast colour, exactly as the hover arm below
            // does. It read `base_field.blend(&Color::WHITE, 0.35)`, and white is not a
            // direction: against a light window it pushed the disabled field *further* from the
            // window than the enabled one (measured 11.90:1 disabled vs 9.04:1 enabled), so the
            // disabled state was the more prominent of the two.
            self.base.disabled_surface_near(base_field, window_fill)
        } else if hover > 0.0 {
            base_field.blend(&base_field.contrast_color(), hover)
        } else {
            base_field
        };
        // The field's ink: the typed text and the placeholder both read on `field_color`, so
        // they are derived from it rather than being two literals written for a light field.
        // The previous pair (`40,40,40` and `160,160,160`) rendered at 1.5:1 and 2.6:1 once the
        // field resolved to a dark surface.
        let field_ink = field_color.contrast_color();
        let corner_radius = rect.height / 2;
        context.fill_rounded_rect(field_rect, corner_radius, field_color);

        // Search icon (magnifying glass)
        let icon_size: u32 = 14;
        let icon_left = rect.x + 10;
        let icon_top = rect.y + (rect.height as i32 - icon_size as i32) / 2;
        // The magnifier is a glyph, so it reads the control's text colour.
        let icon_color = style.text_color.unwrap_or(Color::rgba(140, 140, 140, 255));
        context.draw_circle_stroke(
            Point::new(icon_left + (icon_size / 2) as i32, icon_top + (icon_size / 2) as i32),
            icon_size / 2,
            icon_color,
            2,
        );
        // Search icon handle (diagonal line from bottom-right of circle)
        let handle_offset = icon_size as i32;
        context.draw_line(
            Point::new(icon_left + handle_offset - 2, icon_top + handle_offset - 2),
            Point::new(icon_left + handle_offset + 3, icon_top + handle_offset + 3),
            icon_color,
        );

        // Text or placeholder
        let text_left = icon_left + icon_size as i32 + 6;
        let text_width = if self.text.is_empty() {
            field_width.saturating_sub((text_left - rect.x) as u32)
        } else {
            field_width.saturating_sub((text_left - rect.x) as u32 + 24)
        };
        // The alignment this control's own value is drawn with. The placeholder shares it, so the
        // field reads as one column whichever state it is in.
        let value_align = self.alignment.to_horizontal().unwrap_or(HorizontalAlignment::Left);

        // The value's own origin and advance, shared by the selection band and the caret. The
        // placeholder branch below keeps its own centring; only real text has a caret.
        let value_advance = if self.text.is_empty() {
            0
        } else {
            context.measure_text(&self.text, font).width as i32
        };
        let value_box = Rect::new(
            text_left,
            rect.y
                + (rect.height as i32
                    - context.text_line(Rect::new(0, 0, 1, rect.height), font).height as i32)
                    / 2,
            text_width.max(1),
            rect.height,
        );
        let value_origin = crate::render::fitted_origin(value_box, value_advance, value_align);

        // — Selection band —
        if !self.text.is_empty() {
            if let Some((start, end)) = self.selection_range() {
                let start_x =
                    value_origin.x + context.measure_text(&self.text[..start], font).width as i32;
                let end_x =
                    value_origin.x + context.measure_text(&self.text[..end], font).width as i32;
                let line_height = context.text_line(Rect::new(0, 0, 1, rect.height), font).height;
                context.fill_rect(
                    Rect::new(
                        start_x.min(end_x),
                        value_box.y,
                        (end_x - start_x).unsigned_abs(),
                        line_height.max(1),
                    ),
                    accent.unwrap_or(Color::rgba(52, 120, 246, 255)).with_alpha(90),
                );
            }
        }

        if self.text.is_empty() {
            // Draw placeholder text. Same centring fix as the typed branch below: `ascent` is
            // inside the line box, so adding it to an already-centred y put the hint half a line
            // low, and the fixed grey could not read on a themed field.
            let metrics = context.measure_text(&self.placeholder, font);
            if text_width >= metrics.width {
                let text_y = rect.y + (rect.height as i32 - metrics.height as i32) / 2;
                context.draw_text_fitted_cow(
                    Rect::new(text_left, text_y, text_width.max(1), metrics.height.max(1)),
                    &self.placeholder,
                    font,
                    field_ink.blend(&field_color, 0.45),
                    value_align,
                );
            }
        } else {
            // Draw text
            let metrics = context.measure_text(&self.text, font);
            if text_width >= metrics.width {
                // Two defects in one origin. It added `ascent` on top of an already-centred y —
                // the renderer's origin is the glyph's **top** edge, so the label sat half a
                // line low — and it used a fixed `40,40,40`, which is a light field's ink and
                // rendered at 1.5:1 once the field's background resolved to a dark surface.
                let text_y = rect.y + (rect.height as i32 - metrics.height as i32) / 2;
                if self.tokenizing {
                    // Tokenized display: one rounded chip per term. The value itself stays the
                    // plain space-separated text, so submitting and querying are unchanged.
                    let mut chip_x = text_left;
                    let chip_fill = accent.unwrap_or(Color::rgba(52, 120, 246, 255)).with_alpha(40);
                    let tokens = self.tokens_cached();
                    for token in tokens.iter() {
                        let advance = context.measure_text(token, font).width as i32;
                        let chip_width = (advance + 12).max(1) as u32;
                        if chip_x + chip_width as i32 > text_left + text_width as i32 {
                            break;
                        }
                        context.fill_rounded_rect(
                            Rect::new(chip_x, text_y - 2, chip_width, metrics.height + 4),
                            (metrics.height + 4) / 2,
                            chip_fill,
                        );
                        context.draw_text(
                            Point::new(chip_x + 6, text_y),
                            token,
                            font,
                            field_ink,
                            HorizontalAlignment::Left,
                        );
                        chip_x += chip_width as i32 + 4;
                    }
                } else {
                    context.draw_text_fitted_cow(
                        Rect::new(
                            text_left,
                            text_y,
                            (rect.width as i32 - (text_left - rect.x) - 12).max(1) as u32,
                            metrics.height.max(1),
                        ),
                        &self.text,
                        font,
                        field_ink,
                        value_align,
                    );
                }
            }

            // — Caret —
            if self.focused && self.cursor_blink.is_visible() {
                let caret_byte =
                    crate::widget::text_utils::floor_char_boundary(&self.text, self.cursor);
                let caret_x = value_origin.x
                    + context.measure_text(&self.text[..caret_byte], font).width as i32;
                let caret_x = caret_x.min(text_left + text_width as i32 - 1).max(text_left);
                context.draw_line(
                    Point::new(caret_x, value_box.y + 2),
                    Point::new(caret_x, value_box.y + value_box.height as i32 - 4),
                    field_ink,
                );
                // I-111: the IME composition is painted inline with an underline.
                if !self.preedit.is_empty() {
                    let preedit_x = caret_x + 1;
                    context.draw_text(
                        Point::new(preedit_x, value_box.y + 2),
                        &self.preedit,
                        font,
                        field_ink,
                        HorizontalAlignment::Left,
                    );
                    context.draw_line(
                        Point::new(preedit_x, value_box.y + value_box.height as i32 - 4),
                        Point::new(
                            preedit_x + context.measure_text(&self.preedit, font).width as i32,
                            value_box.y + value_box.height as i32 - 4,
                        ),
                        field_ink,
                    );
                }
            }

            // Draw clear button (X)
            let clear_x = rect.x + field_width as i32 - 22;
            let clear_y = rect.y + (rect.height as i32 - 16) / 2;
            let clear_center = Point::new(clear_x + 8, clear_y + 8);
            context.fill_circle(clear_center, 8, Color::rgba(180, 180, 180, 200));
            // X lines
            context.draw_line(
                Point::new(clear_center.x - 3, clear_center.y - 3),
                Point::new(clear_center.x + 3, clear_center.y + 3),
                Color::WHITE,
            );
            context.draw_line(
                Point::new(clear_center.x + 3, clear_center.y - 3),
                Point::new(clear_center.x - 3, clear_center.y + 3),
                Color::WHITE,
            );
        }

        // — Recent searches —
        // The menu hangs below the field, one row per remembered query. It is painted from the
        // same state the hit test reads, so a row cannot be clickable somewhere it is not drawn.
        if self.show_recent {
            let menu_top = rect.y + rect.height as i32;
            for (row_index, search) in
                self.recent_searches.iter().skip(self.recent_scroll).enumerate()
            {
                let row = Rect::new(
                    rect.x,
                    menu_top + (row_index as i32) * RECENT_ROW_HEIGHT as i32,
                    rect.width,
                    RECENT_ROW_HEIGHT,
                );
                context.fill_rect(row, field_color);
                context.draw_rect(
                    row,
                    style.border_color.or(themed_bg).unwrap_or(Color::rgba(200, 200, 205, 200)),
                );
                context.draw_text(
                    Point::new(row.x + 10, row.y + 4),
                    search,
                    font,
                    field_ink,
                    HorizontalAlignment::Left,
                );
            }
        }
    }
}

impl SearchBar {
    /// The byte offset a horizontal pixel points at, using the same origin the paint uses.
    fn byte_index_at_x(&self, x: i32) -> usize {
        if self.text.is_empty() {
            return 0;
        }
        let rect = self.geometry();
        let cancel_width = if self.is_active && self.cancel_button_visible {
            self.cancel_button_width()
        } else {
            0
        };
        let field_width = rect.width.saturating_sub(cancel_width + 4);
        let text_left = rect.x + 10 + 14 + 6;
        let text_width = field_width.saturating_sub((text_left - rect.x) as u32);
        let font = effective_font(self.style());
        let mut measurement =
            crate::render::SoftwarePaintBackend::new(crate::core::Size::new(0, 0), 1.0);
        let context = RenderContext::new(&mut measurement);
        let advance = context.measure_text(&self.text, font).width as i32;
        let value_box = Rect::new(text_left, rect.y, text_width.max(1), rect.height);
        let origin = crate::render::fitted_origin(
            value_box,
            advance,
            self.alignment.to_horizontal().unwrap_or(HorizontalAlignment::Left),
        );
        let dx = (x - origin.x).max(0);
        // Accumulate the real per-glyph advances; the old proportional mapping put the caret on
        // the wrong glyph for any non-monospaced face (I-P63).
        let mut best = 0usize;
        let mut prefix = 0i32;
        let mut glyph = [0u8; 4];
        for (index, ch) in self.text.char_indices() {
            let advance = context.measure_text(ch.encode_utf8(&mut glyph), font).width as i32;
            if dx < prefix + advance / 2 {
                break;
            }
            prefix += advance;
            best = index + ch.len_utf8();
        }
        best
    }
}

impl EventHandler for SearchBar {
    fn handle_event(&mut self, event: &Event) {
        if !self.base.is_enabled() {
            return;
        }
        match event {
            Event::MousePress { pos, button, .. } => {
                if *button == 1 {
                    // A click on the recent-search menu picks that query; the menu hangs below
                    // the field, so this must be tested before the field hit test.
                    if let Some(row) = self.recent_row_at(*pos) {
                        let chosen = self.recent_searches[row].clone();
                        self.show_recent = false;
                        self.set_text(chosen);
                        self.submit();
                        return;
                    }
                    let rect = self.geometry();
                    let cancel_width = if self.is_active && self.cancel_button_visible {
                        self.cancel_button_width()
                    } else {
                        0
                    };
                    let cancel_x = rect.x + rect.width as i32 - cancel_width as i32;

                    // Cancel button click
                    if self.is_active && self.cancel_button_visible && pos.x >= cancel_x {
                        self.is_active = false;
                        self.set_focused(false);
                        self.canceled.emit();
                        self.base.request_redraw();
                        return;
                    }

                    // Clear button click (when text is non-empty)
                    let field_width = rect.width.saturating_sub(cancel_width + 4);
                    let clear_x = rect.x + field_width as i32 - 22;
                    if !self.text.is_empty()
                        && pos.x >= clear_x
                        && pos.x < clear_x + 16
                        && pos.y >= rect.y
                        && pos.y < rect.y + rect.height as i32
                    {
                        self.clear();
                        return;
                    }

                    // Focus the search bar and place the caret where the press landed.
                    if !self.is_active {
                        self.set_active(true);
                    }
                    self.set_focused(true);
                    self.cursor = self.byte_index_at_x(pos.x);
                    self.selection_anchor = None;
                    // An empty field shows its history; a field with a query keeps it closed so
                    // the menu never covers the user's typing.
                    self.show_recent = self.text.is_empty() && !self.recent_searches.is_empty();
                    self.base.request_redraw();
                }
            }
            Event::FocusGained { .. } => {
                self.set_active(true);
                self.set_focused(true);
            }
            Event::FocusLost => self.set_focused(false),
            Event::KeyPress { key, modifiers } => {
                if !self.focused {
                    return;
                }
                // Undo and redo are the platform primary chord: Control+Z/Y on
                // Windows and Linux, Command+Z/Y on macOS. A shifted chord is a
                // different binding and stays unbound here.
                if primary_shortcut_pressed(*key, *modifiers, Key::Z) {
                    let _ = self.undo();
                    return;
                }
                if primary_shortcut_pressed(*key, *modifiers, Key::Y) {
                    let _ = self.redo();
                    return;
                }
                let mods = crate::shortcut::Modifiers::from_event_bits(*modifiers);
                let shift = mods.contains(crate::shortcut::Modifiers::SHIFT);
                if *key == key_codes::ENTER || *key == key_codes::LINE_FEED {
                    // Enter submits; if the recent menu is open on the keyboard, Enter picks its
                    // first row instead of submitting an empty query.
                    if self.show_recent {
                        if let Some(first) = self.recent_searches.first().cloned() {
                            self.show_recent = false;
                            self.set_text(first);
                        }
                    }
                    self.submit();
                } else if *key == key_codes::ESCAPE {
                    // Escape cancels: the field leaves its active state, drops the query and
                    // tells the host, so a search that was opened by mistake can be dismissed
                    // without a pointer.
                    self.clear();
                    self.is_active = false;
                    self.show_recent = false;
                    self.set_focused(false);
                    self.canceled.emit();
                    self.base.request_redraw();
                } else if *key == key_codes::BACKSPACE {
                    self.backspace();
                } else if *key == 127 {
                    // Delete removes the character after the caret.
                    let (start, end) = self.selection_range().unwrap_or_else(|| {
                        (
                            self.cursor,
                            crate::widget::text_utils::next_char_boundary(&self.text, self.cursor),
                        )
                    });
                    if start != end {
                        let mut next = self.text.clone();
                        next.replace_range(start..end, "");
                        self.set_text(next);
                        self.cursor = start;
                        self.selection_anchor = None;
                    }
                } else if *key == 37 {
                    self.move_caret(-1, shift);
                } else if *key == 39 {
                    self.move_caret(1, shift);
                } else if *key == 36 {
                    self.move_caret(-(self.text.chars().count() as i32), shift);
                } else if *key == 35 {
                    self.move_caret(self.text.chars().count() as i32, shift);
                } else if *key == 40 && self.text.is_empty() {
                    self.show_recent = !self.recent_searches.is_empty();
                    self.base.request_redraw();
                } else if *key >= 32 && *key <= 126 {
                    // Printable ASCII.
                    if let Some(c) = char::from_u32(*key) {
                        self.insert_text(&c.to_string());
                    }
                }
            }
            Event::TextInput { text } | Event::ImeCommit { text } => {
                if self.focused && !text.is_empty() {
                    self.preedit.clear();
                    self.insert_text(text);
                }
            }
            Event::Wheel { delta, modifiers, .. } => {
                // I-111: the history layer scrolls with the wheel while it is open.
                if self.show_recent && !self.recent_searches.is_empty() {
                    let scroll =
                        crate::event::wheel::WheelScroll::from_event(*delta, *modifiers, 1);
                    let max = self.recent_searches.len().saturating_sub(1);
                    let next = (self.recent_scroll as i64 - scroll.lines_y as i64)
                        .clamp(0, max as i64) as usize;
                    if next != self.recent_scroll {
                        self.recent_scroll = next;
                        self.base.request_redraw();
                    }
                }
            }
            Event::ImePreedit { text, .. } => {
                // I-111: show the composition string inline without committing it.
                self.preedit = text.clone();
                self.base.request_redraw();
            }
            _ => {
                self.base.handle_event(event);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::Point;
    use crate::widget::input_widgets::shortcut_editor::primary_modifier_event_bits;
    use crate::widget::svg::render_to_svg;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Mutex};

    fn make_search_bar() -> SearchBar {
        SearchBar::new(Rect::new(0, 0, 300, 40))
    }

    #[test]
    fn search_bar_default_state() {
        let sb = make_search_bar();
        assert_eq!(sb.text(), "");
        assert_eq!(sb.placeholder(), "Search");
        assert!(!sb.is_active());
        assert!(sb.cancel_button_visible());
        assert_eq!(sb.kind(), WidgetKind::SearchBar);
    }

    #[test]
    fn search_bar_set_text() {
        let mut sb = make_search_bar();
        sb.set_text("hello");
        assert_eq!(sb.text(), "hello");
    }

    #[test]
    fn search_bar_text_changed_signal() {
        let mut sb = make_search_bar();
        let captured = Arc::new(Mutex::new(None));
        sb.text_changed.connect({
            let captured = Arc::clone(&captured);
            move |val: Arc<String>| {
                *captured.lock().unwrap() = Some((*val).clone());
            }
        });

        sb.set_text("test");
        assert_eq!(captured.lock().unwrap().as_deref(), Some("test"));
    }

    #[test]
    fn search_bar_submit_signal() {
        let mut sb = make_search_bar();
        sb.set_text("query");
        let captured = Arc::new(Mutex::new(None));
        sb.search_submitted.connect({
            let captured = Arc::clone(&captured);
            move |val: Arc<String>| {
                *captured.lock().unwrap() = Some((*val).clone());
            }
        });

        // Simulate Enter key press
        sb.handle_event(&Event::FocusGained { reason: crate::event::FocusReason::Tab });
        sb.handle_event(&Event::KeyPress { key: 13, modifiers: 0 });
        assert_eq!(captured.lock().unwrap().as_deref(), Some("query"));
    }

    #[test]
    fn search_bar_clear_button_and_signal() {
        let mut sb = make_search_bar();
        sb.set_text("hello");
        let captured = Arc::new(Mutex::new(None));
        sb.text_changed.connect({
            let captured = Arc::clone(&captured);
            move |val: Arc<String>| {
                *captured.lock().unwrap() = Some((*val).clone());
            }
        });

        sb.clear();
        assert_eq!(sb.text(), "");
        assert_eq!(captured.lock().unwrap().as_deref(), Some(""));
    }

    #[test]
    fn search_bar_cancel_signal() {
        let mut sb = make_search_bar();
        sb.set_active(true);
        let captured = Arc::new(AtomicBool::new(false));
        sb.canceled.connect({
            let captured = Arc::clone(&captured);
            move || {
                captured.store(true, Ordering::SeqCst);
            }
        });

        // Click cancel button (at x=240+, since width=300, cancel starts at 300-60=240)
        sb.handle_event(&Event::MousePress { pos: Point::new(260, 20), button: 1, modifiers: 0 });
        assert!(!sb.is_active());
        assert!(captured.load(Ordering::SeqCst));
    }

    #[test]
    fn search_bar_set_placeholder() {
        let mut sb = make_search_bar();
        sb.set_placeholder("Find...");
        assert_eq!(sb.placeholder(), "Find...");
    }

    #[test]
    fn search_bar_svg_output() {
        let mut sb = make_search_bar();
        sb.set_text("foo");
        let svg = render_to_svg(&mut sb);
        assert!(svg.starts_with("<svg"));
        assert!(svg.ends_with("</svg>"));
    }

    #[test]
    fn search_bar_undo_redo_restores_text() {
        let mut sb = make_search_bar();
        sb.set_text("alpha");
        sb.set_text("beta");

        assert!(sb.can_undo());
        assert!(sb.undo());
        assert_eq!(sb.text(), "alpha");
        assert!(sb.can_redo());
        assert!(sb.redo());
        assert_eq!(sb.text(), "beta");
    }

    #[test]
    fn search_bar_key_shortcuts_drive_history() {
        let mut sb = make_search_bar();
        sb.handle_event(&Event::FocusGained { reason: crate::event::FocusReason::Tab });
        sb.handle_event(&Event::KeyPress { key: 97, modifiers: 0 });
        sb.handle_event(&Event::KeyPress { key: 98, modifiers: 0 });
        assert_eq!(sb.text(), "ab");

        let primary = primary_modifier_event_bits();
        sb.handle_event(&Event::KeyPress { key: 90, modifiers: primary });
        assert_eq!(sb.text(), "a");
        sb.handle_event(&Event::KeyPress { key: 89, modifiers: primary });
        assert_eq!(sb.text(), "ab");
    }

    #[test]
    fn search_bar_accepts_unicode_text_input_events() {
        let mut sb = make_search_bar();
        sb.handle_event(&Event::FocusGained { reason: crate::event::FocusReason::Tab });
        sb.handle_event(&Event::text_input("你好"));
        sb.handle_event(&Event::ime_commit("世界"));

        assert_eq!(sb.text(), "你好世界");
        assert!(sb.undo());
        assert_eq!(sb.text(), "你好");
    }

    /// Keystrokes only edit the bar that owns focus.
    #[test]
    fn keys_are_gated_on_focus() {
        let mut sb = make_search_bar();
        sb.handle_event(&Event::KeyPress { key: 97, modifiers: 0 });
        assert_eq!(sb.text(), "", "an unfocused bar must ignore the keyboard");

        sb.handle_event(&Event::FocusGained { reason: crate::event::FocusReason::Tab });
        sb.handle_event(&Event::KeyPress { key: 97, modifiers: 0 });
        assert_eq!(sb.text(), "a");
        sb.handle_event(&Event::FocusLost);
        sb.handle_event(&Event::KeyPress { key: 98, modifiers: 0 });
        assert_eq!(sb.text(), "a", "losing focus stops editing");
    }

    /// Escape is a full cancel: clear, deactivate and notify.
    #[test]
    fn escape_cancels_the_search() {
        let mut sb = make_search_bar();
        sb.set_active(true);
        sb.handle_event(&Event::FocusGained { reason: crate::event::FocusReason::Tab });
        sb.handle_event(&Event::KeyPress { key: 97, modifiers: 0 });
        let canceled = Arc::new(AtomicBool::new(false));
        sb.canceled.connect({
            let canceled = Arc::clone(&canceled);
            move || canceled.store(true, std::sync::atomic::Ordering::SeqCst)
        });

        sb.handle_event(&Event::KeyPress { key: 27, modifiers: 0 });
        assert_eq!(sb.text(), "");
        assert!(!sb.is_active());
        assert!(!sb.is_focused());
        assert!(canceled.load(std::sync::atomic::Ordering::SeqCst));
    }

    /// Submitted queries become recent searches, which can be chosen from the menu.
    #[test]
    fn submitted_queries_fill_the_recent_menu() {
        let mut sb = make_search_bar();
        sb.handle_event(&Event::FocusGained { reason: crate::event::FocusReason::Tab });
        sb.set_text("rust");
        sb.handle_event(&Event::KeyPress { key: 13, modifiers: 0 });
        sb.set_text("widgets");
        sb.handle_event(&Event::KeyPress { key: 13, modifiers: 0 });

        assert_eq!(
            sb.recent_searches(),
            vec!["widgets".to_string(), "rust".to_string()].as_slice()
        );
        assert_eq!(sb.recent_searches().len(), 2);

        // Clicking the first menu row selects that query.
        let menu_row = Point::new(20, sb.geometry().y + sb.geometry().height as i32 + 4);
        sb.set_recent_menu_open(true);
        sb.handle_event(&Event::MousePress { pos: menu_row, button: 1, modifiers: 0 });
        assert_eq!(sb.text(), "widgets");
        assert!(!sb.is_recent_menu_open());
        assert!(sb.recent_searches().len() <= MAX_RECENT_SEARCHES);
    }

    /// The caret edits inside the query, and tokenizing only changes the paint.
    #[test]
    fn the_caret_edits_inside_the_query_and_tokens_split_it() {
        let mut sb = make_search_bar();
        sb.handle_event(&Event::FocusGained { reason: crate::event::FocusReason::Tab });
        sb.set_text("helo");
        sb.handle_event(&Event::KeyPress { key: 37, modifiers: 0 });
        sb.handle_event(&Event::KeyPress { key: 108, modifiers: 0 });
        assert_eq!(sb.text(), "hello");

        sb.set_text("alpha beta");
        sb.set_tokenizing(true);
        assert_eq!(sb.tokens(), vec!["alpha".to_string(), "beta".to_string()]);
        assert!(sb.tokens().len() == 2, "tokenizing splits the value into its terms");
        assert_eq!(sb.text(), "alpha beta", "tokenizing does not alter the value");
    }
}