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
use std::sync::{
  Arc, RwLock,
  atomic::{AtomicU64, Ordering},
};

use super::visibility::{self, Area, Visibility, VisibilityCallback, VisibilityOptions, Watch};
use crate::node::dimension::Dimension;

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

#[derive(Clone, Default)]
pub struct ElementRef {
  inner: Arc<RwLock<ElementRefInner>>,
}

#[derive(Clone, Default)]
pub struct ElementRefMut {
  inner: Arc<RwLock<ElementRefInner>>,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ElementRect {
  pub x: f32,
  pub y: f32,
  pub relative_x: f32,
  pub relative_y: f32,
  pub width: f32,
  pub height: f32,
}

impl ElementRect {
  pub fn center(&self) -> (f32, f32) {
    (self.x + self.width / 2.0, self.y + self.height / 2.0)
  }
}

#[derive(Default)]
struct ElementRefInner {
  #[cfg(feature = "canvas")]
  canvas: Option<crate::canvas::CanvasWeak>,
  x: f32,
  y: f32,
  relative_x: f32,
  relative_y: f32,
  width: f32,
  height: f32,
  attached: bool,
  /// Whether the element is a text drawn in fewer letters than it has, as the last layout drew it
  /// ([`ElementRef::text_cut`]).
  text_cut: bool,
  hovered: bool,
  active: bool,
  focused: bool,
  focus_signal: Option<crate::core::Signal<bool>>,
  focus_visible: bool,
  focus_visible_signal: Option<crate::core::Signal<bool>>,
  focus_visible_within: bool,
  focus_visible_within_signal: Option<crate::core::Signal<bool>>,
  override_rect: Option<ElementRect>,
  /// The width and the height the element is laid out at in place of those it was built with
  /// ([`ElementRefMut::set_size`]).
  size_override: (Option<Dimension>, Option<Dimension>),
  /// What the element is said to be since it was built, by name ([`ElementRefMut::describe`]).
  descriptions: Vec<(Arc<str>, Arc<str>)>,
  /// Whether the element is out of the focus and the pointer ([`ElementRefMut::set_inert`]).
  inert: bool,
  override_clear_generation: u64,
  layout_dirty: bool,
  layout_subscribers: crate::core::layout_dirty::LayoutSubscribers,
  rect_observer: Option<Arc<dyn Fn(ElementRect) + Send + Sync>>,
  /// Whether what `rect_observer` changes is rendered and laid out again before the frame is painted
  /// ([`ElementRef::observe_rect_before_paint`]).
  before_paint: bool,
  /// The observers added beside the ref's own ([`ElementRef::add_rect_observer`]), each with its number and whether
  /// what it changes is shown before paint. Each is removed when its [`RectObserver`] is dropped.
  added_observers: Vec<(u64, RectCallback, bool)>,
  /// The number of the next added observer.
  next_observer: u64,
  /// The observers of how much of the element shows ([`ElementRef::observe_visibility`]), each removed when its
  /// [`VisibilityObserver`] is dropped.
  visibility: Vec<Watch>,
}

type RectCallback = Arc<dyn Fn(ElementRect) + Send + Sync>;

/// An observer of an element's rect added with [`ElementRef::add_rect_observer`] or
/// [`ElementRef::add_rect_observer_before_paint`]: it is called for as long as this is kept, and removed from the
/// ref when this is dropped. A component that adds one in each render keeps the last ones it added and drops those
/// of the render before, so that they do not pile up.
#[must_use = "the observer is removed when this is dropped"]
pub struct RectObserver {
  inner: std::sync::Weak<RwLock<ElementRefInner>>,
  id: u64,
}

impl Drop for RectObserver {
  fn drop(&mut self) {
    if let Some(inner) = self.inner.upgrade() {
      let mut inner = inner.write().unwrap_or_else(|poisoned| poisoned.into_inner());
      inner.added_observers.retain(|(id, ..)| *id != self.id);
    }
  }
}

impl std::fmt::Debug for RectObserver {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("RectObserver").field("id", &self.id).finish()
  }
}

/// An observer of how much of an element shows, added with [`ElementRef::observe_visibility`]: it is called for
/// as long as this is kept, and removed from the ref when this is dropped. A component keeps it beside the ref
/// (in its own fields, from `create`), or keeps the ones of its last render and drops those of the render
/// before.
#[must_use = "the observer is removed when this is dropped"]
pub struct VisibilityObserver {
  inner: std::sync::Weak<RwLock<ElementRefInner>>,
  id: u64,
}

