masonry_core 0.5.0

Traits and types of the Masonry toolkit.
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
// Copyright 2018 the Xilem Authors and the Druid Authors
// SPDX-License-Identifier: Apache-2.0

use std::any::{Any, TypeId};
use std::fmt::{Debug, Display};
use std::num::NonZeroU64;
use std::sync::atomic::{AtomicU64, Ordering};

use accesskit::{Node, Role};
use kurbo::{Axis, Point, Size};
use smallvec::SmallVec;
use tracing::field::DisplayValue;
use tracing::{Span, trace_span};

use crate::core::{
    AccessCtx, AccessEvent, ActionCtx, ComposeCtx, CursorIcon, ErasedAction, EventCtx, LayoutCtx,
    MeasureCtx, NewWidget, PaintCtx, PointerEvent, PropertiesMut, PropertiesRef, QueryCtx,
    RegisterCtx, Subwindow, TextEvent, Update, UpdateCtx, WidgetMut, WidgetRef, pre_paint,
};
use crate::imaging::Painter;
use crate::layout::{LenReq, Length};

/// A unique identifier for a single [`Widget`].
///
/// `WidgetId`s are generated automatically for all widgets in the widget tree.
/// More specifically, each [`WidgetPod`](crate::core::WidgetPod) has a unique `WidgetId`.
///
/// These ids are used internally to route events, and can be used to fetch a specific
/// widget for testing or event handling.
///
/// A widget can retrieve its id via methods on the various contexts, such as
/// [`UpdateCtx::widget_id`].
///
/// # `WidgetId` cannot be reserved
///
/// Ids are only attributed once a widget is added to the widget tree.
///
/// You can't create a widget with a pre-allocated `WidgetId`.
/// If you want to create a widget in a way that lets you refer to it later,
/// see [`NewWidget::with_tag`](crate::core::NewWidget::with_tag).
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct WidgetId(pub(crate) NonZeroU64);

impl WidgetId {
    /// A serialized representation of the `WidgetId` for debugging purposes.
    pub fn trace(self) -> DisplayValue<Self> {
        tracing::field::display(self)
    }
}

#[doc(hidden)]
/// A trait to access a [`Widget`] value as a trait object. It is implemented for all types that implement `Widget`.
pub trait AsDynWidget {
    fn as_box_dyn(self: Box<Self>) -> Box<dyn Widget>;
    fn as_dyn(&self) -> &dyn Widget;
    fn as_mut_dyn(&mut self) -> &mut dyn Widget;
}

impl<T: Widget> AsDynWidget for T {
    fn as_box_dyn(self: Box<Self>) -> Box<dyn Widget> {
        self
    }

    fn as_dyn(&self) -> &dyn Widget {
        self as &dyn Widget
    }

    fn as_mut_dyn(&mut self) -> &mut dyn Widget {
        self as &mut dyn Widget
    }
}

/// A trait that lets functions either downcast to a `Sized` widget or keep a `dyn Widget`.
pub trait FromDynWidget {
    /// Downcasts `widget` if `Self: Sized`, else returns it as-is.
    fn from_dyn(widget: &dyn Widget) -> Option<&Self>;
    /// Downcasts `widget` if `Self: Sized`, else returns it as-is.
    fn from_dyn_mut(widget: &mut dyn Widget) -> Option<&mut Self>;
}

impl<T: Widget> FromDynWidget for T {
    fn from_dyn(widget: &dyn Widget) -> Option<&Self> {
        (widget as &dyn Any).downcast_ref()
    }

    fn from_dyn_mut(widget: &mut dyn Widget) -> Option<&mut Self> {
        (widget as &mut dyn Any).downcast_mut()
    }
}

impl FromDynWidget for dyn Widget {
    fn from_dyn(widget: &dyn Widget) -> Option<&Self> {
        Some(widget)
    }

    fn from_dyn_mut(widget: &mut dyn Widget) -> Option<&mut Self> {
        Some(widget)
    }
}

/// A collection of widget ids, to be returned from [`Widget::children_ids`].
///
/// Internally, this uses a small vector optimisation, but you should treat it as an append-only `Vec<WidgetId>`.
/// You can use `ChildrenIds::from_slice` with an array to make a list of children ids of known size,
/// or use `ChildrenIds::new` then `push` to it.
/// This type also implements [`FromIterator<WidgetId>`](core::iter::FromIterator).
// TODO: Consider making our own wrapper type here, to make future breaking changes easier.?
pub type ChildrenIds = SmallVec<[WidgetId; 16]>;

