lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
Documentation
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
//! Styled spans of a text input: kept in the app's spans signal, moved with
//! every edit, recorded in the undo history with the text, and reported to
//! the app after every change the input makes.
use std::ops::Range;

use super::*;
use crate::components::{
  InlineObject, LinkClick, ParagraphKind, ParagraphStyles, RichText, SelectionStyle, SpanStyle, StyledChange,
  StyledEdit, StyledSpan, StyledText, TextMark, styled_text as spans_ops,
};

type ChangeHandler = Arc<dyn Fn(&StyledChange) + Send + Sync>;
type CopyFormat = Arc<dyn Fn(&StyledText) -> String + Send + Sync>;
type LinkHandler = Arc<dyn Fn(&LinkClick) + Send + Sync>;

/// The spans in canonical form for one version of the value and of the
/// spans signal, so reading them copies nothing until either changes.
#[derive(Clone)]
struct SpansCache {
  value_version: u64,
  spans_version: u64,
  spans: Arc<Vec<StyledSpan>>,
}

/// What makes an input styled. Its hooks come from the builder on every
/// render; the rest is runtime state kept across renders.
#[derive(Clone, Default)]
pub(super) struct StyledState {
  spans: Option<Signal<Vec<StyledSpan>>>,
  /// The spans signal's version the input last wrote or read, to notice an
  /// app's write and lay the input out again.
  synced_version: u64,
  cache: Option<SpansCache>,
  /// What each paragraph is, where the input has kinds of paragraphs, and how each kind is drawn.
  paragraphs: Option<Signal<Vec<ParagraphKind>>>,
  paragraph_styles: Option<Arc<ParagraphStyles>>,
  /// The paragraphs signal's version the input last wrote or read.
  paragraphs_synced: u64,
  /// The things in the text that are not text, where the input has them: see [`InlineObject`].
  objects: Option<Signal<Vec<InlineObject>>>,
  /// The objects signal's version the input last wrote or read.
  objects_synced: u64,
  /// The objects the input's child nodes were built for, a child for each, in order. An edit moves the objects
  /// before the app builds the children again: until then a child is told from the others by what it shows.
  object_nodes: Arc<[InlineObject]>,
  /// The style the next typed text takes, set by the app, while the caret
  /// stays at the offset it was set at with nothing selected.
  pending: Option<(usize, SpanStyle)>,
  /// Whether an object selected alone has a handle that a drag makes it wider or narrower by.
  resizable_objects: bool,
  /// The drag of that handle in progress.
  resize: Option<ObjectResize>,
  /// Whether the pointer is on the handle.
  handle_hovered: bool,
  on_change: Vec<ChangeHandler>,
  copy_format: Option<CopyFormat>,
  /// A copy keeps the Markdown of the selection beside its text, and a paste that has Markdown is inserted with
  /// its marks and kinds.
  rich_clipboard: bool,
  on_link_click: Vec<LinkHandler>,
}

impl StyledState {
  pub(super) fn is_bound(&self) -> bool {
    self.spans.is_some()
  }

  pub(super) fn copy_runtime_state_from(&mut self, old: &Self) {
    if let (Some(new), Some(previous)) = (&self.spans, &old.spans)
      && new.id() == previous.id()
    {
      self.synced_version = old.synced_version;
      self.cache = old.cache.clone();
    }
    if let (Some(new), Some(previous)) = (&self.paragraphs, &old.paragraphs)
      && new.id() == previous.id()
    {
      self.paragraphs_synced = old.paragraphs_synced;
    }
    if let (Some(new), Some(previous)) = (&self.objects, &old.objects)
      && new.id() == previous.id()
    {
      self.objects_synced = old.objects_synced;
    }
    self.pending = old.pending.clone();
    // A drag of an object's handle goes on over the renders its own changes ask for.
    self.resize = old.resize.clone();
    self.handle_hovered = old.handle_hovered;
  }
}

/// How near the corner of an object the pointer takes its handle, and the least an object is made.
const OBJECT_HANDLE_REACH: f32 = 8.0;
const OBJECT_LEAST_WIDTH: f32 = 16.0;

/// A drag of the handle of an object: the byte of the object, where the drag began in the text and how wide the
/// object was then, the most it can be, and whether the drag changed it yet.
#[derive(Clone)]
struct ObjectResize {
  at: usize,
  from_x: f32,
  from_width: f32,
  most: f32,
  changed: bool,
}

/// The spans and the typing style captured before an edit changes the text.
pub(super) struct SpansBeforeEdit {
  spans: Arc<Vec<StyledSpan>>,
  style: SpanStyle,
  /// The kinds of the paragraphs and where each paragraph was, where the input has kinds.
  paragraphs: Option<(SnapshotParagraphs, Vec<Range<usize>>)>,
  /// The objects of the text, where the input has them.
  objects: Option<SnapshotObjects>,
}

