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
//! A fieldset: fields under a legend, which it disables together, as a box that holds its legend or, as a group
//! box, under it. And an error summary: what is wrong in a form, at its top, with a link to each field.

use std::sync::Arc;

use super::{
  Field, icons, names,
  part_style::{Length, PartStyle},
  recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe},
  shared::{fading, glyph, pressable},
  theme::{Themed, current_theme},
};
use crate::{
  app::theme::{BorderSize, IconSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
  components::{Column, Row},
  core::ElementRef,
  layout::{Alignment, layout_kind::Justify, text_style::FontWeight},
  node::{CursorIcon, Element, border::Border, dimension::Dimension},
};

/// The parts of a fieldset a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FieldsetPart {
  /// The fieldset: its edge, its corners, and the legend and the fields one under another in it.
  Root,
  /// The legend's title over its description.
  Legend,
  /// What the fields are, together: "Profile". What the fieldset is called.
  Title,
  /// More of it, under the title, in the muted color.
  Description,
  /// What it holds, one under another: in a group box, the box under the legend.
  Body,
}

names!(FieldsetPart {
  Root => "root",
  Legend => "legend",
  Title => "title",
  Description => "description",
  Body => "body",
});

/// Where a fieldset's edge is.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FieldsetVariant {
  /// Around the legend and the fields: the fieldset of a form.
  #[default]
  Boxed,
  /// Around what it holds, under a small legend: the group box of a desktop dialog, a few checkboxes or radios
  /// that go together.
  GroupBox,
}

names!(FieldsetVariant {
  Boxed => "boxed",
  GroupBox => "group_box",
});

/// The states of a fieldset a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FieldsetState {
  /// It takes no input: every control in it is disabled.
  Disabled,
  /// It is as wide as what holds it.
  FullWidth,
}

names!(FieldsetState {
  Disabled => "disabled",
  FullWidth => "full_width",
});

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

