bevy_vista 0.17.0

A visual UI editor plugin for Bevy with inspector-driven editing and .vista.ron serialization.
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
use arboard::Clipboard;
use bevy::input::ButtonState;
use bevy::input::keyboard::{Key, KeyboardInput};
use bevy::prelude::*;
use bevy::text::TextLayoutInfo;
use bevy::ui::{ComputedNode, RelativeCursorPosition};
use bevy::window::{Ime, PrimaryWindow};
use bevy_vista_macros::{ShowInInspector, Widget};

use crate as bevy_vista;
use crate::theme::{Theme, ThemeBoundary, ThemeScope, resolve_theme_or_global};
use crate::widget::DefaultWidgetBuilder;

pub struct TextFieldPlugin;

impl Plugin for TextFieldPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<FocusedTextField>()
            .init_resource::<TextFieldCursorBlink>()
            .init_resource::<TextClipboard>()
            .add_message::<TextInputChange>()
            .add_message::<TextInputSubmit>()
            .add_systems(
                PostUpdate,
                (
                    text_field_focus,
                    text_field_click_to_move,
                    text_field_drag_select,
                    text_field_ime_state,
                    text_field_input,
                    text_field_ime_input,
                    text_field_layout_behavior,
                    text_field_cursor_blink,
                    text_field_sync_visuals,
                )
                    .chain(),
            );
    }
}

#[derive(Component, Default, Reflect, Clone, Widget, ShowInInspector)]
#[widget("input/text_field")]
#[builder(TextFieldBuilder)]
pub struct TextField {
    #[property(hidden)]
    pub value: String,
    #[property(hidden)]
    pub placeholder: String,
    #[property(hidden)]
    pub max_length: Option<usize>,
    #[property(hidden)]
    pub validator: Option<TextInputValidator>,
    #[property(hidden)]
    pub formatter: Option<TextInputFormatter>,
    #[property(hidden)]
    pub cursor_pos: usize,
    #[property(hidden)]
    pub selection: Option<(usize, usize)>,
    #[property(hidden)]
    pub drag_anchor: Option<usize>,
    #[property(label = "Disabled")]
    pub disabled: bool,
    #[property(label = "Input Type")]
    pub input_type: TextInputType,
    #[property(label = "Layout Mode")]
    pub layout_mode: TextFieldLayoutMode,
    #[property(label = "Min Width", min = 0.0)]
    pub min_width: f32,
    #[property(label = "Min Height", min = 0.0)]
    pub min_height: f32,
}

#[derive(Component)]
struct TextFieldCursor;

#[derive(Component)]
struct TextFieldInputText;

#[derive(Component)]
struct TextFieldSelectionHighlight;

#[derive(Reflect, Clone)]
pub enum TextInputValidator {
    Any,
    Numeric,
    Alpha,
    AlphaNum,
    Email,
}

#[derive(Reflect, Clone)]
pub enum TextInputFormatter {
    Uppercase,
    Lowercase,
    Capitalize,
    Trim,
}

#[derive(Reflect, Default, Clone)]
pub enum TextInputType {
    #[default]
    SingleLine,
    MultiLine,
    Password,
    Search,
    Email,
    Number,
}

#[derive(Reflect, Clone, Copy, Default)]
pub enum TextFieldLayoutMode {
    #[default]
    FixedTruncate,
    AutoWidth,
    AutoWrap,
    MultiLine,
}

#[derive(Message, EntityEvent)]
pub struct TextInputChange {
    pub entity: Entity,
    pub value: String,
}

#[derive(Message, EntityEvent)]
pub struct TextInputSubmit {
    pub entity: Entity,
    pub value: String,
}

#[derive(Resource, Default)]
struct FocusedTextField(pub Option<Entity>);

#[derive(Resource)]
struct TextFieldCursorBlink {
    timer: Timer,
    visible: bool,
}

impl Default for TextFieldCursorBlink {
    fn default() -> Self {
        Self {
            timer: Timer::from_seconds(0.5, TimerMode::Repeating),
            visible: true,
        }
    }
}

#[derive(Resource)]
struct TextClipboard {
    os_clipboard: Option<Clipboard>,
    fallback: String,
}

impl FromWorld for TextClipboard {
    fn from_world(_: &mut World) -> Self {
        Self {
            os_clipboard: Clipboard::new().ok(),
            fallback: String::new(),
        }
    }
}

pub struct TextFieldBuilder {
    text: String,
    placeholder: String,
    max_length: Option<usize>,
    width: Val,
    height: Val,
    disabled: bool,
    validator: Option<TextInputValidator>,
    formatter: Option<TextInputFormatter>,
    layout_mode: TextFieldLayoutMode,
}

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

impl TextFieldBuilder {
    pub fn new() -> Self {
        Self {
            text: String::new(),
            placeholder: "Enter text...".to_owned(),
            max_length: Some(256),
            width: Val::Px(200.0),
            height: Val::Px(30.0),
            disabled: false,
            validator: None,
            formatter: None,
            layout_mode: TextFieldLayoutMode::FixedTruncate,
        }
    }

