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
//! A one-time code: a few characters typed one to a cell, as one field.

use std::{
  cell::RefCell,
  collections::HashMap,
  sync::{
    Arc, Weak,
    atomic::{AtomicBool, Ordering},
  },
  time::Duration,
};

use super::{
  field::{FieldControl, InField},
  icons, names,
  part_style::{Length, PartStyle},
  recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
  shared::glyph,
  text_box::{self, BoxStates, Judgment, TextBoxSize, TextBoxState},
  theme::{Themed, current_theme},
};
use crate::{
  animation::{AnimatableProperty, Animation, Easing, Keyframes, KeyframesId},
  app::{
    events::{EditCommand, KeyboardEvent, MouseEvent, PasteEvent, TextInputEvent},
    theme::{IconSize, OpacityLevel, PaletteColor, Theme, TypographyStyle},
  },
  components::{self, ContentKind, KeyboardKind, Row, Stack},
  core::{ElementRef, Signal},
  layout::{Alignment, StackAlignment, layout_kind::Justify, text_style::TextStyle},
  node::{CursorIcon, Element, color::Color, dimension::Dimension},
};

/// The keyframes of the caret of a code's cell: shown for half of its period, gone for the other half, as a text
/// input's caret is. Every window has them.
pub const INPUT_OTP_CARET_BLINK: KeyframesId = KeyframesId::new(0xFF41);

/// One shown and one hidden half of the caret's blink: a text input's caret blinks at the same pace.
pub const INPUT_OTP_CARET_PERIOD: Duration = Duration::from_millis(1060);

/// The keyframes a window registers for a code's caret.
pub(crate) fn keyframes() -> [Keyframes; 1] {
  let blink = Keyframes::new(INPUT_OTP_CARET_BLINK)
    .frame(0.0, |frame| {
      frame.set(AnimatableProperty::Opacity, 1.0);
    })
    .frame(0.5, |frame| {
      frame.set(AnimatableProperty::Opacity, 1.0);
    })
    .frame(0.501, |frame| {
      frame.set(AnimatableProperty::Opacity, 0.0);
    })
    .frame(1.0, |frame| {
      frame.set(AnimatableProperty::Opacity, 0.0);
    });
  [blink]
}

/// The parts of a code a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InputOtpPart {
  /// The row of cells and separators, and the room between them.
  Root,
  /// A box one character is typed in: its size, its edge, its fill. The cell that has the focus has the ring.
  Cell,
  /// The character in a cell, or the mask's dot for it.
  Value,
  /// The line in the empty cell that has the focus, where the next character goes.
  Caret,
  /// What stands between two groups of cells: a dash.
  Separator,
}

names!(InputOtpPart {
  Root => "root",
  Cell => "cell",
  Value => "value",
  Caret => "caret",
  Separator => "separator",
});

/// The sizes of a code: its cells smaller or larger, with their type.
pub type InputOtpSize = TextBoxSize;

/// The states of a code a recipe styles, besides what the pointer and the keyboard do to it. A cell is in
/// [`Active`](Self::Active) and [`Filled`](Self::Filled) on its own; the rest are the whole code's.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InputOtpState {
  /// What it holds is wrong: the edge of each cell is in the destructive color.
  Invalid,
  /// What it holds is allowed, with a caveat: the edges are in the warning color.
  Warning,
  /// What it holds was checked and is right: the edges are in the success color.
  Valid,
  /// It can be read and copied, and not changed.
  ReadOnly,
  /// It takes no input.
  Disabled,
  /// Its cells show a dot for each character.
  Masked,
  /// The cell the focus is on: what is typed next goes into it.
  Active,
  /// A cell that holds a character.
  Filled,
}

names!(InputOtpState {
  Invalid => "invalid",
  Warning => "warning",
  Valid => "valid",
  ReadOnly => "read_only",
  Disabled => "disabled",
  Masked => "masked",
  Active => "active",
  Filled => "filled",
});

/// What a code is made of: what is taken of what is typed or pasted. Anything else is left out.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum InputOtpKind {
  /// The digits 0 to 9: the keyboard of a phone shows digits.
  #[default]
  Digits,
  /// The letters A to Z, of either case, and the digits.
  Alphanumeric,
}

impl InputOtpKind {
  /// Whether `character` is one a code of this kind holds.
  pub fn takes(self, character: char) -> bool {
    match self {
      Self::Digits => character.is_ascii_digit(),
      Self::Alphanumeric => character.is_ascii_alphanumeric(),
    }
  }
}