/// A fieldset: fields that go together, under a legend that says what they are. The profile of an account, the
/// address of an order, the notifications a person is sent.
///
/// ```
/// use lurq::{core::Signal, ui::{Button, Checkbox, Field, Fieldset, FieldsetVariant, Input}};
///
/// let name = Signal::new(String::new());
/// let profile = Fieldset::new("Profile")
///   .description("Shown to people you share a workspace with.")
///   .field(Field::new("Display name", Input::new(name).full_width(true)).required(true))
///   .child(Button::new("Save profile"));
/// // Every control in it takes no input, and the whole fieldset is dimmed.
/// let locked = Fieldset::new("Billing").disabled(true);
/// // A group box: a small legend over a box of options.
/// let startup = Fieldset::new("Startup")
///   .variant(FieldsetVariant::GroupBox)
///   .child(Checkbox::new("Open at login").checked(true))
///   .child(Checkbox::new("Restore windows"));
/// ```
///
/// What it holds is one under another, as wide as the fieldset: a [`Field`] given with [`Fieldset::field`] is made
/// as wide as it ([`Field::full_width`]), any other element is given with [`Fieldset::child`]. A disabled fieldset
/// takes everything in it out of input, as an element that is disabled does whatever it holds, and is dimmed.
pub struct Fieldset {
  legend: Arc<str>,
  description: Option<Arc<str>>,
  children: Vec<Element>,
  variant: Name,
  disabled: bool,
  full_width: bool,
  states: Vec<Name>,
  rules: Recipe,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl Fieldset {
  /// A fieldset whose legend is `legend`, with nothing in it yet.
  pub fn new(legend: &str) -> Self {
    Self {
      legend: Arc::from(legend),
      description: None,
      children: Vec::new(),
      variant: FieldsetVariant::default().into(),
      disabled: false,
      full_width: false,
      states: Vec::new(),
      rules: Recipe::new(),
      id: None,
      key: None,
    }
  }

  /// A [`FieldsetVariant`], or a variant the theme's recipe names: `GroupBox` is a small legend over a box of
  /// what it holds.
  pub fn variant(mut self, variant: impl Into<Name>) -> Self {
    self.variant = variant.into();
    self
  }

  /// More of what the fields are, under the legend, in the muted color.
  pub fn description(mut self, description: &str) -> Self {
    self.description = Some(Arc::from(description));
    self
  }

  /// Adds a field, as wide as the fieldset.
  pub fn field(mut self, field: Field) -> Self {
    self.children.push(field.full_width(true).into());
    self
  }

  /// Adds an element under those already there: a field, a row of buttons, a group of cards.
  pub fn child(mut self, child: impl Into<Element>) -> Self {
    self.children.push(child.into());
    self
  }

  /// Adds elements under those already there.
  pub fn children(mut self, children: impl IntoIterator<Item = impl Into<Element>>) -> Self {
    self.children.extend(children.into_iter().map(Into::into));
    self
  }

  /// Whether the fieldset takes no input: no control in it is pressed, focused or typed in, and it is dimmed.
  pub fn disabled(mut self, disabled: bool) -> Self {
    self.disabled = disabled;
    self
  }

  /// Whether the fieldset is as wide as what holds it, in place of the 400 the board gives it (250 as a group
  /// box).
  pub fn full_width(mut self, full_width: bool) -> Self {
    self.full_width = full_width;
    self
  }

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

  /// Styles one part of this fieldset 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 fieldset.
  pub fn rules(mut self, rules: &Recipe) -> Self {
    self.rules = self.rules.extend(rules);
    self
  }

  /// The id of the fieldset'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 fieldset in `theme`. Converting a fieldset into an element does this in the theme of what is
  /// being rendered.
  pub fn build(self, theme: &Theme) -> Element {
    let case = Case {
      variant: Some(self.variant),
      size: None,
      density: None,
      breakpoint: super::current_breakpoint(),
      states: self.states.clone(),
    }
    .state(FieldsetState::Disabled, self.disabled)
    .state(FieldsetState::FullWidth, self.full_width);
    let view = FieldsetView {
      legend: self.legend,
      description: self.description,
      children: self.children,
      disabled: self.disabled,
      recipe: theme
        .resolve::<Fieldset>(&case, Some(&self.rules))
        .owned_by(self.id.as_ref()),
      id: self.id,
      key: self.key,
    };
    match theme.renderer::<Fieldset>() {
      Some(renderer) => renderer(view),
      None => view.render(),
    }
  }
}

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

/// A fieldset as a renderer sees it: what it was given and the style of its parts.
pub struct FieldsetView {
  pub legend: Arc<str>,
  pub description: Option<Arc<str>>,
  /// What the app put in it.
  pub children: Vec<Element>,
  /// Whether it takes no input: the default renderer disables its root, and with it everything in it.
  pub disabled: bool,
  /// The style of each part of this fieldset.
  pub recipe: ResolvedRecipe,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl FieldsetView {
  /// The style of a part, as this fieldset is to be drawn.
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    self.recipe.part(part)
  }

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

  /// The legend: its title over its description, each as wide as the fieldset.
  pub fn legend(&self) -> Column {
    let mut legend = Column::new()
      .width(Dimension::Pct(100.0))
      .part(&self.part(FieldsetPart::Legend))
      .child(wrapping(&self.part(FieldsetPart::Title), &self.legend));
    if let Some(description) = &self.description {
      legend = legend.child(wrapping(&self.part(FieldsetPart::Description), description));
    }
    legend
  }

  /// The root with nothing in it yet: a group to what reads the tree, called by the legend and described by its
  /// description, and disabled with everything in it where the fieldset is.
  pub fn root(&self) -> Column {
    let mut root = Column::new()
      .align_items(Alignment::Stretch)
      .part(&self.part(FieldsetPart::Root))
      .describe("a11y_role", "group")
      .describe("a11y_name", self.legend.clone());
    if let Some(description) = &self.description {
      root = root.describe("a11y_description", description.clone());
    }
    if self.disabled {
      root = root.disabled(true);
    }
    if let Some(id) = &self.id {
      root = root.id(id.clone());
    }
    if let Some(key) = &self.key {
      root = root.key(key.clone());
    }
    root
  }