impl Drop for VisibilityObserver {
  fn drop(&mut self) {
    if let Some(inner) = self.inner.upgrade() {
      let mut inner = inner.write().unwrap_or_else(|poisoned| poisoned.into_inner());
      inner.visibility.retain(|watch| watch.id != self.id);
    }
  }
}

impl std::fmt::Debug for VisibilityObserver {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("VisibilityObserver").field("id", &self.id).finish()
  }
}

thread_local! {
  /// Whether an observer of [`ElementRef::observe_rect_before_paint`] ran since the runtime last asked.
  static OBSERVED_BEFORE_PAINT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// Whether an observer of [`ElementRef::observe_rect_before_paint`] ran on this thread since the last call: the
/// runtime renders what it changed and lays the frame out again before painting it.
pub(crate) fn take_observed_before_paint() -> bool {
  OBSERVED_BEFORE_PAINT.with(|observed| observed.replace(false))
}

impl ElementRef {
  pub(crate) fn track_layout(&self, watch: &Arc<crate::core::layout_dirty::LayoutWatch>) {
    self.inner.read().unwrap().layout_subscribers.bind(watch);
  }
  /// Typed access to the attached canvas; `None` before layout or after detachment.
  #[cfg(feature = "canvas")]
  pub fn as_canvas(&self) -> Option<crate::canvas::CanvasHandle> {
    let canvas = self.inner.read().unwrap().canvas.as_ref()?.upgrade()?;
    canvas.is_attached().then_some(canvas)
  }

  #[cfg(feature = "canvas")]
  pub(crate) fn bind_canvas(&self, canvas: &crate::canvas::CanvasHandle) {
    let mut inner = self.inner.write().unwrap();
    if let Some(old) = inner.canvas.as_ref().and_then(crate::canvas::CanvasWeak::upgrade)
      && old.is_attached()
      && old.surface_id() != canvas.surface_id()
    {
      tracing::warn!("an element ref cannot bind to two live canvases");
      return;
    }
    inner.canvas = Some(canvas.downgrade());
  }

  #[cfg(feature = "canvas")]
  pub(crate) fn detach_canvas(&self, id: crate::canvas::CanvasId) {
    let mut inner = self.inner.write().unwrap();
    if inner
      .canvas
      .as_ref()
      .and_then(crate::canvas::CanvasWeak::upgrade)
      .is_some_and(|canvas| canvas.surface_id() == id)
    {
      inner.canvas = None;
      inner.attached = false;
    }
  }

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

  pub fn mutable(&self) -> ElementRefMut {
    ElementRefMut {
      inner: self.inner.clone(),
    }
  }

  pub fn x(&self) -> f32 {
    self.inner.read().unwrap().x
  }

  pub fn y(&self) -> f32 {
    self.inner.read().unwrap().y
  }

  pub fn width(&self) -> f32 {
    self.inner.read().unwrap().width
  }

  pub fn height(&self) -> f32 {
    self.inner.read().unwrap().height
  }

  pub fn rect(&self) -> (f32, f32, f32, f32) {
    let inner = self.inner.read().unwrap();
    (inner.x, inner.y, inner.width, inner.height)
  }

  pub fn bounds(&self) -> ElementRect {
    let inner = self.inner.read().unwrap();
    ElementRect {
      x: inner.x,
      y: inner.y,
      relative_x: inner.relative_x,
      relative_y: inner.relative_y,
      width: inner.width,
      height: inner.height,
    }
  }

  pub fn is_attached(&self) -> bool {
    self.inner.read().unwrap().attached
  }

  /// Whether the element is a text that the last layout drew in fewer letters than it has: ended by an ellipsis
  /// where its room ran out (`TextOverflow::Elipsis`), or cut to a limit of lines. `false` for a text drawn whole,
  /// for an element that is no text, and before it is laid out. A label that says itself whole in a tooltip only
  /// while it is cut reads this ([`ui::Tooltip::when_cut`](crate::ui::Tooltip::when_cut)).
  pub fn text_cut(&self) -> bool {
    self.inner.read().unwrap().text_cut
  }

  /// What the layout drew of the element's text: cut or whole.
  pub(crate) fn set_text_cut(&self, cut: bool) {
    self.inner.write().unwrap().text_cut = cut;
  }

  pub fn hovered(&self) -> bool {
    self.inner.read().unwrap().hovered
  }

  pub fn active(&self) -> bool {
    self.inner.read().unwrap().active
  }

  pub fn focused(&self) -> bool {
    self.focus_signal().get()
  }

  /// Reactive focus state. Reading `focused()` during render subscribes too.
  pub fn focus_signal(&self) -> crate::core::Signal<bool> {
    let mut inner = self.inner.write().unwrap();
    let focused = inner.focused;
    inner
      .focus_signal
      .get_or_insert_with(|| crate::core::Signal::new(focused))
      .clone()
  }

  /// Focused and showing a focus ring, like CSS `:focus-visible`: the focus
  /// came from the keyboard (or a key was pressed since), or the element is a
  /// text input. Reading it during render subscribes, like [`Self::focused`].
  pub fn focus_visible(&self) -> bool {
    self.focus_visible_signal().get()
  }

  /// Reactive focus-visible state. Reading `focus_visible()` during render
  /// subscribes too.
  pub fn focus_visible_signal(&self) -> crate::core::Signal<bool> {
    let mut inner = self.inner.write().unwrap();
    let visible = inner.focus_visible;
    inner
      .focus_visible_signal
      .get_or_insert_with(|| crate::core::Signal::new(visible))
      .clone()
  }

  /// Focus-visible on the element or on something in it, like CSS
  /// `:focus-within` for a focus that shows its ring. Reading it during render
  /// subscribes, like [`Self::focus_visible`].
  pub fn focus_visible_within(&self) -> bool {
    self.focus_visible_within_signal().get()
  }

  /// Reactive focus-visible-within state. Reading `focus_visible_within()`
  /// during render subscribes too.
  pub fn focus_visible_within_signal(&self) -> crate::core::Signal<bool> {
    let mut inner = self.inner.write().unwrap();
    let within = inner.focus_visible_within;
    inner
      .focus_visible_within_signal
      .get_or_insert_with(|| crate::core::Signal::new(within))
      .clone()
  }

  pub(crate) fn set_focus_visible_within(&self, within: bool) {
    let signal = {
      let mut inner = self.inner.write().unwrap();
      if inner.focus_visible_within == within {
        return;
      }
      inner.focus_visible_within = within;
      inner.focus_visible_within_signal.clone()
    };
    if let Some(signal) = signal {
      signal.set(within);
    }
  }

  pub(crate) fn update(&self, x: f32, y: f32, relative_x: f32, relative_y: f32, width: f32, height: f32) {
    let observers = {
      let mut inner = self.inner.write().unwrap();
      let changed = !inner.attached
        || inner.x != x
        || inner.y != y
        || inner.relative_x != relative_x
        || inner.relative_y != relative_y
        || inner.width != width
        || inner.height != height;
      inner.x = x;
      inner.y = y;
      inner.relative_x = relative_x;
      inner.relative_y = relative_y;
      inner.width = width;
      inner.height = height;
      inner.attached = true;
      if changed {
        let before_paint = (inner.before_paint && inner.rect_observer.is_some())
          || inner.added_observers.iter().any(|(_, _, before_paint)| *before_paint);
        if before_paint {
          OBSERVED_BEFORE_PAINT.with(|observed| observed.set(true));
        }
        let mut observers: Vec<RectCallback> = inner.rect_observer.iter().cloned().collect();
        observers.extend(inner.added_observers.iter().map(|(_, observer, _)| observer.clone()));
        observers
      } else {
        Vec::new()
      }
    };
    // Outside the lock: an observer may read this ref (or write signals
    // whose subscribers do), or add and remove observers.
    let rect = ElementRect {
      x,
      y,
      relative_x,
      relative_y,
      width,
      height,
    };
    for observer in observers {
      observer(rect);
    }
  }

  /// Register a callback fired whenever the measured rect changes, including
  /// the first time the element is laid out. It runs during quad resolution
  /// (after layout, before present), so writing a [`crate::core::Signal`]
  /// from it marks the owning component dirty and the runtime schedules a
  /// follow-up pass — the measure-then-render handshake for components whose
  /// render depends on their own measured size.
  ///
  /// This is the ref's own observer: registering again replaces it, so the
  /// owner of the element registers it in every render. Code that watches a
  /// ref it was given, and must not take its owner's observer away (a list of
  /// a page's sections that follows their scroll), adds one of its own with
  /// [`add_rect_observer`](Self::add_rect_observer), beside this one.
  pub fn observe_rect(&self, observer: impl Fn(ElementRect) + Send + Sync + 'static) {
    let mut inner = self.inner.write().unwrap();
    inner.rect_observer = Some(Arc::new(observer));
    inner.before_paint = false;
  }

  /// As [`observe_rect`](Self::observe_rect), but what the observer changes is shown in the frame that laid the
  /// element out: where it writes a signal a component renders from, or gives an element a size through its ref
  /// ([`ElementRefMut::set_size`]), the runtime renders what is to be rendered and lays the frame out again before
  /// painting it. A component whose form follows its own room (a timeline that folds its two sides to one under a
  /// least width) shows the form that fits from its first frame, and as a panel is dragged across the width, with
  /// no frame of the other form; a splitter has its panes at the sizes that fit its room in its first frame.
  ///
  /// What is laid out again may be measured anew, so the frame is laid out again until nothing that is watched
  /// changes, four times at most. The observer should change what it writes only where the rect crosses what it is
  /// watching for: a value that changes on every rect lays every frame out again and again, and settles in none.
  ///
  /// This is the ref's own observer, as [`observe_rect`](Self::observe_rect)'s is, and replaces it; one added beside
  /// it is [`add_rect_observer_before_paint`](Self::add_rect_observer_before_paint).
  pub fn observe_rect_before_paint(&self, observer: impl Fn(ElementRect) + Send + Sync + 'static) {
    let mut inner = self.inner.write().unwrap();
    inner.rect_observer = Some(Arc::new(observer));
    inner.before_paint = true;
  }

  /// Adds an observer of the rect, called as [`observe_rect`](Self::observe_rect)'s is, beside the ref's own
  /// observer and any others added: none replaces another. It is called while the [`RectObserver`] this gives is
  /// kept, and removed when that is dropped. It is not called for the rect the element has now, only when that
  /// changes.
  pub fn add_rect_observer(&self, observer: impl Fn(ElementRect) + Send + Sync + 'static) -> RectObserver {
    self.add_observer(Arc::new(observer), false)
  }

  /// As [`add_rect_observer`](Self::add_rect_observer), and what the observer changes is shown in the frame that
  /// laid the element out, as with [`observe_rect_before_paint`](Self::observe_rect_before_paint).
  pub fn add_rect_observer_before_paint(&self, observer: impl Fn(ElementRect) + Send + Sync + 'static) -> RectObserver {
    self.add_observer(Arc::new(observer), true)
  }

  /// Tells `observer` how much of the element shows: lurq's `IntersectionObserver`. It is called with the
  /// first layout after this, and then each time the share that shows crosses one of the options' thresholds,
  /// either way; with no threshold given, when the element comes into view and when it leaves. It is called
  /// after layout, as [`observe_rect`](Self::observe_rect)'s observer is, while the [`VisibilityObserver`]
  /// this gives is kept, and removed when that is dropped.
  ///
  /// The share is reckoned from the layout at every pass, so it is right whatever moved the content: a finger,
  /// its momentum, a snap to a page, a wheel, the keys or the app's own `set_scroll`. The element is measured
  /// against the scroll container nearest around it, as much of it as shows in the window; against the window,
  /// where nothing around it scrolls; or against [`VisibilityOptions::root`]. The root is grown by
  /// [`VisibilityOptions::margin`] first, which is how what is near the viewport is told from what is far.
  ///
  /// ```
  /// use lurq::core::{ElementRef, Signal, VisibilityOptions};
  ///
  /// let (page, current) = (ElementRef::new(), Signal::new(false));
  /// // Kept for as long as the page is watched.
  /// let watching = page.observe_visibility(VisibilityOptions::new().thresholds([0.99]), {
  ///   let current = current.clone();
  ///   move |visibility| current.set(visibility.ratio >= 0.99)
  /// });
  /// # drop(watching);
  /// ```
  ///
  /// The boxes are the layout's: a transform does not move them, and an element clipped by something that
  /// does not scroll counts as showing. An element taken out of the tree is told of no more; one its row or
  /// column dropped for want of room is told that none of it shows.
  pub fn observe_visibility(
    &self,
    options: VisibilityOptions,
    observer: impl Fn(Visibility) + Send + Sync + 'static,
  ) -> VisibilityObserver {
    let mut inner = self.inner.write().unwrap();
    let id = inner.next_observer;
    inner.next_observer += 1;
    inner.visibility.push(Watch {
      id,
      options,
      observer: Arc::new(observer),
      level: None,
    });
    // The next pass tells it what shows now, whether or not anything else asks for one.
    inner.layout_dirty = true;
    inner.layout_subscribers.mark();
    VisibilityObserver {
      inner: Arc::downgrade(&self.inner),
      id,
    }
  }

  /// Whether anything observes how much of the element shows.
  pub(crate) fn watches_visibility(&self) -> bool {
    !self.inner.read().unwrap().visibility.is_empty()
  }

  /// The element is laid out at `element`, inside a scroll container of which `root` shows (or the window):
  /// each observer whose share crossed one of its thresholds is told. `None` for an element that is not laid
  /// out, of which nothing shows.
  pub(crate) fn tell_visibility(&self, placed: Option<(Area, Area)>) {
    // What each observer measures against is read before the ref is locked: its root may be this element.
    let watched: Vec<(u64, VisibilityOptions)> = {
      let inner = self.inner.read().unwrap();
      inner
        .visibility
        .iter()
        .map(|watch| (watch.id, watch.options.clone()))
        .collect()
    };
    let seen: Vec<(u64, Visibility, usize)> = watched
      .into_iter()
      .map(|(id, options)| {
        let seen = match placed {
          Some((element, root)) => {
            let root = match options.explicit_root() {
              Some(root) => {
                let (x, y, width, height) = root.rect();
                Area::new(x, y, width, height)
              }
              None => root,
            };
            visibility::measure(element, options.grown(root))
          }
          None => visibility::hidden(),
        };
        (id, seen, options.level(&seen))
      })
      .collect();
    let told: Vec<(VisibilityCallback, Visibility)> = {
      let mut inner = self.inner.write().unwrap();
      seen
        .into_iter()
        .filter_map(|(id, seen, level)| {
          let watch = inner.visibility.iter_mut().find(|watch| watch.id == id)?;
          if watch.level == Some(level) {
            return None;
          }
          watch.level = Some(level);
          Some((watch.observer.clone(), seen))
        })
        .collect()
    };
    // Outside the lock: an observer may read this ref, write signals, or add and remove observers.
    for (observer, seen) in told {
      observer(seen);
    }
  }

  fn add_observer(&self, observer: RectCallback, before_paint: bool) -> RectObserver {
    let mut inner = self.inner.write().unwrap();
    let id = inner.next_observer;
    inner.next_observer += 1;
    inner.added_observers.push((id, observer, before_paint));
    RectObserver {
      inner: Arc::downgrade(&self.inner),
      id,
    }
  }

  pub(crate) fn same_handle(&self, other: &Self) -> bool {
    Arc::ptr_eq(&self.inner, &other.inner)
  }

  pub(crate) fn override_rect(&self) -> Option<ElementRect> {
    self.inner.read().unwrap().override_rect
  }

  /// The width and the height [`ElementRefMut::set_size`] gave the element, each `None` where it is laid out
  /// as it was built.
  pub(crate) fn size_override(&self) -> (Option<Dimension>, Option<Dimension>) {
    self.inner.read().unwrap().size_override
  }

  /// Whether [`ElementRefMut::set_inert`] made the element inert.
  pub(crate) fn is_inert(&self) -> bool {
    self.inner.read().unwrap().inert
  }

  /// What [`ElementRefMut::describe`] said of the element. Read only by
  /// tooling.
  #[cfg(any(feature = "devtools", feature = "mcp"))]
  pub(crate) fn descriptions(&self) -> Vec<(Arc<str>, Arc<str>)> {
    self.inner.read().unwrap().descriptions.clone()
  }

  pub(crate) fn override_clear_generation(&self) -> u64 {
    self.inner.read().unwrap().override_clear_generation
  }

  pub(crate) fn has_layout_dirty(&self) -> bool {
    self.inner.read().unwrap().layout_dirty
  }

  pub(crate) fn take_layout_dirty(&self) -> bool {
    let mut inner = self.inner.write().unwrap();
    let dirty = inner.layout_dirty;
    inner.layout_dirty = false;
    dirty
  }

  pub(crate) fn set_hovered(&self, hovered: bool) {
    self.inner.write().unwrap().hovered = hovered;
  }

  pub(crate) fn set_active(&self, active: bool) {
    self.inner.write().unwrap().active = active;
  }

  /// Losing focus also loses focus-visible.
  pub(crate) fn set_focused(&self, focused: bool) {
    if !focused {
      self.set_focus_visible(false);
    }
    let signal = {
      let mut inner = self.inner.write().unwrap();
      if inner.focused == focused {
        return;
      }
      inner.focused = focused;
      inner.focus_signal.clone()
    };
    if let Some(signal) = signal {
      signal.set(focused);
    }
  }

  pub(crate) fn set_focus_visible(&self, visible: bool) {
    let signal = {
      let mut inner = self.inner.write().unwrap();
      if inner.focus_visible == visible {
        return;
      }
      inner.focus_visible = visible;
      inner.focus_visible_signal.clone()
    };
    if let Some(signal) = signal {
      signal.set(visible);
    }
  }
}

impl ElementRefMut {
  #[cfg(feature = "canvas")]
  pub fn as_canvas(&self) -> Option<crate::canvas::CanvasHandle> {
    self.as_ref().as_canvas()
  }

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

  pub fn as_ref(&self) -> ElementRef {
    ElementRef {
      inner: self.inner.clone(),
    }
  }

  pub fn x(&self) -> f32 {
    self.as_ref().x()
  }

  pub fn y(&self) -> f32 {
    self.as_ref().y()
  }

  pub fn width(&self) -> f32 {
    self.as_ref().width()
  }

  pub fn height(&self) -> f32 {
    self.as_ref().height()
  }

  pub fn rect(&self) -> (f32, f32, f32, f32) {
    self.as_ref().rect()
  }

  pub fn bounds(&self) -> ElementRect {
    self.as_ref().bounds()
  }

  /// See [`ElementRef::observe_rect`].
  pub fn observe_rect(&self, observer: impl Fn(ElementRect) + Send + Sync + 'static) {
    self.as_ref().observe_rect(observer);
  }

  /// As [`ElementRef::observe_rect_before_paint`].
  pub fn observe_rect_before_paint(&self, observer: impl Fn(ElementRect) + Send + Sync + 'static) {
    self.as_ref().observe_rect_before_paint(observer);
  }

  /// See [`ElementRef::add_rect_observer`].
  pub fn add_rect_observer(&self, observer: impl Fn(ElementRect) + Send + Sync + 'static) -> RectObserver {
    self.as_ref().add_rect_observer(observer)
  }

  /// See [`ElementRef::add_rect_observer_before_paint`].
  pub fn add_rect_observer_before_paint(&self, observer: impl Fn(ElementRect) + Send + Sync + 'static) -> RectObserver {
    self.as_ref().add_rect_observer_before_paint(observer)
  }

  /// See [`ElementRef::observe_visibility`].
  pub fn observe_visibility(
    &self,
    options: VisibilityOptions,
    observer: impl Fn(Visibility) + Send + Sync + 'static,
  ) -> VisibilityObserver {
    self.as_ref().observe_visibility(options, observer)
  }

  pub fn set_bounds(&self, rect: ElementRect) {
    let mut inner = self.inner.write().unwrap();
    inner.x = rect.x;
    inner.y = rect.y;
    inner.relative_x = rect.relative_x;
    inner.relative_y = rect.relative_y;
    inner.width = rect.width;
    inner.height = rect.height;
    inner.override_rect = Some(rect);
    inner.layout_dirty = true;
    inner.layout_subscribers.mark();
  }

  pub fn set_relative_bounds(&self, relative_x: f32, relative_y: f32, width: f32, height: f32) {
    let current = self.bounds();
    let parent_x = current.x - current.relative_x;
    let parent_y = current.y - current.relative_y;
    self.set_bounds(ElementRect {
      x: parent_x + relative_x,
      y: parent_y + relative_y,
      relative_x,
      relative_y,
      width,
      height,
    });
  }

  /// Lays the element out `width` wide and `height` high in place of the width and the height it was built
  /// with, until this is called again: `None` leaves that side as it was built. The least and the most size
  /// it was built with still hold, and its parent places it as it would any element of that size. Nothing is
  /// rendered again: the element and what holds it are laid out again at the next pass, which this asks for.
  /// A splitter moves its panes so while a divider is dragged, without building what is in them again.
  pub fn set_size(&self, width: Option<Dimension>, height: Option<Dimension>) {
    let mut inner = self.inner.write().unwrap();
    if inner.size_override == (width, height) {
      return;
    }
    inner.size_override = (width, height);
    inner.layout_dirty = true;
    inner.layout_subscribers.mark();
  }

  /// Says `value` of the element under `name` to tooling that reads the tree (the DevTools, the MCP server), in
  /// place of what its `describe(name, ..)` said when it was built, until it is said again. Nothing is rendered
  /// again: a splitter tells where its divider is after a drag so.
  pub fn describe(&self, name: impl Into<Arc<str>>, value: impl Into<Arc<str>>) {
    let (name, value) = (name.into(), value.into());
    let mut inner = self.inner.write().unwrap();
    match inner.descriptions.iter_mut().find(|(said, _)| *said == name) {
      Some((_, held)) => *held = value,
      None => inner.descriptions.push((name, value)),
    }
  }

  /// Makes the element inert, or not, as `.inert(..)` does where it was built, with nothing rendered again: Tab and
  /// a focus group's keys pass by everything in it, no pointer reaches it, and the focus in it leaves at the next
  /// pass, which this asks for. Built inert, it stays inert whatever this says. A splitter makes a pane it closes
  /// inert so while a divider is dragged.
  pub fn set_inert(&self, inert: bool) {
    let mut inner = self.inner.write().unwrap();
    if inner.inert == inert {
      return;
    }
    inner.inert = inert;
    // A pass, after whose layout the focus leaves what became inert.
    inner.layout_dirty = true;
    inner.layout_subscribers.mark();
  }

  /// The width and the height [`Self::set_size`] gave the element.
  pub fn size(&self) -> (Option<Dimension>, Option<Dimension>) {
    self.as_ref().size_override()
  }

  pub fn clear_bounds_override(&self) {
    let mut inner = self.inner.write().unwrap();
    if inner.override_rect.take().is_some() {
      // Each node consuming this handle must discard its cached offset.
      // A shared flag would let the first tree hide the clear from the rest.
      inner.override_clear_generation = OVERRIDE_CLEAR_GENERATION.fetch_add(1, Ordering::Relaxed);
    }
    inner.layout_dirty = true;
    inner.layout_subscribers.mark();
  }

  pub fn is_attached(&self) -> bool {
    self.as_ref().is_attached()
  }

  pub fn hovered(&self) -> bool {
    self.as_ref().hovered()
  }

  pub fn active(&self) -> bool {
    self.as_ref().active()
  }

  pub fn focused(&self) -> bool {
    self.as_ref().focused()
  }

  pub fn focus_signal(&self) -> crate::core::Signal<bool> {
    self.as_ref().focus_signal()
  }

  /// See [`ElementRef::focus_visible`].
  pub fn focus_visible(&self) -> bool {
    self.as_ref().focus_visible()
  }

  /// See [`ElementRef::focus_visible_signal`].
  pub fn focus_visible_signal(&self) -> crate::core::Signal<bool> {
    self.as_ref().focus_visible_signal()
  }
}

impl From<ElementRefMut> for ElementRef {
  fn from(value: ElementRefMut) -> Self {
    value.as_ref()
  }
}

#[cfg(all(test, any(feature = "devtools", feature = "mcp")))]
mod tests {
  use std::sync::Arc;

  use super::ElementRefMut;
  use crate::node::{NodeUpdate, node::Node};

  fn attr(node: &Node, name: &str) -> Option<String> {
    node
      .debug_attrs()
      .iter()
      .find(|(said, _)| said.as_ref() == name)
      .map(|(_, value)| value.to_string())
  }

  #[test]
  fn what_a_ref_describes_stands_for_what_the_element_was_built_with() {
    let handle = ElementRefMut::new();
    let mut node = Node::new().ref_element(handle.clone());
    NodeUpdate::describe(&mut node, Arc::from("a11y_role"), Arc::from("separator"));
    NodeUpdate::describe(&mut node, Arc::from("a11y_valuenow"), Arc::from("120"));
    assert_eq!(attr(&node, "a11y_valuenow").as_deref(), Some("120"));
    handle.describe("a11y_valuenow", "180");
    handle.describe("a11y_valuemax", "240");
    assert_eq!(attr(&node, "a11y_valuenow").as_deref(), Some("180"));
    assert_eq!(attr(&node, "a11y_valuemax").as_deref(), Some("240"));
    assert_eq!(attr(&node, "a11y_role").as_deref(), Some("separator"));
    assert_eq!(node.debug_attrs().len(), 3, "said once each");
    handle.describe("a11y_valuenow", "200");
    assert_eq!(attr(&node, "a11y_valuenow").as_deref(), Some("200"));
  }
}