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
//! How a component that shows code draws its tokens: each name in the style its recipe gives a part of that name,
//! or the theme's code colors give it, dropping the tail of a name neither has.

use std::{
  cell::RefCell,
  collections::HashMap,
  sync::{
    Arc, LazyLock, Mutex, Weak,
    atomic::{AtomicU64, Ordering},
  },
};

use super::recipe::{Name, ResolvedRecipe};
use crate::{
  app::theme::{CodeScheme, CodeStyle, Theme},
  code::{BufferId, CodeEdit, CodeToken, CodeTokenizer, LineCache, TokenName},
  components::{InputToken, InputTokenStyle, InputTokens},
  core::{Signal, Subscription},
  layout::{
    quad::RichTextSpan,
    text_style::{FontStyle, TextStyle},
  },
  node::{TextColor, color::Color},
};

/// The tokens of `code`: by `tokenizer` where it is given, by the tokenizer registered for `language`
/// otherwise, none where neither is. Read in a render, the language's renders that again when its tokenizer
/// changes.
pub(crate) fn code_tokens(code: &str, language: Option<&str>, tokenizer: Option<&CodeTokenizer>) -> Arc<[CodeToken]> {
  code_tokens_kept(code, language, tokenizer, None)
}

/// As [`code_tokens`], from what `kept` holds of the text before where it is given: a text that changes (a block's
/// code streamed in) is re-tokenized from its first changed line.
pub(crate) fn code_tokens_kept(
  code: &str,
  language: Option<&str>,
  tokenizer: Option<&CodeTokenizer>,
  kept: Option<&std::sync::Mutex<LineCache>>,
) -> Arc<[CodeToken]> {
  let tokenizer = match (tokenizer, language) {
    (Some(tokenizer), _) => Some(tokenizer.clone()),
    (None, Some(language)) => crate::code::tokenizer(language),
    _ => None,
  };
  let Some(tokenizer) = tokenizer.filter(|_| !code.is_empty()) else {
    return Arc::from([]);
  };
  match kept {
    Some(kept) => {
      tokenizer.track();
      kept.lock().unwrap().tokens(&tokenizer, code)
    }
    None => tokenizer.tokens(code),
  }
}

static NEXT_BUFFER: AtomicU64 = AtomicU64::new(1);

/// An editable code field's text as its tokenizer sees it: one for each value signal while a field holds it.
pub(crate) struct CodeBuffer {
  id: BufferId,
  inner: Mutex<BufferInner>,
}

struct BufferInner {
  /// The text the tokenizer was last told of.
  text: String,
  /// The tokenizer it was last tokenized by, which is told its edits and that it is gone.
  tokenizer: Option<CodeTokenizer>,
  lines: LineCache,
  /// What tells the buffer each change of the value.
  _watch: Option<Subscription<String>>,
}

/// The buffer of each value signal, while a field holds it.
static BUFFERS: LazyLock<Mutex<HashMap<usize, Weak<CodeBuffer>>>> = LazyLock::new(Default::default);

impl CodeBuffer {
  /// The buffer of `value`: the one a field of it has, or a new one that follows its changes.
  pub(crate) fn of(value: &Signal<String>) -> Arc<CodeBuffer> {
    let mut buffers = BUFFERS.lock().unwrap();
    buffers.retain(|_, buffer| buffer.strong_count() > 0);
    if let Some(buffer) = buffers.get(&value.id()).and_then(Weak::upgrade) {
      return buffer;
    }
    let buffer = Arc::new(CodeBuffer {
      id: BufferId(NEXT_BUFFER.fetch_add(1, Ordering::Relaxed)),
      inner: Mutex::new(BufferInner {
        text: value.get_untracked(),
        tokenizer: None,
        lines: LineCache::default(),
        _watch: None,
      }),
    });
    // Each change of the value is an edit the tokenizer is told, in order, before the tokens after it are asked.
    let weak = Arc::downgrade(&buffer);
    let watch = value.subscribe(move |text: &String| {
      if let Some(buffer) = weak.upgrade() {
        buffer.changed(text);
      }
    });
    buffer.inner.lock().unwrap()._watch = Some(watch);
    buffers.insert(value.id(), Arc::downgrade(&buffer));
    buffer
  }

  pub(crate) fn id(&self) -> BufferId {
    self.id
  }

  fn changed(&self, text: &str) {
    let (edit, tokenizer) = {
      let mut inner = self.inner.lock().unwrap();
      let Some(edit) = CodeEdit::between(&inner.text, text) else {
        return;
      };
      inner.text = text.to_owned();
      (edit, inner.tokenizer.clone())
    };
    if let Some(tokenizer) = tokenizer {
      tokenizer.edited(self.id, &edit);
    }
  }