    pub fn text(mut self, text: &str) -> Self {
        self.text = text.to_string();
        self
    }

    pub fn placeholder(mut self, placeholder: &str) -> Self {
        self.placeholder = placeholder.to_string();
        self
    }

    pub fn max_length(mut self, max_length: Option<usize>) -> Self {
        self.max_length = max_length;
        self
    }

    pub fn width(mut self, width: Val) -> Self {
        self.width = width;
        self
    }

    pub fn height(mut self, height: Val) -> Self {
        self.height = height;
        self
    }

    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    pub fn validator(mut self, validator: TextInputValidator) -> Self {
        self.validator = Some(validator);
        self
    }

    pub fn formatter(mut self, formatter: TextInputFormatter) -> Self {
        self.formatter = Some(formatter);
        self
    }

    pub fn layout_mode(mut self, mode: TextFieldLayoutMode) -> Self {
        self.layout_mode = mode;
        self
    }

    pub fn build(self, commands: &mut Commands, theme: Option<&Theme>) -> Entity {
        let (surface, outline, on_surface, on_surface_muted, primary, radius, text_font) =
            match theme {
                Some(theme) => (
                    theme.palette.surface,
                    theme.palette.outline,
                    theme.palette.on_surface,
                    theme.palette.on_surface_muted,
                    theme.palette.primary,
                    theme.radius.md,
                    theme.typography.body_medium.font.clone(),
                ),
                None => (
                    Color::srgb(0.12, 0.12, 0.12),
                    Color::srgb(0.3, 0.3, 0.3),
                    Color::srgb(0.9, 0.9, 0.9),
                    Color::srgb(0.55, 0.55, 0.55),
                    Color::srgb(0.2, 0.6, 0.95),
                    8.0,
                    TextFont::from_font_size(13.0),
                ),
            };
        let is_multiline = matches!(self.layout_mode, TextFieldLayoutMode::MultiLine);

        let background = commands
            .spawn((
                Node {
                    width: self.width,
                    height: self.height,
                    padding: UiRect::all(px(4.)),
                    border: UiRect::all(px(1.)),
                    align_items: AlignItems::Center,
                    ..default()
                },
                BackgroundColor(surface),
                BorderColor::all(outline),
                BorderRadius::all(px(radius)),
                TextField {
                    value: self.text.clone(),
                    placeholder: self.placeholder.clone(),
                    max_length: self.max_length,
                    validator: self.validator,
                    formatter: self.formatter,
                    input_type: if is_multiline {
                        TextInputType::MultiLine
                    } else {
                        TextInputType::SingleLine
                    },
                    disabled: self.disabled,
                    layout_mode: self.layout_mode,
                    min_width: val_to_px(self.width, 200.0),
                    min_height: val_to_px(self.height, 30.0),
                    ..default()
                },
                Interaction::default(),
            ))
            .id();

        let input_text = commands
            .spawn((
                Node {
                    width: percent(100.0),
                    height: percent(100.0),
                    ..default()
                },
                Text(if self.text.is_empty() {
                    self.placeholder.clone()
                } else {
                    self.text.clone()
                }),
                match self.layout_mode {
                    TextFieldLayoutMode::AutoWrap | TextFieldLayoutMode::MultiLine => {
                        TextLayout::new_with_linebreak(bevy::text::LineBreak::WordOrCharacter)
                    }
                    _ => TextLayout::new_with_no_wrap(),
                },
                text_font,
                TextColor(if self.text.is_empty() {
                    on_surface_muted
                } else {
                    on_surface
                }),
                RelativeCursorPosition::default(),
                TextFieldInputText,
                Name::new("Input Text"),
            ))
            .id();

        let cursor = commands
            .spawn((
                Node {
                    width: px(2.0),
                    height: px(20.0),
                    position_type: PositionType::Absolute,
                    left: px(0.),
                    top: px(2.0),
                    ..default()
                },
                BackgroundColor(on_surface),
                Visibility::Hidden,
                TextFieldCursor,
                Name::new("Text Cursor"),
            ))
            .id();

        let selection = commands
            .spawn((
                Node {
                    width: px(0.0),
                    height: percent(100.0),
                    position_type: PositionType::Absolute,
                    left: px(0.0),
                    top: px(0.0),
                    ..default()
                },
                BackgroundColor(primary.with_alpha(0.45)),
                Visibility::Hidden,
                TextFieldSelectionHighlight,
                Name::new("Text Selection"),
            ))
            .id();

        commands.entity(background).add_children(&[input_text]);
        commands
            .entity(input_text)
            .add_children(&[selection, cursor]);

        background
    }
}

impl DefaultWidgetBuilder for TextFieldBuilder {
    fn spawn_default(commands: &mut Commands, theme: Option<&crate::theme::Theme>) -> Entity {
        TextFieldBuilder::new().build(commands, theme)
    }
}

