lurq 0.48.2

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
use parking_lot::Mutex;

use super::*;

#[path = "../text_input_composition.rs"]
mod composition;
mod controller_access;
mod editing;
mod geometry;
pub(crate) use controller_access::WeakTextInputState;
#[cfg(test)]
#[path = "../../../../tests/input/text_input/source.rs"]
mod source_tests;
mod token_affinity;
mod tokens;
use composition::Composition;
use token_affinity::TokenAffinity;

const MAX_TEXT_INPUT_HISTORY: usize = 128;
type TextInputCallback = EventHandler<TextInputEvent>;

#[derive(Clone)]
pub(crate) struct TextInputState {
  value: Signal<String>,
  inner: Arc<Mutex<TextInputInner>>,
  on_input: Arc<Mutex<Vec<TextInputCallback>>>,
  layout_dirty: Arc<AtomicBool>,
}

struct TextInputInner {
  tokens: Option<crate::components::InputTokens>,
  token_layout: Option<Arc<crate::layout::layout_engine::input_token_layout::TokenLayout>>,
  token_affinity: Option<TokenAffinity>,
  placeholder: Option<String>,
  last_value: String,
  caret: usize,
  selection_anchor: Option<usize>,
  caret_x: f32,
  caret_y: f32,
  caret_height: f32,
  /// Where a line's glyphs sit in its line box by the font's ascent and
  /// descent, as `(top, height)`; selections cover it.
  text_band: Option<(f32, f32)>,
  caret_positions: CaretPositions,
  scroll_x: f32,
  /// The vertical scroll of a multiline input: the offset it shows its text
  /// from and the scrollbar, wheel and scrollbar drags move.
  scroll: ScrollState,
  /// Whether layout keeps the caret's line in view. An edit or a caret move
  /// sets it; scrolling by wheel or scrollbar clears it, so the text stays
  /// where the person scrolled it until the next edit.
  follow_caret: bool,
  /// The column ArrowUp and ArrowDown keep: the x the first of a run of
  /// vertical moves started from, and the caret that run left. Any other
  /// caret move or edit ends the run, and so does a caret found anywhere
  /// else.
  vertical_goal: Option<(usize, f32)>,
  unfocused_overflow_anchor: TextInputOverflowAnchor,
  overflow: TextInputOverflow,
  min_rows: Option<usize>,
  max_rows: Option<usize>,
  mask: Option<char>,
  focused: bool,
  /// An input method composition in progress, shown at the caret.
  composition: Option<Composition>,
  undo_stack: Vec<TextInputSnapshot>,
  redo_stack: Vec<TextInputSnapshot>,
}

#[derive(Clone)]
struct TextInputSnapshot {
  value: String,
  caret: usize,
  selection_anchor: Option<usize>,
}

#[derive(Clone, PartialEq)]
pub(crate) struct TextInputLayoutSignature {
  tokens: Option<crate::components::InputTokens>,
  placeholder: Option<String>,
  unfocused_overflow_anchor: TextInputOverflowAnchor,
  overflow: TextInputOverflow,
  min_rows: Option<usize>,
  max_rows: Option<usize>,
  mask: Option<char>,
}

impl TextInputState {
  pub(crate) fn same_value(&self, other: &Self) -> bool {
    self.value.id() == other.value.id()
  }

  /// Id of the bound value signal, which identifies the input across
  /// re-renders.
  pub(crate) fn value_signal_id(&self) -> usize {
    self.value.id()
  }
  pub(crate) fn new(value: Signal<String>) -> Self {
    let initial_value = value.get_untracked();
    let caret = initial_value.len();
    Self {
      value,
      inner: Arc::new(Mutex::new(TextInputInner {
        tokens: None,
        token_layout: None,
        token_affinity: None,
        placeholder: None,
        last_value: initial_value,
        caret,
        selection_anchor: None,
        caret_x: 0.0,
        caret_y: 0.0,
        caret_height: 0.0,
        text_band: None,
        caret_positions: vec![CaretPosition {
          index: 0,
          x: 0.0,
          y: 0.0,
        }]
        .into(),
        scroll_x: 0.0,
        scroll: ScrollState::new(),
        follow_caret: true,
        vertical_goal: None,
        unfocused_overflow_anchor: TextInputOverflowAnchor::default(),
        overflow: TextInputOverflow::default(),
        min_rows: None,
        max_rows: None,
        mask: None,
        focused: false,
        composition: None,
        undo_stack: Vec::new(),
        redo_stack: Vec::new(),
      })),
      on_input: Arc::new(Mutex::new(Vec::new())),
      layout_dirty: Arc::new(AtomicBool::new(false)),
    }
  }

