fyrox_ui/
text_box.rs

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
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
//! TextBox is a text widget that allows you to edit text and create specialized input fields. See [`TextBox`] docs for more
//! info and usage examples.

#![warn(missing_docs)]

use crate::{
    brush::Brush,
    core::{
        algebra::{Point2, Vector2},
        color::Color,
        math::Rect,
        parking_lot::Mutex,
        pool::Handle,
        reflect::prelude::*,
        type_traits::prelude::*,
        uuid_provider,
        variable::InheritableVariable,
        visitor::prelude::*,
    },
    define_constructor,
    draw::{CommandTexture, Draw, DrawingContext},
    font::FontResource,
    formatted_text::{FormattedText, FormattedTextBuilder, WrapMode},
    message::{CursorIcon, KeyCode, MessageDirection, MouseButton, UiMessage},
    text::TextMessage,
    widget::{Widget, WidgetBuilder, WidgetMessage},
    BuildContext, Control, HorizontalAlignment, UiNode, UserInterface, VerticalAlignment,
    BRUSH_DARKER, BRUSH_TEXT,
};
use copypasta::ClipboardProvider;
use std::{
    cell::RefCell,
    fmt::{Debug, Formatter},
    ops::{Deref, DerefMut},
    sync::Arc,
};
use strum_macros::{AsRefStr, EnumString, VariantNames};

/// A message that could be used to alternate text box widget's state or receive changes from it.
///
/// # Important notes
///
/// Text box widget also supports [`TextMessage`] and [`WidgetMessage`].
#[derive(Debug, Clone, PartialEq)]
pub enum TextBoxMessage {
    /// Used to change selection brush of a text box. Use [TextBoxMessage::selection_brush`] to create the message.
    SelectionBrush(Brush),
    /// Used to change caret brush of a text box. Use [TextBoxMessage::caret_brush`] to create the message.
    CaretBrush(Brush),
    /// Used to change text commit mode of a text box. Use [TextBoxMessage::text_commit_mode`] to create the message.
    TextCommitMode(TextCommitMode),
    /// Used to enable or disable multiline mode of a text box. Use [TextBoxMessage::multiline`] to create the message.
    Multiline(bool),
    /// Used to enable or disable an ability to edit text box content. Use [TextBoxMessage::editable`] to create the message.
    Editable(bool),
}

impl TextBoxMessage {
    define_constructor!(
        /// Creates [`TextBoxMessage::SelectionBrush`].
        TextBoxMessage:SelectionBrush => fn selection_brush(Brush), layout: false
    );
    define_constructor!(
        /// Creates [`TextBoxMessage::CaretBrush`].
        TextBoxMessage:CaretBrush => fn caret_brush(Brush), layout: false
    );
    define_constructor!(
        /// Creates [`TextBoxMessage::TextCommitMode`].
        TextBoxMessage:TextCommitMode => fn text_commit_mode(TextCommitMode), layout: false
    );
    define_constructor!(
        /// Creates [`TextBoxMessage::Multiline`].
        TextBoxMessage:Multiline => fn multiline(bool), layout: false
    );
    define_constructor!(
        /// Creates [`TextBoxMessage::Editable`].
        TextBoxMessage:Editable => fn editable(bool), layout: false
    );
}

/// Specifies a direction on horizontal axis.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum HorizontalDirection {
    /// Left direction.
    Left,
    /// Right direction.
    Right,
}

/// Specifies a direction on vertical axis.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum VerticalDirection {
    /// Down direction.
    Down,
    /// Up direction.
    Up,
}

pub use crate::formatted_text::Position;

/// Defines the way, how the text box widget will commit the text that was typed in
#[derive(
    Copy,
    Clone,
    PartialOrd,
    PartialEq,
    Eq,
    Ord,
    Hash,
    Debug,
    Default,
    Visit,
    Reflect,
    AsRefStr,
    EnumString,
    VariantNames,
    TypeUuidProvider,
)]
#[repr(u32)]
#[type_uuid(id = "5fb7d6f0-c151-4a30-8350-2060749d74c6")]
pub enum TextCommitMode {
    /// Text box will immediately send [`TextMessage::Text`] message after any change (after any pressed button).
    Immediate = 0,

    /// Text box will send Text message only when it loses focus (when a user "clicks" outside of it or with any other
    /// event that forces the text box to lose focus).
    LostFocus = 1,

    /// Text box will send Text message when it loses focus or if Enter key was pressed. This is **default** behavior.
    ///
    /// # Notes
    ///
    /// In case of multiline text box hitting Enter key won't commit the text!
    #[default]
    LostFocusPlusEnter = 2,

    /// Text box will send Text message when it loses focus or if Enter key was pressed, but **only** if the content
    /// of the text box changed since the last time it gained focus or the text was committed.
    ///
    /// # Notes
    ///
    /// In case of multiline text box hitting Enter key won't commit the text!
    Changed = 3,
}

/// Defines a set of two positions in the text, that forms a specific range.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Visit, Reflect, Default, TypeUuidProvider)]
#[type_uuid(id = "04c8101b-cb34-47a5-af34-ecfb9b2fc426")]
pub struct SelectionRange {
    /// Position of the beginning.
    pub begin: Position,
    /// Position of the end.
    pub end: Position,
}

impl SelectionRange {
    /// Creates a new range, that have its begin always before end. It could be useful in case if user
    /// selects a range right-to-left.
    #[must_use = "method creates new value which must be used"]
    pub fn normalized(&self) -> SelectionRange {
        SelectionRange {
            begin: self.left(),
            end: self.right(),
        }
    }
    /// Creates a Range iterator of the positions in this range from left to right, excluding the rightmost position.
    pub fn range(&self) -> std::ops::Range<Position> {
        std::ops::Range {
            start: self.left(),
            end: self.right(),
        }
    }
    /// `true` if the given position is inside this range, including the beginning and end.
    pub fn contains(&self, position: Position) -> bool {
        (self.begin..=self.end).contains(&position)
    }
    /// The leftmost position.
    pub fn left(&self) -> Position {
        Position::min(self.begin, self.end)
    }
    /// The rightmost position.
    pub fn right(&self) -> Position {
        Position::max(self.begin, self.end)
    }
}

/// Defines a function, that could be used to filter out desired characters. It must return `true` for characters, that pass
/// the filter, and `false` - otherwise.
pub type FilterCallback = dyn FnMut(char) -> bool + Send;