  /// What it holds, one under another, each as wide as the fieldset: a group box's edge is around this.
  pub fn body(&mut self) -> Column {
    Column::new()
      .align_items(Alignment::Stretch)
      // The ring of a focused control is drawn outside it.
      .overflow_visible()
      .part(&self.part(FieldsetPart::Body))
      .with_children(std::mem::take(&mut self.children))
  }

  /// How the default renderer builds a fieldset: the legend, and what it holds under it.
  pub fn render(mut self) -> Element {
    let body = self.body();
    self.root().child(self.legend()).child(body).into()
  }
}

/// A text in the style of `part`, as wide as the room it has: a longer one goes on to the next line.
fn wrapping(part: &ResolvedPart, content: &str) -> Column {
  let fill = Dimension::Pct(100.0);
  Column::new()
    .width(fill)
    .min_width(Dimension::Px(0.0))
    .part(part)
    .child(part.text(content).width(fill))
}

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

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

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

  /// The design's fieldset (`pencil/lurq-design-system.pen`, Fieldset): 400 wide, padded 20, with an edge of 1 of
  /// the border's color inside it and corners of `radius-lg`; the legend and the fields one under another, 16
  /// apart. The legend's title is 15 and semibold in the foreground, its description 4 under it in 13 of the
  /// muted color. A disabled one is dimmed. As a group box (Group Box) it is 250 wide: the legend's title 12 and
  /// semibold in the muted color, and 6 under it a box with the edge and corners of `radius-md`, padded 12, what it
  /// holds 10 apart. The board gives a group box no description: it is 12, 2 under the title.
  fn recipe() -> Recipe {
    use FieldsetPart::{Body, Description, Legend, Root, Title};
    use FieldsetVariant::{Boxed, GroupBox};
    use PaletteColor::{Foreground, MutedForeground};
    let edge = || Border::inside(BorderSize::Sm, PaletteColor::Border);
    Recipe::new()
      .base(Root, |s| s.max_width(Length::Fill).foreground(Foreground))
      .variant(Boxed, Root, |s| {
        s.width(400.0)
          .padding_x(20.0)
          .padding_y(20.0)
          .gap(16.0)
          .radius(RadiusSize::Lg)
          .border(edge())
      })
      .variant(GroupBox, Root, |s| s.width(250.0).gap(6.0))
      .base(Legend, |s| s.gap(4.0))
      .variant(GroupBox, Legend, |s| s.gap(2.0))
      .base(Title, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(15.0)
          .weight(FontWeight::SemiBold)
          .foreground(Foreground)
      })
      .variant(GroupBox, Title, |s| s.font_size(12.0).foreground(MutedForeground))
      .base(Description, |s| {
        s.typography(TypographyStyle::Description)
          .font_size(13.0)
          .foreground(MutedForeground)
      })
      .variant(GroupBox, Description, |s| s.font_size(12.0))
      .base(Body, |s| s.gap(16.0))
      .variant(GroupBox, Body, |s| {
        s.padding_x(12.0)
          .padding_y(12.0)
          .gap(10.0)
          .radius(RadiusSize::Md)
          .border(edge())
      })
      .state(FieldsetState::FullWidth, Root, |s| s.width(Length::Fill))
      .state(FieldsetState::Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
  }
}

/// What is wrong with one field of a form: a line of an [`ErrorSummary`].
///
/// ```
/// use lurq::{core::ElementRef, ui::FieldError};
///
/// let name = ElementRef::new();
/// let error = FieldError::new("Display name", "Display name is required").focuses(&name);
/// ```
#[derive(Clone)]
pub struct FieldError {
  /// What the field is called: its label.
  pub label: Arc<str>,
  /// What is wrong with what it holds, as its field says it.
  pub error: Arc<str>,
  /// What a press of the line focuses: the first element that takes the focus under the one the ref is on.
  pub focus: Option<ElementRef>,
  /// The id of the line's element.
  pub id: Option<Arc<str>>,
  /// The key of the line's element.
  pub key: Option<Arc<str>>,
}

impl FieldError {
  /// The `error` of the field called `label`: what its field says of it.
  pub fn new(label: &str, error: &str) -> Self {
    Self {
      label: Arc::from(label),
      error: Arc::from(error),
      focus: None,
      id: None,
      key: None,
    }
  }