/// The trait implemented by all widgets.
///
/// For details on how to implement this trait, see the [tutorials](crate::doc).
///
/// Whenever external events affect the given widget, methods
/// [`on_pointer_event`](Self::on_pointer_event),
/// [`on_text_event`](Self::on_text_event),
/// [`on_access_event`](Self::on_access_event),
/// [`on_anim_frame`](Self::on_anim_frame) and [`update`](Self::update) are called.
///
/// Later on, when the widget is laid out and displayed, methods
/// [`measure`](Self::measure), [`layout`](Self::layout), [`compose`](Self::compose),
/// [`paint`](Self::paint), and [`accessibility`](Self::accessibility) are called.
///
/// These trait methods are provided with a corresponding context. The widget can
/// request things and cause actions by calling methods on that context.
///
/// Generally all coordinates given to the widget and taken as input by context methods
/// are going to be in the widget's local content-box coordinate space. Exceptions are documented
/// for the relevant methods, e.g. mouse events will arrive in the window's coordinate space.
/// There are helper methods on the context to convert these to the local coordinate space.
///
/// Widgets also have a [`children_ids`](Self::children_ids) method. Leaf widgets return an empty array,
/// whereas container widgets return an array of [`WidgetId`].
/// Container widgets have some validity invariants to maintain regarding their children.
///
/// Generally speaking, widgets aren't used directly. They are stored by Masonry and accessed
/// through [`WidgetPod`](crate::core::WidgetPod)s. Widget methods are called by Masonry, and a
/// widget should only be mutated either during a method call or through a [`WidgetMut`].
#[allow(unused_variables, reason = "Default impls don't use method arguments")]
pub trait Widget: AsDynWidget + Any {
    /// The action type that this widget will submit, through [`EventCtx::submit_action`]
    /// (or the method of the same name on a different context).
    /// The type of actions submitted by this widget will be validated against this type.
    ///
    /// If this widget never submits action, this can be an empty type
    /// such as [`NoAction`](crate::core::NoAction).
    type Action: Any + Debug
    where
        Self: Sized;

    /// Handles a pointer event.
    ///
    /// Pointer events will target the widget under the pointer, and then the
    /// event will bubble to each of its parents.
    fn on_pointer_event(
        &mut self,
        ctx: &mut EventCtx<'_>,
        props: &mut PropertiesMut<'_>,
        event: &PointerEvent,
    ) {
    }

    /// Handles a text event.
    ///
    /// Text events will target the [focused widget], then bubble to each parent.
    ///
    /// [focused widget]: crate::doc::masonry_concepts#text-focus
    fn on_text_event(
        &mut self,
        ctx: &mut EventCtx<'_>,
        props: &mut PropertiesMut<'_>,
        event: &TextEvent,
    ) {
    }

    /// Handles an event from the platform's accessibility API.
    ///
    /// Accessibility events target a specific widget id, then bubble to each parent.
    fn on_access_event(
        &mut self,
        ctx: &mut EventCtx<'_>,
        props: &mut PropertiesMut<'_>,
        event: &AccessEvent,
    ) {
    }

    /// Called at the beginning of a new animation frame.
    ///
    /// An animation frame does not implicitly request a repaint of this widget.
    /// That is, if you change something which changes how this widget is
    /// drawn, you should call
    /// [`request_paint_only`](UpdateCtx::request_paint_only)
    /// ([`request_render`](UpdateCtx::request_render) if an accessibility
    /// update is also required). This method should itself call
    /// [`request_anim`](UpdateCtx::request_anim_frame) unless the animation
    /// has finished.
    ///
    /// On the first frame when transitioning from idle to animating, `interval`
    /// will be 0. (This logic is presently per-window but might change to
    /// per-widget to make it more consistent). Otherwise it is in nanoseconds.
    ///
    /// The `paint` method will often be called shortly after this event is finished.
    /// For that reason, you should try to avoid doing anything computationally
    /// intensive in response to an `AnimFrame` event: it might make the app miss
    /// the monitor's refresh, causing lag or jerky animations.
    ///
    /// Do not use animation frames for delayed one-shot UI behavior
    /// such as cursor blinking, tooltip delays, debounce, or long press
    /// recognition; you should use timers instead.
    fn on_anim_frame(
        &mut self,
        ctx: &mut UpdateCtx<'_>,
        props: &mut PropertiesMut<'_>,
        interval: u64,
    ) {
    }