/// TextBox is a text widget that allows you to edit text and create specialized input fields. It has various options like
/// word wrapping, text alignment, and so on.
///
/// ## How to create
///
/// An instance of the TextBox widget could be created like so:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// #     core::pool::Handle,
/// #     text_box::TextBoxBuilder, widget::WidgetBuilder, UiNode, UserInterface
/// # };
/// fn create_text_box(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
///     TextBoxBuilder::new(WidgetBuilder::new())
///         .with_text(text)
///         .build(&mut ui.build_ctx())
/// }
/// ```
///
/// ## Text alignment and word wrapping
///
/// There are various text alignment options for both vertical and horizontal axes. Typical alignment values are:
/// [`HorizontalAlignment::Left`], [`HorizontalAlignment::Center`], [`HorizontalAlignment::Right`] for horizontal axis,
/// and [`VerticalAlignment::Top`], [`VerticalAlignment::Center`], [`VerticalAlignment::Bottom`] for vertical axis.
/// An instance of centered text could be created like so:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// #     core::pool::Handle,
/// #     text_box::TextBoxBuilder, widget::WidgetBuilder, HorizontalAlignment, UiNode, UserInterface,
/// #     VerticalAlignment,
/// # };
/// fn create_centered_text(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
///     TextBoxBuilder::new(WidgetBuilder::new())
///         .with_horizontal_text_alignment(HorizontalAlignment::Center)
///         .with_vertical_text_alignment(VerticalAlignment::Center)
///     .with_text(text)
///     .build(&mut ui.build_ctx())
/// }
/// ```
///
/// Long text is usually needs to wrap on available bounds, there are three possible options for word wrapping:
/// [`WrapMode::NoWrap`], [`WrapMode::Letter`], [`WrapMode::Word`]. An instance of text with word-based wrapping could be
/// created like so:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// #     core::pool::Handle,
/// #     formatted_text::WrapMode, text_box::TextBoxBuilder, widget::WidgetBuilder, UiNode,
/// #     UserInterface,
/// # };
/// fn create_text_with_word_wrap(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
///     TextBoxBuilder::new(WidgetBuilder::new())
///         .with_wrap(WrapMode::Word)
///         .with_text(text)
///         .build(&mut ui.build_ctx())
/// }
/// ```
///
/// ## Fonts and colors
///
/// To set a color of the text just use [`WidgetBuilder::with_foreground`] while building the text instance:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// #     core::{color::Color, pool::Handle},
/// #     brush::Brush, text_box::TextBoxBuilder, widget::WidgetBuilder, UiNode, UserInterface
/// # };
/// fn create_text(ui: &mut UserInterface, text: &str) -> Handle<UiNode> {
///     //                  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
///     TextBoxBuilder::new(WidgetBuilder::new().with_foreground(Brush::Solid(Color::RED)))
///         .with_text(text)
///         .build(&mut ui.build_ctx())
/// }
/// ```
///
/// By default, text is created with default font, however it is possible to set any custom font:
///
/// ```rust,no_run
/// # use fyrox_resource::manager::ResourceManager;
/// # use fyrox_ui::{
/// #     core::{futures::executor::block_on, pool::Handle},
/// #     text_box::TextBoxBuilder,
/// #     font::{Font},
/// #     widget::WidgetBuilder,
/// #     UiNode, UserInterface,
/// # };
///
/// fn create_text(ui: &mut UserInterface, resource_manager: &ResourceManager, text: &str) -> Handle<UiNode> {
///     TextBoxBuilder::new(WidgetBuilder::new())
///         .with_font(resource_manager.request::<Font>("path/to/your/font.ttf"))
///         .with_text(text)
///         .with_font_size(20.0)
///         .build(&mut ui.build_ctx())
/// }
/// ```
///
/// Please refer to [`FontResource`] to learn more about fonts.
///
/// ### Font size
///
/// Use [`TextBoxBuilder::with_font_size`] or send [`TextMessage::font_size`] to your TextBox widget instance
/// to set the font size of it.
///
/// ## Messages
///
/// TextBox widget accepts the following list of messages:
///
/// - [`TextBoxMessage::SelectionBrush`] - change the brush that is used to highlight selection.
/// - [`TextBoxMessage::CaretBrush`] - changes the brush of the caret (small blinking vertical line).
/// - [`TextBoxMessage::TextCommitMode`] - changes the [text commit mode](TextBox#text-commit-mode).
/// - [`TextBoxMessage::Multiline`] - makes the TextBox either multiline (`true`) or single line (`false`)
/// - [`TextBoxMessage::Editable`] - enables or disables editing of the text.
///
/// **Important:** Please keep in mind, that TextBox widget also accepts [`TextMessage`]s. An example of changing text at
/// runtime could be something like this:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// #     core::pool::Handle,
/// #     message::{MessageDirection},
/// #     UiNode, UserInterface,
/// #     text::TextMessage
/// # };
/// fn request_change_text(ui: &UserInterface, text_box_widget_handle: Handle<UiNode>, text: &str) {
///     ui.send_message(TextMessage::text(
///         text_box_widget_handle,
///         MessageDirection::ToWidget,
///         text.to_owned(),
///     ))
/// }
/// ```
///
/// Please keep in mind, that like any other situation when you "changing" something via messages, you should remember
/// that the change is **not** immediate. The change will be applied on `ui.poll_message(..)` call somewhere in your
/// code.
///
/// ## Shortcuts
///
/// There are number of default shortcuts that can be used to speed up text editing:
///
/// - `Ctrl+A` - select all
/// - `Ctrl+C` - copy selected text
/// - `Ctrl+V` - paste text from clipboard
/// - `Ctrl+Home` - move caret to the beginning of the text
/// - `Ctrl+End` - move caret to the beginning of the text
/// - `Shift+Home` - select everything from current caret position until the beginning of current line
/// - `Shift+End` - select everything from current caret position until the end of current line
/// - `Arrows` - move caret accordingly
/// - `Delete` - deletes next character
/// - `Backspace` - deletes previous character
/// - `Enter` - new line (if multiline mode is set) or `commit` message
///
/// ## Multiline Text Box
///
/// By default, text box will not add new line character to the text if you press `Enter` on keyboard. To enable this
/// functionality use [`TextBoxBuilder::with_multiline`]
///
/// ## Read-only Mode
///
/// You can enable or disable content editing by using read-only mode. Use [`TextBoxBuilder::with_editable`] at build stage.
///
/// ## Mask Character
///
/// You can specify replacement character for every other characters, this is useful option for password fields. Use
/// [`TextBoxBuilder::with_mask_char`] at build stage. For example, you can set replacement character to asterisk `*` using
/// `.with_mask_char(Some('*'))`
///
/// ## Text Commit Mode
///
/// In many situations you don't need the text box to send `new text` message every new character, you either want this
/// message if `Enter` key is pressed or TextBox has lost keyboard focus (or both). There is [`TextBoxBuilder::with_text_commit_mode`]
/// on builder specifically for that purpose. Use one of the following modes:
///
/// - [`TextCommitMode::Immediate`] - text box will immediately send [`TextMessage::Text`] message after any change.
/// - [`TextCommitMode::LostFocus`] - text box will send [`TextMessage::Text`] message only when it loses focus.
/// - [`TextCommitMode::LostFocusPlusEnter`] - text box will send [`TextMessage::Text`] message when it loses focus or if Enter
/// key was pressed. This is **default** behavior. In case of multiline text box hitting Enter key won't commit text!
///
/// ## Filtering
///
/// It is possible specify custom input filter, it can be useful if you're creating special input fields like numerical or
/// phone number. A filter can be specified at build stage like so:
///
/// ```rust,no_run
/// # use fyrox_ui::{
/// #     core::pool::Handle,
/// #     text_box::TextBoxBuilder, widget::WidgetBuilder, UiNode, UserInterface
/// # };
/// # use std::sync::Arc;
/// # use fyrox_core::parking_lot::Mutex;
/// fn create_text_box(ui: &mut UserInterface) -> Handle<UiNode> {
///     TextBoxBuilder::new(WidgetBuilder::new())
///         // Specify a filter that will pass only digits.
///         .with_filter(Arc::new(Mutex::new(|c: char| c.is_ascii_digit())))
///         .build(&mut ui.build_ctx())
/// }
/// ```
///
/// ## Style
///
/// You can change brush of caret by using [`TextBoxBuilder::with_caret_brush`] and also selection brush by using
/// [`TextBoxBuilder::with_selection_brush`], it could be useful if you don't like default colors.
#[derive(Default, Clone, Visit, Reflect, ComponentProvider)]
pub struct TextBox {
    /// Base widget of the text box.
    pub widget: Widget,
    /// Current position of the caret in the text box.
    pub caret_position: InheritableVariable<Position>,
    /// Whether the caret is visible or not.
    pub caret_visible: InheritableVariable<bool>,
    /// Internal blinking timer.
    pub blink_timer: InheritableVariable<f32>,
    /// Blinking interval in seconds.
    pub blink_interval: InheritableVariable<f32>,
    /// Formatted text that stores actual text and performs its layout. See [`FormattedText`] docs for more info.
    pub formatted_text: RefCell<FormattedText>,
    /// Current selection range.
    pub selection_range: InheritableVariable<Option<SelectionRange>>,
    /// `true` if the text box is in selection mode.
    pub selecting: bool,
    /// Stores the location of the caret before it was moved by mouse click.
    #[visit(skip)]
    pub before_click_position: Position,
    /// `true` if the text box is focused.
    pub has_focus: bool,
    /// Current caret brush of the text box.
    pub caret_brush: InheritableVariable<Brush>,
    /// Current selection brush of the text box.
    pub selection_brush: InheritableVariable<Brush>,
    /// Current character filter of the text box.
    #[visit(skip)]
    #[reflect(hidden)]
    pub filter: Option<Arc<Mutex<FilterCallback>>>,
    /// Current text commit mode of the text box.
    pub commit_mode: InheritableVariable<TextCommitMode>,
    /// `true` if the the multiline mode is active.
    pub multiline: InheritableVariable<bool>,
    /// `true` if the text box is editable.
    pub editable: InheritableVariable<bool>,
    /// Position of the local "camera" (viewing rectangle) of the text box.
    pub view_position: InheritableVariable<Vector2<f32>>,
    /// A list of custom characters that will be treated as whitespace.
    pub skip_chars: InheritableVariable<Vec<char>>,
    /// Stored copy of most recent commit, when `commit_mode` is [TextCommitMode::Changed].
    #[visit(skip)]
    #[reflect(hidden)]
    pub recent: Vec<char>,
}

