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
use super::{ControllerContext, InputController};
use crate::event::{InputEvent, KeyCode, KeyEvent, PointerEvent};
use crate::ActionEnvelope;
use crate::ActionId;
use fission_diagnostics::prelude as diag;
use fission_ir::semantics::InputMask;
use fission_ir::{
op::{self, LayoutOp, Op},
NodeId, Semantics,
};
use fission_layout::LayoutSnapshot;
use serde_json;
use unicode_segmentation::UnicodeSegmentation;
pub struct TextInputController;
impl InputController for TextInputController {
fn handle_event(&mut self, ctx: &mut ControllerContext, event: &InputEvent) -> bool {
match event {
InputEvent::Keyboard(KeyEvent::Down {
key_code,
modifiers,
}) => self.handle_key(ctx, key_code.clone(), *modifiers),
InputEvent::Ime(ime) => self.handle_ime(ctx, ime),
InputEvent::Pointer(PointerEvent::Down { point, .. }) => {
if let Some(focused_id) = ctx.interaction.focused {
if let Some(node) = ctx.ir.nodes.get(&focused_id) {
if let Op::Semantics(sem) = &node.op {
if sem.role == fission_ir::semantics::Role::TextInput {
// Only handle pointer-down as a caret/selection update when the
// pointer is inside the currently focused TextInput.
//
// Otherwise, allow the generic focus logic in `Runtime::handle_input`
// to run so clicks can move focus to other widgets (including other
// TextInputs, buttons, etc).
if let Some(geom) = ctx.layout.get_node_geometry(focused_id) {
if !geom.rect.contains(*point) {
return false;
}
}
if let Some((scroll_id, _text_op_node_id, scroll_direction)) =
Self::find_scroll_container_and_text_op(
ctx.ir,
focused_id,
sem.multiline,
)
{
if let Some(scroll_geom) =
ctx.layout.get_node_geometry(scroll_id)
{
let value = sem.value.as_deref().unwrap_or("");
let offset = ctx.scroll.get_offset(scroll_id);
let caret = if let Some(measurer) = ctx.measurer {
let font_size = 16.0;
let max_width = if sem.multiline
&& scroll_geom.rect.width() > 0.0
{
Some(scroll_geom.rect.width())
} else {
None
};
measurer.hit_test(
value,
font_size,
max_width,
point.x - scroll_geom.rect.origin.x + offset,
point.y - scroll_geom.rect.origin.y,
)
} else {
Self::caret_from_point_in_text_fallback(
value,
16.0,
scroll_geom.rect.origin.x,
scroll_geom.rect.size.width,
scroll_geom.content_size.width,
offset,
point.x,
)
};
let st = ctx.text_edit.get_mut_or_default(focused_id);
st.caret = caret;
st.anchor = caret;
Self::dispatch_cursor_change(ctx, sem, focused_id, caret, caret);
}
}
return true;
}
}
}
}
false
}
InputEvent::Pointer(PointerEvent::Move { point, .. }) => {
if let Some(focused_id) = ctx.interaction.focused {
if let Some(node) = ctx.ir.nodes.get(&focused_id) {
if let Op::Semantics(sem) = &node.op {
if sem.role == fission_ir::semantics::Role::TextInput {
if !ctx.interaction.pressed.is_empty() {
let mut moved_enough = true;
if let Some(start) = ctx.interaction.last_down_point {
let dx = point.x - start.x;
let dy = point.y - start.y;
if dx * dx + dy * dy < 4.0 {
moved_enough = false;
}
}
if moved_enough {
if let Some((
scroll_id,
_text_op_node_id,
scroll_direction,
)) = Self::find_scroll_container_and_text_op(
ctx.ir,
focused_id,
sem.multiline,
) {
if let Some(scroll_geom) =
ctx.layout.get_node_geometry(scroll_id)
{
let value = sem.value.as_deref().unwrap_or("");
let offset = ctx.scroll.get_offset(scroll_id);
let new_caret = if let Some(measurer) = ctx.measurer
{
let font_size = 16.0;
let max_width = if sem.multiline
&& scroll_geom.rect.width() > 0.0
{
Some(scroll_geom.rect.width())
} else {
None
};
measurer.hit_test(
value,
font_size,
max_width,
point.x - scroll_geom.rect.origin.x
+ offset,
point.y - scroll_geom.rect.origin.y,
)
} else {
Self::caret_from_point_in_text_fallback(
value,
16.0,
scroll_geom.rect.origin.x,
scroll_geom.rect.size.width,
scroll_geom.content_size.width,
offset,
point.x,
)
};
let st =
ctx.text_edit.get_mut_or_default(focused_id);
st.caret = new_caret;
let current_anchor = st.anchor;
Self::auto_scroll_textinput(ctx, focused_id);
Self::dispatch_cursor_change(ctx, sem, focused_id, new_caret, current_anchor);
}
}
}
}
return true;
}
}
}
}
false
}
_ => false,
}
}
}
impl TextInputController {
fn handle_key(
&mut self,
ctx: &mut ControllerContext,
key_code: KeyCode,
modifiers: u8,
) -> bool {
let focused_id = if let Some(id) = ctx.interaction.focused {
id
} else {
return false;
};
let mut semantics_node = None;
let mut current_id = Some(focused_id);
while let Some(node_id) = current_id {
if let Some(node) = ctx.ir.nodes.get(&node_id) {
if let Op::Semantics(s) = &node.op {
if s.role == fission_ir::semantics::Role::TextInput {
semantics_node = Some(s);
break;
}
}
current_id = node.parent;
} else {
break;
}
}
let semantics = if let Some(s) = semantics_node {
s
} else {
return false;
};
let (value, mut caret, mut anchor) =
Self::resolve_editing_value(ctx, focused_id, semantics.value.as_deref().unwrap_or(""));
caret = Self::clamp_caret_to_value(&value, caret);
anchor = Self::clamp_caret_to_value(&value, anchor);
let sel = if caret != anchor {
Some((anchor, caret))
} else {
None
};
// Logic for state changes
let mut next_caret = caret;
let mut next_anchor = anchor;
let mut next_text: Option<String> = None;
let mut handled = false;
// Undo/Redo logic result
let mut undo_redo_result: Option<(String, usize, usize)> = None;
match key_code {
KeyCode::Space => {
let (txt, c) = Self::insert_text(&value, caret, sel, " ");
next_text = Some(txt);
next_caret = c;
next_anchor = c;
handled = true;
}
KeyCode::Char(ch) if ((modifiers & 4) != 0) || ((modifiers & 8) != 0) => {
let lower = ch.to_ascii_lowercase();
let (s, e) = if caret <= anchor {
(caret, anchor)
} else {
(anchor, caret)
};
match lower {
'c' => {
if s != e {
let txt = value[s..e].to_string();
if let Some(cb) = ctx.clipboard {
cb.set_text(&txt);
}
}
handled = true;
}
'x' => {
if s != e {
let txt = value[s..e].to_string();
if let Some(cb) = ctx.clipboard {
cb.set_text(&txt);
}
let mut out = String::with_capacity(value.len() - (e - s));
out.push_str(&value[..s]);
out.push_str(&value[e..]);
next_text = Some(out);
next_caret = s;
next_anchor = s;
}
handled = true;
}
'v' => {
let text_to_paste = if let Some(cb) = ctx.clipboard {
cb.get_text().unwrap_or_default()
} else {
String::new()
};
if !text_to_paste.is_empty() {
let (txt, c) = Self::insert_text(&value, caret, sel, &text_to_paste);
next_text = Some(txt);
next_caret = c;
next_anchor = c;
}
handled = true;
}
'z' => {
let (ctrl_or_super, shift) = (
((modifiers & 4) != 0) || ((modifiers & 8) != 0),
(modifiers & 1) != 0,
);
if ctrl_or_super {
let st = ctx.text_edit.get_mut_or_default(focused_id);
if shift {
if let Some((v, c, a)) = st.history.redo() {
undo_redo_result = Some((v.clone(), *c, *a));
}
} else {
if let Some((v, c, a)) = st.history.undo() {
undo_redo_result = Some((v.clone(), *c, *a));
}
}
handled = true;
}
}
_ => {} // Do nothing for other shortcuts
}
}
KeyCode::Char(c) => {
// Check against input mask
if let Some(mask) = &semantics.input_mask {
if !mask.is_valid_char(c) {
return true; // Ignore invalid character
}
}
let (txt, nc) = Self::insert_text(&value, caret, sel, &c.to_string());
next_text = Some(txt);
next_caret = nc;
next_anchor = nc;
handled = true;
}
KeyCode::Backspace => {
let (txt, nc) = if (modifiers & 2) != 0 && sel.is_none() {
// Ctrl+Backspace
let mut at = caret;
while at > 0 {
let prev = Self::prev_grapheme_boundary(&value, at);
let ch = value[prev..].chars().next().unwrap_or('\0');
if !ch.is_whitespace() {
at = prev;
break;
}
at = prev;
}
while at > 0 {
let prev = Self::prev_grapheme_boundary(&value, at);
let ch = value[prev..].chars().next().unwrap_or('\0');
if ch.is_alphanumeric() || ch == '_' {
at = prev;
} else {
break;
}
}
let mut out = String::with_capacity(value.len() - (caret - at));
out.push_str(&value[..at]);
out.push_str(&value[caret..]);
(out, at)
} else {
Self::delete_prev_grapheme(&value, caret, sel)
};
next_text = Some(txt);
next_caret = nc;
next_anchor = nc;
handled = true;
}
KeyCode::Left => {
let prev = if (modifiers & 2) != 0 {
// Ctrl+Left
Self::prev_word_boundary(&value, caret)
} else {
Self::prev_grapheme_boundary(&value, caret)
};
next_caret = prev;
if (modifiers & 1) != 0 {
next_anchor = anchor;
} else {
next_anchor = prev;
}
handled = true;
}
KeyCode::Right => {
let next = if (modifiers & 2) != 0 {
// Ctrl+Right
Self::next_word_boundary(&value, caret)
} else {
Self::next_grapheme_boundary(&value, caret)
};
next_caret = next;
if (modifiers & 1) != 0 {
next_anchor = anchor;
} else {
next_anchor = next;
}
handled = true;
}
KeyCode::Home => {
next_caret = 0;
if (modifiers & 1) != 0 {
next_anchor = anchor;
} else {
next_anchor = 0;
}
handled = true;
}
KeyCode::End => {
let end = value.len();
next_caret = end;
if (modifiers & 1) != 0 {
next_anchor = anchor;
} else {
next_anchor = end;
}
handled = true;
}
KeyCode::Enter => {
if semantics.multiline {
let insert_str = if semantics.auto_indent {
// Find the leading whitespace of the current line
let line_start = value[..caret].rfind('\n').map(|p| p + 1).unwrap_or(0);
let leading: String = value[line_start..]
.chars()
.take_while(|c| *c == ' ' || *c == '\t')
.collect();
format!("\n{}", leading)
} else {
"\n".to_string()
};
let (txt, nc) = Self::insert_text(&value, caret, sel, &insert_str);
next_text = Some(txt);
next_caret = nc;
next_anchor = nc;
handled = true;
}
}
KeyCode::Up => {
if semantics.multiline {
self.handle_vertical_navigation(
ctx, focused_id, semantics, &value, caret, modifiers, true,
);
return true; // Return early as handle_vertical_navigation does its own state update
}
}
KeyCode::Down => {
if semantics.multiline {
self.handle_vertical_navigation(
ctx, focused_id, semantics, &value, caret, modifiers, false,
);
return true;
}
}
KeyCode::Tab => {
if semantics.capture_tab {
let tab_str = " "; // 4 spaces
let (txt, nc) = Self::insert_text(&value, caret, sel, tab_str);
next_text = Some(txt);
next_caret = nc;
next_anchor = nc;
handled = true;
}
// If capture_tab is false, fall through (return false) so focus
// navigation can handle Tab normally.
}
_ => {} // Do nothing for other keys
}
if let Some((v, c, a)) = undo_redo_result {
// Apply undo/redo result
let st = ctx.text_edit.get_mut_or_default(focused_id);
st.caret = c;
st.anchor = a;
st.last_value = v.clone();
self.dispatch_change(ctx, semantics, focused_id, v, c);
Self::dispatch_cursor_change(ctx, semantics, focused_id, c, a);
return true;
}
if let Some(txt) = next_text {
// Apply text change
let st = ctx.text_edit.get_mut_or_default(focused_id);
st.caret = next_caret;
st.anchor = next_anchor;
st.history.push(txt.clone(), next_caret, next_anchor);
st.last_value = txt.clone();
self.dispatch_change(ctx, semantics, focused_id, txt, next_caret);
Self::dispatch_cursor_change(ctx, semantics, focused_id, next_caret, next_anchor);
} else if handled {
// Cursor movement only
let st = ctx.text_edit.get_mut_or_default(focused_id);
st.caret = next_caret;
st.anchor = next_anchor;
Self::auto_scroll_textinput(ctx, focused_id);
Self::dispatch_cursor_change(ctx, semantics, focused_id, next_caret, next_anchor);
}
handled
}
fn handle_ime(&mut self, ctx: &mut ControllerContext, ime: &crate::event::ImeEvent) -> bool {
match ime {
crate::event::ImeEvent::Commit { text } => {
if let Some(focused_id) = ctx.interaction.focused {
if let Some(node) = ctx.ir.nodes.get(&focused_id) {
if let Op::Semantics(semantics) = &node.op {
if semantics.role == fission_ir::semantics::Role::TextInput {
let (value, caret, anchor) = Self::resolve_editing_value(
ctx,
focused_id,
semantics.value.as_deref().unwrap_or(""),
);
let st = ctx.text_edit.get_mut_or_default(focused_id);
let caret = Self::clamp_caret_to_value(&value, caret);
let sel = if caret != anchor {
Some((anchor, caret))
} else {
None
};
let mut filtered_text = String::new();
if let Some(mask) = &semantics.input_mask {
for ch in text.chars() {
if mask.is_valid_char(ch) {
filtered_text.push(ch);
}
}
} else {
filtered_text = text.clone();
}
if !filtered_text.is_empty() {
// Only insert if something valid
let (new_text, new_caret) =
Self::insert_text(&value, caret, sel, &filtered_text);
st.caret = new_caret;
st.anchor = new_caret;
st.last_value = new_text.clone();
st.history.push(new_text.clone(), new_caret, new_caret);
self.dispatch_change(
ctx, semantics, focused_id, new_text, new_caret,
);
}
*ctx.ime_preedit = None;
return true;
}
}
}
}
}
crate::event::ImeEvent::Preedit { text } => {
if let Some(focused_id) = ctx.interaction.focused {
*ctx.ime_preedit = Some((focused_id, text.clone()));
Self::auto_scroll_textinput(ctx, focused_id);
return true;
}
}
}
false
}
fn dispatch_change(
&self,
ctx: &mut ControllerContext,
semantics: &fission_ir::Semantics,
node_id: NodeId,
new_text: String,
new_caret: usize,
) {
if let Some(st) = ctx.text_edit.states.get_mut(&node_id) {
st.last_value = new_text.clone();
st.pending_model_sync = true;
}
if let Some(action_entry) = semantics.actions.entries.iter().find(|e| {
e.trigger == fission_ir::semantics::ActionTrigger::Change
}) {
let payload = serde_json::to_vec(&new_text).unwrap();
let envelope = ActionEnvelope {
id: ActionId::from_u128(action_entry.action_id),
payload,
};
ctx.dispatched_actions
.push((node_id, envelope, crate::ActionInput::None));
// State update moved to handle_key to avoid double borrow
Self::auto_scroll_textinput(ctx, node_id);
}
}
fn dispatch_cursor_change(
ctx: &mut ControllerContext,
semantics: &fission_ir::Semantics,
node_id: NodeId,
new_caret: usize,
new_anchor: usize,
) {
// Deduplicate: skip dispatch if cursor position hasn't actually changed
// since our last dispatch. This prevents unnecessary model updates that
// would trigger extra rebuild cycles.
if let Some(st) = ctx.text_edit.states.get(&node_id) {
if st.last_dispatched_cursor == Some((new_caret, new_anchor)) {
return;
}
}
if let Some(action_entry) = semantics.actions.entries.iter().find(|e| {
e.trigger == fission_ir::semantics::ActionTrigger::CursorChange
}) {
// Record the dispatched position before dispatching
if let Some(st) = ctx.text_edit.states.get_mut(&node_id) {
st.last_dispatched_cursor = Some((new_caret, new_anchor));
}
let cursor_changed = crate::action::CursorChanged {
caret: new_caret,
anchor: new_anchor,
};
let payload = serde_json::to_vec(&cursor_changed).unwrap();
let envelope = ActionEnvelope {
id: ActionId::from_u128(action_entry.action_id),
payload,
};
ctx.dispatched_actions
.push((node_id, envelope, crate::ActionInput::None));
}
}
fn resolve_editing_value(
ctx: &mut ControllerContext,
focused_id: NodeId,
semantic_value: &str,
) -> (String, usize, usize) {
let st = ctx.text_edit.get_mut_or_default(focused_id);
// If the latest lowered semantics value has caught up with local edits,
// stop treating local state as newer-than-model.
if st.pending_model_sync && st.last_value == semantic_value {
st.pending_model_sync = false;
}
// When we are not waiting for model sync, semantics is authoritative.
// This picks up external state changes (e.g. programmatic clears/sets).
if !st.pending_model_sync && st.last_value != semantic_value {
st.last_value = semantic_value.to_string();
st.caret = st.caret.min(st.last_value.len());
st.anchor = st.anchor.min(st.last_value.len());
st.history.push(st.last_value.clone(), st.caret, st.anchor);
}
let value = if st.pending_model_sync {
st.last_value.clone()
} else {
semantic_value.to_string()
};
if st.history.stack.is_empty() {
st.history.push(value.clone(), st.caret, st.anchor);
}
(value, st.caret, st.anchor)
}
fn clamp_caret_to_value(value: &str, caret: usize) -> usize {
if caret > value.len() {
value.len()
} else {
caret
}
}
fn prev_grapheme_boundary(value: &str, idx: usize) -> usize {
let mut last = 0;
for (pos, _) in value.grapheme_indices(true) {
if pos >= idx {
break;
}
last = pos;
}
last
}
fn next_grapheme_boundary(value: &str, idx: usize) -> usize {
for (pos, _) in value.grapheme_indices(true) {
if pos > idx {
return pos;
}
}
value.len()
}
fn prev_word_boundary(value: &str, idx: usize) -> usize {
let mut at = idx.min(value.len());
while at > 0 {
let prev = Self::prev_grapheme_boundary(value, at);
let ch = value[prev..].chars().next().unwrap_or('\0');
if !ch.is_whitespace() {
at = prev;
break;
}
at = prev;
}
while at > 0 {
let prev = Self::prev_grapheme_boundary(value, at);
let ch = value[prev..].chars().next().unwrap_or('\0');
if ch.is_alphanumeric() || ch == '_' {
at = prev;
} else {
break;
}
}
at
}
fn next_word_boundary(value: &str, idx: usize) -> usize {
let mut at = idx.min(value.len());
while at < value.len() {
let next = Self::next_grapheme_boundary(value, at);
let ch = value[at..].chars().next().unwrap_or('\0');
if !ch.is_whitespace() {
at = next;
break;
}
at = next;
}
while at < value.len() {
let next = Self::next_grapheme_boundary(value, at);
let ch = value[at..].chars().next().unwrap_or('\0');
if ch.is_alphanumeric() || ch == '_' {
at = next;
} else {
break;
}
}
at
}
fn delete_prev_grapheme(
value: &str,
caret: usize,
sel: Option<(usize, usize)>,
) -> (String, usize) {
if let Some((a, b)) = sel {
let (s, e) = if a <= b { (a, b) } else { (b, a) };
let mut out = String::with_capacity(value.len() - (e - s));
out.push_str(&value[..s]);
out.push_str(&value[e..]);
return (out, s);
}
let at = caret.min(value.len());
if at == 0 {
return (value.to_string(), 0);
}
let prev = Self::prev_grapheme_boundary(value, at);
let mut out = String::with_capacity(value.len() - (at - prev));
out.push_str(&value[..prev]);
out.push_str(&value[at..]);
(out, prev)
}
fn insert_text(
value: &str,
caret: usize,
sel: Option<(usize, usize)>,
text: &str,
) -> (String, usize) {
let (s, e) = sel
.map(|(a, b)| if a <= b { (a, b) } else { (b, a) })
.unwrap_or((caret, caret));
let mut out = String::with_capacity(value.len() - (e - s) + text.len());
out.push_str(&value[..s]);
out.push_str(text);
out.push_str(&value[e..]);
(out, s + text.len())
}
fn find_scroll_container_and_text_op(
ir: &fission_ir::CoreIR,
root: NodeId,
multiline_semantics: bool,
) -> Option<(NodeId, NodeId, op::FlexDirection)> {
let mut stack = vec![root];
while let Some(id) = stack.pop() {
if let Some(n) = ir.nodes.get(&id) {
if let Op::Layout(op::LayoutOp::Scroll { direction, .. }) = &n.op {
let matches_multiline_config = (multiline_semantics
&& *direction == op::FlexDirection::Column)
|| (!multiline_semantics && *direction == op::FlexDirection::Row);
if matches_multiline_config {
let mut q = vec![id]; // Start BFS from scroll node to find text
while let Some(cid) = q.pop() {
if let Some(cn) = ir.nodes.get(&cid) {
if matches!(
cn.op,
Op::Paint(fission_ir::PaintOp::DrawText { .. })
| Op::Paint(fission_ir::PaintOp::DrawRichText { .. })
) {
return Some((id, cid, *direction));
}
for &gc in &cn.children {
q.push(gc);
}
}
}
return None; // Should find text inside. For now, assume it's directly related.
}
}
for &c in &n.children {
stack.push(c);
}
}
}
None
}
fn find_caret_in_scroll(ir: &fission_ir::CoreIR, scroll_id: NodeId) -> Option<NodeId> {
let mut q = vec![scroll_id];
while let Some(id) = q.pop() {
if let Some(n) = ir.nodes.get(&id) {
if let Op::Layout(op::LayoutOp::Box { width: Some(w), .. }) = &n.op {
if (*w - 2.0).abs() < 0.01 {
let mut has_paint = false;
for &cid in &n.children {
if let Some(cn) = ir.nodes.get(&cid) {
if let Op::Paint(fission_ir::PaintOp::DrawRect { .. }) = cn.op {
has_paint = true;
break;
}
}
}
if has_paint {
return Some(id);
}
}
}
for &c in &n.children {
q.push(c);
}
}
}
None
}
fn caret_from_point_in_text_fallback(
value: &str,
font_size: f32,
viewport_x: f32,
viewport_w: f32,
content_w: f32,
scroll_offset: f32,
point_x: f32,
) -> usize {
// Simplified fallback: always return 0 if no proper measurer is available.
// In a real scenario, this would ideally not be hit in interactive UIs.
0
}
fn auto_scroll_textinput(ctx: &mut ControllerContext, text_root: NodeId) {
let font_size = 16.0; // Default font size
if let Some(measurer) = ctx.measurer {
// Need to get multiline status from semantics here
let is_multiline = if let Some(node) = ctx.ir.nodes.get(&text_root) {
if let Op::Semantics(sem) = &node.op {
sem.multiline
} else {
false
}
} else {
false
};
if let Some((scroll_id, _text_op_node_id, scroll_direction)) =
Self::find_scroll_container_and_text_op(ctx.ir, text_root, is_multiline)
{
if let Some(scroll_geom) = ctx.layout.get_node_geometry(scroll_id) {
let viewport_size = scroll_geom.rect.size;
let current_text_value = if let Some(node) = ctx.ir.nodes.get(&text_root) {
if let Op::Semantics(sem) = &node.op {
sem.value.clone().unwrap_or_default()
} else {
String::new()
}
} else {
String::new()
};
let current_caret_idx = if let Some(st) = ctx.text_edit.get(text_root) {
st.caret
} else {
0
};
let measurer_width = if scroll_direction == op::FlexDirection::Column {
Some(viewport_size.width)
} else {
None
};
let (caret_x, caret_y) = measurer.get_caret_position(
¤t_text_value,
font_size,
measurer_width,
current_caret_idx,
);
let mut offset = ctx.scroll.get_offset(scroll_id);
if scroll_direction == op::FlexDirection::Row {
// Handle horizontal scrolling for single-line text
let caret_left = caret_x;
let caret_width = 2.0f32;
let caret_right = caret_left + caret_width;
let margin_left = 2.0f32;
let margin_right = 3.0f32;
let visible_left = caret_left - offset;
let visible_right = caret_right - offset;
if visible_right > (viewport_size.width - margin_right) {
offset =
(caret_right - (viewport_size.width - margin_right)).max(0.0f32);
} else if visible_left < margin_left {
offset = (caret_left - margin_left).max(0.0f32);
}
let content_w = scroll_geom.content_size.width.max(viewport_size.width);
let max_offset = (content_w - viewport_size.width).max(0.0f32);
offset = offset.clamp(0.0f32, max_offset);
ctx.scroll.set_offset(scroll_id, offset);
} else {
// op::FlexDirection::Column
// Handle vertical scrolling for multi-line text
let caret_top = caret_y;
let caret_height = measurer
.measure("Tg", font_size, Some(viewport_size.width))
.1;
let caret_bottom = caret_top + caret_height;
let margin_top = 2.0f32;
let margin_bottom = 3.0f32;
let visible_top = caret_top - offset;
let visible_bottom = caret_bottom - offset;
if visible_bottom > (viewport_size.height - margin_bottom) {
offset =
(caret_bottom - (viewport_size.height - margin_bottom)).max(0.0f32);
} else if visible_top < margin_top {
offset = (caret_top - margin_top).max(0.0f32);
}
let content_h = scroll_geom.content_size.height.max(viewport_size.height);
let max_offset = (content_h - viewport_size.height).max(0.0f32);
offset = offset.clamp(0.0f32, max_offset);
ctx.scroll.set_offset(scroll_id, offset);
}
}
}
}
}
fn handle_vertical_navigation(
&mut self,
ctx: &mut ControllerContext,
focused_id: NodeId,
semantics: &Semantics,
value: &str,
caret: usize,
modifiers: u8,
is_up: bool,
) {
if let Some(measurer) = ctx.measurer {
if let Some((scroll_id, _text_op_node_id, scroll_direction)) =
Self::find_scroll_container_and_text_op(ctx.ir, focused_id, semantics.multiline)
{
if let Some(scroll_geom) = ctx.layout.get_node_geometry(scroll_id) {
let viewport_w = scroll_geom.rect.size.width;
let font_size = 16.0;
let (current_caret_x, current_caret_y) =
measurer.get_caret_position(value, font_size, Some(viewport_w), caret);
let line_metrics =
measurer.get_line_metrics(value, font_size, Some(viewport_w));
let mut current_line_idx = 0;
for (idx, line) in line_metrics.iter().enumerate() {
if caret >= line.start_index && caret <= line.end_index {
current_line_idx = idx;
break;
}
}
let target_line_idx = if is_up {
current_line_idx.saturating_sub(1)
} else {
(current_line_idx + 1).min(line_metrics.len().saturating_sub(1))
};
if let Some(target_line) = line_metrics.get(target_line_idx) {
let target_y = target_line.baseline;
let mut new_caret_pos = measurer.hit_test(
value,
font_size,
Some(viewport_w),
current_caret_x,
target_y,
);
// Ensure we stay within the target line's bounds
new_caret_pos = new_caret_pos.max(target_line.start_index).min(target_line.end_index);
let st = ctx.text_edit.get_mut_or_default(focused_id);
st.caret = new_caret_pos;
if (modifiers & 1) == 0 {
st.anchor = new_caret_pos;
} // If no shift, collapse selection
let final_anchor = st.anchor;
Self::auto_scroll_textinput(ctx, focused_id);
Self::dispatch_cursor_change(ctx, semantics, focused_id, new_caret_pos, final_anchor);
}
}
}
}
}
}
// This pub fn is no longer needed since Controller uses measurer directly in handle_event
// But other parts of code might still call it, so keep it.
pub fn caret_from_point_in_text(
measurer: Option<&std::sync::Arc<dyn fission_layout::TextMeasurer>>,
value: &str,
font_size: f32,
viewport_x: f32,
viewport_w: f32,
content_w: f32,
scroll_offset: f32,
point_x: f32,
) -> usize {
let local_x = (point_x - viewport_x) + scroll_offset;
if local_x <= 0.0 {
return 0;
}
let max_x = content_w.max(viewport_w);
if local_x >= max_x {
return value.len();
}
if let Some(measurer) = measurer {
// This function is for single line mostly. measurer.hit_test is better.
// Single-line hit-testing should not wrap text to the viewport width.
measurer.hit_test(value, font_size, None, local_x, 0.0)
} else {
TextInputController::caret_from_point_in_text_fallback(
value,
font_size,
viewport_x,
viewport_w,
content_w,
scroll_offset,
point_x,
)
}
}