fn text_field_focus(
    mouse_btn: Res<ButtonInput<MouseButton>>,
    mut focused: ResMut<FocusedTextField>,
    mut fields: Query<(Entity, &Interaction, &mut TextField), With<TextField>>,
) {
    let mut is_focused = false;

    for (entity, interaction, mut field) in fields.iter_mut() {
        if matches!(interaction, Interaction::Pressed)
            || (matches!(interaction, Interaction::Hovered)
                && (mouse_btn.just_pressed(MouseButton::Left)
                    || mouse_btn.just_released(MouseButton::Left)))
        {
            if focused.0 != Some(entity) {
                field.drag_anchor = None;
            }
            focused.0 = Some(entity);
            is_focused = true;
            break;
        }
    }

    if mouse_btn.just_pressed(MouseButton::Left) && !is_focused {
        focused.0 = None;
        for (_, _, mut field) in fields.iter_mut() {
            field.drag_anchor = None;
        }
    }
}

fn text_field_click_to_move(
    mouse_btn: Res<ButtonInput<MouseButton>>,
    focused: Res<FocusedTextField>,
    text_entities: Query<(), With<TextFieldInputText>>,
    text_nodes: Query<
        (&RelativeCursorPosition, &ComputedNode, &TextLayoutInfo),
        With<TextFieldInputText>,
    >,
    children_query: Query<&Children>,
    mut fields: Query<(Entity, &Children, &Interaction, &mut TextField)>,
) {
    if !mouse_btn.just_pressed(MouseButton::Left) {
        return;
    }

    for (field_entity, children, interaction, mut field) in fields.iter_mut() {
        if *interaction != Interaction::Pressed {
            continue;
        }
        if focused.0 != Some(field_entity) || field.disabled {
            continue;
        }

        let text_entity = find_descendant_with(children, &children_query, &text_entities);
        let Some(text_entity) = text_entity else {
            continue;
        };
        let Ok((cursor_pos, node, layout)) = text_nodes.get(text_entity) else {
            continue;
        };
        if !cursor_pos.cursor_over {
            continue;
        }
        let Some(normalized) = cursor_pos.normalized else {
            continue;
        };
        // left-based coordinates: [0, width]
        let local_x = (normalized.x + 0.5) * node.size.x;
        let local_y = (normalized.y + 0.5) * node.size.y;

        let new_pos = cursor_pos_from_xy_nearest(&field.value, local_x, local_y, layout);
        field.cursor_pos = new_pos;
        field.selection = None;
        field.drag_anchor = Some(new_pos);
    }
}

fn text_field_drag_select(
    mouse_btn: Res<ButtonInput<MouseButton>>,
    focused: Res<FocusedTextField>,
    text_entities: Query<(), With<TextFieldInputText>>,
    text_nodes: Query<
        (&RelativeCursorPosition, &ComputedNode, &TextLayoutInfo),
        With<TextFieldInputText>,
    >,
    children_query: Query<&Children>,
    mut fields: Query<(Entity, &Children, &mut TextField)>,
) {
    let Some(focused_entity) = focused.0 else {
        return;
    };

    if mouse_btn.just_released(MouseButton::Left) {
        if let Ok((_, _, mut field)) = fields.get_mut(focused_entity) {
            field.drag_anchor = None;
        }
        return;
    }

    if !mouse_btn.pressed(MouseButton::Left) {
        return;
    }

    let Ok((_, children, mut field)) = fields.get_mut(focused_entity) else {
        return;
    };
    if field.disabled {
        return;
    }

    let Some(anchor) = field.drag_anchor else {
        field.drag_anchor = Some(field.cursor_pos);
        return;
    };

    let text_entity = find_descendant_with(children, &children_query, &text_entities);
    let Some(text_entity) = text_entity else {
        return;
    };
    let Ok((cursor_pos, node, layout)) = text_nodes.get(text_entity) else {
        return;
    };
    let Some(normalized) = cursor_pos.normalized else {
        return;
    };
    let local_x = ((normalized.x + 0.5) * node.size.x).clamp(0.0, node.size.x);
    let local_y = ((normalized.y + 0.5) * node.size.y).clamp(0.0, node.size.y);
    let new_pos = cursor_pos_from_xy_nearest(&field.value, local_x, local_y, layout);
    field.cursor_pos = new_pos;
    field.selection = if new_pos == anchor {
        None
    } else {
        Some((anchor, new_pos))
    };
}

fn text_field_ime_state(
    focused: Res<FocusedTextField>,
    mut window: Single<&mut Window, With<PrimaryWindow>>,
) {
    if focused.0.is_some() {
        if let Some(pos) = window.cursor_position() {
            window.ime_position = pos;
        }
        window.ime_enabled = true;
    } else {
        window.ime_enabled = false;
    }
}