/// The spans a snapshot of the undo history keeps, shared.
pub(super) type SnapshotSpans = Arc<Vec<StyledSpan>>;

/// The kinds of the paragraphs a snapshot of the undo history keeps, shared.
pub(super) type SnapshotParagraphs = Arc<Vec<ParagraphKind>>;

/// The objects a snapshot of the undo history keeps, shared.
pub(super) type SnapshotObjects = Arc<Vec<InlineObject>>;

/// Text inserted with its own spans, with the kinds of its paragraphs where it brings them, and with the objects
/// that are in it, their bytes counted from its start.
pub(super) struct InsertedText<'a> {
  pub(super) styled: &'a StyledText,
  pub(super) kinds: Option<&'a [ParagraphKind]>,
  pub(super) objects: &'a [InlineObject],
}

impl TextInputState {
  pub(crate) fn set_spans_signal(&self, spans: Signal<Vec<StyledSpan>>) {
    let mut inner = self.inner.lock();
    if inner
      .styled
      .spans
      .as_ref()
      .is_some_and(|bound| bound.id() == spans.id())
    {
      return;
    }
    inner.styled.synced_version = spans.version();
    inner.styled.cache = None;
    inner.styled.spans = Some(spans);
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// Binds the kinds of the input's paragraphs and how each kind is drawn.
  pub(crate) fn set_paragraphs(&self, kinds: Signal<Vec<ParagraphKind>>, styles: ParagraphStyles) {
    let mut inner = self.inner.lock();
    let same_signal = inner
      .styled
      .paragraphs
      .as_ref()
      .is_some_and(|bound| bound.id() == kinds.id());
    let same_styles = inner
      .styled
      .paragraph_styles
      .as_ref()
      .is_some_and(|held| **held == styles);
    if same_signal && same_styles {
      return;
    }
    if !same_signal {
      inner.styled.paragraphs_synced = kinds.version();
      inner.styled.paragraphs = Some(kinds);
    }
    if !same_styles {
      inner.styled.paragraph_styles = Some(Arc::new(styles));
    }
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// Binds the objects of the input's text. `built` are those its child nodes were built for, a child for each,
  /// in order.
  pub(crate) fn set_objects(&self, objects: Signal<Vec<InlineObject>>, built: Vec<InlineObject>) {
    let mut inner = self.inner.lock();
    let same_signal = inner
      .styled
      .objects
      .as_ref()
      .is_some_and(|bound| bound.id() == objects.id());
    let same_nodes = inner.styled.object_nodes.as_ref() == built.as_slice();
    if !same_signal {
      inner.styled.objects_synced = objects.version();
      inner.styled.objects = Some(objects);
    }
    if !same_nodes {
      inner.styled.object_nodes = built.into();
    }
    drop(inner);
    if !(same_signal && same_nodes) {
      self.mark_layout_dirty_keeping_scroll();
    }
  }

  /// The objects of the current text, in its order, shared; `None` for an input without objects.
  pub(crate) fn current_objects(&self) -> Option<SnapshotObjects> {
    let signal = {
      let inner = self.inner.lock();
      if inner.mask.is_some() || inner.styled.spans.is_none() {
        return None;
      }
      inner.styled.objects.clone()?
    };
    let value = self.shared_value();
    Some(Arc::new(signal.with_untracked(|objects| {
      spans_ops::normalize_objects(&value, objects.clone())
    })))
  }

  /// The objects of the text the input shows: the current ones, moved over a composition in progress.
  pub(crate) fn shown_objects(&self) -> Option<SnapshotObjects> {
    let objects = self.current_objects()?;
    let Some(composed) = self.composed_text() else {
      return Some(objects);
    };
    let value = self.shared_value();
    let (start, end) = self.selection_or_caret(&value);
    let inserted = composed.len() + (end - start) - value.len();
    Some(Arc::new(spans_ops::splice_objects(&objects, start..end, inserted)))
  }

  /// For each object the input shows, in order, which of the input's child nodes was built for it: the next
  /// one, in the order they were built, that shows the same thing. `None` for an object no child was built for
  /// yet; a child no object is left for is in none.
  pub(crate) fn object_children(&self) -> Vec<Option<usize>> {
    let Some(objects) = self.shown_objects() else {
      return Vec::new();
    };
    let built = self.inner.lock().styled.object_nodes.clone();
    let mut next = 0;
    objects
      .iter()
      .map(|object| {
        let found = next + built[next..].iter().position(|node| node.same_thing(object))?;
        next = found + 1;
        Some(found)
      })
      .collect()
  }

  /// The bytes of each object of the current text: what is one thing to the caret in a styled input.
  pub(super) fn object_ranges(&self) -> Vec<Range<usize>> {
    self
      .current_objects()
      .map(|objects| objects.iter().map(InlineObject::range).collect())
      .unwrap_or_default()
  }

  /// The object whose box is at `(x, y)`, in the coordinates the caret is placed by: its bytes.
  pub(super) fn object_at_point(&self, x: f32, y: f32) -> Option<Range<usize>> {
    let objects = self.current_objects().filter(|objects| !objects.is_empty())?;
    let layout = self.token_layout()?;
    let (x, y) = {
      let inner = self.inner.lock();
      (x + inner.scroll_x, y + inner.scroll_y())
    };
    let room = layout
      .objects
      .iter()
      .find(|room| x >= room.x && x < room.x + room.width && y >= room.y && y < room.y + room.height)?;
    objects
      .iter()
      .find(|object| object.at == room.byte)
      .map(InlineObject::range)
  }

  pub(crate) fn set_resizable_objects(&self, resizable: bool) {
    self.inner.lock().styled.resizable_objects = resizable;
  }

  /// The object that has a handle: the one the selection is, where it is one object and nothing else, in an
  /// input whose objects are resized. Its box, in the coordinates the glyphs have, and its record.
  pub(crate) fn resized_object(
    &self,
  ) -> Option<(crate::app::glyph_engine::input_tokens::InputObjectBox, InlineObject)> {
    if !self.inner.lock().styled.resizable_objects {
      return None;
    }
    let (start, end) = self.selection_bounds()?;
    let objects = self.current_objects()?;
    let object = objects
      .iter()
      .find(|object| object.at == start && object.range().end == end)?
      .clone();
    let layout = self.token_layout()?;
    let room = *layout.objects.iter().find(|room| room.byte == object.at)?;
    Some((room, object))
  }

  /// The object whose handle is at `(x, y)`, in the coordinates the caret is placed by.
  fn object_handle_at(
    &self,
    x: f32,
    y: f32,
  ) -> Option<(crate::app::glyph_engine::input_tokens::InputObjectBox, InlineObject)> {
    let (room, object) = self.resized_object()?;
    let (x, y) = {
      let inner = self.inner.lock();
      (x + inner.scroll_x, y + inner.scroll_y())
    };
    let near = |point: f32, corner: f32| (point - corner).abs() <= OBJECT_HANDLE_REACH;
    (near(x, room.x + room.width) && near(y, room.y + room.height)).then_some((room, object))
  }

  /// Starts a drag of the handle at `(x, y)`: `false` where there is none there.
  pub(super) fn begin_object_resize(&self, x: f32, y: f32) -> bool {
    self.inner.lock().styled.resize = None;
    let Some((room, object)) = self.object_handle_at(x, y) else {
      return false;
    };
    let most = self
      .token_layout()
      .map(|layout| layout.config.width)
      .filter(|width| width.is_finite() && *width < 100_000.0)
      .unwrap_or(f32::MAX);
    let mut inner = self.inner.lock();
    inner.styled.resize = Some(ObjectResize {
      at: object.at,
      from_x: x + inner.scroll_x,
      from_width: room.width,
      most,
      changed: false,
    });
    true
  }

  /// Moves a drag of a handle to `x`: the object is as much wider as the pointer went. `false` where no drag
  /// is in progress.
  pub(super) fn drag_object_resize(&self, x: f32) -> bool {
    let (resize, scroll_x) = {
      let inner = self.inner.lock();
      let Some(resize) = inner.styled.resize.clone() else {
        return false;
      };
      (resize, inner.scroll_x)
    };
    let least = OBJECT_LEAST_WIDTH.min(resize.most);
    let width = (resize.from_width + x + scroll_x - resize.from_x).clamp(least, resize.most.max(least));
    let width = width.round() as u32;
    let Some(objects) = self.current_objects() else {
      return true;
    };
    let Some(index) = objects.iter().position(|object| object.at == resize.at) else {
      return true;
    };
    // An object with no width of its own is as wide as it was laid out until the drag moves it.
    let held = objects[index].width.unwrap_or(resize.from_width.round() as u32);
    if held == width {
      return true;
    }
    if !resize.changed {
      self.push_undo_snapshot();
      if let Some(resize) = self.inner.lock().styled.resize.as_mut() {
        resize.changed = true;
      }
    }
    let mut changed = objects.to_vec();
    changed[index].width = Some(width);
    self.write_objects(changed);
    self.mark_layout_dirty_keeping_scroll();
    true
  }

  /// Ends a drag of a handle: what it changed is reported, once.
  pub(crate) fn end_object_resize(&self) {
    let Some(resize) = self.inner.lock().styled.resize.take() else {
      return;
    };
    if !resize.changed {
      return;
    }
    if let Some(spans) = self.current_spans() {
      let range = resize.at..resize.at + crate::components::OBJECT_CHARACTER.len_utf8();
      self.commit_spans(spans.to_vec(), StyledEdit::Style { range });
    }
  }

  /// Notes whether the pointer, at `point` in the coordinates the caret is placed by, is on a handle.
  pub(super) fn hover_object_handle(&self, point: Option<(f32, f32)>) {
    let on = point.is_some_and(|(x, y)| self.object_handle_at(x, y).is_some());
    self.inner.lock().styled.handle_hovered = on;
  }

  /// Whether the pointer is on the handle of an object, or drags it: what the cursor says.
  pub(crate) fn object_handle_hovered(&self) -> bool {
    let inner = self.inner.lock();
    inner.styled.handle_hovered || inner.styled.resize.is_some()
  }

  /// Writes the objects, without a report.
  fn write_objects(&self, objects: Vec<InlineObject>) {
    let Some(signal) = self.inner.lock().styled.objects.clone() else {
      return;
    };
    if signal.with_untracked(|current| *current != objects) {
      signal.set(objects);
    }
    self.inner.lock().styled.objects_synced = signal.version();
  }

  /// Puts back the objects of an undo snapshot. The spans are put back after them, which reports the change.
  pub(super) fn restore_objects(&self, objects: SnapshotObjects) {
    self.write_objects(objects.to_vec());
  }

  /// Moves the objects over an edit that replaced `range` with `inserted` bytes, among them `added`, whose
  /// bytes are counted from the start of what was inserted: before the spans are committed and the change is
  /// reported.
  fn splice_objects(&self, edit: &SpansBeforeEdit, range: &Range<usize>, inserted: usize, added: &[InlineObject]) {
    let Some(objects) = &edit.objects else {
      return;
    };
    let moved = spans_ops::splice_objects(objects, range.clone(), inserted);
    let moved = spans_ops::place_objects(moved, range.start, added);
    self.write_objects(spans_ops::normalize_objects(&self.shared_value(), moved));
  }

  /// Puts an object in place of the selection, as one undo step. `false` for an input without objects, and
  /// where an `on_input` handler refused the edit.
  pub(crate) fn insert_object(&self, object: &InlineObject, keyboard: &KeyboardEvent) -> bool {
    if self.current_objects().is_none() {
      return false;
    }
    let text = StyledText::plain(crate::components::OBJECT_CHARACTER.to_string());
    let object = InlineObject {
      at: 0,
      ..object.clone()
    };
    let insert = InsertedText {
      styled: &text,
      kinds: None,
      objects: std::slice::from_ref(&object),
    };
    self.replace_selection_inserting(&insert, keyboard)
  }

  /// The kinds of the paragraphs of the current text, one for each, shared;
  /// `None` for an input without kinds of paragraphs.
  pub(crate) fn current_paragraphs(&self) -> Option<SnapshotParagraphs> {
    let signal = {
      let inner = self.inner.lock();
      if inner.mask.is_some() || inner.styled.spans.is_none() {
        return None;
      }
      inner.styled.paragraphs.clone()?
    };
    let count = spans_ops::paragraph_ranges(&self.shared_value()).len();
    Some(Arc::new(
      signal.with_untracked(|kinds| spans_ops::normalize_kinds(kinds.clone(), count)),
    ))
  }

  /// What the input draws its paragraphs as: the kind of each paragraph of
  /// the text it shows (with a composition in progress), and the styles of
  /// the kinds.
  pub(crate) fn paragraph_display(&self) -> Option<(SnapshotParagraphs, Arc<ParagraphStyles>)> {
    let styles = self.inner.lock().styled.paragraph_styles.clone()?;
    let kinds = self.current_paragraphs()?;
    let Some(composed) = self.composed_text() else {
      return Some((kinds, styles));
    };
    let value = self.shared_value();
    let (start, end) = self.selection_or_caret(&value);
    let inserted = composed.len() + (end - start) - value.len();
    let shown = spans_ops::splice_kinds(
      &kinds,
      &spans_ops::paragraph_ranges(&value),
      &spans_ops::paragraph_ranges(&composed),
      start..end,
      inserted,
    );
    Some((Arc::new(shown), styles))
  }

  /// Writes the kinds of the paragraphs, without a report.
  fn write_paragraphs(&self, kinds: Vec<ParagraphKind>) {
    let Some(signal) = self.inner.lock().styled.paragraphs.clone() else {
      return;
    };
    if signal.with_untracked(|current| *current != kinds) {
      signal.set(kinds);
    }
    self.inner.lock().styled.paragraphs_synced = signal.version();
  }

  /// Puts back the kinds of an undo snapshot. The spans are put back after
  /// them, which reports the change.
  pub(super) fn restore_paragraphs(&self, kinds: SnapshotParagraphs) {
    self.write_paragraphs(kinds.to_vec());
  }

  /// The paragraphs the selection touches, or the one the caret is in: their
  /// indices, and where each paragraph of the text is.
  fn selected_paragraph_range(&self) -> (Range<usize>, Vec<Range<usize>>) {
    let value = self.shared_value();
    let ranges = spans_ops::paragraph_ranges(&value);
    let (start, end) = self.selection_or_caret(&value);
    let first = spans_ops::paragraph_of(&ranges, start);
    let last = spans_ops::paragraph_of(&ranges, end);
    (first..last + 1, ranges)
  }

  /// The kinds of the paragraphs the selection touches, in order.
  pub(crate) fn selected_paragraphs(&self) -> Option<Vec<ParagraphKind>> {
    let kinds = self.current_paragraphs()?;
    let (range, _) = self.selected_paragraph_range();
    Some(kinds[range].to_vec())
  }

  /// The paragraph the caret is in: its index, its kind and where its text is.
  pub(crate) fn caret_paragraph(&self) -> Option<(usize, ParagraphKind, Range<usize>)> {
    let kinds = self.current_paragraphs()?;
    let value = self.shared_value();
    let ranges = spans_ops::paragraph_ranges(&value);
    let caret = clamp_to_char_boundary(&value, self.inner.lock().caret);
    let index = spans_ops::paragraph_of(&ranges, caret);
    Some((index, kinds[index], ranges[index].clone()))
  }

  /// Makes every paragraph the selection touches a `kind`, as one undo
  /// step. `false` where they all are already, and for an input without
  /// kinds of paragraphs.
  pub(crate) fn set_paragraph_kind(&self, kind: ParagraphKind) -> bool {
    let Some(kinds) = self.current_paragraphs() else {
      return false;
    };
    let kind = kind.canonical();
    let (range, _) = self.selected_paragraph_range();
    if kinds[range.clone()].iter().all(|held| *held == kind) {
      return false;
    }
    let mut changed = kinds.to_vec();
    changed[range.clone()].fill(kind);
    self.push_undo_snapshot();
    self.write_paragraphs(changed);
    if let Some(spans) = self.current_spans() {
      self.commit_spans(spans.to_vec(), StyledEdit::Paragraphs { range });
    }
    self.mark_layout_dirty();
    true
  }

  /// Moves the kinds of the paragraphs over an edit, before its spans are
  /// committed and the change is reported.
  fn splice_paragraphs(&self, edit: &SpansBeforeEdit, range: &Range<usize>, inserted: usize) {
    let Some((kinds, before)) = &edit.paragraphs else {
      return;
    };
    let after = spans_ops::paragraph_ranges(&self.shared_value());
    self.write_paragraphs(spans_ops::splice_kinds(kinds, before, &after, range.clone(), inserted));
  }

  pub(crate) fn add_styled_change_handler(&self, handler: impl Fn(&StyledChange) + Send + Sync + 'static) {
    self.inner.lock().styled.on_change.push(Arc::new(handler));
  }

  pub(crate) fn set_copy_format(&self, format: impl Fn(&StyledText) -> String + Send + Sync + 'static) {
    self.inner.lock().styled.copy_format = Some(Arc::new(format));
  }

  pub(crate) fn add_link_click_handler(&self, handler: impl Fn(&LinkClick) + Send + Sync + 'static) {
    self.inner.lock().styled.on_link_click.push(Arc::new(handler));
  }

  /// Whether the input edits styled text: it has a spans signal and is not
  /// masked.
  pub(crate) fn is_styled(&self) -> bool {
    let inner = self.inner.lock();
    inner.styled.spans.is_some() && inner.mask.is_none()
  }

  /// The value. A copy: the value signal's own allocation is never shared,
  /// since `Signal::update` writes it in place.
  fn shared_value(&self) -> String {
    self.value()
  }

  /// The spans of the current text in canonical form, shared; `None` for a
  /// plain input. They are normalized again only after the value or the
  /// spans signal changed.
  pub(crate) fn current_spans(&self) -> Option<SnapshotSpans> {
    let (signal, cache) = {
      let inner = self.inner.lock();
      if inner.mask.is_some() {
        return None;
      }
      (inner.styled.spans.clone()?, inner.styled.cache.clone())
    };
    let versions = (self.value.version(), signal.version());
    if let Some(cache) = cache.filter(|cache| (cache.value_version, cache.spans_version) == versions) {
      return Some(cache.spans);
    }
    let value = self.shared_value();
    let spans = Arc::new(signal.with_untracked(|spans| spans_ops::normalize(&value, spans.clone())));
    self.inner.lock().styled.cache = Some(SpansCache {
      value_version: versions.0,
      spans_version: versions.1,
      spans: spans.clone(),
    });
    Some(spans)
  }

  pub(crate) fn styled_text(&self) -> Option<StyledText> {
    let spans = self.current_spans()?;
    Some(StyledText {
      text: self.value(),
      spans: spans.to_vec(),
    })
  }

  /// What the input draws: the text, with a composition in progress in the
  /// style typing would give it, and its spans. `None` for a plain input
  /// and for an empty one showing its placeholder, unless `empty` asks for that one too: an input whose kinds
  /// of paragraphs are drawn lays its one empty paragraph out as what it is, and its placeholder in it.
  pub(crate) fn styled_display(&self, empty: bool) -> Option<(Arc<str>, SnapshotSpans)> {
    let spans = self.current_spans()?;
    let Some(composed) = self.composed_text() else {
      if self.is_showing_placeholder() && !(empty && self.current_paragraphs().is_some()) {
        return None;
      }
      return Some((self.shared_value().into(), spans));
    };
    let value = self.shared_value();
    let (start, end) = self.selection_or_caret(&value);
    let inserted = composed.len() + (end - start) - value.len();
    let style = self.typing_style_for(&value, &spans);
    let spans = spans_ops::splice(&spans, start..end, inserted, &style);
    Some((composed.into(), Arc::new(spans)))
  }

  /// Notices an app's write of the objects signal: the input lays out again.
  fn sync_external_objects(&self) {
    let mut inner = self.inner.lock();
    let Some(version) = inner.styled.objects.as_ref().map(Signal::version) else {
      return;
    };
    if version == inner.styled.objects_synced {
      return;
    }
    inner.styled.objects_synced = version;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// Notices an app's write of the spans signal: the input lays out again
  /// and drops a pending typing style.
  pub(super) fn sync_external_spans(&self) {
    self.sync_external_paragraphs();
    self.sync_external_objects();
    let mut inner = self.inner.lock();
    let Some(version) = inner.styled.spans.as_ref().map(Signal::version) else {
      return;
    };
    if version == inner.styled.synced_version {
      return;
    }
    inner.styled.synced_version = version;
    inner.styled.pending = None;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// Notices an app's write of the paragraphs signal: the input lays out again.
  fn sync_external_paragraphs(&self) {
    let mut inner = self.inner.lock();
    let Some(version) = inner.styled.paragraphs.as_ref().map(Signal::version) else {
      return;
    };
    if version == inner.styled.paragraphs_synced {
      return;
    }
    inner.styled.paragraphs_synced = version;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// Captures what an edit needs before it changes the text.
  pub(super) fn styled_before_edit(&self) -> Option<SpansBeforeEdit> {
    let spans = self.current_spans()?;
    let value = self.shared_value();
    let style = self.typing_style_for(&value, &spans);
    let paragraphs = self
      .current_paragraphs()
      .map(|kinds| (kinds, spans_ops::paragraph_ranges(&value)));
    Some(SpansBeforeEdit {
      spans,
      style,
      paragraphs,
      objects: self.current_objects(),
    })
  }

  /// Moves the spans over an edit that replaced `range` with `inserted`
  /// bytes typed in the captured style, and reports the change.
  pub(super) fn styled_after_edit(&self, edit: Option<SpansBeforeEdit>, range: Range<usize>, inserted: usize) {
    let Some(edit) = edit else {
      return;
    };
    self.splice_paragraphs(&edit, &range, inserted);
    self.splice_objects(&edit, &range, inserted, &[]);
    let spans = spans_ops::splice(&edit.spans, range.clone(), inserted, &edit.style);
    self.commit_spans(
      spans,
      StyledEdit::Text {
        replaced: range,
        inserted,
      },
    );
  }

  /// Like [`Self::styled_after_edit`] for inserted text that brings its own
  /// spans, relative to its start.
  pub(super) fn styled_after_insert(
    &self,
    edit: Option<SpansBeforeEdit>,
    range: Range<usize>,
    inserted: &InsertedText<'_>,
  ) {
    let Some(edit) = edit else {
      return;
    };
    let (inserted, pasted, objects) = (inserted.styled, inserted.kinds, inserted.objects);
    self.splice_paragraphs(&edit, &range, inserted.text.len());
    self.splice_objects(&edit, &range, inserted.text.len(), objects);
    if let (Some(pasted), Some(kinds)) = (pasted, self.current_paragraphs()) {
      let after = spans_ops::paragraph_ranges(&self.shared_value());
      self.write_paragraphs(spans_ops::place_kinds(
        kinds.to_vec(),
        &after,
        range.start,
        inserted.text.len(),
        pasted,
      ));
    }
    let start = range.start;
    let mut spans = spans_ops::splice(&edit.spans, range.clone(), inserted.text.len(), &SpanStyle::default());
    spans.extend(
      inserted
        .spans
        .iter()
        .map(|span| StyledSpan::new(span.range.start + start..span.range.end + start, span.style.clone())),
    );
    let spans = spans_ops::normalize(&self.shared_value(), spans);
    self.commit_spans(
      spans,
      StyledEdit::Text {
        replaced: range,
        inserted: inserted.text.len(),
      },
    );
  }

  /// Puts back the spans of an undo snapshot.
  pub(super) fn restore_spans(&self, spans: SnapshotSpans) {
    self.commit_spans(spans.to_vec(), StyledEdit::Replaced);
  }

  /// Writes `spans` for the current text and reports the change to the
  /// app's change handlers, sharing the spans with them.
  fn commit_spans(&self, spans: Vec<StyledSpan>, edit: StyledEdit) {
    let (signal, handlers) = {
      let mut inner = self.inner.lock();
      let Some(signal) = inner.styled.spans.clone() else {
        return;
      };
      inner.styled.pending = None;
      (signal, inner.styled.on_change.clone())
    };
    let shared = Arc::new(spans);
    if signal.with_untracked(|current| current != shared.as_ref()) {
      signal.set(shared.to_vec());
    }
    {
      let mut inner = self.inner.lock();
      inner.styled.synced_version = signal.version();
      inner.styled.cache = Some(SpansCache {
        value_version: self.value.version(),
        spans_version: signal.version(),
        spans: shared.clone(),
      });
    }
    if handlers.is_empty() {
      return;
    }
    let change = StyledChange {
      value: self.value.clone(),
      spans: shared,
      paragraphs: self.current_paragraphs(),
      objects: self.current_objects(),
      edit,
    };
    for handler in handlers {
      handler(&change);
    }
  }

  /// The marks over the selection, or of the next typed text.
  pub(crate) fn selection_style(&self) -> Option<SelectionStyle> {
    let spans = self.current_spans()?;
    let value = self.shared_value();
    let (start, end) = self.selection_or_caret(&value);
    if start < end {
      return Some(spans_ops::summarize(&spans, value.len(), start..end));
    }
    Some(SelectionStyle::of_style(&self.typing_style_for(&value, &spans)))
  }

  /// Turns `mark` on over the selection unless all of it has it, then off;
  /// with nothing selected, for the next typed text. `false` for a plain
  /// input.
  pub(crate) fn toggle_mark(&self, mark: TextMark) -> bool {
    let Some(style) = self.selection_style() else {
      return false;
    };
    let on = style.mark(mark) != crate::components::MarkPresence::All;
    self.set_mark(mark, on)
  }

  pub(crate) fn set_mark(&self, mark: TextMark, on: bool) -> bool {
    self.restyle_selection(|style| *style = std::mem::take(style).with(mark, on))
  }

  /// Links the selection to `target`, or unlinks it with `None`. With
  /// nothing selected it changes the link the caret is in, else the next
  /// typed text.
  pub(crate) fn set_link(&self, target: Option<Arc<str>>) -> bool {
    let Some(spans) = self.current_spans() else {
      return false;
    };
    let value = self.shared_value();
    let (start, end) = self.selection_or_caret(&value);
    if start == end
      && let Some(range) = link_range(&spans, start).filter(|range| range.start < start && start < range.end)
    {
      return self.restyle_range(&spans, range, |style| style.link = target.clone());
    }
    self.restyle_selection(|style| style.link = target.clone())
  }

  /// Sets the style the next typed text takes at the caret; `None` goes
  /// back to the style of the text before the caret.
  pub(crate) fn set_pending_style(&self, style: Option<SpanStyle>) -> bool {
    if !self.is_styled() {
      return false;
    }
    let mut inner = self.inner.lock();
    inner.styled.pending = style.map(|style| (inner.caret, style));
    true
  }

  /// The selection with its spans, offsets from its start.
  pub(crate) fn selected_styled_text(&self) -> Option<StyledText> {
    let spans = self.current_spans()?;
    let value = self.shared_value();
    let (start, end) = self.selection_or_caret(&value);
    Some(StyledText {
      text: value[start..end].to_owned(),
      spans: spans_ops::slice(&spans, start..end),
    })
  }

  /// The objects in the selection, their bytes counted from its start.
  fn selected_objects(&self) -> Vec<InlineObject> {
    let Some(objects) = self.current_objects() else {
      return Vec::new();
    };
    let (start, end) = self.selection_or_caret(&self.shared_value());
    spans_ops::slice_objects(&objects, start..end)
  }

  /// What copying the selection puts on the clipboard: the app's format of
  /// the styled selection when it gave one, else the selected text, with
  /// the label of an object in its place. Nothing while the input is
  /// masked, as a password field copies nothing.
  pub(crate) fn clipboard_selection(&self) -> Option<String> {
    if self.is_masked() {
      return None;
    }
    let selected = self.selected_text()?;
    let format = self.inner.lock().styled.copy_format.clone();
    match (format, self.selected_styled_text()) {
      (Some(format), Some(styled)) => Some(format(&styled)),
      _ => Some(spans_ops::text_with_labels(&selected, &self.selected_objects())),
    }
  }

  pub(crate) fn set_rich_clipboard(&self, on: bool) {
    self.inner.lock().styled.rich_clipboard = on;
  }

  /// The Markdown of the selection, which a copy keeps beside its text: its marks and the kinds of the
  /// paragraphs it touches. Only an input with kinds of paragraphs that keeps its formatting through the
  /// clipboard has one, and never while it is masked.
  pub(crate) fn clipboard_markdown(&self) -> Option<String> {
    if !self.inner.lock().styled.rich_clipboard || self.is_masked() {
      return None;
    }
    let kinds = self.current_paragraphs()?;
    let selected = self.selected_styled_text()?;
    if selected.text.is_empty() {
      return None;
    }
    let (range, _) = self.selected_paragraph_range();
    let rich =
      RichText::new(selected.text, selected.spans, kinds[range].to_vec()).with_objects(self.selected_objects());
    Some(rich.to_markdown())
  }

  /// Pastes formatted text into an input that keeps its formatting through the clipboard: the `markdown` beside
  /// the text, or the text itself where it reads as Markdown with block marks. `None` where the paste is plain
  /// text: an input without kinds of paragraphs, a caret in a line of code, a text that is not Markdown.
  pub(crate) fn paste_rich(&self, text: &str, markdown: Option<&str>, keyboard: &KeyboardEvent) -> Option<bool> {
    if !self.inner.lock().styled.rich_clipboard || self.is_masked() {
      return None;
    }
    let kinds = self.current_paragraphs()?;
    // Code is pasted into code as it is written.
    let (touched, _) = self.selected_paragraph_range();
    if kinds[touched.start] == ParagraphKind::Code {
      return None;
    }
    let source = match markdown {
      Some(markdown) => markdown,
      None if RichText::reads_as_markdown(text) => text,
      None => return None,
    };
    let rich = RichText::from_markdown(source);
    if rich.is_empty() {
      return None;
    }
    Some(self.replace_selection_rich(&rich, keyboard))
  }

  /// Reports the link under a point of the content box to the input's link
  /// handlers. `false` when no handler or no link is there.
  pub(crate) fn click_link_at_point(&self, x: f32, y: f32) -> bool {
    let handlers = self.inner.lock().styled.on_link_click.clone();
    if handlers.is_empty() {
      return false;
    }
    let Some(click) = self.link_at_point(x, y) else {
      return false;
    };
    for handler in handlers {
      handler(&click);
    }
    true
  }

  /// The link drawn under a point of the content box.
  fn link_at_point(&self, x: f32, y: f32) -> Option<LinkClick> {
    let spans = self.current_spans()?;
    let layout = self.token_layout()?;
    let (scroll_x, scroll_y) = {
      let inner = self.inner.lock();
      (inner.scroll_x, inner.scroll_y())
    };
    let cluster = layout.cluster_at(x + scroll_x, y + scroll_y)?;
    let range = link_range(&spans, cluster.start)?;
    let target = spans_ops::style_at(&spans, cluster.start).link?;
    Some(LinkClick { target, range })
  }

  fn restyle_selection(&self, change: impl Fn(&mut SpanStyle)) -> bool {
    let Some(spans) = self.current_spans() else {
      return false;
    };
    let value = self.shared_value();
    let (start, end) = self.selection_or_caret(&value);
    if start == end {
      let mut style = self.typing_style_for(&value, &spans);
      change(&mut style);
      let mut inner = self.inner.lock();
      inner.styled.pending = Some((inner.caret, style));
      return true;
    }
    self.restyle_range(&spans, start..end, change)
  }

  fn restyle_range(&self, spans: &[StyledSpan], range: Range<usize>, change: impl Fn(&mut SpanStyle)) -> bool {
    let restyled = spans_ops::restyle(spans, self.shared_value().len(), range.clone(), change);
    if restyled.as_slice() == spans {
      return false;
    }
    self.push_undo_snapshot();
    self.commit_spans(restyled, StyledEdit::Style { range });
    self.mark_layout_dirty();
    true
  }

  /// The style typed text takes over the selection or at the caret.
  fn typing_style_for(&self, value: &str, spans: &[StyledSpan]) -> SpanStyle {
    let (start, end) = self.selection_or_caret(value);
    let inner = self.inner.lock();
    if let Some((caret, style)) = &inner.styled.pending
      && start == end
      && *caret == start
    {
      return style.clone();
    }
    drop(inner);
    spans_ops::insertion_style(value, spans, start, end)
  }

  fn selection_or_caret(&self, value: &str) -> (usize, usize) {
    let inner = self.inner.lock();
    selection_range_indices(value, inner.selection_anchor, inner.caret).unwrap_or_else(|| {
      let caret = clamp_to_char_boundary(value, inner.caret);
      (caret, caret)
    })
  }
}

/// The whole link around `offset`: the spans next to each other that carry
/// the link of the span at `offset` (or ending there).
fn link_range(spans: &[StyledSpan], offset: usize) -> Option<Range<usize>> {
  let index = spans
    .iter()
    .position(|span| span.range.start <= offset && offset < span.range.end && span.style.link.is_some())
    .or_else(|| {
      spans
        .iter()
        .position(|span| span.range.end == offset && span.style.link.is_some())
    })?;
  let target = spans[index].style.link.clone();
  let mut first = index;
  while first > 0 && spans[first - 1].range.end == spans[first].range.start && spans[first - 1].style.link == target {
    first -= 1;
  }
  let mut last = index;
  while last + 1 < spans.len()
    && spans[last + 1].range.start == spans[last].range.end
    && spans[last + 1].style.link == target
  {
    last += 1;
  }
  Some(spans[first].range.start..spans[last].range.end)
}