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
//! What a theme holds for components: their recipes and their renderers, and
//! the theme a component being built is in.

use std::{
  any::Any,
  cell::RefCell,
  collections::HashMap,
  sync::{Arc, Mutex, OnceLock},
};

use super::{
  icon::SpinMotion,
  part_style::PartStyle,
  recipe::{Case, Name, Recipe, ResolvedRecipe, ResolvedStyles, resolve_styles},
  skeleton::SkeletonMotion,
};
use crate::{
  app::theme::{Breakpoint, Theme},
  core::Memo,
};

/// A component the theme styles: it has a name the theme keeps its recipe
/// under, the recipe of the design, and a type of renderer.
///
/// lurq's own components implement it, and so does a component of an app
/// that wants to be themed the same way.
pub trait Themed: 'static {
  /// The name the theme keeps the component's recipe and renderer under.
  const NAME: &'static str;

  /// The part the component puts its id on, which [`ResolvedRecipe::owned_by`] names no other node after: `"root"`
  /// unless the component says another (an overlay's `"panel"`).
  const ROOT: &'static str = super::recipe::ROOT;

  /// What builds the component's nodes: usually
  /// `Arc<dyn Fn(&View) -> Element + Send + Sync>` for the component's view.
  type Renderer: Clone + Send + Sync + 'static;

  /// The recipe of the design: what the component looks like in the neutral
  /// theme, and in any theme that says nothing about it.
  fn recipe() -> Recipe;

  /// The parts a recipe styles, in the order the component declares them: its `*Part` enum
  /// (`SwitchPart::declared()`). What a gallery lists and a tool offers to restyle. A component that declares none
  /// has the parts its design's recipe styles, as names.
  fn parts() -> Declared {
    Declared::parts_of(&Self::recipe())
  }

  /// The states a recipe styles the component in besides what the pointer and the keyboard do to it, in the order
  /// the component declares them: its `*State` enum. A component that declares none has the states its design's
  /// recipe names.
  fn states() -> Declared {
    Declared::states_of(&Self::recipe())
  }
}

/// A builder of a themed component, styled as one instance: every component of `lurq::ui` is one, so that what
/// styles any of them (a gallery's playground, an app's helper over several kinds) is written once. Its rules are
/// laid over the recipe of [`Component`](Styled::Component): the builder's own, or that of the component it
/// dresses (a data grid's table, a tour's teaching tip).
///
/// ```
/// use lurq::{
///   app::theme::RadiusSize,
///   ui::{Button, ButtonPart, Name, Styled},
/// };
///
/// // A pill, whatever the component is.
/// fn pill<C: Styled>(component: C, part: impl Into<Name>) -> C {
///   Styled::style(component, part, |s| s.radius(RadiusSize::Full))
/// }
/// let save = pill(Button::new("Save"), ButtonPart::Root);
/// ```
pub trait Styled: Sized {
  /// The component whose recipe the rules are laid over, whose parts and states they name.
  type Component: Themed;

  /// Lays rules over the theme's recipe for this instance, the later over the earlier: what its own `rules` does.
  fn rules(self, rules: &Recipe) -> Self;

  /// Puts this instance in a state its recipe names, or takes it out: what its own `state` does.
  fn state(self, state: impl Into<Name>, on: bool) -> Self;

  /// Styles one part of this instance over what the theme gives it: one rule that always holds, laid as
  /// [`rules`](Styled::rules).
  fn style(self, part: impl Into<Name>, style: impl FnOnce(PartStyle) -> PartStyle) -> Self {
    Styled::rules(self, &Recipe::new().base(part, style))
  }

  /// Says `value` of this instance's root under `name` to tooling that reads the tree, the DevTools and the MCP
  /// tree, as an element's own `describe` does: the state an app's tests and an agent read of it.
  ///
  /// ```
  /// use lurq::ui::{Callout, Progress, Styled};
  ///
  /// let quota = Callout::new("The plan's quota is near.").describe("tone", "warning");
  /// let upload = Progress::new(0.4).describe("share", "40");
  /// ```
  ///
  /// It is said on the node of the root part, laid as [`rules`](Styled::rules) ([`Recipe::describe`]), before what
  /// the component says of itself there: a reader that takes the first value of a name takes the app's. It says
  /// nothing unless a tooling feature (`devtools`, `mcp`) is on.
  fn describe(self, name: impl Into<Arc<str>>, value: impl Into<Arc<str>>) -> Self {
    Styled::rules(self, &Recipe::new().describe(name, value))
  }
}

