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
//! A tag: a label on a thing, which the app may let a person take away.

use std::sync::Arc;

use super::{
  icon::Icon,
  icons, names,
  part_style::Length,
  recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
  shared::{fading, glyph, pressable, tab_stop},
  theme::{Themed, current_theme},
};
use crate::{
  app::{
    events::{KeyboardEvent, MouseEvent},
    theme::{BorderSize, IconSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
  },
  components::{self, Row},
  layout::{Alignment, text_style::FontWeight},
  node::{CursorIcon, Element, border::Border as Line, outline::Outline},
};

/// The parts of a tag a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TagPart {
  /// The tag: its fill, its line, its corners and the color of its text.
  Root,
  /// The icon before its text.
  Icon,
  /// The term before its text, where it has one: the `branch:` of `branch: main`.
  Term,
  /// Its text.
  Label,
  /// What takes it away, at its end: a button of its own.
  Remove,
  /// The cross on that button: its side.
  RemoveIcon,
}

names!(TagPart {
  Root => "root",
  Icon => "icon",
  Term => "term",
  Label => "label",
  Remove => "remove",
  RemoveIcon => "remove_icon",
});

/// The states of a tag a recipe styles, besides what the pointer and the
/// keyboard do to its remove button.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TagState {
  /// It has the button that takes it away: the part `Remove`.
  Remove,
  /// It is round at both ends, with no line around it: a pill.
  Pill,
  /// It takes no input: its remove button is not pressed.
  Disabled,
}

names!(TagState {
  Remove => "remove",
  Pill => "pill",
  Disabled => "disabled",
});

/// What a tag says by its color.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TagVariant {
  /// A keyword, a category: the fill of what is secondary, with a line around it.
  #[default]
  Neutral,
  /// A state that is told: in review, a filter that is set.
  Info,
  /// Something that went well: passing, done.
  Success,
  /// Something to look at: pending, due soon.
  Warning,
  /// Something wrong: failing, blocked.
  Destructive,
}

names!(TagVariant {
  Neutral => "neutral",
  Info => "info",
  Success => "success",
  Warning => "warning",
  Destructive => "destructive",
});

/// How tall a tag is: 20, 24 or 28.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TagSize {
  Sm,
  #[default]
  Md,
  Lg,
}

names!(TagSize {
  Sm => "sm",
  Md => "md",
  Lg => "lg",
});

/// What took a tag away, given to [`Tag::on_remove_event`]: the press of its remove button (a click, or Enter or
/// Space on it), or Backspace or Delete while it has the focus. A handler that stops it keeps it from what is around
/// the tag: a row a press opens hears nothing of it.
#[derive(Clone)]
pub enum TagRemoveEvent {
  /// A press of the remove button.
  Press(MouseEvent),
  /// Backspace or Delete on the focused remove button.
  Key(KeyboardEvent),
}

impl TagRemoveEvent {
  /// Keeps the press or the key from reaching what is around the tag.
  pub fn stop_propagation(&self) {
    match self {
      Self::Press(event) => event.stop_propagation(),
      Self::Key(event) => event.stop_propagation(),
    }
  }

  /// Says that the press or the key was used: what else hears it may leave it alone.
  pub fn prevent_default(&self) {
    match self {
      Self::Press(event) => event.prevent_default(),
      Self::Key(event) => event.prevent_default(),
    }
  }
}

/// What is told a tag was taken away.
type Removed = Arc<dyn Fn(&TagRemoveEvent) + Send + Sync>;

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

/// The shape of a tag.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TagShape {
  /// The theme's small radius, with a line inside it.
  #[default]
  Default,
  /// Round at both ends, with no line around it: a pill.
  Pill,
}