/// What builds a code's nodes: a theme's in place of the default one.
pub type InputOtpRenderer = Arc<dyn Fn(InputOtpView) -> Element + Send + Sync>;

/// What is called with the code: as a person changes it, or once its last cell is filled.
type CodeHandler = Arc<dyn Fn(&str) + Send + Sync>;

/// A one-time code: a few characters typed one to a cell, the code a message or an authenticator app gives to
/// prove who a person is.
///
/// ```
/// use lurq::{core::Signal, ui::{InputOtp, InputOtpKind}};
///
/// let code = Signal::new(String::new());
/// let otp = InputOtp::new(code.clone())
///   .groups([3, 3])
///   .label("Verification code")
///   .on_complete(|code| println!("verify {code}"));
/// let recovery = InputOtp::new(Signal::new(String::new()))
///   .length(8)
///   .groups([4, 4])
///   .kind(InputOtpKind::Alphanumeric)
///   .uppercase(true);
/// ```
///
/// To a person it is one field: typing fills the cell that has the focus and goes on to the next, Backspace
/// empties the cell and goes back, Left and Right move, a paste of a whole code fills every cell from the first,
/// and a click puts the focus on the cell clicked, or on the first empty one past it. To a form and to what reads
/// the tree it is one text input, called by its label, whose value is the code: under the cells it draws is the
/// engine's [`TextInput`](crate::components::TextInput), which takes what is typed, pasted or filled in by the
/// platform (it says it holds a [`ContentKind::OneTimeCode`]), and is the one stop of Tab.
pub struct InputOtp {
  value: Signal<String>,
  length: usize,
  groups: Vec<usize>,
  kind: InputOtpKind,
  uppercase: bool,
  mask: bool,
  on_change: Vec<CodeHandler>,
  on_complete: Vec<CodeHandler>,
  boxed: BoxStates,
  states: Vec<Name>,
  rules: Recipe,
  forced: Option<Interaction>,
  name: Option<Arc<str>>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl InputOtp {
  /// A code of six digits bound to `value`: its cells show what the signal holds, and what is typed is set on it.
  pub fn new(value: Signal<String>) -> Self {
    Self {
      value,
      length: 6,
      groups: Vec::new(),
      kind: InputOtpKind::default(),
      uppercase: false,
      mask: false,
      on_change: Vec::new(),
      on_complete: Vec::new(),
      boxed: BoxStates::default(),
      states: Vec::new(),
      rules: Recipe::new(),
      forced: None,
      name: None,
      id: None,
      key: None,
    }
  }

  /// How many characters the code has: a cell for each. At least one.
  pub fn length(mut self, length: usize) -> Self {
    self.length = length.max(1);
    self
  }

  /// How the cells are grouped, from the first: `[3, 3]` is three cells, a separator, and three more. Cells past
  /// the groups are a group of their own; groups past the length are left out.
  pub fn groups(mut self, groups: impl IntoIterator<Item = usize>) -> Self {
    self.groups = groups.into_iter().filter(|group| *group > 0).collect();
    self
  }

  /// What the code is made of: digits, or letters and digits.
  pub fn kind(mut self, kind: InputOtpKind) -> Self {
    self.kind = kind;
    self
  }

  /// Whether a letter typed or pasted is taken as a capital, so that a code is the same however it was typed.
  pub fn uppercase(mut self, uppercase: bool) -> Self {
    self.uppercase = uppercase;
    self
  }

  /// Whether the cells show a dot for each character in place of it, as a PIN is shown.
  pub fn mask(mut self, mask: bool) -> Self {
    self.mask = mask;
    self
  }

  /// Called with the code when a person changes it: a character typed, pasted or deleted. Not when the app sets
  /// the signal. A second handler is called after the first.
  pub fn on_change(mut self, handler: impl Fn(&str) + Send + Sync + 'static) -> Self {
    self.on_change.push(Arc::new(handler));
    self
  }

  /// Called with the code when an edit fills its last cell: what verifies it, or moves on. A second handler is
  /// called after the first.
  pub fn on_complete(mut self, handler: impl Fn(&str) + Send + Sync + 'static) -> Self {
    self.on_complete.push(Arc::new(handler));
    self
  }

  /// An [`InputOtpSize`], or a size the theme's recipe names.
  pub fn size(mut self, size: impl Into<Name>) -> Self {
    self.boxed.size = size.into();
    self
  }

  /// Whether the code is wrong. Of invalid, warning and valid, the first that is set is the one shown.
  pub fn invalid(mut self, invalid: bool) -> Self {
    self.boxed.invalid = invalid;
    self
  }

  /// Whether the code is allowed with a caveat.
  pub fn warning(mut self, warning: bool) -> Self {
    self.boxed.warning = warning;
    self
  }

  /// Whether the code was checked and is right.
  pub fn valid(mut self, valid: bool) -> Self {
    self.boxed.valid = valid;
    self
  }

  /// Whether the code can be read and copied, and not changed. It is still a stop of Tab, and Left and Right
  /// still move.
  pub fn read_only(mut self, read_only: bool) -> Self {
    self.boxed.read_only = read_only;
    self
  }

  /// Whether the code takes no input.
  pub fn disabled(mut self, disabled: bool) -> Self {
    self.boxed.disabled = disabled;
    self
  }

  /// What the code is called, to what reads the tree. It draws nothing: the label a person reads is the field's,
  /// above it, which gives the code this name unless it has its own.
  pub fn label(mut self, label: &str) -> Self {
    self.boxed.label = Some(Arc::from(label));
    self
  }

  /// Puts the code in a state the theme's recipe names, or takes it out.
  pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
    let state = state.into();
    self.states.retain(|held| *held != state);
    if on {
      self.states.push(state);
    }
    self
  }