/// What a component declares of itself for a recipe, its parts or its states: each as a name, and as the Rust
/// that writes it. [`Themed::parts`] and [`Themed::states`] give it.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Declared {
  /// The enum that writes them, `SwitchPart`; `None` where they are names only, written as strings.
  pub enum_name: Option<&'static str>,
  /// Each name, with the variant of the enum that writes it: `("thumb", "Thumb")`. A name only has itself.
  pub names: Vec<(Name, &'static str)>,
}

impl Declared {
  /// Names an enum writes: what `names!` gives each enum of parts and states.
  pub fn of(enum_name: &'static str, names: Vec<(Name, &'static str)>) -> Self {
    Self {
      enum_name: Some(enum_name),
      names,
    }
  }

  /// Names alone, written as strings, each once.
  pub fn names_only(names: impl IntoIterator<Item = Name>) -> Self {
    let mut declared = Self::default();
    for name in names {
      if !declared.contains(name) {
        declared.names.push((name, name.as_str()));
      }
    }
    declared
  }

  /// The parts `recipe` styles, in the order it first styles them.
  pub fn parts_of(recipe: &Recipe) -> Self {
    Self::names_only(recipe.rules().iter().map(|rule| rule.part))
  }

  /// The states `recipe` names in its rules, in the order it first names them.
  pub fn states_of(recipe: &Recipe) -> Self {
    Self::names_only(
      recipe
        .rules()
        .iter()
        .flat_map(|rule| rule.selector.held_states().iter().copied()),
    )
  }

  pub fn is_empty(&self) -> bool {
    self.names.is_empty()
  }

  pub fn len(&self) -> usize {
    self.names.len()
  }

  /// Whether `name` is one of them.
  pub fn contains(&self, name: Name) -> bool {
    self.names.iter().any(|(held, _)| *held == name)
  }

  /// The names, in order.
  pub fn names(&self) -> impl Iterator<Item = Name> + '_ {
    self.names.iter().map(|(name, _)| *name)
  }

  /// The Rust that writes `name`: `SwitchPart::Thumb`, or `"thumb"` where there is no enum or the enum does not
  /// have it (a name an app adds).
  pub fn code(&self, name: Name) -> String {
    match (self.enum_name, self.names.iter().find(|(held, _)| *held == name)) {
      (Some(enum_name), Some((_, variant))) => format!("{enum_name}::{variant}"),
      _ => format!("{:?}", name.as_str()),
    }
  }
}

type Stored = Arc<dyn Any + Send + Sync>;

/// What a theme says about components: a part of a
/// [`ThemeDefinition`](crate::app::theme::ThemeDefinition).
///
/// For each component, by its name: a recipe in place of the design's, rules
/// laid over its recipe, a renderer in place of the default one, and
/// renderers for single parts. A definition that says nothing leaves every
/// component as the design has it; a [bare](Self::bare) one leaves every
/// component with no style at all.
#[derive(Clone, Default)]
pub struct UiDefinition {
  recipes: HashMap<Name, Arc<Recipe>>,
  patches: HashMap<Name, Arc<Recipe>>,
  renderers: HashMap<Name, Stored>,
  part_renderers: HashMap<(Name, Name), Stored>,
  bare: bool,
  spin_motion: SpinMotion,
  skeleton_motion: SkeletonMotion,
}

impl UiDefinition {
  pub fn new() -> Self {
    Self::default()
  }

  /// A definition in which no component has a recipe: every part comes out
  /// unstyled, and every component still works. The base for a look that
  /// shares nothing with the design's.
  pub fn bare() -> Self {
    Self {
      bare: true,
      ..Self::default()
    }
  }