    /// Handles an `action` from a `source` widget deeper in the tree.
    ///
    /// If the action isn't marked as handled then it will bubble up the widget tree
    /// and eventually to the app driver.
    fn on_action(
        &mut self,
        ctx: &mut ActionCtx<'_>,
        props: &mut PropertiesMut<'_>,
        action: &ErasedAction,
        source: WidgetId,
    ) {
    }

    // TODO - Reorder methods to match 02_implementing_widget.md

    /// Registers child widgets with Masonry.
    ///
    /// Leaf widgets can implement this with an empty body.
    ///
    /// Container widgets need to call [`RegisterCtx::register_child`] for all
    /// their children. Forgetting to do so is a logic error and may lead to debug panics.
    /// All the children returned by `children_ids` should be visited.
    fn register_children(&mut self, ctx: &mut RegisterCtx<'_>);

    /// Handles an update to the widget's state.
    ///
    /// This method is called to notify your widget of certain special events,
    /// (available in the [`Update`] enum) that are generally related to
    /// changes in the widget graph or in the state of your specific widget.
    fn update(&mut self, ctx: &mut UpdateCtx<'_>, props: &mut PropertiesMut<'_>, event: &Update) {}

    // TODO - Remove default implementation
    /// Handles a property being added, changed, or removed.
    fn property_changed(&mut self, ctx: &mut UpdateCtx<'_>, property_type: TypeId) {}

    /// Computes the content-box [`Length`] that the widget wants to be on the given `axis`.
    ///
    /// The goal of this method is for a parent to learn how its children want to be sized.
    /// All the inputs are hints towards what the parent is planning for its child.
    /// These hints should be followed, but that is not strictly required. For example,
    /// it is completely valid for the child to always return the same size regardless of space.
    /// Which is to say, at the end of the day, a widget chooses how it measures itself.
    ///
    /// It's a question of preferred size. As `measure` will only be called when there is
    /// no defined length present for the `axis`. So `measure` is not about reading the widget's
    /// [`Dimensions`] from `props` and returning that. That is handled earlier by Masonry.
    /// Instead, it's a task of measuring the contents of the widget. Combining both local state
    /// and child measurements to come up with a answer for the total length of this `axis`.
    ///
    /// Call [`compute_length`] to measure children and to get their `axis` length.
    /// Then account for the child's positioning and any other unique layout factors you might have
    /// to determine the total length of this widget on the given `axis`.
    ///
    /// If you have a thin wrapper widget that wants to mimic its child in terms of layout,
    /// then you should use [`redirect_measurement`] to have the child answer for you.
    ///
    /// The `cross_length`, if present, says that the cross-axis of the given `axis` of this
    /// measured widget's content-box can be presumed to be `cross_length` long.
    /// This information is often very useful for measuring `axis` and should be used.
    /// However, ultimately it may end up not materializing. That is to say, it is
    /// a valid assumption for the duration of this `measure` call but there is
    /// no guarantee that it will still be true when the final size is decided.
    ///
    /// How exactly the length is calculated should be determined based on [`LenReq`].
    /// Adapting to it is a cornerstone of a well running cooperative layout system,
    /// and generally all built-in Masonry widgets will respect the request. However,
    /// ultimately it is completely valid for the widget to return whatever length it wants.
    ///
    /// It is not guaranteed that `measure` will be called even once during a layout pass.
    /// Don't design `measure` to be the only source of critical work that the widget depends on.
    ///
    /// Nor is it guaranteed that a [`layout`] call will follow a `measure` call, because
    /// the parent widget might end up choosing the same size for this widget as last time.
    /// Be very careful with mutating state inside `measure`. You should only mutate state
    /// in a way that is useful for `measure` itself, or in a way that the rest of your widget
    /// will correctly understand. Basically, don't commit any layout choices. You can save
    /// speculative data that [`layout`] might later read and commit. However, generally,
    /// no other part of the widget should be affected by the data that `measure` mutates.
    ///
    /// There might be any number of `measure` calls per layout pass. Depending on the specific
    /// parent's layout algorithm, which might measure its children multiple times. Hence, it's
    /// important for good performance to cache any expensive computations that can be reused.
    ///
    /// Masonry will, by default, cache the results of measurement. The cache key is derived
    /// from `axis`, `len_req`, and `cross_length`. If the widget uses any other data to influence
    /// the result of the measurement, then the widget is responsible for requesting layout
    /// when any of that data changes. For properties, this is handled in [`property_changed`].
    /// For any other data you reference, the exact mechanism of detecting changes is up to you.
    /// Once you've detected a change in that data, call `request_layout` to clear the cache.
    /// If you can't detect changes of the referenced data, disable the cache via [`cache_result`].
    ///
    /// The cache provided by Masonry is small, with only a few entries per widget. If your widget
    /// does expensive computations that aren't necessarily different per the aforementioned inputs,
    /// or you need to request layout for reasons that don't affect the result of this computation,
    /// then your widget should also have its own inner cache layer to avoid redoing the same work.
    ///
    /// [`compute_length`]: MeasureCtx::compute_length
    /// [`redirect_measurement`]: MeasureCtx::redirect_measurement
    /// [sanitized]: crate::util::Sanitize
    /// [`cache_result`]: MeasureCtx::cache_result
    /// [`Dimensions`]: crate::properties::Dimensions
    /// [`layout`]: Self::layout
    /// [`property_changed`]: Self::property_changed
    fn measure(
        &mut self,
        ctx: &mut MeasureCtx<'_>,
        props: &PropertiesRef<'_>,
        axis: Axis,
        len_req: LenReq,
        cross_length: Option<Length>,
    ) -> Length;

    /// Lays out the widget with the given content-box `size`.
    ///
    /// A container widget must ensure all its direct children are laid out in this method.
    ///
    /// For every child widget, as defined by [`children_ids`], the container must:
    ///
    /// 1. (Optionally) Call [`compute_size`] to get the border-box size the child wants to be.
    ///    If the container has somehow already decided on the child length on one axis, then it
    ///    should instead call [`compute_length`] with the correct border-box `cross_length`.
    /// 2. Decide what the border-box [`Size`] of the child should be. The parent is in control.
    ///    Note, however, that the child will still be in control of its own [`paint`] method.
    ///    If a child is given a size smaller than its [`MinContent`], its painting is likely
    ///    to overflow its bounds, depending on both the child's and the parent's clip settings.
    /// 3. Call [`run_layout`] on the child with the chosen border-box size.
    ///    This will recursively trigger the layout pass on both the child and all its descendants.
    /// 4. Call [`place_child`] to give the child a location, relative to the parent's content-box.
    ///    With that, the laying out of the child is finished.
    ///
    /// The order of laying out children doesn't matter. It is also valid to interleave the calls.
    /// For example you might `compute_size` for a few, lay out one, re-compute the others.
    ///
    /// Failing to lay out and place some child is a logic error and may lead to panics.
    ///
    /// Container widgets must not add or remove children during layout.
    /// Doing so is a logic error and may lead to panics.
    ///
    /// The `size` given to this method must be finite, non-negative, and in logical pixels.
    /// When Masonry calls `layout` during the layout pass, it will guarantee that for `size`.
    ///
    /// [`compute_size`]: LayoutCtx::compute_size
    /// [`compute_length`]: LayoutCtx::compute_length
    /// [`run_layout`]: LayoutCtx::run_layout
    /// [`place_child`]: LayoutCtx::place_child
    /// [`children_ids`]: Self::children_ids
    /// [`paint`]: Self::paint
    /// [`MinContent`]: crate::layout::Dim::MinContent
    fn layout(&mut self, ctx: &mut LayoutCtx<'_>, props: &PropertiesRef<'_>, size: Size);

    /// Updates compose-only state like scroll translations.
    ///
    /// Cheaper alternative than doing a full layout.
    ///
    /// This method is guaranteed to be called after this widget's [`layout`] method runs,
    /// and after this widget explicitly requests compose. It may also be called in other scenarios.
    ///
    /// [`layout`]: Self::layout
    fn compose(&mut self, ctx: &mut ComposeCtx<'_>) {}

    /// Paints the widget's background.
    ///
    /// This is where box shadow, background, and borders are painted.
    ///
    /// This method is not constrained by the clip defined in [`LayoutCtx::set_clip_path`],
    /// and can paint things outside the clip.
    fn pre_paint(
        &mut self,
        ctx: &mut PaintCtx<'_>,
        props: &PropertiesRef<'_>,
        painter: &mut Painter<'_>,
    ) {
        pre_paint(ctx, props, painter);
    }

    /// Paints the widget's content.
    ///
    /// This is called before the children are drawn.
    /// To draw on top of children, see [`Widget::post_paint`].
    fn paint(
        &mut self,
        ctx: &mut PaintCtx<'_>,
        props: &PropertiesRef<'_>,
        painter: &mut Painter<'_>,
    );

    /// Final paint method, which paints on top of the widget's children.
    ///
    /// This method is not constrained by the clip defined in [`LayoutCtx::set_clip_path`],
    /// and can paint things outside the clip.
    fn post_paint(
        &mut self,
        ctx: &mut PaintCtx<'_>,
        props: &PropertiesRef<'_>,
        painter: &mut Painter<'_>,
    ) {
    }

    /// Returns what kind of "thing" the widget fundamentally is.
    fn accessibility_role(&self) -> Role;

    /// Describes the widget's contents for accessibility APIs.
    ///
    /// This method takes a mutable reference to a node which is already initialized
    /// with some information about the current widget (coordinates, status flags), and
    /// and mutates that node to set widget-specific information.
    ///
    /// **Note:** A new node is created each time this method is called.
    /// Changes to the node don't persist between accessibility passes, and must instead
    /// be re-applied by this method every time it's called.
    fn accessibility(
        &mut self,
        ctx: &mut AccessCtx<'_>,
        props: &PropertiesRef<'_>,
        node: &mut Node,
    );

    /// Returns ids of this widget's children.
    ///
    /// Leaf widgets return an empty array. Container widgets return ids of
    /// their children.
    ///
    /// The list returned by this method is considered the "canonical" list of children
    /// by Masonry.
    ///
    /// This method has some validity invariants. A widget's children list must be
    /// consistent. If children are added or removed, the parent widget should call
    /// `children_changed` on one of the Ctx parameters. Container widgets are
    /// responsible for visiting all their children during `layout` and `register_children`.
    fn children_ids(&self) -> ChildrenIds;

    /// Return `Some(self)` if the widget also implements [`Subwindow`].
    ///
    /// Default implementation returns `None`.
    fn as_subwindow(&mut self) -> Option<&mut dyn Subwindow> {
        None
    }

    /// Whether this widget gets pointer events and [hovered] status. True by default.
    ///
    /// If false, the widget will be treated as "transparent" for the pointer, meaning
    /// that the pointer will be considered as hovering whatever is under this widget.
    ///
    /// **Note:** The value returned by this method is cached at widget creation and can't be changed.
    ///
    /// [hovered]: crate::doc::masonry_concepts#hovered
    fn accepts_pointer_interaction(&self) -> bool {
        true
    }

    /// Whether this widget should get a tooltip when hovered long enough. False by default.
    ///
    /// If this value is true, when the user stops a pointer on this widget, Masonry will set
    /// a timer. When this timer fires, Masonry will send an [`Update::TooltipRequested`] event to this widget.
    ///
    /// Handling that event is left to this widget. Masonry will not automatically create a
    /// tooltip when this happens. See [`UpdateCtx::create_subwindow`] for how to do that.
    ///
    /// **Note:** The value returned by this method is cached at widget creation and can't be changed.
    ///
    /// [hovered]: crate::doc::masonry_concepts#hovered
    fn accepts_tooltips(&self) -> bool {
        false
    }

    /// Whether this widget lets pointer events and [hovered] status reach its children. True by default.
    ///
    /// If false, the widget's children will not get pointer events, affect the cursor icon,
    /// will never be considered hovered, and will not be returned by `find_widget_under_pointer`.
    ///
    /// **Note:** The value returned by this method is cached at widget creation and can't be changed.
    ///
    /// [hovered]: crate::doc::masonry_concepts#hovered
    fn propagates_pointer_interaction(&self) -> bool {
        true
    }

    /// Whether this widget gets [text focus]. False by default.
    ///
    /// If true, pressing Tab can focus this widget.
    ///
    /// **Note:** The value returned by this method is cached at widget creation and can't be changed.
    ///
    /// [text focus]: crate::doc::masonry_concepts#text-focus
    fn accepts_focus(&self) -> bool {
        false
    }

    /// Whether this widget gets IME events. False by default.
    ///
    /// If true, focusing this widget will start an IME session.
    ///
    /// **Note:** The value returned by this method is cached at widget creation and can't be changed.
    fn accepts_text_input(&self) -> bool {
        false
    }

    // TODO - Write a generic default implementation once
    // `const std::any::type_name` is stable.
    // See https://github.com/rust-lang/rust/issues/63084
    /// Returns a span for tracing.
    ///
    /// As methods recurse through the widget tree, trace spans are added for each child
    /// widget visited, and popped when control flow goes back to the parent. This method
    /// returns a static span (that you can use to filter traces and logs).
    fn make_trace_span(&self, id: WidgetId) -> Span {
        trace_span!("Widget", r#type = self.short_type_name(), id = id.trace())
    }

    /// Returns a small string representing important info about this widget instance.
    ///
    /// When using [`WidgetRef`]'s [`Debug`] implementation, widgets
    /// will be displayed as a tree of values. Widgets which return a non-null value in
    /// `get_debug_text` will appear with that text next to their type name. This can
    /// be eg a label's text, or whether a checkbox is checked.
    fn get_debug_text(&self) -> Option<String> {
        None
    }

    /// Returns the cursor icon for this widget.
    ///
    /// This will be called when the mouse moves or [`request_cursor_icon_change`](crate::core::MutateCtx::request_cursor_icon_change) is called.
    ///
    /// **pos** - the mouse position in the window's coordinate space,
    /// e.g. `(0,0)` is the top-left corner of the window.
    fn get_cursor(&self, ctx: &QueryCtx<'_>, pos: Point) -> CursorIcon {
        CursorIcon::Default
    }

    // --- Auto-generated implementations ---

    /// Returns the first innermost widget composed by this (including `self`), that contains/intersects with `pos` and accepts pointer interaction, if any.
    ///
    /// In case of overlapping children, the last child as determined by [`Widget::children_ids`] is chosen. No widget is
    /// returned if `pos` is outside the widget's clip path.
    ///
    /// Has a default implementation that can be overridden to search children more efficiently.
    /// Custom implementations must uphold the conditions outlined above.
    ///
    /// **pos** - the position is in the window's coordinate space,
    /// e.g. `(0,0)` is the top-left corner of the window.
    fn find_widget_under_pointer<'c>(
        &'c self,
        ctx: QueryCtx<'c>,
        pos: Point,
    ) -> Option<WidgetRef<'c, dyn Widget>> {
        find_widget_under_pointer(self.as_dyn(), ctx, pos)
    }

    /// Gets the (verbose) type name of the widget for debugging purposes.
    /// You should not override this method.
    #[doc(hidden)]
    fn type_name(&self) -> &'static str {
        std::any::type_name::<Self>()
    }

    /// Gets the (abridged) type name of the widget for debugging purposes.
    /// You should not override this method.
    #[doc(hidden)]
    fn short_type_name(&self) -> &'static str {
        let name = self.type_name();
        name.split('<')
            .next()
            .unwrap_or(name)
            .split("::")
            .last()
            .unwrap_or(name)
    }