fn text_field_ime_input(
    focused: Res<FocusedTextField>,
    mut ime_reader: MessageReader<Ime>,
    mut fields: Query<(Entity, &mut TextField)>,
    mut change_events: MessageWriter<TextInputChange>,
) {
    let Some(focused) = focused.0 else {
        ime_reader.clear();
        return;
    };

    let Ok((entity, mut field)) = fields.get_mut(focused) else {
        ime_reader.clear();
        return;
    };

    if field.disabled {
        ime_reader.clear();
        return;
    }

    let mut changed = false;
    let mut text_len = field.value.chars().count();

    for ime in ime_reader.read() {
        if let Ime::Commit { value, .. } = ime {
            for ch in value.chars().filter(is_printable_char) {
                if let Some(max) = field.max_length {
                    if field.value.chars().count() >= max {
                        break;
                    }
                }

                if let Some((start, end)) = selection_range(field.selection, text_len) {
                    remove_char_range(&mut field.value, start, end);
                    field.cursor_pos = start;
                    field.selection = None;
                    text_len = field.value.chars().count();
                }

                let char_index = field.cursor_pos;
                insert_char_at(&mut field.value, char_index, ch);
                field.cursor_pos += 1;
                text_len += 1;
                changed = true;
            }
        }
    }

    if changed {
        change_events.write(TextInputChange {
            entity,
            value: field.value.clone(),
        });
    }
}

fn text_field_layout_behavior(
    mut fields: Query<(&mut TextField, &Children, &mut Node)>,
    children_query: Query<&Children>,
    text_entities: Query<(), With<TextFieldInputText>>,
    text_infos: Query<&TextLayoutInfo, With<TextFieldInputText>>,
    mut text_layouts: Query<&mut TextLayout, With<TextFieldInputText>>,
) {
    for (mut field, children, mut field_node) in fields.iter_mut() {
        let text_entity = find_descendant_with(children, &children_query, &text_entities);
        let Some(text_entity) = text_entity else {
            continue;
        };
        let Ok(layout_info) = text_infos.get(text_entity) else {
            continue;
        };
        let Ok(mut text_layout) = text_layouts.get_mut(text_entity) else {
            continue;
        };

        match field.layout_mode {
            TextFieldLayoutMode::FixedTruncate => {
                text_layout.linebreak = bevy::text::LineBreak::NoWrap;
                if let Val::Px(width_px) = field_node.width {
                    let available = (width_px - 8.0).max(1.0);
                    if !field.value.is_empty() && layout_info.size.x > available {
                        let len = field.value.chars().count();
                        if len > 0 {
                            let keep =
                                ((len as f32) * (available / layout_info.size.x)).floor() as usize;
                            let keep = keep.min(len);
                            field.value = field.value.chars().take(keep).collect();
                            field.cursor_pos = field.cursor_pos.min(keep);
                            field.selection = None;
                        }
                    }
                }
            }
            TextFieldLayoutMode::AutoWidth => {
                text_layout.linebreak = bevy::text::LineBreak::NoWrap;
                let desired = (layout_info.size.x + 12.0).max(field.min_width);
                field_node.width = px(desired);
            }
            TextFieldLayoutMode::AutoWrap => {
                text_layout.linebreak = bevy::text::LineBreak::WordOrCharacter;
                let desired_h = (layout_info.size.y + 8.0).max(field.min_height);
                field_node.height = px(desired_h);
            }
            TextFieldLayoutMode::MultiLine => {
                text_layout.linebreak = bevy::text::LineBreak::WordOrCharacter;
                let desired_h = (layout_info.size.y + 8.0).max(field.min_height);
                field_node.height = px(desired_h);
            }
        }
    }
}