  /// The name the code has in the form it is in: its value is the whole code.
  #[cfg(feature = "form")]
  pub fn name(mut self, name: impl Into<Arc<str>>) -> Self {
    self.name = Some(name.into());
    self
  }

  /// Styles one part of this code over what the theme gives it.
  pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(PartStyle) -> PartStyle) -> Self {
    self.rules = self.rules.base(part, style);
    self
  }

  /// Lays rules over the theme's recipe for this code: what the component it is in says of it in this place.
  pub fn rules(mut self, rules: &Recipe) -> Self {
    self.rules = self.rules.extend(rules);
    self
  }

  /// Draws the code as it is focused, with nothing focusing it: for a gallery or a test. The focus is on the
  /// first empty cell.
  pub fn force(mut self, interaction: Option<Interaction>) -> Self {
    self.forced = interaction;
    self
  }

  /// The id of the input under the cells: what is typed in and focused.
  pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
    self.id = Some(id.into());
    self
  }

  pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
    self.key = Some(key.into());
    self
  }

  /// Builds the code in `theme`. Converting a code into an element does this in the theme of what is being
  /// rendered.
  pub fn build(self, theme: &Theme) -> Element {
    let case = self
      .boxed
      .case(self.states.clone())
      .state(InputOtpState::Masked, self.mask);
    let cell_case = |active: bool, filled: bool| {
      case
        .clone()
        .state(InputOtpState::Active, active)
        .state(InputOtpState::Filled, filled)
    };
    let recipe = theme
      .resolve::<InputOtp>(&case, Some(&self.rules))
      .owned_by(self.id.as_ref());
    let resolve = |case: &Case| theme.resolve::<InputOtp>(case, Some(&self.rules)).owned_like(&recipe);
    let cells = [
      resolve(&cell_case(false, false)),
      resolve(&cell_case(false, true)),
      resolve(&cell_case(true, false)),
      resolve(&cell_case(true, true)),
    ];
    let cursor = cursor(&self.value);
    // A value the app set is taken as typing takes it: the characters of the code's kind, as capitals where it
    // says, no more than it has cells. What it held besides is let go of.
    let held = self.value.get();
    let characters: Vec<char> = taken(&held, self.kind, self.uppercase)
      .into_iter()
      .take(self.length)
      .collect();
    let code: String = characters.iter().collect();
    if code != held {
      self.value.set(code);
    }
    let last = last_cell(characters.len(), self.length);
    let focused = cursor.focused.get() || self.forced == Some(Interaction::FocusVisible);
    let active = focused.then(|| {
      if cursor.focused.get_untracked() {
        cursor.at.get().min(last)
      } else {
        last
      }
    });
    let editor = Editor {
      value: self.value.clone(),
      cursor,
      length: self.length,
      kind: self.kind,
      uppercase: self.uppercase,
      on_change: self.on_change,
      on_complete: self.on_complete,
    };
    let view = InputOtpView {
      value: self.value,
      length: self.length,
      characters,
      active,
      separators: separators(&self.groups, self.length),
      kind: self.kind,
      mask: self.mask,
      label: self.boxed.label.clone(),
      invalid: self.boxed.judgment() == Some(Judgment::Invalid),
      read_only: self.boxed.read_only,
      disabled: self.boxed.disabled,
      recipe,
      cells,
      cell_refs: (0..self.length).map(|_| ElementRef::new()).collect(),
      forced: self.forced,
      boxed: self.boxed,
      editor,
      name: self.name,
      id: self.id,
      key: self.key,
    };
    match theme.renderer::<InputOtp>() {
      Some(renderer) => renderer(view),
      None => view.render(),
    }
  }
}