    /// Convenience method to wrap this in a [`NewWidget`].
    fn prepare(self) -> NewWidget<Self>
    where
        Self: Sized,
    {
        NewWidget::new(self)
    }
}

/// See [`Widget::find_widget_under_pointer`] for more details.
pub fn find_widget_under_pointer<'c>(
    widget: &'c dyn Widget,
    ctx: QueryCtx<'c>,
    pos: Point,
) -> Option<WidgetRef<'c, dyn Widget>> {
    // TEST EARLY-EXITS
    if !ctx.bounding_box().contains(pos) {
        return None;
    }
    if ctx.is_stashed() {
        return None;
    }

    let local_pos = ctx.to_local(pos);

    if let Some(clip) = ctx.clip_path()
        && !clip.contains(local_pos)
    {
        return None;
    }

    // TEST CHILDREN
    if ctx.propagates_pointer_interaction() {
        // Assumes `Self::children_ids` is in increasing "z-order", picking the last child in case
        // of overlapping children.
        for child_id in widget.children_ids().iter().rev() {
            let child_ref = ctx.get(*child_id);
            if let Some(child) = child_ref
                .widget
                .find_widget_under_pointer(child_ref.ctx, pos)
            {
                return Some(child);
            }
        }
    }

    // TEST THE CURRENT WIDGET
    if !ctx.accepts_pointer_interaction() {
        return None;
    }
    if !ctx.border_box().contains(local_pos) {
        return None;
    }
    Some(WidgetRef { widget, ctx })
}