/// A tag: a label on a thing, a keyword or a category, which the app may let
/// a person take away.
///
/// ```
/// use lurq::ui::{Tag, TagShape, TagVariant, icons};
///
/// let plain = Tag::new("design");
/// let status = Tag::new("in review").variant(TagVariant::Info).icon(icons::CIRCLE);
/// // With a button at its end that takes it away: called "Remove rust" to what reads the tree.
/// let removable = Tag::new("rust").on_remove(|| println!("rust taken away"));
/// // Round, with no line around it: a token in a box that is typed in.
/// let token = Tag::new("React").shape(TagShape::Pill).on_remove(|| println!("React taken away"));
/// // A term and its value: the term before the label, quieter, as the app writes it.
/// let branch = Tag::new("main").term("branch:").icon(icons::BRANCH);
/// ```
///
/// A tag does not keep whether it is there: the app holds its list, and is
/// told by [`Tag::on_remove`] which one a person took away. A tag with a
/// handler has its remove button, which Tab reaches; a click, Enter, Space,
/// Backspace and Delete press it.
pub struct Tag {
  label: Arc<str>,
  term: Option<Arc<str>>,
  variant: Name,
  size: Name,
  icon: Option<Name>,
  shape: TagShape,
  disabled: bool,
  tab_stop: bool,
  remove_label: Option<Arc<str>>,
  states: Vec<Name>,
  rules: Recipe,
  forced: Option<Interaction>,
  on_remove: Vec<Removed>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl Tag {
  pub fn new(label: &str) -> Self {
    Self {
      label: Arc::from(label),
      term: None,
      variant: TagVariant::default().into(),
      size: TagSize::default().into(),
      icon: None,
      shape: TagShape::Default,
      disabled: false,
      tab_stop: true,
      remove_label: None,
      states: Vec::new(),
      rules: Recipe::new(),
      forced: None,
      on_remove: Vec::new(),
      id: None,
      key: None,
    }
  }

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

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

  /// An icon before the text, by its name in the theme's set.
  pub fn icon(mut self, name: impl Into<Name>) -> Self {
    self.icon = Some(name.into());
    self
  }

  /// A term before the text, in the part `Term`: what the text is the value of, `branch:` before `main`. It is shown
  /// as it is written, its punctuation too, and quieter than the text. The tag is still called by its text, and its
  /// remove button "Remove main".
  pub fn term(mut self, term: &str) -> Self {
    self.term = Some(Arc::from(term));
    self
  }

  /// The shape of the tag: [`TagShape::Pill`] is round at both ends, with no line around it.
  pub fn shape(mut self, shape: TagShape) -> Self {
    self.shape = shape;
    self
  }

  /// What the remove button is called to what reads the tree: "Remove" and
  /// the tag's text by default ("Remove rust").
  pub fn remove_label(mut self, label: &str) -> Self {
    self.remove_label = Some(Arc::from(label));
    self
  }

  /// Whether the tag takes no input: it is dimmed, and its remove button is not pressed.
  pub fn disabled(mut self, disabled: bool) -> Self {
    self.disabled = disabled;
    self
  }

  /// Whether Tab stops at the remove button; it does unless told otherwise. `false` is a tag whose remove button
  /// the pointer reaches, and the app's own keys: Tab passes it by, and a press of it takes the tag away and leaves
  /// the focus where it was. Focused by the app, a click, Enter, Space, Backspace and Delete press it as before,
  /// and it is the same button to what reads the tree.
  pub fn tab_stop(mut self, stops: bool) -> Self {
    self.tab_stop = stops;
    self
  }

  /// Puts the tag 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
  }

  /// Called on a press of its remove button, which a tag with a handler has.
  pub fn on_remove(mut self, handler: impl Fn() + Send + Sync + 'static) -> Self {
    self.on_remove.push(Arc::new(move |_: &TagRemoveEvent| handler()));
    self
  }

  /// [`on_remove`](Self::on_remove) given what took the tag away: the press of its remove button or the key on it.
  /// A tag in a row a press opens stops it (`event.stop_propagation()`), so the row hears nothing. A second handler
  /// is called after the first, and with `on_remove`'s in the order they were given.
  pub fn on_remove_event(mut self, handler: impl Fn(&TagRemoveEvent) + Send + Sync + 'static) -> Self {
    self.on_remove.push(Arc::new(handler));
    self
  }

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

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

  /// Draws the remove button as it is under the pointer, held down or
  /// focused from the keyboard, with nothing pointing at it: for a gallery or
  /// a test.
  pub fn force(mut self, interaction: Option<Interaction>) -> Self {
    self.forced = interaction;
    self
  }

  /// The id of the tag's root.
  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 tag in `theme`. Converting a tag into an element does this
  /// in the theme of what is being rendered.
  pub fn build(self, theme: &Theme) -> Element {
    let removable = !self.on_remove.is_empty();
    let case = Case {
      variant: Some(self.variant),
      size: Some(self.size),
      density: None,
      breakpoint: super::current_breakpoint(),
      states: self.states.clone(),
    }
    .state(TagState::Remove, removable)
    .state(TagState::Pill, self.shape == TagShape::Pill)
    .state(TagState::Disabled, self.disabled);
    let remove_label = self.remove_label.unwrap_or_else(|| {
      Arc::from(crate::core::fill(
        &super::current_locale().common.remove_named,
        &[("name", &self.label)],
      ))
    });
    let view = TagView {
      label: self.label,
      term: self.term,
      remove_label,
      variant: self.variant,
      size: self.size,
      icon: self.icon,
      shape: self.shape,
      removable,
      disabled: self.disabled,
      tab_stop: self.tab_stop,
      recipe: theme
        .resolve::<Tag>(&case, Some(&self.rules))
        .owned_by(self.id.as_ref()),
      forced: self.forced,
      on_remove: self.on_remove,
      id: self.id,
      key: self.key,
    };
    match theme.renderer::<Tag>() {
      Some(renderer) => renderer(view),
      None => view.render(),
    }
  }
}

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