impl FieldControl for InputOtp {
  /// Called by the field's label unless it has a name of its own, judged as the field is unless the app judged the
  /// code itself, and disabled where the field is.
  fn in_field(mut self, field: &InField) -> Element {
    self.boxed.in_field(field);
    self.into()
  }
}

impl From<InputOtp> for Element {
  fn from(otp: InputOtp) -> Self {
    otp.build(&current_theme())
  }
}

/// What of `text` a code of `kind` takes: its characters of that kind, as capitals where `uppercase` says.
fn taken(text: &str, kind: InputOtpKind, uppercase: bool) -> Vec<char> {
  text
    .chars()
    .filter(|character| kind.takes(*character))
    .map(|character| {
      if uppercase {
        character.to_ascii_uppercase()
      } else {
        character
      }
    })
    .collect()
}

/// The last cell the focus may be on: the first empty one, or the last cell of a full code.
fn last_cell(filled: usize, length: usize) -> usize {
  filled.min(length.saturating_sub(1))
}

/// The cells a separator follows: the ends of the groups, short of the last cell.
fn separators(groups: &[usize], length: usize) -> Vec<usize> {
  let mut after = Vec::new();
  let mut end = 0;
  for group in groups {
    end += group;
    if end >= length {
      break;
    }
    after.push(end);
  }
  after
}

/// Where a person is in a code: whether its input has the focus, and the cell the focus is on. A builder keeps
/// nothing from one render to the next, so this is kept by the code's signal for as long as the input bound to it
/// is: the input's handlers hold it, and it goes when they do.
struct CursorState {
  focused: Signal<bool>,
  at: Signal<usize>,
  /// The last character typed filled the last cell: the cursor is past the end, and what is typed now goes
  /// nowhere until a key or a click moves it back onto a cell.
  past: AtomicBool,
  /// The whole code is selected (Ctrl+A, Cmd+A): Backspace and Delete clear it, and what is typed starts it again.
  all: AtomicBool,
}

type Cursor = Arc<CursorState>;

thread_local! {
  /// The cursor of each code that is shown, by the id of its signal. A window's codes are all on its thread.
  static CURSORS: RefCell<HashMap<usize, Weak<CursorState>>> = RefCell::new(HashMap::new());
}

/// The cursor of the code bound to `value`: the one its input holds, or a new one, out of the focus, where there is
/// none.
fn cursor(value: &Signal<String>) -> Cursor {
  CURSORS.with(|cursors| {
    let mut cursors = cursors.borrow_mut();
    if let Some(cursor) = cursors.get(&value.id()).and_then(Weak::upgrade) {
      return cursor;
    }
    cursors.retain(|_, cursor| cursor.strong_count() > 0);
    let cursor = Arc::new(CursorState {
      focused: Signal::new(false),
      at: Signal::new(0),
      past: AtomicBool::new(false),
      all: AtomicBool::new(false),
    });
    cursors.insert(value.id(), Arc::downgrade(&cursor));
    cursor
  })
}

/// What edits a code: the handlers of its input put what is typed, pasted and deleted into its cells.
#[derive(Clone)]
struct Editor {
  value: Signal<String>,
  cursor: Cursor,
  length: usize,
  kind: InputOtpKind,
  uppercase: bool,
  on_change: Vec<CodeHandler>,
  on_complete: Vec<CodeHandler>,
}

impl Editor {
  /// What the code holds, a character for each filled cell.
  fn characters(&self) -> Vec<char> {
    taken(&self.value.get_untracked(), self.kind, self.uppercase)
      .into_iter()
      .take(self.length)
      .collect()
  }

  /// The cell the focus is on, of a code with `filled` cells filled.
  fn at(&self, filled: usize) -> usize {
    self.cursor.at.get_untracked().min(last_cell(filled, self.length))
  }