  /// The id of the line's element.
  pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
    self.id = Some(id.into());
    self
  }

  /// The key of the line's element, for the engine to tell it from the others as errors come and go.
  pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
    self.key = Some(key.into());
    self
  }

  /// A press of the line focuses `field`: the first element that takes the focus under the one the ref is on. A
  /// [`Field`]'s control is under the field's root, so a ref on a row around the field will do.
  pub fn focuses(mut self, field: &ElementRef) -> Self {
    self.focus = Some(field.clone());
    self
  }
}

/// The parts of an error summary a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ErrorSummaryPart {
  /// The box: its fill, its edge, its corners, with the icon beside the body.
  Root,
  /// The icon at its start.
  Icon,
  /// The title over the lines.
  Text,
  /// How many fields are wrong.
  Title,
  /// One line: the bullet and the error. What is pressed, where it focuses its field.
  Item,
  /// The dot before an error.
  Bullet,
  /// What is wrong.
  Error,
}

names!(ErrorSummaryPart {
  Root => "root",
  Icon => "icon",
  Text => "text",
  Title => "title",
  Item => "item",
  Bullet => "bullet",
  Error => "error",
});

/// The states of an error summary a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ErrorSummaryState {
  /// It is as wide as what holds it.
  FullWidth,
}

names!(ErrorSummaryState {
  FullWidth => "full_width",
});

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

/// An error summary: what is wrong in a form, at its top, a line for each field with a link to it. What a person
/// reads when a form they sent was not accepted.
///
/// ```
/// use lurq::{core::ElementRef, ui::{ErrorSummary, FieldError}};
///
/// let (name, email) = (ElementRef::new(), ElementRef::new());
/// let summary = ErrorSummary::new([
///   FieldError::new("Display name", "Display name is required").focuses(&name),
///   FieldError::new("Email", "Email must include a domain").focuses(&email),
/// ]);
/// ```
///
/// Its title counts the fields ("2 fields need attention") unless it is given one. A press of a line, or Enter or
/// Space while it has the focus, focuses its field; each line that focuses a field is a stop of Tab. A summary with
/// no errors draws nothing. It is an alert to what reads the tree, called by its title; each line is a link, called
/// by the field's label and its message.
pub struct ErrorSummary {
  errors: Vec<FieldError>,
  title: Option<Arc<str>>,
  full_width: bool,
  states: Vec<Name>,
  rules: Recipe,
  forced: Option<Interaction>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl ErrorSummary {
  /// A summary of `errors`, in the order of the form.
  pub fn new(errors: impl IntoIterator<Item = FieldError>) -> Self {
    Self {
      errors: errors.into_iter().collect(),
      title: None,
      full_width: false,
      states: Vec::new(),
      rules: Recipe::new(),
      forced: None,
      id: None,
      key: None,
    }
  }

  /// A summary of what `form` says is wrong, once a submit of it was attempted: a line for each of `fields`, given
  /// as its name in the form, its label and what holds it, that has an error, in their order, then a line for each
  /// other field with an error, called by its name and focusing nothing. It reads the form's errors as signals: what
  /// renders it renders again when they change.
  ///
  /// ```
  /// use lurq::{
  ///   components::{FormHandle, FormOptions},
  ///   core::ElementRef,
  ///   ui::ErrorSummary,
  /// };
  ///
  /// let form = FormHandle::new(FormOptions::new());
  /// let (name, email) = (ElementRef::new(), ElementRef::new());
  /// let summary = ErrorSummary::form(&form, [("name", "Display name", &name), ("email", "Email", &email)]);
  /// ```
  #[cfg(feature = "form")]
  pub fn form<'a>(
    form: &crate::components::FormHandle,
    fields: impl IntoIterator<Item = (&'a str, &'a str, &'a ElementRef)>,
  ) -> Self {
    if !form.submit_attempted().get() {
      return Self::new([]);
    }
    let fields: Vec<(&str, &str, &ElementRef)> = fields.into_iter().collect();
    let mut errors: Vec<FieldError> = fields
      .iter()
      .filter_map(|(name, label, field)| {
        let message = form.error(*name).get()?;
        Some(FieldError::new(label, &message).focuses(field))
      })
      .collect();
    let all = form.errors();
    for (name, messages) in all.entries() {
      if fields.iter().any(|(listed, ..)| *listed == name) {
        continue;
      }
      if let Some(message) = messages.first() {
        errors.push(FieldError::new(name, message));
      }
    }
    Self::new(errors)
  }

  /// Adds a line after the others.
  pub fn error(mut self, error: FieldError) -> Self {
    self.errors.push(error);
    self
  }

  /// Adds lines after the others.
  pub fn errors(mut self, errors: impl IntoIterator<Item = FieldError>) -> Self {
    self.errors.extend(errors);
    self
  }

  /// The title in words of the app's own, in place of the count: in another language.
  pub fn title(mut self, title: &str) -> Self {
    self.title = Some(Arc::from(title));
    self
  }

  /// Whether the summary is as wide as what holds it, in place of the 400 the board gives it.
  pub fn full_width(mut self, full_width: bool) -> Self {
    self.full_width = full_width;
    self
  }

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

  /// Styles one part of this summary 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 summary.
  pub fn rules(mut self, rules: &Recipe) -> Self {
    self.rules = self.rules.extend(rules);
    self
  }

  /// Draws every line 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 summary'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 summary in `theme`. Converting a summary into an element does this in the theme of what is being
  /// rendered.
  pub fn build(self, theme: &Theme) -> Element {
    let case = Case {
      variant: None,
      size: None,
      density: None,
      breakpoint: super::current_breakpoint(),
      states: self.states.clone(),
    }
    .state(ErrorSummaryState::FullWidth, self.full_width);
    let title = self.title.unwrap_or_else(|| {
      let locale = super::current_locale();
      Arc::from(locale.count(&locale.fields.fields_need_attention, self.errors.len()))
    });
    let view = ErrorSummaryView {
      title,
      errors: self.errors,
      recipe: theme
        .resolve::<ErrorSummary>(&case, Some(&self.rules))
        .owned_by(self.id.as_ref()),
      forced: self.forced,
      id: self.id,
      key: self.key,
    };
    match theme.renderer::<ErrorSummary>() {
      Some(renderer) => renderer(view),
      None => view.render(),
    }
  }
}

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

/// An error summary as a renderer sees it: what it was given and the style of its parts.
pub struct ErrorSummaryView {
  /// The title: the count of the fields, or the app's own words.
  pub title: Arc<str>,
  pub errors: Vec<FieldError>,
  /// The style of each part of this summary.
  pub recipe: ResolvedRecipe,
  forced: Option<Interaction>,
  id: Option<Arc<str>>,
  key: Option<Arc<str>>,
}

impl ErrorSummaryView {
  /// The style of a part, as this summary is to be drawn.
  pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
    self.recipe.part(part).forced(self.forced)
  }

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