  /// The definition of [`ThemeDefinition::legacy`](crate::app::theme::ThemeDefinition::legacy): the design's
  /// recipes for the components of `lurq::ui`, and the colors of 0.62 for the engine's own styled pieces, which
  /// take theirs from a recipe too: the phone's sheet, the host around it, its tab bar, navigation bar, pull to
  /// refresh and swipe row, the stack's shades, and the engine's select, checkbox and slider.
  pub fn legacy() -> Self {
    let mut ui = Self::new();
    ui.set_recipe::<crate::node::SelectStyle>(crate::node::SelectStyle::legacy_recipe());
    #[allow(deprecated)]
    ui.set_recipe::<crate::components::Checkbox>(crate::components::Checkbox::legacy_recipe());
    ui.set_recipe::<crate::components::Slider>(crate::components::Slider::legacy_recipe());
    ui.set_recipe::<crate::components::SheetStyle>(crate::components::SheetStyle::legacy_recipe());
    ui.set_recipe::<crate::components::SheetHostStyle>(crate::components::SheetHostStyle::legacy_recipe());
    ui.set_recipe::<crate::components::PullToRefreshStyle>(crate::components::PullToRefreshStyle::legacy_recipe());
    ui.set_recipe::<crate::components::SwipeRowStyle>(crate::components::SwipeRowStyle::legacy_recipe());
    #[cfg(feature = "router")]
    {
      ui.set_recipe::<crate::components::TabBar>(crate::components::TabBar::legacy_recipe());
      ui.set_recipe::<crate::components::NavigationBar>(crate::components::NavigationBar::legacy_recipe());
      ui.set_recipe::<crate::components::StackMotion>(crate::components::StackMotion::legacy_recipe());
    }
    ui
  }

  /// Whether components fall back to the design's recipe where the
  /// definition has none for them.
  pub fn is_bare(&self) -> bool {
    self.bare
  }

  /// How every spinning icon and spinner moves that is not told its own motion: see [`SpinMotion`].
  pub fn set_spin_motion(&mut self, motion: SpinMotion) -> &mut Self {
    self.spin_motion = motion;
    self
  }

  /// How every spinning icon and spinner moves that is not told its own motion: smooth unless set.
  pub fn spin_motion(&self) -> SpinMotion {
    self.spin_motion
  }

  /// How every skeleton moves that is not told its own motion: see [`SkeletonMotion`].
  pub fn set_skeleton_motion(&mut self, motion: SkeletonMotion) -> &mut Self {
    self.skeleton_motion = motion;
    self
  }

  /// How every skeleton moves that is not told its own motion: smooth unless set.
  pub fn skeleton_motion(&self) -> SkeletonMotion {
    self.skeleton_motion
  }

  /// Gives a component a recipe in place of the design's, all of it: nothing
  /// of the design's recipe is left under it.
  pub fn set_recipe<C: Themed>(&mut self, recipe: Recipe) -> &mut Self {
    self.recipes.insert(Name::new(C::NAME), Arc::new(recipe));
    self
  }

  /// Lays rules over a component's recipe: `patch` is given the rules laid
  /// over it so far, and what it adds wins where both style a case.
  ///
  /// ```
  /// use lurq::{
  ///   app::theme::{RadiusSize, ThemeDefinition},
  ///   ui::{Button, ButtonPart, ButtonVariant},
  /// };
  ///
  /// let mut theme = ThemeDefinition::neutral();
  /// // Every primary button is a pill.
  /// theme.ui.patch::<Button>(|rules| rules.variant(ButtonVariant::Primary, ButtonPart::Root, |s| s.radius(RadiusSize::Full)));
  /// ```
  pub fn patch<C: Themed>(&mut self, patch: impl FnOnce(Recipe) -> Recipe) -> &mut Self {
    let name = Name::new(C::NAME);
    let so_far = self
      .patches
      .get(&name)
      .map(|rules| (**rules).clone())
      .unwrap_or_default();
    self.patches.insert(name, Arc::new(patch(so_far)));
    self
  }

  /// Takes back what the definition said of a component's recipe: it is the
  /// design's again, or none in a bare definition.
  pub fn reset_recipe<C: Themed>(&mut self) -> &mut Self {
    let name = Name::new(C::NAME);
    self.recipes.remove(&name);
    self.patches.remove(&name);
    self
  }