  /// What of `text` the code takes: its characters of the code's kind, as capitals where it says.
  fn taken(&self, text: &str) -> Vec<char> {
    taken(text, self.kind, self.uppercase)
  }

  /// Sets the code and the cell the focus is on, and tells the app when the edit filled the last cell. An `at`
  /// past the last cell of a full code leaves the cursor past the end.
  fn set(&self, before: &[char], characters: Vec<char>, at: usize) {
    let full = characters.len() == self.length && characters != before;
    let code: String = characters.iter().collect();
    self.put_at(last_cell(characters.len(), self.length).min(at));
    let past = characters.len() == self.length && at >= self.length;
    self.cursor.past.store(past, Ordering::Relaxed);
    if self.value.get_untracked() != code {
      self.value.set(code.clone());
      for handler in &self.on_change {
        handler(&code);
      }
    }
    if full {
      for handler in &self.on_complete {
        handler(&code);
      }
    }
  }

  /// Moves the cursor onto a cell, by a key or a click: it is past the end no longer, and the code is not
  /// selected.
  fn move_to(&self, at: usize) {
    self.cursor.past.store(false, Ordering::Relaxed);
    self.cursor.all.store(false, Ordering::Relaxed);
    self.put_at(at);
  }

  /// Whether the whole code is selected, and from now on not: what a key does to a selected code it does once.
  fn take_all(&self) -> bool {
    self.cursor.all.swap(false, Ordering::Relaxed)
  }

  /// Puts the focus on cell `at`, telling what reads the cursor only of a change.
  fn put_at(&self, at: usize) {
    if self.cursor.at.get_untracked() != at {
      self.cursor.at.set(at);
    }
  }

  /// Puts `text` into the cells: a whole code from the first cell, anything shorter over the cells from the one
  /// that has the focus. The focus goes on past what was put.
  fn write(&self, text: &str) {
    let taken = self.taken(text);
    if taken.is_empty() {
      return;
    }
    let before = self.characters();
    // Typed over the whole code selected, the code starts again from what was typed.
    if self.take_all() {
      let code: Vec<char> = taken.into_iter().take(self.length).collect();
      let at = code.len();
      self.set(&before, code, at);
      return;
    }
    // Past the end of a full code, what is typed goes nowhere: the last cell is not typed over again and again.
    if taken.len() < self.length && self.cursor.past.load(Ordering::Relaxed) && before.len() == self.length {
      return;
    }
    if taken.len() >= self.length {
      let code: Vec<char> = taken.into_iter().take(self.length).collect();
      let at = code.len();
      self.set(&before, code, at);
      return;
    }
    let start = self.at(before.len());
    let mut code = before.clone();
    for (offset, character) in taken.iter().enumerate() {
      let cell = start + offset;
      if cell >= self.length {
        break;
      }
      if cell < code.len() {
        code[cell] = *character;
      } else {
        code.push(*character);
      }
    }
    self.set(&before, code, start + taken.len());
  }

  /// Backspace: empties the cell that has the focus where it is filled, else goes back and empties the one before.
  /// The cells after an emptied one move up, so that a code has no gaps.
  fn backspace(&self) {
    if self.take_all() {
      self.clear();
      return;
    }
    self.cursor.past.store(false, Ordering::Relaxed);
    let before = self.characters();
    let at = self.at(before.len());
    let mut code = before.clone();
    if at < code.len() {
      code.remove(at);
      self.set(&before, code, at);
    } else if at > 0 {
      code.remove(at - 1);
      self.set(&before, code, at - 1);
    }
  }

  /// Delete: empties the cell that has the focus, and the focus stays.
  fn delete(&self) {
    if self.take_all() {
      self.clear();
      return;
    }
    let before = self.characters();
    let at = self.at(before.len());
    if at < before.len() {
      let mut code = before.clone();
      code.remove(at);
      self.set(&before, code, at);
    }
  }

  /// Empties every cell, and puts the focus on the first.
  fn clear(&self) {
    let before = self.characters();
    self.set(&before, Vec::new(), 0);
  }

  /// Moves the focus by `by` cells, no further than the first empty one.
  fn step(&self, by: isize) {
    let filled = self.characters().len();
    let at = self.at(filled).saturating_add_signed(by);
    self.move_to(at.min(last_cell(filled, self.length)));
  }

  /// Puts the focus on the cell that was clicked, or on the first empty one where that is before it.
  fn click(&self, cell: usize) {
    self.move_to(cell.min(last_cell(self.characters().len(), self.length)));
  }
}