impl Debug for TextBox {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str("TextBox")
    }
}

crate::define_widget_deref!(TextBox);

impl TextBox {
    fn commit_if_changed(&mut self, ui: &mut UserInterface) {
        let formatted_text = self.formatted_text.borrow();
        let raw = formatted_text.get_raw_text();
        if self.recent != raw {
            self.recent.clear();
            self.recent.extend(raw);
            ui.send_message(TextMessage::text(
                self.handle,
                MessageDirection::FromWidget,
                formatted_text.text(),
            ));
        }
    }
    fn filter_paste_str_multiline(&self, str: &str) -> String {
        let mut str = str.replace("\r\n", "\n");
        str.retain(|c| c == '\n' || !c.is_control());
        if let Some(filter) = self.filter.as_ref() {
            let filter = &mut *filter.lock();
            str.retain(filter);
        }
        str
    }

    fn filter_paste_str_single_line(&self, str: &str) -> String {
        let mut str: String = str
            .chars()
            .map(|c| if c == '\n' { ' ' } else { c })
            .filter(|c| !c.is_control())
            .collect();
        if let Some(filter) = self.filter.as_ref() {
            let filter = &mut *filter.lock();
            str.retain(filter);
        }
        str
    }

    fn reset_blink(&mut self) {
        self.caret_visible.set_value_and_mark_modified(true);
        self.blink_timer.set_value_and_mark_modified(0.0);
    }

    fn move_caret(&mut self, position: Position, select: bool) {
        let text = self.formatted_text.borrow();
        let lines = text.get_lines();
        if select && !lines.is_empty() {
            if self.selection_range.is_none() {
                self.selection_range
                    .set_value_and_mark_modified(Some(SelectionRange {
                        begin: *self.caret_position,
                        end: *self.caret_position,
                    }));
            }
        } else {
            self.selection_range.set_value_and_mark_modified(None);
        }

        if lines.is_empty() {
            drop(text);
            self.set_caret_position(Default::default());
            return;
        }

        drop(text);

        if let Some(selection_range) = self.selection_range.as_mut() {
            if select {
                if *self.caret_position != selection_range.end {
                    if !selection_range.contains(position) {
                        if position < selection_range.left() {
                            selection_range.begin = selection_range.right();
                        } else {
                            selection_range.begin = selection_range.left();
                        }
                        selection_range.end = position;
                    }
                } else {
                    selection_range.end = position;
                }
            }
        }

        self.set_caret_position(position);
        self.ensure_caret_visible();
    }

    fn move_caret_x(&mut self, offset: isize, select: bool) {
        let pos = self
            .formatted_text
            .borrow()
            .get_relative_position_x(*self.caret_position, offset);
        self.move_caret(pos, select);
    }

    fn move_caret_y(&mut self, offset: isize, select: bool) {
        let pos = self
            .formatted_text
            .borrow()
            .get_relative_position_y(*self.caret_position, offset);
        self.move_caret(pos, select);
    }

    /// Maps input [`Position`] to a linear position in character array.
    /// The index returned is the index of the character after the position, which may be
    /// out-of-bounds if thee position is at the end of the text.
    /// You should check the index before trying to use it to fetch data from inner array of characters.
    pub fn position_to_char_index_unclamped(&self, position: Position) -> Option<usize> {
        self.formatted_text
            .borrow()
            .position_to_char_index_unclamped(position)
    }