  /// The icon at the start, in the style of its part.
  pub fn icon(&self) -> Element {
    glyph(
      &self.part(ErrorSummaryPart::Icon),
      Name::new(icons::ALERT_CIRCLE),
      false,
    )
  }

  /// The line of one error: the bullet and the error. Where the error focuses a field, the line is pressed and
  /// focused, and is a link to what reads the tree, called by the field's label and the error.
  pub fn item(&self, error: &FieldError) -> Element {
    let line = self.line(error);
    let line = match &error.id {
      Some(id) => line.id(id.clone()),
      None => line,
    };
    match &error.key {
      Some(key) => line.key(key.clone()),
      None => line,
    }
  }

  fn line(&self, error: &FieldError) -> Element {
    // A line the app gave an id names its parts after it, one it gave a key after the summary's id and the key.
    let recipe = match (&error.id, &error.key) {
      (None, Some(key)) => self.recipe.keyed_item(key, ErrorSummaryPart::Item),
      (id, _) => self.recipe.item(id.as_ref(), ErrorSummaryPart::Item),
    };
    let part = |part: ErrorSummaryPart| recipe.part(part).forced(self.forced);
    let message = part(ErrorSummaryPart::Error);
    let (part, bullet) = (
      part(ErrorSummaryPart::Item),
      Row::new().part(&part(ErrorSummaryPart::Bullet)),
    );
    match &error.focus {
      Some(field) => pressable(&part, false)
        .justify(Justify::Start)
        .align_items(Alignment::Center)
        .describe("a11y_role", "link")
        .describe("a11y_name", format!("{}: {}", error.label, error.error))
        .focuses(field)
        .child(bullet)
        .child(wrapping(&message, &error.error).flex(1.0))
        .into(),
      // A line that goes nowhere is read: it is not underlined under the pointer, nor shows it can be pressed.
      None => {
        let fill = Dimension::Pct(100.0);
        let mut text = Column::new()
          .flex(1.0)
          .min_width(Dimension::Px(0.0))
          .child(message.text(&error.error).width(fill));
        if let Some(color) = message.rest().foreground {
          text = text.foreground(color);
        }
        Row::new()
          .align_items(Alignment::Center)
          .part(&part)
          .cursor(CursorIcon::Default)
          .child(bullet)
          .child(text)
          .into()
      }
    }
  }