fn text_field_input(
    focused: Res<FocusedTextField>,
    mut inputs: MessageReader<KeyboardInput>,
    keys: Res<ButtonInput<KeyCode>>,
    mut fields: Query<(Entity, &mut TextField)>,
    mut clipboard: ResMut<TextClipboard>,
    mut change_events: MessageWriter<TextInputChange>,
    mut submit_events: MessageWriter<TextInputSubmit>,
) {
    let Some(focused) = focused.0 else {
        return;
    };

    let Ok((entity, mut field)) = fields.get_mut(focused) else {
        return;
    };

    if field.disabled {
        return;
    }

    let mut text_len = field.value.chars().count();
    field.cursor_pos = field.cursor_pos.min(text_len);

    let mut changed = false;
    let ctrl = keys.pressed(KeyCode::ControlLeft) || keys.pressed(KeyCode::ControlRight);

    // text entry
    for input in inputs.read() {
        if input.state != ButtonState::Pressed {
            continue;
        }

        if ctrl {
            match input.key_code {
                KeyCode::KeyA => {
                    let len = field.value.chars().count();
                    if len > 0 {
                        field.selection = Some((0, len));
                        field.cursor_pos = len;
                    }
                    continue;
                }
                KeyCode::KeyC => {
                    if let Some((start, end)) =
                        selection_range(field.selection, field.value.chars().count())
                    {
                        let selected = text_slice_chars(&field.value, start, end).to_string();
                        clipboard.fallback = selected.clone();
                        if let Some(os) = clipboard.os_clipboard.as_mut() {
                            let _ = os.set_text(selected);
                        }
                    }
                    continue;
                }
                KeyCode::KeyX => {
                    if let Some((start, end)) =
                        selection_range(field.selection, field.value.chars().count())
                    {
                        let selected = text_slice_chars(&field.value, start, end).to_string();
                        clipboard.fallback = selected.clone();
                        if let Some(os) = clipboard.os_clipboard.as_mut() {
                            let _ = os.set_text(selected);
                        }
                        remove_char_range(&mut field.value, start, end);
                        field.cursor_pos = start;
                        field.selection = None;
                        text_len = field.value.chars().count();
                        changed = true;
                    }
                    continue;
                }
                KeyCode::KeyV => {
                    let pasted = if let Some(os) = clipboard.os_clipboard.as_mut() {
                        os.get_text()
                            .ok()
                            .unwrap_or_else(|| clipboard.fallback.clone())
                    } else {
                        clipboard.fallback.clone()
                    };

                    if !pasted.is_empty() {
                        if let Some((start, end)) =
                            selection_range(field.selection, field.value.chars().count())
                        {
                            remove_char_range(&mut field.value, start, end);
                            field.cursor_pos = start;
                            field.selection = None;
                            text_len = field.value.chars().count();
                        }
                        for ch in pasted.chars().filter(is_printable_char) {
                            if let Some(max) = field.max_length {
                                if field.value.chars().count() >= max {
                                    break;
                                }
                            }
                            let char_index = field.cursor_pos;
                            insert_char_at(&mut field.value, char_index, ch);
                            field.cursor_pos += 1;
                            text_len += 1;
                            changed = true;
                        }
                    }
                    continue;
                }
                _ => {
                    // With Ctrl pressed, do not treat key events as text input.
                    continue;
                }
            }
        }

        if input.key_code == KeyCode::ArrowLeft {
            if let Some((start, _)) = selection_range(field.selection, text_len) {
                field.cursor_pos = start;
            } else if field.cursor_pos > 0 {
                field.cursor_pos -= 1;
            }
            field.selection = None;
            continue;
        }

        if input.key_code == KeyCode::ArrowRight {
            if let Some((_, end)) = selection_range(field.selection, text_len) {
                field.cursor_pos = end;
            } else if field.cursor_pos < text_len {
                field.cursor_pos += 1;
            }
            field.selection = None;
            continue;
        }

        if input.key_code == KeyCode::Backspace {
            if let Some((start, end)) = selection_range(field.selection, text_len) {
                remove_char_range(&mut field.value, start, end);
                field.cursor_pos = start;
                field.selection = None;
                text_len = field.value.chars().count();
                changed = true;
                continue;
            }
            if field.cursor_pos > 0 {
                let remove_at = field.cursor_pos - 1;
                let end = field.cursor_pos;
                remove_char_range(&mut field.value, remove_at, end);
                field.cursor_pos = remove_at;
                text_len = field.value.chars().count();
                changed = true;
            }
            continue;
        }

        let text: Option<&str> = input.text.as_deref().or_else(|| match &input.logical_key {
            Key::Character(s) => Some(s.as_str()),
            _ => None,
        });
        let Some(text) = text else {
            continue;
        };

        for ch in text.chars() {
            if ch.is_control() {
                continue;
            }
            if let Some(max) = field.max_length {
                if field.value.chars().count() >= max {
                    break;
                }
            }

            if let Some((start, end)) = selection_range(field.selection, text_len) {
                remove_char_range(&mut field.value, start, end);
                field.cursor_pos = start;
                field.selection = None;
                text_len = field.value.chars().count();
            }

            let char_index = field.cursor_pos;
            insert_char_at(&mut field.value, char_index, ch);
            field.cursor_pos += 1;
            text_len += 1;
            changed = true;
        }
    }

    if changed {
        change_events.write(TextInputChange {
            entity,
            value: field.value.clone(),
        });
    }

    // submit / newline
    if keys.just_pressed(KeyCode::Enter) {
        if matches!(field.input_type, TextInputType::MultiLine) {
            if field
                .max_length
                .is_none_or(|max| field.value.chars().count() < max)
            {
                if let Some((start, end)) = selection_range(field.selection, text_len) {
                    remove_char_range(&mut field.value, start, end);
                    field.cursor_pos = start;
                    field.selection = None;
                }
                let insert_at = field.cursor_pos;
                insert_char_at(&mut field.value, insert_at, '\n');
                field.cursor_pos = insert_at + 1;
                change_events.write(TextInputChange {
                    entity,
                    value: field.value.clone(),
                });
            }
        } else {
            submit_events.write(TextInputSubmit {
                entity,
                value: field.value.clone(),
            });
        }
    }
}