    /// Maps input [`Position`] to a linear position in character array.
    /// The index returned is usually the index of the character after the position,
    /// but if the position is at the end of a line then return the index of the character _before_ the position.
    /// In other words, the last two positions of each line are mapped to the same character index.
    /// Output index will always be valid for fetching, if the method returned `Some(index)`.
    /// The index however cannot be used for text insertion, because it cannot point to a "place after last char".
    pub fn position_to_char_index_clamped(&self, position: Position) -> Option<usize> {
        self.formatted_text
            .borrow()
            .position_to_char_index_clamped(position)
    }

    /// Maps linear character index (as in string) to its actual location in the text.
    pub fn char_index_to_position(&self, i: usize) -> Option<Position> {
        self.formatted_text.borrow().char_index_to_position(i)
    }

    /// Returns end position of the text.
    pub fn end_position(&self) -> Position {
        self.formatted_text.borrow().end_position()
    }

    /// Returns a position of a next word after the caret in the text.
    pub fn find_next_word(&self, from: Position) -> Position {
        self.position_to_char_index_unclamped(from)
            .and_then(|i| {
                self.formatted_text
                    .borrow()
                    .get_raw_text()
                    .iter()
                    .enumerate()
                    .skip(i)
                    .skip_while(|(_, c)| !(c.is_whitespace() || self.skip_chars.contains(*c)))
                    .find(|(_, c)| !(c.is_whitespace() || self.skip_chars.contains(*c)))
                    .and_then(|(n, _)| self.char_index_to_position(n))
            })
            .unwrap_or_else(|| self.end_position())
    }

    /// Returns a position of a next word before the caret in the text.
    pub fn find_prev_word(&self, from: Position) -> Position {
        self.position_to_char_index_unclamped(from)
            .and_then(|i| {
                let text = self.formatted_text.borrow();
                let len = text.get_raw_text().len();
                text.get_raw_text()
                    .iter()
                    .enumerate()
                    .rev()
                    .skip(len.saturating_sub(i))
                    .skip_while(|(_, c)| !(c.is_whitespace() || self.skip_chars.contains(*c)))
                    .find(|(_, c)| !(c.is_whitespace() || self.skip_chars.contains(*c)))
                    .and_then(|(n, _)| self.char_index_to_position(n + 1))
            })
            .unwrap_or_default()
    }

    /// Inserts given character at current caret position.
    fn insert_char(&mut self, c: char, ui: &UserInterface) {
        self.remove_before_insert();
        let position = self
            .position_to_char_index_unclamped(*self.caret_position)
            .unwrap_or_default();
        self.formatted_text
            .borrow_mut()
            .insert_char(c, position)
            .build();
        self.set_caret_position(
            self.char_index_to_position(position + 1)
                .unwrap_or_default(),
        );
        if *self.commit_mode == TextCommitMode::Immediate {
            ui.send_message(TextMessage::text(
                self.handle,
                MessageDirection::FromWidget,
                self.formatted_text.borrow().text(),
            ));
        }
    }

    fn insert_str(&mut self, str: &str, ui: &UserInterface) {
        if str.is_empty() {
            return;
        }
        let str: String = if *self.multiline {
            self.filter_paste_str_multiline(str)
        } else {
            self.filter_paste_str_single_line(str)
        };
        self.remove_before_insert();
        let position = self
            .position_to_char_index_unclamped(*self.caret_position)
            .unwrap_or_default();
        let mut text = self.formatted_text.borrow_mut();
        text.insert_str(&str, position);
        text.build();
        drop(text);
        self.set_caret_position(
            self.char_index_to_position(position + str.chars().count())
                .unwrap_or_default(),
        );
        if *self.commit_mode == TextCommitMode::Immediate {
            ui.send_message(TextMessage::text(
                self.handle,
                MessageDirection::FromWidget,
                self.formatted_text.borrow().text(),
            ));
        }
    }

    fn remove_before_insert(&mut self) {
        let Some(selection) = *self.selection_range else {
            return;
        };
        let range = self
            .formatted_text
            .borrow()
            .position_range_to_char_index_range(selection.range());
        if range.is_empty() {
            return;
        }
        self.formatted_text.borrow_mut().remove_range(range);
        self.selection_range.set_value_and_mark_modified(None);
        self.set_caret_position(selection.left());
    }

    /// Returns current text length in characters.
    pub fn get_text_len(&self) -> usize {
        self.formatted_text.borrow_mut().get_raw_text().len()
    }

    /// Returns current position the caret in the local coordinates.
    pub fn caret_local_position(&self) -> Vector2<f32> {
        self.formatted_text
            .borrow_mut()
            .position_to_local(*self.caret_position)
    }

    fn point_to_view_pos(&self, position: Vector2<f32>) -> Vector2<f32> {
        position - *self.view_position
    }

    fn rect_to_view_pos(&self, mut rect: Rect<f32>) -> Rect<f32> {
        rect.position -= *self.view_position;
        rect
    }

    fn ensure_caret_visible(&mut self) {
        let local_bounds = self.bounding_rect();
        let caret_view_position = self.point_to_view_pos(self.caret_local_position());
        // Move view position to contain the caret + add some spacing.
        let spacing_step = self
            .formatted_text
            .borrow()
            .get_font()
            .state()
            .data()
            .map(|font| font.ascender(*self.height))
            .unwrap_or_default();
        let spacing = spacing_step * 3.0;
        let top_left_corner = local_bounds.left_top_corner();
        let bottom_right_corner = local_bounds.right_bottom_corner();
        if caret_view_position.x > bottom_right_corner.x {
            self.view_position.x += caret_view_position.x - bottom_right_corner.x + spacing;
        }
        if caret_view_position.x < top_left_corner.x {
            self.view_position.x -= top_left_corner.x - caret_view_position.x + spacing;
        }
        if caret_view_position.y > bottom_right_corner.y {
            self.view_position.y += bottom_right_corner.y - caret_view_position.y + spacing;
        }
        if caret_view_position.y < top_left_corner.y {
            self.view_position.y -= top_left_corner.y - caret_view_position.y + spacing;
        }
        self.view_position.x = self.view_position.x.max(0.0);
        self.view_position.y = self.view_position.y.max(0.0);
    }

    fn remove_char(&mut self, direction: HorizontalDirection, ui: &UserInterface) {
        if let Some(selection) = *self.selection_range {
            self.remove_range(ui, selection);
            return;
        }
        let Some(position) = self.position_to_char_index_unclamped(*self.caret_position) else {
            return;
        };

        let text_len = self.get_text_len();
        if text_len != 0 {
            let position = match direction {
                HorizontalDirection::Left => {
                    if position == 0 {
                        return;
                    }
                    position - 1
                }
                HorizontalDirection::Right => {
                    if position >= text_len {
                        return;
                    }
                    position
                }
            };

            let mut text = self.formatted_text.borrow_mut();
            text.remove_at(position);
            text.build();
            drop(text);

            if *self.commit_mode == TextCommitMode::Immediate {
                ui.send_message(TextMessage::text(
                    self.handle(),
                    MessageDirection::FromWidget,
                    self.formatted_text.borrow().text(),
                ));
            }

            self.set_caret_position(self.char_index_to_position(position).unwrap_or_default());
        }
    }