/// Marker trait for widgets whose parents can get a raw mutable reference to them.
///
/// "Raw mut" means using a mutable reference (eg `&mut MyWidget`) to the data
/// structure, instead of going through the [`Widget`] trait methods
/// (`on_text_event`, `update`, `layout`, etc) or through `WidgetMut`.
///
/// A parent widget can use [`EventCtx::get_raw_mut`], [`UpdateCtx::get_raw_mut`],
/// or [`LayoutCtx::get_raw_mut`] to directly access a child widget. In that case,
/// these methods return both a mutable reference to the child widget and a new
/// [`RawCtx`](crate::core::RawCtx) context scoped to the child. The parent is
/// responsible for calling the context methods (eg `request_layout`,
/// `request_accessibility_update`) for the child.
///
/// Widgets implementing `AllowRawMut` are usually private widgets used as an
/// internal implementation detail of public widgets.
pub trait AllowRawMut: Widget {}

impl WidgetId {
    /// Allocates a new, unique `WidgetId`.
    ///
    /// All widgets are assigned ids automatically; you should only create
    /// an explicit id if you need to know it ahead of time, for instance
    /// if you want two sibling widgets to know each others' ids.
    ///
    /// You must ensure that a given `WidgetId` is only ever used for one
    /// widget at a time.
    pub(crate) fn next() -> Self {
        static WIDGET_ID_COUNTER: AtomicU64 = AtomicU64::new(1);
        let id = WIDGET_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
        Self(id.try_into().unwrap())
    }