fn text_field_cursor_blink(time: Res<Time>, mut blink: ResMut<TextFieldCursorBlink>) {
    blink.timer.tick(time.delta());
    if blink.timer.just_finished() {
        blink.visible = !blink.visible;
    }
}

fn text_field_sync_visuals(
    focused: Res<FocusedTextField>,
    blink: Res<TextFieldCursorBlink>,
    global_theme: Option<Res<Theme>>,
    fields: Query<(Entity, &TextField, &Children)>,
    parents: Query<&ChildOf>,
    scopes: Query<&ThemeScope>,
    boundaries: Query<(), With<ThemeBoundary>>,
    children_query: Query<&Children>,
    text_entities: Query<(), With<TextFieldInputText>>,
    cursor_entities: Query<(), With<TextFieldCursor>>,
    selection_entities: Query<(), With<TextFieldSelectionHighlight>>,
    text_layouts: Query<&TextLayoutInfo, With<TextFieldInputText>>,
    text_nodes: Query<&ComputedNode, With<TextFieldInputText>>,
    mut text_query: Query<(&mut Text, &mut TextColor), With<TextFieldInputText>>,
    mut overlays: ParamSet<(
        Query<(&mut Node, &mut Visibility), With<TextFieldCursor>>,
        Query<
            (&mut Node, &mut Visibility, &mut BackgroundColor),
            With<TextFieldSelectionHighlight>,
        >,
    )>,
) {
    for (field_entity, field, children) in fields.iter() {
        let (on_surface, on_surface_muted, primary) = match resolve_theme_or_global(
            field_entity,
            &parents,
            &scopes,
            &boundaries,
            global_theme.as_deref(),
        ) {
            Some(theme) => (
                theme.palette.on_surface,
                theme.palette.on_surface_muted,
                theme.palette.primary,
            ),
            None => (
                Color::srgb(0.9, 0.9, 0.9),
                Color::srgb(0.55, 0.55, 0.55),
                Color::srgb(0.2, 0.6, 0.95),
            ),
        };

        let text_entity = find_descendant_with(children, &children_query, &text_entities);
        let cursor_entity = find_descendant_with(children, &children_query, &cursor_entities);
        let selection_entity = find_descendant_with(children, &children_query, &selection_entities);

        let Some(text_entity) = text_entity else {
            continue;
        };
        let Some(cursor_entity) = cursor_entity else {
            continue;
        };

        if let Ok((mut text, mut text_color)) = text_query.get_mut(text_entity) {
            if field.value.is_empty() {
                if text.0 != field.placeholder {
                    text.0 = field.placeholder.clone();
                }
                if text_color.0 != on_surface_muted {
                    *text_color = TextColor(on_surface_muted);
                }
            } else {
                if text.0 != field.value {
                    text.0 = field.value.clone();
                }
                if text_color.0 != on_surface {
                    *text_color = TextColor(on_surface);
                }
            }
        }

        let layout = text_layouts.get(text_entity).ok();
        let inv_scale = text_nodes
            .get(text_entity)
            .map(|n| n.inverse_scale_factor())
            .unwrap_or(1.0);
        if let Ok((mut cursor_node, mut cursor_vis)) = overlays.p0().get_mut(cursor_entity) {
            let is_focused = focused.0 == Some(field_entity) && !field.disabled;
            *cursor_vis = if is_focused && blink.visible {
                Visibility::Visible
            } else {
                Visibility::Hidden
            };

            let (cursor_left, cursor_top, cursor_height) = match layout {
                Some(layout) if !field.value.is_empty() => {
                    if matches!(
                        field.layout_mode,
                        TextFieldLayoutMode::AutoWrap | TextFieldLayoutMode::MultiLine
                    ) {
                        cursor_metrics_from_pos(&field.value, field.cursor_pos, layout)
                    } else {
                        (
                            cursor_x_from_pos(&field.value, field.cursor_pos, layout),
                            2.0,
                            20.0,
                        )
                    }
                }
                _ => (0.0, 2.0, 20.0),
            };
            cursor_node.left = px(cursor_left * inv_scale);
            cursor_node.top = px((cursor_top * inv_scale).max(0.0));
            cursor_node.height = px((cursor_height * inv_scale).max(12.0));
        }

        if let (Some(selection_entity), Some(layout)) = (selection_entity, layout) {
            if let Ok((mut sel_node, mut sel_vis, mut sel_bg)) =
                overlays.p1().get_mut(selection_entity)
            {
                let is_focused = focused.0 == Some(field_entity) && !field.disabled;
                if is_focused {
                    if let Some((start, end)) =
                        selection_range(field.selection, field.value.chars().count())
                    {
                        let x0 = cursor_x_from_pos(&field.value, start, layout);
                        let x1 = cursor_x_from_pos(&field.value, end, layout);
                        let left = x0.min(x1) * inv_scale;
                        let width = (x1 - x0).abs() * inv_scale;
                        sel_node.left = px(left);
                        sel_node.width = px(width.max(1.0));
                        sel_bg.0 = primary.with_alpha(0.45);
                        *sel_vis = Visibility::Visible;
                    } else {
                        *sel_vis = Visibility::Hidden;
                    }
                } else {
                    *sel_vis = Visibility::Hidden;
                }
            }
        }
    }
}