  /// Gives a component a renderer in place of its default one: other nodes
  /// for the same behavior.
  pub fn set_renderer<C: Themed>(&mut self, renderer: C::Renderer) -> &mut Self {
    self.renderers.insert(Name::new(C::NAME), Arc::new(renderer));
    self
  }

  pub fn reset_renderer<C: Themed>(&mut self) -> &mut Self {
    self.renderers.remove(&Name::new(C::NAME));
    self
  }

  /// The renderer the definition gives a component, if it gives one.
  pub fn renderer<C: Themed>(&self) -> Option<C::Renderer> {
    self
      .renderers
      .get(&Name::new(C::NAME))
      .and_then(|stored| stored.downcast_ref::<C::Renderer>())
      .cloned()
  }

  /// Gives one part of a component a renderer of its own: the rest of the
  /// component is still built by the renderer it had. `R` is the type the
  /// component documents for the part.
  pub fn set_part_renderer<C: Themed, R: Clone + Send + Sync + 'static>(
    &mut self,
    part: impl Into<Name>,
    renderer: R,
  ) -> &mut Self {
    self
      .part_renderers
      .insert((Name::new(C::NAME), part.into()), Arc::new(renderer));
    self
  }

  /// Takes back the renderer the definition gave one part of a component.
  pub fn reset_part_renderer<C: Themed>(&mut self, part: impl Into<Name>) -> &mut Self {
    self.part_renderers.remove(&(Name::new(C::NAME), part.into()));
    self
  }

  /// The renderer the definition gives one part of a component.
  pub fn part_renderer<C: Themed, R: Clone + Send + Sync + 'static>(&self, part: impl Into<Name>) -> Option<R> {
    self
      .part_renderers
      .get(&(Name::new(C::NAME), part.into()))
      .and_then(|stored| stored.downcast_ref::<R>())
      .cloned()
  }

  /// The recipes a component is styled by, the later over the earlier: its
  /// own or the definition's in its place, then the rules laid over it.
  fn layers<C: Themed>(&self) -> Vec<Arc<Recipe>> {
    let name = Name::new(C::NAME);
    let mut layers = Vec::with_capacity(2);
    match self.recipes.get(&name) {
      Some(recipe) => layers.push(recipe.clone()),
      None if !self.bare => layers.push(design_recipe::<C>()),
      None => {}
    }
    if let Some(patch) = self.patches.get(&name) {
      layers.push(patch.clone());
    }
    layers
  }
}

/// The design's recipe of a component, built once.
fn design_recipe<C: Themed>() -> Arc<Recipe> {
  static RECIPES: OnceLock<Mutex<HashMap<Name, Arc<Recipe>>>> = OnceLock::new();
  let mut recipes = RECIPES.get_or_init(Default::default).lock().unwrap();
  recipes
    .entry(Name::new(C::NAME))
    .or_insert_with(|| Arc::new(C::recipe()))
    .clone()
}

/// The styles a theme has resolved, for the version of the theme they were
/// resolved at.
#[derive(Default)]
pub(crate) struct ResolvedCache {
  version: u64,
  styles: HashMap<(Name, Case), Arc<ResolvedStyles>>,
}

impl Theme {
  /// The style of every part of a component in `case`. `instance` is what one
  /// instance lays over the theme's recipe: the rules of the place it is in,
  /// and its own.
  ///
  /// What the theme resolves it keeps until the theme changes, so a list of
  /// a thousand buttons resolves the recipe once.
  pub fn resolve<C: Themed>(&self, case: &Case, instance: Option<&Recipe>) -> ResolvedRecipe {
    self.track_access();
    let layers = self.with_ui(|ui| ui.layers::<C>());
    let case = &case.in_theme(self);
    let mut recipes: Vec<&Recipe> = layers.iter().map(|layer| &**layer).collect();
    let lent = instance.and_then(|rules| rules.lent()).cloned();
    if let Some(instance) = instance.filter(|rules| !rules.is_empty()) {
      recipes.push(instance);
      return ResolvedRecipe::new(self, Arc::new(resolve_styles(&recipes, case)), C::ROOT, lent).described(&recipes);
    }
    let key = (Name::new(C::NAME), case.clone());
    let version = self.version();
    let mut cache = self.resolved_cache().lock().unwrap();
    if cache.version != version {
      cache.version = version;
      cache.styles.clear();
    }
    let styles = cache
      .styles
      .entry(key)
      .or_insert_with(|| Arc::new(resolve_styles(&recipes, case)))
      .clone();
    // An instance with no rules may still describe its root.
    recipes.extend(instance);
    ResolvedRecipe::new(self, styles, C::ROOT, lent).described(&recipes)
  }