  /// The tokens of `code`, the text the buffer holds now, by `tokenizer`: line by line from what was kept, or all of
  /// them, the tokenizer told which buffer they are of.
  pub(crate) fn tokens(&self, tokenizer: &CodeTokenizer, code: &str) -> Arc<[CodeToken]> {
    let mut inner = self.inner.lock().unwrap();
    if inner.tokenizer.as_ref() != Some(tokenizer) {
      if let Some(old) = inner.tokenizer.replace(tokenizer.clone()) {
        old.dropped(self.id);
      }
      inner.lines = LineCache::default();
    }
    if inner.text != code {
      // A change the signal did not tell (a value set before the buffer was made): one edit for it.
      if let Some(edit) = CodeEdit::between(&inner.text, code) {
        tokenizer.edited(self.id, &edit);
      }
      inner.text = code.to_owned();
    }
    let id = self.id;
    inner
      .lines
      .tokens_with(tokenizer, code, |tokenizer, code| tokenizer.tokens_in(id, code))
  }
}

impl Drop for CodeBuffer {
  fn drop(&mut self) {
    let tokenizer = self.inner.get_mut().ok().and_then(|inner| inner.tokenizer.take());
    if let Some(tokenizer) = tokenizer {
      tokenizer.dropped(self.id);
    }
  }
}

/// The style of each name of a token, as a component draws it.
pub(crate) struct TokenStyles {
  recipe: ResolvedRecipe,
  /// The component's own parts, which are no tokens' even where a token has their name.
  own: &'static [&'static str],
  scheme: Arc<CodeScheme>,
  base: TextStyle,
  styles: RefCell<HashMap<TokenName, Option<TextStyle>>>,
}

impl TokenStyles {
  /// The styles of tokens in `recipe` and `scheme` (the theme's code colors where it is none), over `base`, the
  /// style of the code where no token is.
  pub(crate) fn new(
    recipe: &ResolvedRecipe,
    own: &'static [&'static str],
    base: TextStyle,
    scheme: Option<Arc<CodeScheme>>,
  ) -> Self {
    Self {
      scheme: scheme.unwrap_or_else(|| recipe.theme().code_scheme()),
      recipe: recipe.clone(),
      own,
      base,
      styles: RefCell::default(),
    }
  }

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

  /// What the recipe and the scheme say of `name`, each of color, weight and leaning from its most particular name
  /// that says it, the recipe's before the scheme's at each.
  pub(crate) fn code_style(&self, name: &str) -> CodeStyle {
    let mut style = CodeStyle::new();
    for name in crate::code::lookup_names(name) {
      let part = Name::new(name);
      if !self.own.contains(&name) && self.recipe.has(part) {
        let rest = self.recipe.part(part).rest();
        style = style.or(&CodeStyle {
          color: rest.foreground,
          weight: rest.text.weight,
          italic: rest.text.style.map(|style| style == FontStyle::Italic),
        });
      }
      if let Some(own) = self.scheme.get(part) {
        style = style.or(own);
      }
      if style.is_whole() {
        break;
      }
    }
    style
  }

  /// The style a token named `name` is drawn in; `None` where it is drawn as the code is.
  pub(crate) fn style(&self, name: TokenName) -> Option<TextStyle> {
    if let Some(style) = self.styles.borrow().get(&name) {
      return style.clone();
    }
    let own = self.code_style(name.as_str());
    let style = (!own.is_empty()).then(|| {
      let mut style = self.base.clone();
      if let Some(color) = &own.color {
        style.color = self.color(color);
      }
      if let Some(weight) = own.weight {
        style.weight = weight;
      }
      if let Some(italic) = own.italic {
        style.style = if italic { FontStyle::Italic } else { FontStyle::Normal };
      }
      style
    });
    self.styles.borrow_mut().insert(name, style.clone());
    style
  }

  fn color(&self, color: &TextColor) -> Color {
    match color {
      TextColor::Color(color) => *color,
      TextColor::Palette(role) => self.theme().palette_color(role.clone()),
    }
  }

  /// `text` (the bytes `range` of the code) in spans of the styles of `tokens`, which are in bytes of the code and in
  /// order.
  pub(crate) fn spans(&self, code: &str, range: std::ops::Range<usize>, tokens: &[CodeToken]) -> Vec<RichTextSpan> {
    let mut spans: Vec<RichTextSpan> = Vec::new();
    let mut push = |text: &str, style: Option<TextStyle>| {
      if text.is_empty() {
        return;
      }
      let style = style.unwrap_or_else(|| self.base.clone());
      match spans.last_mut() {
        Some(last) if last.style == style => last.text.push_str(text),
        _ => spans.push(RichTextSpan {
          text: text.to_owned(),
          style,
        }),
      }
    };
    let first = tokens.partition_point(|token| token.range.end <= range.start);
    let mut at = range.start;
    for token in &tokens[first..] {
      if token.range.start >= range.end {
        break;
      }
      let start = token.range.start.max(range.start);
      let end = token.range.end.min(range.end);
      push(&code[at..start], None);
      push(&code[start..end], self.style(token.kind));
      at = end;
    }
    push(&code[at..range.end], None);
    if spans.is_empty() {
      spans.push(RichTextSpan {
        text: String::new(),
        style: self.base.clone(),
      });
    }
    spans
  }

  /// The tokens of `code` as an input draws them: a run of each on one line, in its color, and its weight and its
  /// leaning where they are not the code's.
  pub(crate) fn input_tokens(&self, code: &str, tokens: &[CodeToken]) -> InputTokens {
    let clear = Color::new(0, 0, 0, 0);
    let mut runs = Vec::new();
    let mut styles: HashMap<TokenName, Option<InputTokenStyle>> = HashMap::new();
    for token in tokens {
      let style = styles
        .entry(token.kind)
        .or_insert_with(|| {
          self.style(token.kind).map(|style| {
            let mut input = InputTokenStyle::new(style.color, clear);
            if style.weight != self.base.weight {
              input = input.weight(style.weight);
            }
            if style.style != self.base.style {
              input = input.font_style(style.style);
            }
            input
          })
        })
        .clone();
      let Some(style) = style else {
        continue;
      };
      // A token of the input stands on one line: one of many lines is one run a line.
      let mut at = token.range.start;
      for piece in code[token.range.clone()].split_inclusive('\n') {
        let text = piece.trim_end_matches(['\n', '\r']);
        if !text.is_empty() {
          runs.push(InputToken::new(at..at + text.len(), style.clone()));
        }
        at += piece.len();
      }
    }
    InputTokens::new(code, runs)
      .unwrap_or_else(|_| InputTokens::new(code, Vec::new()).expect("no tokens are always valid"))
  }
}

