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
use super::*;

impl TextInputState {
  pub(crate) fn copy_runtime_state_from(&self, old: &Self, layout_signature_matches: bool) {
    if Arc::ptr_eq(&self.inner, &old.inner) {
      return;
    }
    // An input that keeps its text while focused and is bound to another signal by this render takes the text it
    // showed into the new signal: what is being typed is the input's until it is left.
    let carried = {
      let old_inner = old.inner.lock();
      (old_inner.focused && self.inner.lock().keeps_text_while_focused && !self.same_value(old))
        .then(|| old.value.get_untracked())
    };
    if let Some(text) = carried
      && self.value.get_untracked() != text
    {
      self.value.set(text);
    }
    let old_inner = old.inner.lock();
    let old_caret = old_inner.caret;
    let old_affinity = old_inner.token_affinity.clone();
    let old_last_value = old_inner.last_value.clone();
    let old_selection_anchor = old_inner.selection_anchor;
    let old_caret_x = old_inner.caret_x;
    let old_caret_y = old_inner.caret_y;
    let old_caret_height = old_inner.caret_height;
    let old_text_band = old_inner.text_band;
    let old_caret_positions = old_inner.caret_positions.clone();
    let old_scroll_x = old_inner.scroll_x;
    let old_scroll = old_inner.scroll.clone();
    let old_follow_caret = old_inner.follow_caret;
    let old_vertical_goal = old_inner.vertical_goal;
    let old_focused = old_inner.focused;
    let old_composition = old_inner.composition.clone();
    let old_undo_stack = old_inner.undo_stack.clone();
    let old_redo_stack = old_inner.redo_stack.clone();
    // The run of typing goes on in the input built again for what was typed, as the steps of undo do.
    let old_typing = (old_inner.typing, old_inner.undo_grouping, old_inner.typing_pause);
    let old_styled = old_inner.styled.clone();
    let old_hover = (old_inner.hovered_token, old_inner.token_icon_hovered);
    let layout_dirty = if layout_signature_matches {
      old.layout_dirty.load(Ordering::Relaxed)
    } else {
      self.layout_dirty.load(Ordering::Relaxed) || old.layout_dirty.load(Ordering::Relaxed)
    };
    let len = self.value().len();
    let mut inner = self.inner.lock();
    inner.last_value = old_last_value;
    inner.caret = old_caret.min(len);
    inner.token_affinity = old_affinity;
    inner.selection_anchor = old_selection_anchor.map(|anchor| anchor.min(len));
    inner.caret_x = old_caret_x;
    inner.caret_y = old_caret_y;
    inner.caret_height = old_caret_height;
    inner.text_band = old_text_band;
    inner.caret_positions = old_caret_positions;
    inner.scroll_x = old_scroll_x;
    inner.scroll = old_scroll;
    inner.follow_caret = old_follow_caret;
    inner.vertical_goal = old_vertical_goal;
    inner.focused = old_focused;
    inner.composition = old_composition;
    inner.undo_stack = old_undo_stack;
    inner.redo_stack = old_redo_stack;
    (inner.typing, inner.undo_grouping, inner.typing_pause) = old_typing;
    inner.styled.copy_runtime_state_from(&old_styled);
    // The pointer is where it was: on the same token, until it moves.
    (inner.hovered_token, inner.token_icon_hovered) = old_hover;
    self.layout_dirty.store(layout_dirty, Ordering::Relaxed);
  }

  pub(crate) fn sync_caret_metrics_to_position(&self, line_height: f32) {
    if self.sync_token_caret() {
      return;
    }
    let value = self.value();
    let composed = self.composed_text();
    let mut inner = self.inner.lock();
    // While composing, caret positions index the displayed text and the
    // caret sits at the input method's cursor inside the composition.
    let (text, caret) = match composed {
      Some(composed) => {
        let caret = Self::composed_caret(&inner, &value).unwrap_or(inner.caret);
        (composed, caret)
      }
      None => (value, inner.caret),
    };
    let caret = clamp_to_char_boundary(&text, caret);
    if inner.overflow == TextInputOverflow::Multiline
      && caret == text.len()
      && trailing_break_len(&text[..caret]) > 0
      && inner.token_layout.is_none()
    {
      inner.caret_x = 0.0;
      inner.caret_y = trailing_break_row_y(&inner.caret_positions, &text, line_height);
      return;
    }
    inner.caret_x = caret_x_for_index(&inner.caret_positions, caret);
    inner.caret_y = caret_y_for_index(&inner.caret_positions, caret).unwrap_or_else(|| {
      if inner.overflow == TextInputOverflow::Multiline {
        text[..caret].chars().filter(|ch| *ch == '\n').count() as f32 * line_height
      } else {
        0.0
      }
    });
  }