/// A tag as a renderer sees it: what it was given, the style of its parts,
/// and its remove button ready to be dressed.
pub struct TagView {
  pub label: Arc<str>,
  /// The term before the text, if it has one.
  pub term: Option<Arc<str>>,
  /// What the remove button is called to what reads the tree.
  pub remove_label: Arc<str>,
  pub variant: Name,
  pub size: Name,
  /// The name of its icon, if it has one.
  pub icon: Option<Name>,
  pub shape: TagShape,
  /// Whether it has a handler for its remove button, and so the button.
  pub removable: bool,
  pub disabled: bool,
  /// Whether Tab stops at the remove button.
  tab_stop: bool,
  /// The style of each part of this tag.
  pub recipe: ResolvedRecipe,
  forced: Option<Interaction>,
  on_remove: Vec<Removed>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl TagView {
  /// The style of a part, as this tag is to be drawn. The remove button is
  /// the part the pointer and the keyboard act on, and what `force` draws.
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    let part = part.into();
    let resolved = self.recipe.part(part);
    if part == Name::from(TagPart::Remove) {
      resolved.forced(self.forced)
    } else {
      resolved
    }
  }

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

  /// What takes the tag away: a button with the cross in it, which a press,
  /// Enter, Space, Backspace and Delete press while the focus is on it. It is
  /// called by `remove_label` to what reads the tree. `None` for a
  /// tag with no handler.
  pub fn remove(&self) -> Option<components::Button> {
    if !self.removable {
      return None;
    }
    let part = self.part(TagPart::Remove);
    let side = self
      .part(TagPart::RemoveIcon)
      .rest()
      .width
      .unwrap_or(Length::Icon(IconSize::Sm));
    let mut remove = tab_stop(pressable(&part, self.disabled), self.tab_stop)
      .describe("a11y_role", "button")
      .describe("a11y_name", self.remove_label.clone())
      .child(Icon::new(icons::CLOSE).size(side).build(self.theme()));
    if let Some(id) = &self.id {
      remove = remove.id(format!("{id}-remove"));
    }
    if !self.disabled {
      let (clicked, keyed) = (self.on_remove.clone(), self.on_remove.clone());
      remove = remove
        .on_click(move |event: MouseEvent| {
          let event = TagRemoveEvent::Press(event);
          for handler in &clicked {
            handler(&event);
          }
        })
        // A key reaches every node: these two take the tag away only while the focus is on its button.
        .on_key_down(move |event: KeyboardEvent| {
          let plain = !event.ctrl && !event.alt && !event.meta && !event.shift;
          if event.focus_within && plain && matches!(event.key.as_str(), "Backspace" | "Delete") {
            event.prevent_default();
            let event = TagRemoveEvent::Key(event);
            for handler in &keyed {
              handler(&event);
            }
          }
        });
    }
    Some(remove)
  }

  /// How the default renderer builds a tag: its icon, its text and its remove button, side by side.
  pub fn render(self) -> Element {
    // The ring of the remove button's focus is drawn outside it, at the tag's own edge.
    let mut root = Row::new()
      .align_items(Alignment::Center)
      .overflow_visible()
      .part(&self.part(TagPart::Root));
    if let Some(name) = self.icon {
      root = root.child(glyph(&self.part(TagPart::Icon), name, false));
    }
    if let Some(term) = &self.term {
      let part = self.part(TagPart::Term);
      root = root.child(part.text(term).nowrap().part(&part));
    }
    let label = self.part(TagPart::Label);
    root = root.child(label.text(&self.label).nowrap().part(&label));
    if let Some(remove) = self.remove() {
      root = root.child(remove);
    }
    if let Some(id) = &self.id {
      root = root.id(id.clone());
    }
    if let Some(key) = &self.key {
      root = root.key(key.clone());
    }
    root.into()
  }
}