  /// The title over the lines.
  pub fn text(&self) -> Column {
    let mut body = Column::new()
      .flex(1.0)
      .min_width(Dimension::Px(0.0))
      .align_items(Alignment::Stretch)
      // The ring of the focus of a line reaches past it.
      .overflow_visible()
      .part(&self.part(ErrorSummaryPart::Text))
      .child(wrapping(&self.part(ErrorSummaryPart::Title), &self.title));
    for error in &self.errors {
      body = body.child(self.item(error));
    }
    body
  }

  /// How the default renderer builds a summary: the icon beside the body, in the box; nothing where there are no
  /// errors.
  pub fn render(self) -> Element {
    if self.errors.is_empty() {
      return Row::new().into();
    }
    let mut root = Row::new()
      .align_items(Alignment::Start)
      .overflow_visible()
      .part(&self.part(ErrorSummaryPart::Root))
      .describe("a11y_role", "alert")
      .describe("a11y_name", self.title.clone())
      .child(self.icon())
      .child(self.text());
    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 ErrorSummary {
  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 ErrorSummary {
  const NAME: &'static str = "error_summary";
  type Renderer = ErrorSummaryRenderer;

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

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

  /// The design's error summary (`pencil/lurq-design-system.pen`, Error Summary): 400 wide, padded 12 and 14, in
  /// the muted destructive color with an edge of 1 of the destructive color inside it and corners of `radius-md`.
  /// The alert icon of 16 in the destructive color is 10 before the body: the title in 13 and semibold in the
  /// destructive color, and under it, 6 apart, a line for each error, a dot of 4 in the destructive color 8 before
  /// its message in 13 of the foreground. A line is underlined under the pointer.
  fn recipe() -> Recipe {
    use ErrorSummaryPart::{Bullet, Error, Icon, Item, Root, Text, Title};
    use PaletteColor::{Destructive, DestructiveMuted, Foreground};
    Recipe::new()
      .base(Root, |s| {
        s.width(400.0)
          .max_width(Length::Fill)
          .padding_x(14.0)
          .padding_y(12.0)
          .gap(10.0)
          .background(DestructiveMuted)
          .border(Border::inside(BorderSize::Sm, Destructive))
          .radius(RadiusSize::Md)
          .foreground(Foreground)
      })
      .base(Icon, |s| s.square(IconSize::Md).foreground(Destructive))
      .base(Text, |s| s.gap(6.0))
      .base(Title, |s| {
        s.typography(TypographyStyle::Label)
          .weight(FontWeight::SemiBold)
          .foreground(Destructive)
      })
      .base(Item, |s| {
        fading(s.gap(8.0).radius(RadiusSize::Sm).cursor(CursorIcon::Pointer))
      })
      .base(Bullet, |s| {
        s.square(4.0).radius(RadiusSize::Full).background(Destructive)
      })
      .base(Error, |s| {
        s.typography(TypographyStyle::Body)
          .font_size(13.0)
          .foreground(Foreground)
      })
      .hovered(Error, |s| s.underline(Border::inside(1.0, Foreground)))
      .focus_visible(Item, |s| s.focus_ring())
      .state(ErrorSummaryState::FullWidth, Root, |s| s.width(Length::Fill))
  }
}