  /// Every place a caret can be, up to `limit` of them: its index in the displayed text and where it is in the
  /// input's text coordinates, as [`caret_x`](Self::caret_x) and [`caret_y`](Self::caret_y) are for the caret.
  pub(crate) fn caret_table(&self, limit: usize) -> Vec<(usize, f32, f32)> {
    let inner = self.inner.lock();
    let (scroll_x, scroll_y) = (inner.scroll_x, inner.scroll_y());
    inner
      .caret_positions
      .iter()
      .take(limit)
      .map(|position| (position.index, position.x - scroll_x, position.y - scroll_y))
      .collect()
  }

  pub(crate) fn caret_x(&self) -> f32 {
    let inner = self.inner.lock();
    inner.caret_x - inner.scroll_x
  }

  /// Whether the caret is at the left edge of its line's text box (the start
  /// of a left-aligned line), with no glyph before it.
  pub(crate) fn caret_at_line_start(&self) -> bool {
    self.inner.lock().caret_x <= 0.0
  }

  pub(crate) fn caret_y(&self) -> f32 {
    let inner = self.inner.lock();
    inner.caret_y - inner.scroll_y()
  }

  pub(crate) fn set_caret_height(&self, caret_height: f32) {
    self.inner.lock().caret_height = caret_height;
  }

  pub(crate) fn caret_height(&self) -> f32 {
    self.inner.lock().caret_height
  }

  pub(crate) fn set_text_band(&self, band: Option<(f32, f32)>) {
    self.inner.lock().text_band = band;
  }

  /// `(top, height)` of the glyphs' band within a line box: the font's ascent
  /// and descent, centered in the line height. The whole line box until the
  /// input has been laid out.
  pub(crate) fn text_band(&self) -> (f32, f32) {
    let inner = self.inner.lock();
    inner.text_band.unwrap_or((0.0, inner.caret_height))
  }

  pub(crate) fn set_caret_positions(&self, positions: CaretPositions) {
    self.inner.lock().caret_positions = positions;
  }

  pub(crate) fn set_scroll_x(&self, scroll_x: f32) {
    self.inner.lock().scroll_x = scroll_x.max(0.0);
  }

  pub(crate) fn scroll_x(&self) -> f32 {
    self.inner.lock().scroll_x
  }

  /// Records where layout put the text: the input's box, `viewport_width` by
  /// `viewport_height`, showing `content_height` of text and padding from
  /// `scroll_y`.
  pub(crate) fn set_vertical_scroll(
    &self,
    viewport_width: f32,
    viewport_height: f32,
    content_height: f32,
    scroll_y: f32,
  ) {
    self
      .inner
      .lock()
      .scroll
      .set_text_layout(viewport_width, viewport_height, content_height, scroll_y);
  }

  pub(crate) fn scroll_y(&self) -> f32 {
    self.inner.lock().scroll_y()
  }

  /// How tall the text with its padding is, as layout last recorded it: what
  /// the input's vertical scroll ranges over.
  pub(crate) fn scroll_content_height(&self) -> f32 {
    self.inner.lock().scroll.content_height()
  }

  /// The scroll a multiline input shows its text with, for the wheel and the
  /// scrollbar; a single-line input scrolls only with its caret.
  pub(crate) fn scroll_state(&self) -> Option<ScrollState> {
    let inner = self.inner.lock();
    (inner.overflow == TextInputOverflow::Multiline).then(|| inner.scroll.clone())
  }