  /// How every spinning icon and spinner moves that is not told its own motion ([`Icon::spin_motion`],
  /// [`Spinner::motion`]): smooth unless set. See [`SpinMotion`].
  ///
  /// [`Icon::spin_motion`]: super::Icon::spin_motion
  /// [`Spinner::motion`]: super::Spinner::motion
  pub fn spin_motion(&self) -> SpinMotion {
    self.track_access();
    self.with_ui(UiDefinition::spin_motion)
  }

  /// Gives every spinning icon and spinner that is not told its own motion `motion`, the ring of a loading button
  /// too: `SpinMotion::stepped(24).still_when_reduced(true)` for an app whose loaders turn in 24 poses a second and
  /// stand still where motion is reduced.
  pub fn set_spin_motion(&self, motion: SpinMotion) {
    self.update_ui(|ui| {
      ui.set_spin_motion(motion);
    });
  }

  /// How every skeleton moves that is not told its own motion ([`Skeleton::motion`]): smooth unless set. See
  /// [`SkeletonMotion`].
  ///
  /// [`Skeleton::motion`]: super::Skeleton::motion
  pub fn skeleton_motion(&self) -> SkeletonMotion {
    self.track_access();
    self.with_ui(UiDefinition::skeleton_motion)
  }

  /// Gives every skeleton that is not told its own motion `motion`: `SkeletonMotion::stepped(20)` for an app whose
  /// skeletons pulse in 20 poses a second, so that the window draws no more frames than that while one is shown.
  pub fn set_skeleton_motion(&self, motion: SkeletonMotion) {
    self.update_ui(|ui| {
      ui.set_skeleton_motion(motion);
    });
  }

  /// The renderer the theme gives a component in place of its default one.
  pub fn renderer<C: Themed>(&self) -> Option<C::Renderer> {
    self.track_access();
    self.with_ui(|ui| ui.renderer::<C>())
  }

  /// The renderer the theme gives one part of a component.
  pub fn part_renderer<C: Themed, R: Clone + Send + Sync + 'static>(&self, part: impl Into<Name>) -> Option<R> {
    self.track_access();
    let part = part.into();
    self.with_ui(|ui| ui.part_renderer::<C, R>(part))
  }

  /// Gives a component a recipe in place of the design's: see [`UiDefinition::set_recipe`].
  pub fn set_recipe<C: Themed>(&self, recipe: Recipe) {
    self.update_ui(|ui| {
      ui.set_recipe::<C>(recipe);
    });
  }

  /// Lays rules over a component's recipe: see [`UiDefinition::patch`].
  pub fn patch_recipe<C: Themed>(&self, patch: impl FnOnce(Recipe) -> Recipe) {
    self.update_ui(|ui| {
      ui.patch::<C>(patch);
    });
  }

  /// Takes back what the theme said of a component's recipe: see [`UiDefinition::reset_recipe`].
  pub fn reset_recipe<C: Themed>(&self) {
    self.update_ui(|ui| {
      ui.reset_recipe::<C>();
    });
  }

  /// Gives a component a renderer in place of its default one: see [`UiDefinition::set_renderer`].
  pub fn set_renderer<C: Themed>(&self, renderer: C::Renderer) {
    self.update_ui(|ui| {
      ui.set_renderer::<C>(renderer);
    });
  }

  /// Gives one part of a component a renderer of its own: see [`UiDefinition::set_part_renderer`].
  pub fn set_part_renderer<C: Themed, R: Clone + Send + Sync + 'static>(&self, part: impl Into<Name>, renderer: R) {
    let part = part.into();
    self.update_ui(|ui| {
      ui.set_part_renderer::<C, R>(part, renderer);
    });
  }

  /// Takes back the renderer the theme gave a component: its default one builds it again.
  pub fn reset_renderer<C: Themed>(&self) {
    self.update_ui(|ui| {
      ui.reset_renderer::<C>();
    });
  }

  /// Takes back the renderer the theme gave one part of a component.
  pub fn reset_part_renderer<C: Themed>(&self, part: impl Into<Name>) {
    let part = part.into();
    self.update_ui(|ui| {
      ui.reset_part_renderer::<C>(part);
    });
  }
}