    /// Returns the integer value of the `WidgetId`.
    pub fn to_raw(self) -> u64 {
        self.0.into()
    }
}

impl From<WidgetId> for u64 {
    fn from(id: WidgetId) -> Self {
        id.0.into()
    }
}

impl From<WidgetId> for accesskit::NodeId {
    fn from(id: WidgetId) -> Self {
        Self(id.0.into())
    }
}

impl Display for WidgetId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "#{}", self.0)
    }
}

impl PartialEq<accesskit::NodeId> for WidgetId {
    fn eq(&self, other: &accesskit::NodeId) -> bool {
        self.to_raw() == other.0
    }
}

impl PartialEq<WidgetId> for accesskit::NodeId {
    fn eq(&self, other: &WidgetId) -> bool {
        self.0 == other.to_raw()
    }
}

/// Trait implemented by collection widgets.
///
/// It provides a standard set of functions to manage children.
pub trait CollectionWidget<Params>: Widget {
    /// Returns the number of children.
    fn len(&self) -> usize;

    /// Returns `true` if there are no children.
    fn is_empty(&self) -> bool;

    /// Returns a mutable reference to the child widget at the given index.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is out of bounds.
    fn get_mut<'t>(this: &'t mut WidgetMut<'_, Self>, idx: usize) -> WidgetMut<'t, dyn Widget>;