  /// Takes a scroll by wheel or scrollbar since the last layout: from then
  /// on, layout leaves the text where it was scrolled instead of bringing
  /// the caret back, until the next edit or caret move. Returns whether the
  /// input was scrolled.
  pub(crate) fn take_user_scroll(&self) -> bool {
    let mut inner = self.inner.lock();
    let scrolled = inner.scroll.take_scroll_dirty();
    if scrolled {
      inner.follow_caret = false;
    }
    scrolled
  }

  pub(crate) fn follows_caret(&self) -> bool {
    self.inner.lock().follow_caret
  }

  pub(crate) fn set_unfocused_overflow_anchor(&self, anchor: TextInputOverflowAnchor) {
    let mut inner = self.inner.lock();
    if inner.unfocused_overflow_anchor == anchor {
      return;
    }
    inner.unfocused_overflow_anchor = anchor;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  pub(crate) fn unfocused_overflow_anchor(&self) -> TextInputOverflowAnchor {
    self.inner.lock().unfocused_overflow_anchor
  }

  pub(crate) fn set_overflow(&self, overflow: TextInputOverflow) {
    let mut inner = self.inner.lock();
    if inner.overflow == overflow {
      return;
    }
    inner.overflow = overflow;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  pub(crate) fn overflow(&self) -> TextInputOverflow {
    self.inner.lock().overflow
  }

  /// Sets whether a multiline input's lines wrap at its width.
  pub(crate) fn set_line_wrap(&self, wrap: bool) {
    let mut inner = self.inner.lock();
    if inner.line_wrap == wrap {
      return;
    }
    inner.line_wrap = wrap;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// Whether the input's lines wrap at its width: a multiline input's do unless it was told otherwise.
  pub(crate) fn wraps_lines(&self) -> bool {
    let inner = self.inner.lock();
    inner.overflow == TextInputOverflow::Multiline && inner.line_wrap
  }

  /// Sets the numbers and the bands drawn beside the lines.
  pub(crate) fn set_gutter(&self, gutter: Option<std::sync::Arc<crate::components::LineGutter>>) {
    self.inner.lock().gutter = gutter;
  }

  /// The numbers and the bands drawn beside the lines, if the input has them.
  pub(crate) fn gutter(&self) -> Option<std::sync::Arc<crate::components::LineGutter>> {
    self.inner.lock().gutter.clone()
  }

  pub(crate) fn set_rows(&self, min_rows: usize, max_rows: usize) {
    let min_rows = min_rows.max(1);
    let max_rows = max_rows.max(min_rows);
    let mut inner = self.inner.lock();
    if inner.overflow == TextInputOverflow::Multiline
      && inner.min_rows == Some(min_rows)
      && inner.max_rows == Some(max_rows)
    {
      return;
    }
    inner.overflow = TextInputOverflow::Multiline;
    inner.min_rows = Some(min_rows);
    inner.max_rows = Some(max_rows);
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  pub(crate) fn set_min_rows(&self, min_rows: usize) {
    let min_rows = min_rows.max(1);
    let mut inner = self.inner.lock();
    let next_max_rows = inner.max_rows.map(|max_rows| max_rows.max(min_rows));
    if inner.overflow == TextInputOverflow::Multiline
      && inner.min_rows == Some(min_rows)
      && inner.max_rows == next_max_rows
    {
      return;
    }
    inner.overflow = TextInputOverflow::Multiline;
    inner.min_rows = Some(min_rows);
    inner.max_rows = next_max_rows;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  pub(crate) fn set_max_rows(&self, max_rows: usize) {
    let max_rows = max_rows.max(1);
    let mut inner = self.inner.lock();
    let next_min_rows = inner.min_rows.map(|min_rows| min_rows.min(max_rows));
    if inner.overflow == TextInputOverflow::Multiline
      && inner.max_rows == Some(max_rows)
      && inner.min_rows == next_min_rows
    {
      return;
    }
    inner.overflow = TextInputOverflow::Multiline;
    inner.max_rows = Some(max_rows);
    inner.min_rows = next_min_rows;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  pub(crate) fn set_rows_exact(&self, rows: usize) {
    let rows = rows.max(1);
    let mut inner = self.inner.lock();
    if inner.overflow == TextInputOverflow::Multiline && inner.min_rows == Some(rows) && inner.max_rows == Some(rows) {
      return;
    }
    inner.overflow = TextInputOverflow::Multiline;
    inner.min_rows = Some(rows);
    inner.max_rows = Some(rows);
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  pub(crate) fn row_limits(&self) -> (Option<usize>, Option<usize>) {
    let inner = self.inner.lock();
    (inner.min_rows, inner.max_rows)
  }

  pub(crate) fn selection_ranges(&self) -> Vec<TextSelectionRange> {
    let value = self.value();
    let inner = self.inner.lock();
    // A composition is shown in place of the selection.
    if inner.composition.is_some() {
      return Vec::new();
    }
    let Some((start, end)) = selection_range_indices(&value, inner.selection_anchor, inner.caret) else {
      return Vec::new();
    };
    drop(inner);
    self.range_rects(start, end)
  }

  /// Where the text from the byte `start` to the byte `end` is drawn: a rectangle for each row it is on, from
  /// the start of the text's box with the scroll taken off, as a selection of it would be.
  pub(crate) fn range_rects(&self, start: usize, end: usize) -> Vec<TextSelectionRange> {
    let value = self.value();
    let (start, end) = (
      clamp_to_char_boundary(&value, start.min(value.len())),
      clamp_to_char_boundary(&value, end.min(value.len())),
    );
    if start >= end {
      return Vec::new();
    }
    let decorated = self.token_layout();
    let inner = self.inner.lock();
    if let Some(layout) = decorated {
      return layout.selection(start..end, (inner.scroll_x, inner.scroll_y()));
    }
    selection_ranges_for_positions(
      &inner.caret_positions,
      None,
      start,
      end,
      inner.scroll_x,
      inner.scroll_y(),
    )
  }

  /// Sets the wavy lines drawn under ranges of the text.
  pub(crate) fn set_squiggles(&self, squiggles: Option<crate::components::InputSquiggles>) {
    let mut inner = self.inner.lock();
    if inner.squiggles == squiggles {
      return;
    }
    inner.squiggles = squiggles;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// The wavy lines to draw: none while the text is masked or an input method composes over it.
  pub(crate) fn squiggles(&self) -> Option<crate::components::InputSquiggles> {
    let inner = self.inner.lock();
    if inner.mask.is_some() || inner.composition.is_some() {
      return None;
    }
    inner.squiggles.clone()
  }

  /// Sets what is drawn after the end of the text while the caret is there.
  pub(crate) fn set_ghost(&self, ghost: Option<crate::components::InputGhost>) {
    let mut inner = self.inner.lock();
    if inner.ghost == ghost {
      return;
    }
    inner.ghost = ghost;
    drop(inner);
    self.mark_layout_dirty_keeping_scroll();
  }

  /// The suggestion to draw: while the input has the focus and holds text, with the caret at its end and
  /// nothing selected, in a plain input that is neither masked nor composed over.
  pub(crate) fn shown_ghost(&self) -> Option<crate::components::InputGhost> {
    let value = self.value();
    let inner = self.inner.lock();
    let shown = inner.focused
      && inner.mask.is_none()
      && inner.composition.is_none()
      && inner.token_layout.is_none()
      && !value.is_empty()
      && inner.caret == value.len()
      && selection_range_indices(&value, inner.selection_anchor, inner.caret).is_none();
    inner.ghost.clone().filter(|ghost| shown && !ghost.text.is_empty())
  }

  pub(crate) fn set_focused(&self, focused: bool) {
    let mut inner = self.inner.lock();
    if inner.focused == focused {
      return;
    }
    inner.focused = focused;
    // A composition belongs to the focused input; leaving it cancels it.
    if !focused {
      inner.composition = None;
    }
    // An input told so has all its text selected as the focus comes: what is typed takes its place. A press
    // that brings the focus then puts the caret where it is.
    if focused && inner.select_on_focus && inner.mask.is_none() {
      let end = self.value.with_untracked(String::len);
      inner.selection_anchor = (end > 0).then_some(0);
      inner.caret = end;
      inner.token_affinity = None;
    }
    drop(inner);
    // Taking the focus shows the caret; losing it leaves the text where it is.
    if focused {
      self.mark_layout_dirty();
    } else {
      self.mark_layout_dirty_keeping_scroll();
    }
  }

  pub(crate) fn is_focused(&self) -> bool {
    self.inner.lock().focused
  }
}