/// What a component being rendered is in: its theme, and the breakpoint of its window.
struct Surroundings {
  theme: Theme,
  breakpoint: Option<Memo<Option<Breakpoint>>>,
  /// How the app writes dates: what a component's render was given of its app.
  dates: Option<crate::core::AppDateText>,
  /// The app's locale: every word lurq writes by itself.
  locale: Option<crate::core::AppLocale>,
  /// What the person points with, the app's.
  pointer: Option<crate::app::pointer::AppPointer>,
  /// The window what is being built is in.
  window: Option<crate::app::window::Window>,
  /// Which render of a component on this thread it is, one number for each: what the ids of parts are counted in
  /// (`ui::part_ids`).
  render: u64,
}

thread_local! {
  /// The surroundings of the components being rendered on this thread, innermost last.
  static RENDERING_IN: RefCell<Vec<Surroundings>> = const { RefCell::new(Vec::new()) };
  /// The number of the last render of a component on this thread.
  static RENDERS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

/// Which render of a component on this thread is going on, a number no other render has had: 0 outside one. A
/// subtree in a theme of its own is in the render around it.
pub(crate) fn current_render() -> u64 {
  RENDERING_IN.with(|rendering| rendering.borrow().last().map_or(0, |surroundings| surroundings.render))
}

/// The theme of what is being built: that of the component being rendered,
/// or of the scope around it ([`Ctx::with_theme`](crate::app::ctx::Ctx::with_theme)).
///
/// Outside a render there is no such theme, and this is a neutral one of its
/// own that follows nothing: build components where they are rendered.
pub fn current_theme() -> Theme {
  RENDERING_IN
    .with(|rendering| rendering.borrow().last().map(|surroundings| surroundings.theme.clone()))
    .unwrap_or_else(|| {
      static NEUTRAL: OnceLock<Theme> = OnceLock::new();
      NEUTRAL.get_or_init(Theme::new).clone()
    })
}

/// The breakpoint of the window what is being built is in: `None` below the
/// smallest one, and outside a render. What reads it is rendered again when
/// the window's width crosses a breakpoint, not at every resize.
pub fn current_breakpoint() -> Option<Breakpoint> {
  let breakpoint = RENDERING_IN.with(|rendering| {
    rendering
      .borrow()
      .last()
      .and_then(|surroundings| surroundings.breakpoint.clone())
  });
  breakpoint.and_then(|breakpoint| breakpoint.get())
}

/// What the person points with, in the app of what is being built
/// ([`App::pointer`](crate::app::App::pointer)): a finger or a mouse, a trackpad, a pen. What reads it is rendered
/// again when it changes. Outside a render, what the platform starts with
/// ([`Pointer::platform_default`](crate::app::Pointer::platform_default)).
pub fn current_pointer() -> crate::app::Pointer {
  RENDERING_IN
    .with(|rendering| {
      rendering
        .borrow()
        .last()
        .and_then(|surroundings| surroundings.pointer.clone())
    })
    .map_or_else(crate::app::Pointer::platform_default, |pointer| pointer.get())
}

/// Whether what is being built takes its touch form, dressed for a finger: swipes in place of buttons over its
/// sides, larger targets, dots in place of arrows, a phone's chrome. It is the person's pointer and nothing else:
/// a coarse one ([`Pointer::Coarse`](crate::app::Pointer::Coarse), [`current_pointer`]), which an app on iOS and
/// Android starts with and any app has once a finger touches it. Neither the window's width nor the theme's density
/// is asked: a narrow window of a desktop is a narrow room, laid out narrow, and a density is how large things are.
/// Every component with a touch form reads it, and a component's own prop for its form (`variant(..)`,
/// `touch(bool)`) wins over it. What reads it is rendered again when the pointer changes.
pub fn touch_form() -> bool {
  current_pointer() == crate::app::Pointer::Coarse
}

/// How many times the app or a test set the pointer rather than input moving it: what an open component that holds
/// its form against a finger landing on it gives way to. Read so that what reads it is rendered again.
pub(crate) fn pointer_told() -> u64 {
  RENDERING_IN
    .with(|rendering| {
      rendering
        .borrow()
        .last()
        .and_then(|surroundings| surroundings.pointer.clone())
    })
    .map_or(0, |pointer| pointer.told())
}

pub(crate) fn enter_theme(
  theme: Theme,
  breakpoint: Option<Memo<Option<Breakpoint>>>,
  dates: Option<crate::core::AppDateText>,
  locale: Option<crate::core::AppLocale>,
  pointer: Option<crate::app::pointer::AppPointer>,
  window: Option<crate::app::window::Window>,
) {
  let render = RENDERS.with(|renders| {
    renders.set(renders.get() + 1);
    renders.get()
  });
  RENDERING_IN.with(|rendering| {
    rendering.borrow_mut().push(Surroundings {
      theme,
      breakpoint,
      dates,
      locale,
      pointer,
      window,
      render,
    })
  });
}

/// The window what is being built is in, in a component's render; `None` outside one. What reads the window
/// through it is rendered again as the window changes.
pub(crate) fn current_window() -> Option<crate::app::window::Window> {
  RENDERING_IN.with(|rendering| {
    rendering
      .borrow()
      .last()
      .and_then(|surroundings| surroundings.window.clone())
  })
}

/// How the app of what is being built writes and reads dates: what
/// [`App::set_date_locale`](crate::app::App::set_date_locale) set, read so that what reads it is rendered again when
/// it changes. Outside a render, and in an app that set none, English. This is where a calendar or a date picker with
/// no `.locale(..)` of its own takes its text, mounted or built in a render.
pub fn current_date_locale() -> std::sync::Arc<dyn crate::core::DateText> {
  RENDERING_IN
    .with(|rendering| {
      rendering
        .borrow()
        .last()
        .and_then(|surroundings| surroundings.dates.clone())
    })
    .map_or_else(crate::core::english_date_text, |dates| dates.get())
}

/// The locale of the app of what is being built: every word lurq writes by itself, as
/// [`App::set_locale`](crate::app::App::set_locale) set it, read so that what reads it is rendered again when it
/// changes. Outside a render, and in an app that set none, English. This is where a component takes the words it
/// was not told itself, mounted or built in a render.
pub fn current_locale() -> std::sync::Arc<crate::core::Locale> {
  RENDERING_IN
    .with(|rendering| {
      rendering
        .borrow()
        .last()
        .and_then(|surroundings| surroundings.locale.clone())
    })
    .map_or_else(crate::core::english_locale, |locale| locale.get())
}

pub(crate) fn leave_theme() {
  RENDERING_IN.with(|rendering| {
    rendering.borrow_mut().pop();
  });
}

/// Runs `build` with `theme` as the theme of what it builds.
pub fn in_theme<R>(theme: &Theme, build: impl FnOnce() -> R) -> R {
  struct Leave;
  impl Drop for Leave {
    fn drop(&mut self) {
      leave_theme();
    }
  }
  // The window and the app are the ones around: only the theme changes.
  let (breakpoint, dates, locale, pointer, window) = RENDERING_IN.with(|rendering| {
    rendering
      .borrow()
      .last()
      .map_or((None, None, None, None, None), |surroundings| {
        (
          surroundings.breakpoint.clone(),
          surroundings.dates.clone(),
          surroundings.locale.clone(),
          surroundings.pointer.clone(),
          surroundings.window.clone(),
        )
      })
  });
  let render = current_render();
  enter_theme(theme.clone(), breakpoint, dates, locale, pointer, window);
  // The subtree is built in the render around it.
  if render != 0 {
    RENDERING_IN.with(|rendering| {
      if let Some(surroundings) = rendering.borrow_mut().last_mut() {
        surroundings.render = render;
      }
    });
  }
  let _leave = Leave;
  build()
}