/// A code as a renderer sees it: what it was given, the style of its parts, and its behavior ready to be dressed.
pub struct InputOtpView {
  /// The code, which the input under the cells is bound to.
  pub value: Signal<String>,
  /// How many cells it has.
  pub length: usize,
  /// What the cells hold, from the first: no more than `length` of what the value holds.
  pub characters: Vec<char>,
  /// The cell the focus is on, while the input has the focus.
  pub active: Option<usize>,
  /// The cells a separator follows.
  pub separators: Vec<usize>,
  pub kind: InputOtpKind,
  pub mask: bool,
  /// What it is called to what reads the tree: its own label, or its field's.
  pub label: Option<Arc<str>>,
  /// Whether the code is wrong, as it is shown.
  pub invalid: bool,
  pub read_only: bool,
  pub disabled: bool,
  /// The style of each part of the code, as a cell that is neither active nor filled has it.
  pub recipe: ResolvedRecipe,
  /// The recipe of a cell: neither active nor filled, filled, active, active and filled.
  cells: [ResolvedRecipe; 4],
  /// What is put on each cell, so that a click on the input is known to be on a cell.
  cell_refs: Vec<ElementRef>,
  forced: Option<Interaction>,
  boxed: BoxStates,
  editor: Editor,
  // Read only where the engine has forms.
  #[cfg_attr(not(feature = "form"), allow(dead_code))]
  name: Option<Arc<str>>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl InputOtpView {
  /// The style of a part, as the code is drawn.
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    self.recipe.part(part).forced(self.forced)
  }

  /// The style of a part of the cell at `index`: the cell itself, its value or its caret.
  pub fn cell_part(&self, index: usize, part: impl Into<Name>) -> ResolvedPart {
    let active = self.active == Some(index);
    let filled = index < self.characters.len();
    self.cells[usize::from(active) * 2 + usize::from(filled)]
      .part(part)
      .forced(self.forced)
  }

  pub fn theme(&self) -> &Theme {
    self.recipe.theme()
  }

  /// What the cell at `index` shows: its character, a dot where the code is masked, or nothing.
  pub fn shown(&self, index: usize) -> Option<char> {
    let character = *self.characters.get(index)?;
    Some(if self.mask { '\u{2022}' } else { character })
  }

  /// The width and the height of the row of cells, where the recipe gives them in pixels: what the input under the
  /// cells takes, so that a click anywhere on the code is the input's.
  pub fn size(&self) -> Option<(f32, f32)> {
    let theme = self.theme();
    let px = |length: Option<Length>| match length?.resolve(theme)? {
      Dimension::Px(px) => Some(px),
      _ => None,
    };
    let cell = self.part(InputOtpPart::Cell).rest();
    let (width, height) = (px(cell.width)?, px(cell.height)?);
    let separator = px(
      self
        .part(InputOtpPart::Separator)
        .rest()
        .width
        .or(Some(Length::Icon(IconSize::Md))),
    )?;
    let gap = self
      .part(InputOtpPart::Root)
      .rest()
      .gap
      .map(|gap| gap.resolve(&theme.spacing(), 0.0))
      .unwrap_or(0.0);
    let separators = self.separators.len();
    let items = self.length + separators;
    let row = width * self.length as f32 + separator * separators as f32 + gap * items.saturating_sub(1) as f32;
    Some((row, height.max(separator)))
  }