  pub(crate) fn value(&self) -> String {
    self.value.get_untracked()
  }

  /// Programmatic value write, DOM `el.value = x` semantics: updates the
  /// backing signal (the same one the app holds) and clamps caret/selection
  /// via [`TextInputState::sync_external_value`], but does NOT fire
  /// `on_input` handlers.
  pub(crate) fn set_value(&self, value: impl Into<String>) {
    let value = value.into();
    if self.value.get_untracked() == value {
      return;
    }
    self.value.set(value);
    self.sync_external_value();
  }

  pub(crate) fn set_on_input(&self, f: impl IntoTextInputEventHandler) {
    self.on_input.lock().push(f.into_event_handler());
  }

  pub(crate) fn clear_on_input(&self, f: impl IntoTextInputEventHandler) {
    let handler = f.into_event_handler();
    self.on_input.lock().retain(|existing| !existing.same_handler(&handler));
  }

  /// Replace the value from outside the keyboard path (automation tooling).
  /// Writes the app's value signal and reconciles caret/selection the same
  /// way any external signal write is reconciled; `on_input` handlers do not
  /// fire, matching a controlled update rather than a keystroke.
  #[cfg(feature = "mcp")]
  pub(crate) fn set_value_external(&self, value: String) {
    self.value.set(value);
    self.sync_external_value();
  }

  pub(crate) fn sync_external_value(&self) -> bool {
    let value = self.value();
    let mut inner = self.inner.lock();
    if inner.last_value == value {
      return false;
    }

    let previous_len = inner.last_value.len();
    if inner.selection_anchor.is_none() && inner.caret == previous_len {
      inner.caret = value.len();
    } else {
      inner.caret = clamp_to_char_boundary(&value, inner.caret.min(value.len()));
      inner.selection_anchor = inner
        .selection_anchor
        .map(|anchor| clamp_to_char_boundary(&value, anchor.min(value.len())));
    }
    inner.last_value = value;
    inner.token_affinity = None;
    drop(inner);
    self.mark_layout_dirty();
    true
  }