    /// Appends a child widget to the collection.
    fn add(
        this: &mut WidgetMut<'_, Self>,
        child: NewWidget<impl Widget + ?Sized>,
        params: impl Into<Params>,
    );

    /// Inserts a child widget at the given index.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is larger than the number of children.
    fn insert(
        this: &mut WidgetMut<'_, Self>,
        idx: usize,
        child: NewWidget<impl Widget + ?Sized>,
        params: impl Into<Params>,
    );

    /// Replaces the child widget at the given index.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is out of bounds.
    fn set(
        this: &mut WidgetMut<'_, Self>,
        idx: usize,
        child: NewWidget<impl Widget + ?Sized>,
        params: impl Into<Params>,
    );

    /// Sets the child params at the given index.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is out of bounds.
    fn set_params(this: &mut WidgetMut<'_, Self>, idx: usize, params: impl Into<Params>);

    /// Swaps the index of two children.
    ///
    /// # Panics
    ///
    /// Panics if `a` or `b` are out of bounds.
    fn swap(this: &mut WidgetMut<'_, Self>, a: usize, b: usize);

    /// Removes the child at the given index.
    ///
    /// # Panics
    ///
    /// Panics if `idx` is out of bounds.
    fn remove(this: &mut WidgetMut<'_, Self>, idx: usize);

    /// Removes all children.
    fn clear(this: &mut WidgetMut<'_, Self>);
}