fn find_descendant_with<T: Component>(
    children: &Children,
    children_query: &Query<&Children>,
    query: &Query<(), With<T>>,
) -> Option<Entity> {
    let mut stack: Vec<Entity> = children.iter().collect();
    while let Some(entity) = stack.pop() {
        if query.get(entity).is_ok() {
            return Some(entity);
        }
        if let Ok(kids) = children_query.get(entity) {
            stack.extend(kids.iter());
        }
    }
    None
}

fn selection_range(selection: Option<(usize, usize)>, len: usize) -> Option<(usize, usize)> {
    let (a, b) = selection?;
    let start = a.min(b).min(len);
    let end = a.max(b).min(len);
    if start == end {
        None
    } else {
        Some((start, end))
    }
}

fn char_index_to_byte(s: &str, char_index: usize) -> usize {
    if char_index == 0 {
        return 0;
    }
    s.char_indices()
        .nth(char_index)
        .map(|(i, _)| i)
        .unwrap_or_else(|| s.len())
}

fn byte_index_to_char(s: &str, byte_index: usize) -> usize {
    let mut count = 0;
    for (idx, _) in s.char_indices() {
        if idx >= byte_index {
            break;
        }
        count += 1;
    }
    count
}

fn remove_char_range(s: &mut String, start: usize, end: usize) {
    if start >= end {
        return;
    }
    let start_b = char_index_to_byte(s, start);
    let end_b = char_index_to_byte(s, end);
    s.replace_range(start_b..end_b, "");
}

fn insert_char_at(s: &mut String, char_index: usize, ch: char) {
    let byte_index = char_index_to_byte(s, char_index);
    s.insert(byte_index, ch);
}

fn text_slice_chars(s: &str, start: usize, end: usize) -> &str {
    if start >= end {
        return "";
    }
    let start_b = char_index_to_byte(s, start);
    let end_b = char_index_to_byte(s, end);
    &s[start_b..end_b]
}

fn val_to_px(val: Val, default_px: f32) -> f32 {
    match val {
        Val::Px(v) => v,
        _ => default_px,
    }
}

fn cursor_x_from_pos(text: &str, pos: usize, layout: &TextLayoutInfo) -> f32 {
    let spans = build_glyph_spans(text, layout);
    if spans.is_empty() {
        return 0.0;
    }

    if pos <= spans[0].start {
        return spans[0].left;
    }

    let mut prev: Option<GlyphSpan> = None;
    for span in &spans {
        if pos < span.start {
            if let Some(prev) = prev {
                if pos == prev.end {
                    return prev.right;
                }
            }
            return span.left;
        }

        if pos >= span.start && pos < span.end {
            let chars = (span.end - span.start).max(1) as f32;
            let t = (pos - span.start) as f32 / chars;
            return span.left + (span.right - span.left) * t;
        }

        prev = Some(*span);
    }

    spans.last().map(|span| span.right).unwrap_or(0.0)
}

fn cursor_pos_from_xy_nearest(text: &str, x: f32, y: f32, layout: &TextLayoutInfo) -> usize {
    let len = text.chars().count();
    if len == 0 {
        return 0;
    }

    let spans = build_glyph_spans(text, layout);
    if spans.is_empty() {
        return 0;
    }

    let target_line = nearest_line_from_y(y, &spans);
    let mut best_pos = 0usize;
    let mut best_dist = f32::INFINITY;
    for pos in 0..=len {
        if line_for_pos(text, pos, &spans) != target_line {
            continue;
        }
        let px = cursor_x_from_pos_on_line(pos, target_line, &spans);
        let dist = (px - x).abs();
        if dist < best_dist {
            best_dist = dist;
            best_pos = pos;
        }
    }

    if best_dist.is_finite() { best_pos } else { 0 }
}

#[derive(Clone, Copy)]
struct GlyphSpan {
    start: usize,
    end: usize,
    line: usize,
    left: f32,
    right: f32,
    top: f32,
    bottom: f32,
}

fn build_glyph_spans(text: &str, layout: &TextLayoutInfo) -> Vec<GlyphSpan> {
    if text.is_empty() || layout.glyphs.is_empty() {
        return Vec::new();
    }

    let mut glyphs = layout.glyphs.clone();
    glyphs.sort_by_key(|g| g.byte_index);

    let mut spans = Vec::with_capacity(glyphs.len());
    for glyph in glyphs {
        let start = byte_index_to_char(text, glyph.byte_index);
        let end = byte_index_to_char(text, glyph.byte_index + glyph.byte_length);
        if end <= start {
            continue;
        }

        let left = glyph.position.x - glyph.size.x * 0.5;
        let right = glyph.position.x + glyph.size.x * 0.5;
        let top = glyph.position.y - glyph.size.y * 0.5;
        let bottom = glyph.position.y + glyph.size.y * 0.5;
        spans.push(GlyphSpan {
            start,
            end,
            line: glyph.line_index as usize,
            left,
            right,
            top,
            bottom,
        });
    }

    spans
}