  pub(crate) fn set_placeholder(&self, placeholder: impl Into<String>) {
    let placeholder = placeholder.into();
    let mut inner = self.inner.lock();
    if inner.placeholder.as_deref() == Some(placeholder.as_str()) {
      return;
    }
    inner.placeholder = Some(placeholder);
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  pub(crate) fn placeholder(&self) -> Option<String> {
    self.inner.lock().placeholder.clone()
  }

  pub(crate) fn rendered_text(&self) -> Option<String> {
    if let Some(composed) = self.composed_text() {
      return Some(composed);
    }
    let value = self.value();
    if value.is_empty() {
      self.placeholder()
    } else {
      Some(value)
    }
  }

  pub(crate) fn is_showing_placeholder(&self) -> bool {
    self.value().is_empty() && self.placeholder().is_some() && !self.is_composing()
  }

  pub(crate) fn rendered_text_for_layout(&self) -> String {
    let text = self.rendered_text().unwrap_or_default();
    let text = match self.overflow() {
      TextInputOverflow::Multiline => text,
      TextInputOverflow::Scroll => text.replace(['\r', '\n'], " "),
    };
    match self.mask() {
      Some(mask) if !self.is_showing_placeholder() => mask_text(&text, mask),
      _ => text,
    }
  }

  /// Text used to compute caret geometry. Masked the same way the rendered text
  /// is, so caret x-coordinates line up with the displayed mask glyphs.
  pub(crate) fn caret_source_text(&self) -> String {
    let value = self.value();
    match self.mask() {
      Some(mask) => mask_text(&value, mask),
      None => value,
    }
  }

  /// Text the caret positions are laid out over: the displayed text while an
  /// input method composes (indices then point into it, see
  /// `text_input_composition`), else [`TextInputState::caret_source_text`].
  /// Unmasked Scroll breaks use one space per byte, matching paint without changing source offsets.
  pub(crate) fn caret_layout_text(&self) -> String {
    let text = self.composed_text().unwrap_or_else(|| self.caret_source_text());
    if self.overflow() == TextInputOverflow::Scroll && !self.is_masked() {
      text.replace(['\r', '\n'], " ")
    } else {
      text
    }
  }

  /// Caret positions are computed over the masked string, so their `index` is a
  /// byte offset into the mask — not the real value. Remap each back to the real
  /// value's char boundaries so editing and hit-testing stay correct.
  pub(crate) fn remap_caret_positions(&self, positions: &mut CaretPositions) {
    let Some(mask) = self.mask() else {
      return;
    };
    let mask_len = mask.len_utf8();
    let value = self.value();
    let boundaries: Vec<usize> = value
      .char_indices()
      .map(|(index, _)| index)
      .chain(std::iter::once(value.len()))
      .collect();
    for position in positions.make_mut().iter_mut() {
      let char_index = position.index / mask_len;
      position.index = boundaries.get(char_index).copied().unwrap_or(value.len());
    }
  }

  pub(crate) fn set_mask(&self, mask: Option<char>) {
    let mut inner = self.inner.lock();
    if inner.mask == mask {
      return;
    }
    inner.mask = mask;
    inner.token_affinity = None;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// The configured display mask, if this is a masked input.
  pub fn mask(&self) -> Option<char> {
    self.inner.lock().mask
  }

  pub fn is_masked(&self) -> bool {
    self.mask().is_some()
  }
}

impl TextInputState {
  fn closest_caret_to_point(&self, x: f32, y: f32) -> usize {
    if let Some(byte) = self.token_hit(x, y) {
      return byte;
    }
    let value = self.value();
    let inner = self.inner.lock();
    let content_x = x + inner.scroll_x;
    let content_y = y + inner.scroll_y();
    let index = closest_caret_to_point(&inner.caret_positions, content_x, content_y);
    Self::composed_to_value_index(&inner, &value, index)
  }

  fn delete_selection_if_present(&self) -> Option<usize> {
    let mut selected = None;
    {
      let value = self.value();
      let inner = self.inner.lock();
      if let Some(range) = selection_range_indices(&value, inner.selection_anchor, inner.caret) {
        selected = Some(range);
      }
    }

    let (start, end) = selected?;
    self.value.update(|value| {
      value.replace_range(start..end, "");
    });
    let mut inner = self.inner.lock();
    inner.caret = start;
    inner.selection_anchor = None;
    drop(inner);
    self.mark_layout_dirty();
    Some(start)
  }

  pub(crate) fn layout_signature(&self) -> TextInputLayoutSignature {
    let inner = self.inner.lock();
    TextInputLayoutSignature {
      tokens: inner.tokens.clone(),
      placeholder: inner.placeholder.clone(),
      unfocused_overflow_anchor: inner.unfocused_overflow_anchor,
      overflow: inner.overflow,
      min_rows: inner.min_rows,
      max_rows: inner.max_rows,
      mask: inner.mask,
    }
  }

  pub(crate) fn take_layout_dirty(&self) -> bool {
    self.layout_dirty.swap(false, Ordering::Relaxed)
  }

  pub(crate) fn has_layout_dirty(&self) -> bool {
    self.layout_dirty.load(Ordering::Relaxed)
  }

  /// Marks an edit or a caret move: lay the input out again and bring the
  /// caret's line into view. It ends a run of vertical moves.
  fn mark_layout_dirty(&self) {
    let mut inner = self.inner.lock();
    inner.follow_caret = true;
    inner.vertical_goal = None;
    drop(inner);
    self.layout_dirty.store(true, Ordering::Relaxed);
  }

  /// Marks a change of how the input looks (its placeholder, mode, rows or
  /// focus) that leaves the scroll where it is.
  fn mark_layout_dirty_keeping_scroll(&self) {
    self.layout_dirty.store(true, Ordering::Relaxed);
  }

  /// Fire `on_input` handlers with the text this edit is about to produce.
  /// `new_value` is only evaluated when handlers are attached.
  fn fire_input(&self, keyboard: &KeyboardEvent, new_value: impl FnOnce() -> String) -> bool {
    let handlers = self.on_input.lock().clone();
    if handlers.is_empty() {
      return true;
    }

    let event = TextInputEvent::new(self.value.clone(), new_value(), keyboard.clone());
    for handler in handlers {
      handler.call(&event);
    }
    self.sync_external_value();
    !event.default_prevented()
  }

  /// The text as it will read after the current selection is removed, plus
  /// the caret position after that removal. The value is unchanged (and the
  /// caret current) when nothing is selected.
  fn value_without_selection(&self) -> (String, usize) {
    let mut value = self.value();
    let inner = self.inner.lock();
    match selection_range_indices(&value, inner.selection_anchor, inner.caret) {
      Some((start, end)) => {
        value.replace_range(start..end, "");
        (value, start)
      }
      None => {
        let caret = inner.caret;
        (value, caret)
      }
    }
  }

  pub(crate) fn has_selection(&self) -> bool {
    let value = self.value();
    let inner = self.inner.lock();
    selection_range_indices(&value, inner.selection_anchor, inner.caret).is_some()
  }

  fn snapshot(&self) -> TextInputSnapshot {
    let value = self.value();
    let inner = self.inner.lock();
    TextInputSnapshot {
      value,
      caret: inner.caret,
      selection_anchor: inner.selection_anchor,
    }
  }

  fn push_undo_snapshot(&self) {
    let snapshot = self.snapshot();
    let mut inner = self.inner.lock();
    push_limited(&mut inner.undo_stack, snapshot);
    inner.redo_stack.clear();
  }

  fn push_undo_snapshot_value(&self, snapshot: TextInputSnapshot) {
    let mut inner = self.inner.lock();
    push_limited(&mut inner.undo_stack, snapshot);
  }

  fn push_redo_snapshot(&self, snapshot: TextInputSnapshot) {
    let mut inner = self.inner.lock();
    push_limited(&mut inner.redo_stack, snapshot);
  }

  fn restore_snapshot(&self, snapshot: TextInputSnapshot) {
    let value = snapshot.value;
    let len = value.len();
    let caret = clamp_to_char_boundary(&value, snapshot.caret.min(len));
    let selection_anchor = snapshot
      .selection_anchor
      .map(|anchor| clamp_to_char_boundary(&value, anchor.min(len)));
    self.value.set(value.clone());
    let mut inner = self.inner.lock();
    inner.last_value = value;
    inner.token_affinity = None;
    inner.caret = caret;
    inner.selection_anchor = selection_anchor;
  }
}

impl TextInputInner {
  fn scroll_y(&self) -> f32 {
    self.scroll.scroll_y()
  }
}

fn mask_text(text: &str, mask: char) -> String {
  let mut out = String::with_capacity(text.chars().count() * mask.len_utf8());
  for _ in text.chars() {
    out.push(mask);
  }
  out
}

fn push_limited(stack: &mut Vec<TextInputSnapshot>, snapshot: TextInputSnapshot) {
  if stack.last().is_some_and(|last| {
    last.value == snapshot.value && last.caret == snapshot.caret && last.selection_anchor == snapshot.selection_anchor
  }) {
    return;
  }
  stack.push(snapshot);
  if stack.len() > MAX_TEXT_INPUT_HISTORY {
    stack.remove(0);
  }
}

fn move_inner_to(inner: &mut TextInputInner, target: usize, selecting: bool) {
  if selecting {
    if inner.selection_anchor.is_none() {
      inner.selection_anchor = Some(inner.caret);
    }
  } else {
    inner.selection_anchor = None;
  }
  inner.caret = target;
}