impl crate::ui::Styled for Tag {
  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 Tag {
  const NAME: &'static str = "tag";
  type Renderer = TagRenderer;

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

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

  /// The design's tag (`pencil/lurq-design-system.pen`, Tag): 24 high, padded
  /// 8, the corners of a small radius, in the fill of what is secondary with a
  /// line of the border's color inside it, its text 12 and medium. A
  /// removable one is padded 4 at its end, where a button of 16 with a cross
  /// of 11 in the muted color takes it away. An informative one is in the
  /// muted fill of its color and its text in the color. The design's pill
  /// (Pill) is a tag round at both ends with no line, padded 10 and 6, and a
  /// cross of 12. A term before the text is in its type at regular weight, in the muted foreground on a neutral
  /// tag and in the variant's color on another.
  fn recipe() -> Recipe {
    use PaletteColor::*;
    use TagPart::{Icon, Label, Remove, RemoveIcon, Root, Term};
    let line = |color: PaletteColor| Line::inside(BorderSize::Sm, color);
    let colored = |recipe: Recipe, variant: TagVariant, fill: PaletteColor, ink: PaletteColor| {
      let term = ink.clone();
      recipe
        .variant(variant, Root, move |s| {
          s.background(fill.clone()).border(line(fill)).foreground(ink)
        })
        .variant(variant, Term, |s| s.foreground(term))
    };
    let recipe = Recipe::new()
      .base(Root, |s| {
        s.height(24.0)
          .padding_x(8.0)
          .gap(4.0)
          .radius(RadiusSize::Sm)
          .background(Secondary)
          .border(line(Border))
          .foreground(SecondaryForeground)
      })
      .base(Label, |s| {
        s.typography(TypographyStyle::Caption).weight(FontWeight::Medium)
      })
      .base(Term, |s| {
        s.typography(TypographyStyle::Caption)
          .weight(FontWeight::Normal)
          .foreground(MutedForeground)
      })
      .base(Icon, |s| s.square(IconSize::Xs))
      .base(Remove, |s| {
        fading(
          s.square(16.0)
            .radius(3.0)
            .foreground(MutedForeground)
            .cursor(CursorIcon::Pointer),
        )
      })
      .base(RemoveIcon, |s| s.square(11.0))
      .hovered(Remove, |s| s.background(StateHover).foreground(Foreground))
      .pressed(Remove, |s| s.background(StatePressed).foreground(Foreground))
      // The ring hugs the button: one at the theme's offset would cover the end of the label, 4 before it.
      .focus_visible(Remove, |s| s.outline(Outline::new(BorderSize::Md, Ring)))
      .size(TagSize::Sm, Root, |s| s.height(20.0).padding_x(6.0))
      .size(TagSize::Sm, Label, |s| s.font_size(11.0))
      .size(TagSize::Sm, Term, |s| s.font_size(11.0))
      .size(TagSize::Sm, Remove, |s| s.square(14.0))
      .size(TagSize::Sm, RemoveIcon, |s| s.square(10.0))
      .size(TagSize::Lg, Root, |s| s.height(28.0).padding_x(10.0).gap(6.0))
      .size(TagSize::Lg, Label, |s| s.font_size(13.0))
      .size(TagSize::Lg, Term, |s| s.font_size(13.0))
      .size(TagSize::Lg, Icon, |s| s.square(IconSize::Sm))
      .size(TagSize::Lg, Remove, |s| s.square(18.0))
      .size(TagSize::Lg, RemoveIcon, |s| s.square(12.0))
      .state(TagState::Remove, Root, |s| s.padding_end(4.0))
      .when(
        Selector::new().size(TagSize::Sm).state(TagState::Remove),
        Root,
        |s| s.padding_end(3.0),
      )
      .when(
        Selector::new().size(TagSize::Lg).state(TagState::Remove),
        Root,
        |s| s.padding_end(5.0),
      )
      .state(TagState::Pill, Root, |s| {
        s.radius(RadiusSize::Full)
          .border(Line::inside(0.0, Border))
          .padding_x(10.0)
      })
      .state(TagState::Pill, Remove, |s| s.square(12.0).radius(RadiusSize::Full))
      .state(TagState::Pill, RemoveIcon, |s| s.square(12.0))
      .when(Selector::new().size(TagSize::Sm).state(TagState::Pill), Root, |s| {
        s.padding_x(8.0)
      })
      .when(Selector::new().size(TagSize::Lg).state(TagState::Pill), Root, |s| {
        s.padding_x(12.0)
      })
      .when(
        Selector::new().state(TagState::Pill).state(TagState::Remove),
        Root,
        |s| s.padding_end(6.0),
      )
      .when(
        Selector::new()
          .size(TagSize::Sm)
          .state(TagState::Pill)
          .state(TagState::Remove),
        Root,
        |s| s.padding_end(4.0),
      )
      .when(
        Selector::new()
          .size(TagSize::Lg)
          .state(TagState::Pill)
          .state(TagState::Remove),
        Root,
        |s| s.padding_end(8.0),
      )
      .state(TagState::Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
      .state(TagState::Disabled, Remove, |s| s.cursor(CursorIcon::Default));
    let recipe = colored(recipe, TagVariant::Info, InfoMuted, Info);
    let recipe = colored(recipe, TagVariant::Success, SuccessMuted, Success);
    let recipe = colored(recipe, TagVariant::Warning, WarningMuted, Warning);
    colored(recipe, TagVariant::Destructive, DestructiveMuted, Destructive)
  }
}