    fn remove_range(&mut self, ui: &UserInterface, selection: SelectionRange) {
        let range = self
            .formatted_text
            .borrow()
            .position_range_to_char_index_range(selection.range());
        if range.is_empty() {
            return;
        }
        self.formatted_text.borrow_mut().remove_range(range);
        self.formatted_text.borrow_mut().build();
        self.set_caret_position(selection.left());
        self.selection_range.set_value_and_mark_modified(None);
        if *self.commit_mode == TextCommitMode::Immediate {
            ui.send_message(TextMessage::text(
                self.handle(),
                MessageDirection::FromWidget,
                self.formatted_text.borrow().text(),
            ));
        }
    }

    /// Checks whether the input position is correct (in bounds) or not.
    pub fn is_valid_position(&self, position: Position) -> bool {
        self.formatted_text
            .borrow()
            .get_lines()
            .get(position.line)
            .map_or(false, |line| position.offset < line.len())
    }

    fn set_caret_position(&mut self, position: Position) {
        self.caret_position.set_value_and_mark_modified(
            self.formatted_text
                .borrow()
                .nearest_valid_position(position),
        );
        self.ensure_caret_visible();
        self.reset_blink();
    }

    /// Tries to map screen space position to a position in the text.
    pub fn screen_pos_to_text_pos(&self, screen_point: Vector2<f32>) -> Option<Position> {
        // Transform given point into local space of the text box - this way calculations can be done
        // as usual, without a need for special math.
        let point_to_check = self
            .visual_transform
            .try_inverse()?
            .transform_point(&Point2::from(screen_point))
            .coords;

        Some(
            self.formatted_text
                .borrow_mut()
                .local_to_position(point_to_check),
        )
    }

    /// Returns current text of text box.
    pub fn text(&self) -> String {
        self.formatted_text.borrow().text()
    }

    /// Returns current word wrapping mode of text box.
    pub fn wrap_mode(&self) -> WrapMode {
        self.formatted_text.borrow().wrap_mode()
    }

    /// Returns current font of text box.
    pub fn font(&self) -> FontResource {
        self.formatted_text.borrow().get_font()
    }

    /// Returns current vertical alignment of text box.
    pub fn vertical_alignment(&self) -> VerticalAlignment {
        self.formatted_text.borrow().vertical_alignment()
    }

    /// Returns current horizontal alignment of text box.
    pub fn horizontal_alignment(&self) -> HorizontalAlignment {
        self.formatted_text.borrow().horizontal_alignment()
    }

    fn select_word(&mut self, position: Position) {
        if let Some(index) = self.position_to_char_index_clamped(position) {
            let text_ref = self.formatted_text.borrow();
            let text = text_ref.get_raw_text();
            let search_whitespace = !text[index].is_whitespace();

            let mut left_index = index;
            while left_index > 0 {
                let is_whitespace = text[left_index].is_whitespace();
                if search_whitespace && is_whitespace || !search_whitespace && !is_whitespace {
                    left_index += 1;
                    break;
                }
                left_index = left_index.saturating_sub(1);
            }

            let mut right_index = index;
            while right_index < text.len() {
                let is_whitespace = text[right_index].is_whitespace();
                if search_whitespace && is_whitespace || !search_whitespace && !is_whitespace {
                    break;
                }

                right_index += 1;
            }

            drop(text_ref);

            if let (Some(left), Some(right)) = (
                self.char_index_to_position(left_index),
                self.char_index_to_position(right_index),
            ) {
                self.selection_range
                    .set_value_and_mark_modified(Some(SelectionRange {
                        begin: left,
                        end: right,
                    }));
            }
        }
    }
}

uuid_provider!(TextBox = "536276f2-a175-4c05-a376-5a7d8bf0d10b");

impl Control for TextBox {
    fn measure_override(&self, _: &UserInterface, available_size: Vector2<f32>) -> Vector2<f32> {
        self.formatted_text
            .borrow_mut()
            .set_constraint(available_size)
            .build()
    }

    fn draw(&self, drawing_context: &mut DrawingContext) {
        let bounds = self.widget.bounding_rect();
        drawing_context.push_rect_filled(&bounds, None);
        drawing_context.commit(
            self.clip_bounds(),
            self.widget.background(),
            CommandTexture::None,
            None,
        );

        self.formatted_text
            .borrow_mut()
            .set_brush(self.widget.foreground());

        let view_bounds = self.rect_to_view_pos(bounds);
        if let Some(ref selection_range) = self.selection_range.map(|r| r.normalized()) {
            let text = self.formatted_text.borrow();
            let lines = text.get_lines();
            if selection_range.begin.line == selection_range.end.line {
                if let Some(line) = lines.get(selection_range.begin.line) {
                    // Begin line
                    let offset = text
                        .get_range_width(line.begin..(line.begin + selection_range.begin.offset));
                    let width = text.get_range_width(
                        (line.begin + selection_range.begin.offset)
                            ..(line.begin + selection_range.end.offset),
                    );
                    let selection_bounds = Rect::new(
                        view_bounds.x() + line.x_offset + offset,
                        view_bounds.y() + line.y_offset,
                        width,
                        line.height,
                    );
                    drawing_context.push_rect_filled(&selection_bounds, None);
                }
            } else {
                for (i, line) in text.get_lines().iter().enumerate() {
                    if i >= selection_range.begin.line && i <= selection_range.end.line {
                        let selection_bounds = if i == selection_range.begin.line {
                            // Begin line
                            let offset = text.get_range_width(
                                line.begin..(line.begin + selection_range.begin.offset),
                            );
                            let width = text.get_range_width(
                                (line.begin + selection_range.begin.offset)..line.end,
                            );
                            Rect::new(
                                view_bounds.x() + line.x_offset + offset,
                                view_bounds.y() + line.y_offset,
                                width,
                                line.height,
                            )
                        } else if i == selection_range.end.line {
                            // End line
                            let width = text.get_range_width(
                                line.begin..(line.begin + selection_range.end.offset),
                            );
                            Rect::new(
                                view_bounds.x() + line.x_offset,
                                view_bounds.y() + line.y_offset,
                                width,
                                line.height,
                            )
                        } else {
                            // Everything between
                            Rect::new(
                                view_bounds.x() + line.x_offset,
                                view_bounds.y() + line.y_offset,
                                line.width,
                                line.height,
                            )
                        };
                        drawing_context.push_rect_filled(&selection_bounds, None);
                    }
                }
            }
        }
        drawing_context.commit(
            self.clip_bounds(),
            (*self.selection_brush).clone(),
            CommandTexture::None,
            None,
        );

        let local_position = self.point_to_view_pos(bounds.position);
        drawing_context.draw_text(
            self.clip_bounds(),
            local_position,
            &self.formatted_text.borrow(),
        );

        if *self.caret_visible {
            let caret_pos = self.point_to_view_pos(self.caret_local_position());
            let caret_bounds = Rect::new(
                caret_pos.x,
                caret_pos.y,
                2.0,
                self.formatted_text.borrow().font_size(),
            );
            drawing_context.push_rect_filled(&caret_bounds, None);
            drawing_context.commit(
                self.clip_bounds(),
                (*self.caret_brush).clone(),
                CommandTexture::None,
                None,
            );
        }
    }