  /// The input the code is typed into, under the cells: the engine's text input bound to the code, which draws
  /// nothing of its own. It is the one stop of Tab and the one `textbox` to what reads the tree; its handlers put
  /// what is typed, pasted and deleted into the cells, and a click on it puts the focus on the cell under the
  /// pointer.
  pub fn input(&self) -> components::TextInput {
    // The input is there to be typed in and read by tooling: whatever it would paint, the cells paint.
    let clear = Color::new(0, 0, 0, 0);
    let mut control = components::TextInput::new(self.value.clone())
      .single_line()
      .text_style(TextStyle {
        color: clear,
        ..TextStyle::default()
      })
      .caret_color(clear)
      .selection_color(clear)
      .keyboard(match self.kind {
        InputOtpKind::Digits => KeyboardKind::Number,
        InputOtpKind::Alphanumeric => KeyboardKind::Text,
      })
      .content(ContentKind::OneTimeCode)
      .cursor(CursorIcon::Text);
    if self.mask {
      control = control.mask();
    }
    control = match self.size() {
      Some((width, height)) => control.width(width).height(height),
      None => control.width(Dimension::Pct(100.0)).height(Dimension::Pct(100.0)),
    };
    let mut control = text_box::text_box(control, &self.boxed, Vec::new());
    let (read_only, disabled) = (self.boxed.read_only, self.boxed.disabled);
    let editor = self.editor.clone();
    control = control.on_input(move |event: TextInputEvent| {
      // The code is the editor's to change: the input's own edit, at its own caret, is never made.
      event.prevent_default();
      if read_only || disabled {
        return;
      }
      let keyboard = &event.keyboard;
      if event.new_value.is_empty() {
        // Everything selected, and cut or typed over with nothing.
        editor.clear();
      } else if !keyboard.ctrl && !keyboard.meta && event.new_value.contains(keyboard.key.as_str()) {
        // A key typed, or what an input method or the platform put in: the event's key is that text.
        editor.write(&keyboard.key);
      }
    });
    let editor = self.editor.clone();
    control = control.on_paste(move |event: PasteEvent| {
      event.prevent_default();
      if read_only || disabled {
        return;
      }
      if let Some(text) = event.text() {
        editor.write(text);
      }
    });
    let editor = self.editor.clone();
    control = control.on_key_down(move |event: KeyboardEvent| {
      if !event.focus_within || disabled || event.composing {
        return;
      }
      // Select all (its key read where lurq reads it): the whole code selected, so Backspace or Delete then clears
      // it. The input selects its text as well.
      if event.edit_command() == Some(EditCommand::SelectAll) {
        editor.cursor.all.store(true, Ordering::Relaxed);
        return;
      }
      let edits = !read_only;
      match event.key.as_str() {
        "Backspace" if edits => editor.backspace(),
        "Delete" if edits => editor.delete(),
        "Backspace" | "Delete" => {}
        "ArrowLeft" => editor.step(-1),
        "ArrowRight" => editor.step(1),
        "Home" | "ArrowUp" => editor.move_to(0),
        "End" | "ArrowDown" => editor.step(isize::MAX),
        _ => return,
      }
      event.prevent_default();
    });
    let cursor = self.editor.cursor.clone();
    let editor = self.editor.clone();
    control = control
      .on_focus(move || {
        // Tab, or a click: the focus goes on the first empty cell, and a click then moves it to its cell.
        editor.step(isize::MAX);
        if !cursor.focused.get_untracked() {
          cursor.focused.set(true);
        }
      })
      .on_blur({
        let cursor = self.editor.cursor.clone();
        move || {
          cursor.all.store(false, Ordering::Relaxed);
          if cursor.focused.get_untracked() {
            cursor.focused.set(false);
          }
        }
      });
    let (editor, cells) = (self.editor.clone(), self.cell_refs.clone());
    control = control.on_click(move |event: MouseEvent| {
      if disabled || event.from_keyboard {
        return;
      }
      let under = cells.iter().position(|cell| {
        let bounds = cell.bounds();
        cell.is_attached() && event.x >= bounds.x && event.x < bounds.x + bounds.width
      });
      if let Some(cell) = under {
        editor.click(cell);
      }
    });
    #[cfg(feature = "form")]
    if let Some(name) = &self.name {
      control = control.name(name.clone());
    }
    if let Some(id) = &self.id {
      control = control.id(id.clone());
    }
    if let Some(key) = &self.key {
      control = control.key(key.clone());
    }
    control
  }

  /// The cell at `index`, in the style of its part, with its character, or with the caret where it is the empty
  /// cell the focus is on. The caret blinks unless the theme reduces motion.
  pub fn cell(&self, index: usize) -> Row {
    let mut cell = Row::new()
      .part(&self.cell_part(index, InputOtpPart::Cell))
      .justify(Justify::Center)
      .align_items(Alignment::Center);
    if let Some(cell_ref) = self.cell_refs.get(index) {
      cell = cell.ref_element(cell_ref.clone());
    }
    if let Some(shown) = self.shown(index) {
      let value = self.cell_part(index, InputOtpPart::Value);
      cell = cell.child(value.text(&shown.to_string()).nowrap());
    } else if self.active == Some(index) {
      let mut caret = Row::new().part(&self.cell_part(index, InputOtpPart::Caret));
      if !self.theme().reduced_motion() {
        caret = caret.animation(
          Animation::new(INPUT_OTP_CARET_BLINK)
            .duration(INPUT_OTP_CARET_PERIOD)
            .easing(Easing::Linear)
            .infinite(),
        );
      }
      cell = cell.child(caret);
    }
    cell
  }