impl crate::components::TextInput {
  /// Colors its value as code in `language`, as a code block colors it: told apart by the tokenizer registered for the
  /// language, each token in the color, the weight and the leaning the theme's code colors give its name, in the
  /// input's own type, again after every edit. The colors are the theme's as the input is built.
  ///
  /// ```
  /// use lurq::{components::TextInput, core::Signal};
  ///
  /// let query = TextInput::new(Signal::new("select * from notes".to_owned()))
  ///   .multiline()
  ///   .code_language("sql");
  /// ```
  pub fn code_language(self, language: &str) -> Self {
    let language: Arc<str> = Arc::from(language);
    // The language's tokenizer as it is now: a render after it is replaced, or after it says its tokens are stale,
    // builds the input again.
    if let Some(tokenizer) = crate::code::tokenizer(&language) {
      tokenizer.track();
    }
    self.code_tokens(move || crate::code::tokenizer(&language))
  }

  /// Colors its value as code by `tokenizer`, as [`code_language`](Self::code_language) does by a language's.
  pub fn code_tokenizer(self, tokenizer: impl Into<CodeTokenizer>) -> Self {
    let tokenizer = tokenizer.into();
    tokenizer.track();
    self.code_tokens(move || Some(tokenizer.clone()))
  }

  /// Its value as a buffer its tokenizer is told the edits of, and its tokens by the tokenizer `resolve` gives.
  fn code_tokens(mut self, resolve: impl Fn() -> Option<CodeTokenizer> + Send + Sync + 'static) -> Self {
    let buffer = self.value_signal().map(|value| CodeBuffer::of(&value));
    self.code_tokens_in(&super::theme::current_theme(), move |value| {
      match (resolve(), &buffer) {
        (Some(tokenizer), _) if value.is_empty() => {
          let _ = tokenizer;
          Arc::from([])
        }
        (Some(tokenizer), Some(buffer)) => buffer.tokens(&tokenizer, value),
        (Some(tokenizer), None) => tokenizer.tokens(value),
        (None, _) => Arc::from([]),
      }
    })
  }