    fn update(&mut self, dt: f32, _ui: &mut UserInterface) {
        if self.has_focus {
            *self.blink_timer += dt;
            if *self.blink_timer >= *self.blink_interval {
                self.blink_timer.set_value_and_mark_modified(0.0);
                self.caret_visible
                    .set_value_and_mark_modified(!*self.caret_visible);
            }
        } else {
            self.caret_visible.set_value_and_mark_modified(false);
        }
    }

    fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
        self.widget.handle_routed_message(ui, message);

        if message.destination() == self.handle() {
            if let Some(msg) = message.data::<WidgetMessage>() {
                match msg {
                    WidgetMessage::Text(text)
                        if !ui.keyboard_modifiers().control
                            && !ui.keyboard_modifiers().alt
                            && *self.editable =>
                    {
                        for symbol in text.chars() {
                            let insert = !symbol.is_control()
                                && if let Some(filter) = self.filter.as_ref() {
                                    let filter = &mut *filter.lock();
                                    filter(symbol)
                                } else {
                                    true
                                };
                            if insert {
                                self.insert_char(symbol, ui);
                            }
                        }
                    }
                    WidgetMessage::KeyDown(code) => {
                        match code {
                            KeyCode::ArrowUp if !self.selecting => {
                                self.move_caret_y(-1, ui.keyboard_modifiers().shift);
                            }
                            KeyCode::ArrowDown if !self.selecting => {
                                self.move_caret_y(1, ui.keyboard_modifiers().shift);
                            }
                            KeyCode::ArrowRight if !self.selecting => {
                                if ui.keyboard_modifiers.control {
                                    self.move_caret(
                                        self.find_next_word(*self.caret_position),
                                        ui.keyboard_modifiers().shift,
                                    );
                                } else {
                                    self.move_caret_x(1, ui.keyboard_modifiers().shift);
                                }
                            }
                            KeyCode::ArrowLeft if !self.selecting => {
                                if ui.keyboard_modifiers.control {
                                    self.move_caret(
                                        self.find_prev_word(*self.caret_position),
                                        ui.keyboard_modifiers().shift,
                                    );
                                } else {
                                    self.move_caret_x(-1, ui.keyboard_modifiers().shift);
                                }
                            }
                            KeyCode::Delete
                                if !message.handled() && *self.editable && !self.selecting =>
                            {
                                self.remove_char(HorizontalDirection::Right, ui);
                            }
                            KeyCode::NumpadEnter | KeyCode::Enter if *self.editable => {
                                if *self.multiline {
                                    self.insert_char('\n', ui);
                                } else if *self.commit_mode == TextCommitMode::LostFocusPlusEnter {
                                    ui.send_message(TextMessage::text(
                                        self.handle,
                                        MessageDirection::FromWidget,
                                        self.text(),
                                    ));
                                } else if *self.commit_mode == TextCommitMode::Changed {
                                    self.commit_if_changed(ui);
                                }
                                // Don't set has_focus = false when enter is pressed.
                                // That messes up keyboard navigation.
                            }
                            KeyCode::Backspace if *self.editable && !self.selecting => {
                                self.remove_char(HorizontalDirection::Left, ui);
                            }
                            KeyCode::End if !self.selecting => {
                                let select = ui.keyboard_modifiers().shift;
                                let position = if ui.keyboard_modifiers().control {
                                    self.end_position()
                                } else {
                                    self.formatted_text
                                        .borrow()
                                        .get_line_range(self.caret_position.line)
                                        .end
                                };
                                self.move_caret(position, select);
                            }
                            KeyCode::Home if !self.selecting => {
                                let select = ui.keyboard_modifiers().shift;
                                let position = if ui.keyboard_modifiers().control {
                                    Position::default()
                                } else {
                                    self.formatted_text
                                        .borrow()
                                        .get_line_range(self.caret_position.line)
                                        .start
                                };
                                self.move_caret(position, select);
                            }
                            KeyCode::KeyA if ui.keyboard_modifiers().control => {
                                let end = self.end_position();
                                if end != Position::default() {
                                    self.selection_range.set_value_and_mark_modified(Some(
                                        SelectionRange {
                                            begin: Position::default(),
                                            end: self.end_position(),
                                        },
                                    ));
                                }
                            }
                            KeyCode::KeyC if ui.keyboard_modifiers().control => {
                                if let Some(mut clipboard) = ui.clipboard_mut() {
                                    if let Some(selection_range) = self.selection_range.as_ref() {
                                        let range = self
                                            .formatted_text
                                            .borrow()
                                            .position_range_to_char_index_range(
                                                selection_range.range(),
                                            );
                                        if !range.is_empty() {
                                            let _ = clipboard.set_contents(
                                                self.formatted_text.borrow().text_range(range),
                                            );
                                        }
                                    }
                                }
                            }
                            KeyCode::KeyV if ui.keyboard_modifiers().control => {
                                if let Some(mut clipboard) = ui.clipboard_mut() {
                                    if let Ok(content) = clipboard.get_contents() {
                                        self.insert_str(&content, ui);
                                    }
                                }
                            }
                            KeyCode::KeyX if ui.keyboard_modifiers().control => {
                                if let Some(mut clipboard) = ui.clipboard_mut() {
                                    if let Some(selection_range) = self.selection_range.as_ref() {
                                        let range = self
                                            .formatted_text
                                            .borrow()
                                            .position_range_to_char_index_range(
                                                selection_range.range(),
                                            );
                                        if !range.is_empty() {
                                            let _ = clipboard.set_contents(
                                                self.formatted_text.borrow().text_range(range),
                                            );
                                            self.remove_char(HorizontalDirection::Left, ui);
                                        }
                                    }
                                }
                            }
                            _ => (),
                        }

                        // TextBox "eats" all input by default, some of the keys are used for input control while
                        // others are used directly to enter text.
                        message.set_handled(true);
                    }
                    WidgetMessage::Focus => {
                        if message.direction() == MessageDirection::FromWidget {
                            self.reset_blink();
                            self.has_focus = true;
                            let end = self.end_position();
                            if end != Position::default() {
                                self.set_caret_position(end);
                                self.selection_range.set_value_and_mark_modified(Some(
                                    SelectionRange {
                                        begin: Position::default(),
                                        end,
                                    },
                                ));
                            }
                            if *self.commit_mode == TextCommitMode::Changed {
                                self.recent.clear();
                                self.recent
                                    .extend_from_slice(self.formatted_text.borrow().get_raw_text());
                            }
                        }
                    }
                    WidgetMessage::Unfocus => {
                        if message.direction() == MessageDirection::FromWidget {
                            self.selection_range.set_value_and_mark_modified(None);
                            self.has_focus = false;

                            match *self.commit_mode {
                                TextCommitMode::LostFocus | TextCommitMode::LostFocusPlusEnter => {
                                    ui.send_message(TextMessage::text(
                                        self.handle,
                                        MessageDirection::FromWidget,
                                        self.text(),
                                    ));
                                }
                                TextCommitMode::Changed => {
                                    self.commit_if_changed(ui);
                                }
                                _ => (),
                            }
                            // There is no reason to keep the stored recent value in memory
                            // while this TextBox does not have focus. Maybe this should be stored globally in UserInterface,
                            // since we only ever need one.
                            self.recent.clear();
                            self.recent.shrink_to(0);
                        }
                    }
                    WidgetMessage::MouseDown { pos, button } => {
                        if *button == MouseButton::Left {
                            let select = ui.keyboard_modifiers().shift;
                            if !select {
                                self.selection_range.set_value_and_mark_modified(None);
                            }
                            self.selecting = true;
                            self.has_focus = true;
                            self.before_click_position = *self.caret_position;

                            if let Some(position) = self.screen_pos_to_text_pos(*pos) {
                                self.move_caret(position, select);
                            }

                            ui.capture_mouse(self.handle());
                        }
                    }
                    WidgetMessage::DoubleClick {
                        button: MouseButton::Left,
                    } => {
                        if let Some(position) = self.screen_pos_to_text_pos(ui.cursor_position) {
                            if position == self.before_click_position {
                                self.select_word(position);
                            }
                        }
                    }
                    WidgetMessage::MouseMove { pos, .. } => {
                        if self.selecting {
                            if let Some(position) = self.screen_pos_to_text_pos(*pos) {
                                self.move_caret(position, true);
                            }
                        }
                    }
                    WidgetMessage::MouseUp { .. } => {
                        self.selecting = false;
                        ui.release_mouse_capture();
                    }
                    _ => {}
                }
            } else if let Some(msg) = message.data::<TextMessage>() {
                if message.direction() == MessageDirection::ToWidget {
                    let mut text = self.formatted_text.borrow_mut();

                    match msg {
                        TextMessage::Text(new_text) => {
                            fn text_equals(
                                formatted_text: &FormattedText,
                                input_string: &str,
                            ) -> bool {
                                let raw_text = formatted_text.get_raw_text();

                                if raw_text.len() != input_string.chars().count() {
                                    false
                                } else {
                                    for (raw_char, input_char) in
                                        raw_text.iter().zip(input_string.chars())
                                    {
                                        if *raw_char != input_char {
                                            return false;
                                        }
                                    }

                                    true
                                }
                            }
                            if !text_equals(&text, new_text) {
                                text.set_text(new_text);
                                drop(text);
                                self.invalidate_layout();
                                self.formatted_text.borrow_mut().build();

                                if *self.commit_mode == TextCommitMode::Immediate {
                                    ui.send_message(message.reverse());
                                }
                            }
                        }
                        TextMessage::Wrap(wrap_mode) => {
                            if text.wrap_mode() != *wrap_mode {
                                text.set_wrap(*wrap_mode);
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                        TextMessage::Font(font) => {
                            if &text.get_font() != font {
                                text.set_font(font.clone());
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                        TextMessage::VerticalAlignment(alignment) => {
                            if &text.vertical_alignment() != alignment {
                                text.set_vertical_alignment(*alignment);
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                        TextMessage::HorizontalAlignment(alignment) => {
                            if &text.horizontal_alignment() != alignment {
                                text.set_horizontal_alignment(*alignment);
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                        &TextMessage::Shadow(shadow) => {
                            if *text.shadow != shadow {
                                text.set_shadow(shadow);
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                        TextMessage::ShadowBrush(brush) => {
                            if &*text.shadow_brush != brush {
                                text.set_shadow_brush(brush.clone());
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                        &TextMessage::ShadowDilation(dilation) => {
                            if *text.shadow_dilation != dilation {
                                text.set_shadow_dilation(dilation);
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                        &TextMessage::ShadowOffset(offset) => {
                            if *text.shadow_offset != offset {
                                text.set_shadow_offset(offset);
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                        &TextMessage::FontSize(height) => {
                            if text.font_size() != height {
                                text.set_font_size(height);
                                drop(text);
                                self.invalidate_layout();
                                ui.send_message(message.reverse());
                            }
                        }
                    }
                }
            } else if let Some(msg) = message.data::<TextBoxMessage>() {
                if message.direction() == MessageDirection::ToWidget {
                    match msg {
                        TextBoxMessage::SelectionBrush(brush) => {
                            if &*self.selection_brush != brush {
                                self.selection_brush
                                    .set_value_and_mark_modified(brush.clone());
                                ui.send_message(message.reverse());
                            }
                        }
                        TextBoxMessage::CaretBrush(brush) => {
                            if &*self.caret_brush != brush {
                                self.caret_brush.set_value_and_mark_modified(brush.clone());
                                ui.send_message(message.reverse());
                            }
                        }
                        TextBoxMessage::TextCommitMode(mode) => {
                            if &*self.commit_mode != mode {
                                self.commit_mode.set_value_and_mark_modified(*mode);
                                ui.send_message(message.reverse());
                            }
                        }
                        TextBoxMessage::Multiline(multiline) => {
                            if &*self.multiline != multiline {
                                self.multiline.set_value_and_mark_modified(*multiline);
                                ui.send_message(message.reverse());
                            }
                        }
                        TextBoxMessage::Editable(editable) => {
                            if &*self.editable != editable {
                                self.editable.set_value_and_mark_modified(*editable);
                                ui.send_message(message.reverse());
                            }
                        }
                    }
                }
            }
        }
    }
}

/// Text box builder creates new [`TextBox`] instances and adds them to the user interface.
pub struct TextBoxBuilder {
    widget_builder: WidgetBuilder,
    font: Option<FontResource>,
    text: String,
    caret_brush: Brush,
    selection_brush: Brush,
    filter: Option<Arc<Mutex<FilterCallback>>>,
    vertical_alignment: VerticalAlignment,
    horizontal_alignment: HorizontalAlignment,
    wrap: WrapMode,
    commit_mode: TextCommitMode,
    multiline: bool,
    editable: bool,
    mask_char: Option<char>,
    shadow: bool,
    shadow_brush: Brush,
    shadow_dilation: f32,
    shadow_offset: Vector2<f32>,
    skip_chars: Vec<char>,
    font_size: f32,
}

impl TextBoxBuilder {
    /// Creates new text box widget builder with the base widget builder specified.
    pub fn new(widget_builder: WidgetBuilder) -> Self {
        Self {
            widget_builder,
            font: None,
            text: "".to_owned(),
            caret_brush: Brush::Solid(Color::WHITE),
            selection_brush: Brush::Solid(Color::opaque(80, 118, 178)),
            filter: None,
            vertical_alignment: VerticalAlignment::Top,
            horizontal_alignment: HorizontalAlignment::Left,
            wrap: WrapMode::NoWrap,
            commit_mode: TextCommitMode::LostFocusPlusEnter,
            multiline: false,
            editable: true,
            mask_char: None,
            shadow: false,
            shadow_brush: Brush::Solid(Color::BLACK),
            shadow_dilation: 1.0,
            shadow_offset: Vector2::new(1.0, 1.0),
            skip_chars: Default::default(),
            font_size: 14.0,
        }
    }

    /// Sets the desired font of the text box.
    pub fn with_font(mut self, font: FontResource) -> Self {
        self.font = Some(font);
        self
    }

    /// Sets the desired text of the text box.
    pub fn with_text<P: AsRef<str>>(mut self, text: P) -> Self {
        text.as_ref().clone_into(&mut self.text);
        self
    }

    /// Sets the desired caret brush of the text box.
    pub fn with_caret_brush(mut self, brush: Brush) -> Self {
        self.caret_brush = brush;
        self
    }

    /// Sets the desired selection brush of the text box.
    pub fn with_selection_brush(mut self, brush: Brush) -> Self {
        self.selection_brush = brush;
        self
    }

    /// Sets the desired character filter of the text box. See [`FilterCallback`] for more info.
    pub fn with_filter(mut self, filter: Arc<Mutex<FilterCallback>>) -> Self {
        self.filter = Some(filter);
        self
    }

    /// Sets the desired vertical text alignment of the text box.
    pub fn with_vertical_text_alignment(mut self, alignment: VerticalAlignment) -> Self {
        self.vertical_alignment = alignment;
        self
    }

    /// Sets the desired horizontal text alignment of the text box.
    pub fn with_horizontal_text_alignment(mut self, alignment: HorizontalAlignment) -> Self {
        self.horizontal_alignment = alignment;
        self
    }

    /// Sets the desired word wrapping of the text box.
    pub fn with_wrap(mut self, wrap: WrapMode) -> Self {
        self.wrap = wrap;
        self
    }

    /// Sets the desired text commit mode of the text box.
    pub fn with_text_commit_mode(mut self, mode: TextCommitMode) -> Self {
        self.commit_mode = mode;
        self
    }

    /// Enables or disables multiline mode of the text box.
    pub fn with_multiline(mut self, multiline: bool) -> Self {
        self.multiline = multiline;
        self
    }

    /// Enables or disables editing of the text box.
    pub fn with_editable(mut self, editable: bool) -> Self {
        self.editable = editable;
        self
    }

    /// Sets the desired height of the text.
    pub fn with_font_size(mut self, font_size: f32) -> Self {
        self.font_size = font_size;
        self
    }

    /// Sets the desired masking character of the text box.
    pub fn with_mask_char(mut self, mask_char: Option<char>) -> Self {
        self.mask_char = mask_char;
        self
    }

    /// Whether the shadow enabled or not.
    pub fn with_shadow(mut self, shadow: bool) -> Self {
        self.shadow = shadow;
        self
    }

    /// Sets desired shadow brush. It will be used to render the shadow.
    pub fn with_shadow_brush(mut self, brush: Brush) -> Self {
        self.shadow_brush = brush;
        self
    }

    /// Sets desired shadow dilation in units. Keep in mind that the dilation is absolute,
    /// not percentage-based.
    pub fn with_shadow_dilation(mut self, thickness: f32) -> Self {
        self.shadow_dilation = thickness;
        self
    }

    /// Sets desired shadow offset in units.
    pub fn with_shadow_offset(mut self, offset: Vector2<f32>) -> Self {
        self.shadow_offset = offset;
        self
    }

    /// Sets desired set of characters that will be treated like whitespace during Ctrl+Arrow navigation
    /// (Ctrl+Left Arrow and Ctrl+Right Arrow). This could be useful to treat underscores like whitespaces,
    /// which in its turn could be useful for in-game consoles where commands usually separated using
    /// underscores (`like_this_one`).
    pub fn with_skip_chars(mut self, chars: Vec<char>) -> Self {
        self.skip_chars = chars;
        self
    }

    /// Creates a new [`TextBox`] instance and adds it to the user interface.
    pub fn build(mut self, ctx: &mut BuildContext) -> Handle<UiNode> {
        if self.widget_builder.foreground.is_none() {
            self.widget_builder.foreground = Some(BRUSH_TEXT);
        }
        if self.widget_builder.background.is_none() {
            self.widget_builder.background = Some(BRUSH_DARKER);
        }
        if self.widget_builder.cursor.is_none() {
            self.widget_builder.cursor = Some(CursorIcon::Text);
        }

        let text_box = TextBox {
            widget: self
                .widget_builder
                .with_accepts_input(true)
                .with_need_update(true)
                .build(),
            caret_position: Position::default().into(),
            caret_visible: false.into(),
            blink_timer: 0.0.into(),
            blink_interval: 0.5.into(),
            formatted_text: RefCell::new(
                FormattedTextBuilder::new(self.font.unwrap_or_else(|| ctx.default_font()))
                    .with_text(self.text)
                    .with_horizontal_alignment(self.horizontal_alignment)
                    .with_vertical_alignment(self.vertical_alignment)
                    .with_wrap(self.wrap)
                    .with_mask_char(self.mask_char)
                    .with_shadow(self.shadow)
                    .with_shadow_brush(self.shadow_brush)
                    .with_shadow_dilation(self.shadow_dilation)
                    .with_shadow_offset(self.shadow_offset)
                    .with_font_size(self.font_size)
                    .build(),
            ),
            selection_range: None.into(),
            selecting: false,
            before_click_position: Position::default(),
            selection_brush: self.selection_brush.into(),
            caret_brush: self.caret_brush.into(),
            has_focus: false,
            filter: self.filter,
            commit_mode: self.commit_mode.into(),
            multiline: self.multiline.into(),
            editable: self.editable.into(),
            view_position: Default::default(),
            skip_chars: self.skip_chars.into(),
            recent: Default::default(),
        };

        ctx.add_node(UiNode::new(text_box))
    }
}