  /// What stands between two groups of cells: the theme's dash, in the style of its part.
  pub fn separator(&self) -> Element {
    glyph(&self.part(InputOtpPart::Separator), icons::MINUS.into(), false)
  }

  /// The cells, with a separator after each group but the last: what the default renderer lays over the input.
  pub fn cells(&self) -> Row {
    let mut row = Row::new()
      .part(&self.part(InputOtpPart::Root))
      .align_items(Alignment::Center)
      // The pointer is the input's, under the cells: a click on any of them focuses it.
      .pointer_events_none();
    for index in 0..self.length {
      row = row.child(self.cell(index));
      if self.separators.contains(&(index + 1)) {
        row = row.child(self.separator());
      }
    }
    row
  }

  /// How the default renderer builds a code: the input, and its cells over it.
  pub fn render(self) -> Element {
    Stack::new()
      .stack_align(StackAlignment::CenterStart)
      .child(self.input())
      .child(self.cells())
      .into()
  }
}

impl crate::ui::Styled for InputOtp {
  type Component = Self;

  fn rules(self, rules: &crate::ui::Recipe) -> Self {
    Self::rules(self, rules)
  }

  fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
    Self::state(self, state, on)
  }
}

impl Themed for InputOtp {
  const NAME: &'static str = "input_otp";
  type Renderer = InputOtpRenderer;

  fn parts() -> crate::ui::Declared {
    InputOtpPart::declared()
  }

  fn states() -> crate::ui::Declared {
    InputOtpState::declared()
  }

  /// The design's code (`pencil/lurq-design-system.pen`, Input OTP): cells 36 wide and 40 high, 6 apart, each the
  /// background's fill in an edge of 1 in the color of an input's, `radius-md` round, its character in the mono
  /// type at 16 in the foreground. The cell the focus is on has a line of 2 inside it in the ring's color, and
  /// while it is empty a caret 1.5 wide and 18 high in the foreground. Groups are parted by a dash 16 across in
  /// the muted foreground. Small cells are 32 by 36 with type of 14, large ones 40 by 44 with type of 18. The edges
  /// say a judgment as an input's do; a disabled code is half opaque on the muted fill, a read-only one on the
  /// muted fill in the color of a border.
  fn recipe() -> Recipe {
    use InputOtpPart::{Caret, Cell, Root, Separator, Value};
    use PaletteColor::{Border, Foreground, Muted, MutedForeground, Ring};
    let recipe = Recipe::new()
      .base(Root, |s| s.gap(6.0))
      .base(Cell, |s| text_box::box_style(s).width(36.0).height(40.0).padding_x(0.0))
      .base(Value, |s| {
        s.typography(TypographyStyle::Mono)
          .font_size(16.0)
          .foreground(Foreground)
      })
      .base(Caret, |s| s.width(1.5).height(18.0).background(Foreground))
      .base(Separator, |s| s.square(IconSize::Md).foreground(MutedForeground))
      .state(InputOtpState::Active, Cell, |s| s.outline(text_box::ring(Ring)))
      .state(TextBoxState::ReadOnly, Cell, |s| {
        s.background(Muted).border(text_box::edge(Border))
      })
      .state(TextBoxState::Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
      .state(TextBoxState::Disabled, Cell, |s| {
        s.background(Muted).cursor(CursorIcon::Default)
      });
    let recipe = [
      (TextBoxSize::Sm, 32.0, 36.0, 14.0, 16.0),
      (TextBoxSize::Lg, 40.0, 44.0, 18.0, 20.0),
    ]
    .into_iter()
    .fold(recipe, |recipe, (size, width, height, font_size, caret)| {
      recipe
        .size(size, Cell, |s| s.width(width).height(height))
        .size(size, Value, |s| s.font_size(font_size))
        .size(size, Caret, |s| s.height(caret))
    });
    // A judgment colors every edge, and the ring of the cell the focus is on.
    [Judgment::Invalid, Judgment::Warning, Judgment::Valid]
      .into_iter()
      .fold(recipe, |recipe, judgment| {
        let (state, color) = (TextBoxState::from(judgment), judgment.color());
        recipe
          .state(state, Cell, |s| s.border(text_box::edge(color.clone())))
          .when(Selector::new().state(state).state(InputOtpState::Active), Cell, |s| {
            s.outline(text_box::ring(color))
          })
      })
  }
}