  fn code_tokens_in(
    self,
    theme: &crate::app::theme::Theme,
    tokens: impl Fn(&str) -> Arc<[CodeToken]> + Send + Sync + 'static,
  ) -> Self {
    let scheme = theme.code_scheme();
    let clear = Color::new(0, 0, 0, 0);
    self.tokens_with(move |value| {
      let mut styles: HashMap<TokenName, Option<InputTokenStyle>> = HashMap::new();
      let mut runs = Vec::new();
      for token in tokens(value).iter() {
        let style = styles
          .entry(token.kind)
          .or_insert_with(|| {
            let style = scheme.resolve(token.kind.as_str());
            (!style.is_empty()).then(|| {
              // A name with a weight and no color of its own is in the code's color.
              let color = style
                .color
                .clone()
                .unwrap_or(TextColor::Palette(crate::app::theme::PaletteColor::Foreground));
              let mut input = InputTokenStyle::new(color, clear);
              if let Some(weight) = style.weight {
                input = input.weight(weight);
              }
              if let Some(italic) = style.italic {
                input = input.font_style(if italic { FontStyle::Italic } else { FontStyle::Normal });
              }
              input
            })
          })
          .clone();
        let Some(style) = style else {
          continue;
        };
        let mut at = token.range.start;
        for piece in value[token.range.clone()].split_inclusive('\n') {
          let text = piece.trim_end_matches(['\n', '\r']);
          if !text.is_empty() {
            runs.push(InputToken::new(at..at + text.len(), style.clone()));
          }
          at += piece.len();
        }
      }
      InputTokens::new(value, runs)
        .unwrap_or_else(|_| InputTokens::new(value, Vec::new()).expect("no tokens are always valid"))
    })
  }
}

#[cfg(test)]
mod tests {
  use crate::{
    app::theme::PaletteColor,
    code::{CodeToken, token},
    components::TextInput,
    core::Signal,
    node::{Element, TextColor, node_kind::NodeKind},
  };

  /// The colors of the tokens an input draws for the value it holds now, by the text they cover.
  fn drawn(input: &Element, value: &Signal<String>) -> Vec<(String, TextColor)> {
    let NodeKind::TextInput { state, .. } = input.node.node_kind() else {
      panic!("a text input");
    };
    let value = value.get_untracked();
    state
      .tokens()
      .map(|tokens| {
        tokens
          .tokens
          .iter()
          .map(|token| (value[token.range.clone()].to_owned(), token.style.foreground.clone()))
          .collect()
      })
      .unwrap_or_default()
  }

  #[test]
  fn a_code_field_colors_its_value_after_every_edit() {
    let info = TextColor::Palette(PaletteColor::Info);
    let value = Signal::new("let x = 1;".to_owned());
    let input: Element = TextInput::new(value.clone()).multiline().code_language("rust").into();
    let first = drawn(&input, &value);
    assert!(first.contains(&("let".to_owned(), info.clone())), "{first:?}");
    // An edit, or a value the app sets: the tokens are of the new value.
    value.set("fn main() {}".to_owned());
    let next = drawn(&input, &value);
    assert!(next.contains(&("fn".to_owned(), info.clone())), "{next:?}");
    assert!(next.iter().all(|(text, _)| text != "let"));
  }

  #[test]
  fn a_code_field_draws_a_names_weight_and_leaning_in_its_own_type() {
    use crate::{
      app::theme::CodeStyle,
      layout::text_style::{FontStyle, FontWeight},
    };
    let theme = crate::app::theme::Theme::new();
    theme.set_code_style("keyword", CodeStyle::new().foreground(PaletteColor::Info).bold());
    theme.set_code_style("comment", CodeStyle::new().italic());
    let value = Signal::new("let x = 1; // one".to_owned());
    let input: Element = TextInput::new(value.clone())
      .code_tokens_in(&theme, |code: &str| super::code_tokens(code, Some("rust"), None))
      .into();
    let NodeKind::TextInput { state, .. } = input.node.node_kind() else {
      panic!("a text input");
    };
    let tokens = state.tokens().expect("tokens");
    let of = |text: &str| {
      tokens
        .tokens
        .iter()
        .find(|token| &value.get_untracked()[token.range.clone()] == text)
        .map(|token| (token.style.weight, token.style.font_style, token.style.text.is_some()))
    };
    // The keyword is bold and in the theme's keyword color, the comment leans: both in the input's own type.
    assert_eq!(of("let"), Some((Some(FontWeight::Bold), None, false)));
    assert_eq!(of("// one"), Some((None, Some(FontStyle::Italic), false)));
  }

  #[test]
  fn a_code_field_takes_a_tokenizer_of_its_own() {
    let value = Signal::new("alpha beta".to_owned());
    let input: Element = TextInput::new(value.clone())
      .code_tokenizer(|code: &str| vec![CodeToken::new(0..code.find(' ').unwrap_or(0), token::STRING)])
      .into();
    let drawn = drawn(&input, &value);
    let success = TextColor::Palette(PaletteColor::Success);
    assert_eq!(drawn, [("alpha".to_owned(), success)]);
  }
}