fn cursor_x_from_pos_on_line(pos: usize, line: usize, spans: &[GlyphSpan]) -> f32 {
    let Some(first) = spans.iter().find(|s| s.line == line) else {
        return 0.0;
    };

    if pos <= first.start {
        return first.left;
    }

    let mut prev: Option<GlyphSpan> = None;
    for span in spans.iter().filter(|s| s.line == line) {
        if pos < span.start {
            if let Some(prev) = prev {
                if pos == prev.end {
                    return prev.right;
                }
            }
            return span.left;
        }
        if pos >= span.start && pos < span.end {
            let chars = (span.end - span.start).max(1) as f32;
            let t = (pos - span.start) as f32 / chars;
            return span.left + (span.right - span.left) * t;
        }
        prev = Some(*span);
    }

    prev.map(|s| s.right).unwrap_or(first.left)
}

fn line_for_pos(text: &str, pos: usize, spans: &[GlyphSpan]) -> usize {
    if spans.is_empty() {
        return 0;
    }

    let mut prev: Option<GlyphSpan> = None;
    for span in spans {
        if pos < span.start {
            if let Some(prev) = prev {
                if pos == prev.end {
                    return prev.line;
                }
            }
            return span.line;
        }
        if pos >= span.start && pos < span.end {
            return span.line;
        }
        prev = Some(*span);
    }

    if pos == text.chars().count() && text.ends_with('\n') {
        spans.last().map(|s| s.line + 1).unwrap_or(0)
    } else {
        spans.last().map(|s| s.line).unwrap_or(0)
    }
}

fn nearest_line_from_y(y: f32, spans: &[GlyphSpan]) -> usize {
    if spans.is_empty() {
        return 0;
    }

    let mut lines: Vec<usize> = spans.iter().map(|s| s.line).collect();
    lines.sort_unstable();
    lines.dedup();

    let mut best_line = lines[0];
    let mut best_dist = f32::INFINITY;

    for line in lines {
        let (top, bottom, _) = line_bounds(line, spans);
        let dist = if y < top {
            top - y
        } else if y > bottom {
            y - bottom
        } else {
            0.0
        };
        if dist < best_dist {
            best_dist = dist;
            best_line = line;
        }
    }

    best_line
}

fn cursor_metrics_from_pos(text: &str, pos: usize, layout: &TextLayoutInfo) -> (f32, f32, f32) {
    let spans = build_glyph_spans(text, layout);
    if spans.is_empty() {
        return (0.0, 0.0, 20.0);
    }

    let line = line_for_pos(text, pos, &spans);
    let x = cursor_x_from_pos_on_line(pos, line, &spans);

    let is_trailing_newline = pos == text.chars().count() && text.ends_with('\n');
    let (top, bottom, line_left) = if is_trailing_newline && line > 0 {
        line_bounds(line - 1, &spans)
    } else {
        line_bounds(line, &spans)
    };
    let (first_top, _, _) = line_bounds(spans[0].line, &spans);
    let line_height = (bottom - top).max(12.0);
    let top_relative = 2.0 + (top - first_top);

    if is_trailing_newline {
        return (line_left, top_relative + line_height, line_height);
    }

    (x, top_relative, line_height)
}

fn line_bounds(line: usize, spans: &[GlyphSpan]) -> (f32, f32, f32) {
    let mut min_top = f32::INFINITY;
    let mut max_bottom = f32::NEG_INFINITY;
    let mut min_left = f32::INFINITY;
    for span in spans.iter().filter(|s| s.line == line) {
        min_top = min_top.min(span.top);
        max_bottom = max_bottom.max(span.bottom);
        min_left = min_left.min(span.left);
    }

    if !min_top.is_finite() || !max_bottom.is_finite() {
        let fallback_top = spans.first().map(|s| s.top).unwrap_or(0.0);
        let fallback_bottom = spans.first().map(|s| s.bottom).unwrap_or(20.0);
        let fallback_left = spans.first().map(|s| s.left).unwrap_or(0.0);
        return (fallback_top, fallback_bottom, fallback_left);
    }

    (min_top, max_bottom, min_left)
}

// this logic is taken from egui-winit
fn is_printable_char(chr: &char) -> bool {
    let is_in_private_use_area = ('\u{e000}'..='\u{f8ff}').contains(chr)
        || ('\u{f0000}'..='\u{ffffd}').contains(chr)
        || ('\u{100000}'..='\u{10fffd}').contains(chr);

    !is_in_private_use_area && !chr.is_ascii_control()
}