Skip to main content

agg_gui/
widget.rs

1//! Widget trait, tree traversal, and the top-level [`App`] struct.
2//!
3//! # Coordinate system
4//!
5//! Widget bounds are expressed in **parent-local** first-quadrant (Y-up)
6//! coordinates. A widget at `bounds.x = 10, bounds.y = 20` is drawn 10 units
7//! right and 20 units up from its parent's bottom-left corner.
8//!
9//! OS/browser mouse events arrive in Y-down screen coordinates. The single
10//! conversion `y_up = viewport_height - y_down` happens inside
11//! [`App::on_mouse_move`] / [`App::on_mouse_down`] / [`App::on_mouse_up`].
12//! All widget code sees Y-up coordinates only.
13//!
14//! # Tree traversal
15//!
16//! Paint: root → leaves (children painted on top of parents).
17//! Hit test: root → leaves (deepest child under cursor wins).
18//! Event dispatch: leaf → root (events bubble up; any widget can consume).
19
20use crate::draw_ctx::DrawCtx;
21use crate::event::{Event, EventResult, Key, Modifiers, MouseButton};
22use crate::geometry::{Point, Rect, Size};
23use crate::layout_props::{HAnchor, Insets, VAnchor, WidgetBase};
24
25// ---------------------------------------------------------------------------
26// Widget trait
27// ---------------------------------------------------------------------------
28
29/// Every visible element in the UI is a widget.
30///
31/// Implementors handle their own painting and event handling. The framework
32/// takes care of tree traversal, coordinate translation, and focus management.
33pub trait Widget {
34    /// Bounding rectangle in **parent-local** Y-up coordinates.
35    fn bounds(&self) -> Rect;
36
37    /// Set the bounding rectangle. Called by the parent during layout.
38    fn set_bounds(&mut self, bounds: Rect);
39
40    /// Immutable access to child widgets.
41    fn children(&self) -> &[Box<dyn Widget>];
42
43    /// Mutable access to child widgets (required for event dispatch + layout).
44    fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>>;
45
46    /// Compute desired size given available space, and update internal layout.
47    ///
48    /// The parent passes the space it can offer; the widget returns the size it
49    /// actually wants to occupy. The parent uses the returned size to set this
50    /// widget's bounds before calling `layout` on the next sibling.
51    fn layout(&mut self, available: Size) -> Size;
52
53    /// Paint this widget's own content into `ctx`.
54    ///
55    /// The framework has already translated `ctx` so that `(0, 0)` is this
56    /// widget's bottom-left corner. **Do not paint children here** — the
57    /// framework recurses into them automatically after `paint` returns.
58    ///
59    /// `ctx` is a `&mut dyn DrawCtx`; the concrete type is either a software
60    /// `GfxCtx` (back-buffer path) or a `GlGfxCtx` (hardware GL path).
61    fn paint(&mut self, ctx: &mut dyn DrawCtx);
62
63    /// Return `true` if `local_pos` (in this widget's local coordinates) falls
64    /// inside this widget's interactive area. Default: axis-aligned rect test.
65    fn hit_test(&self, local_pos: Point) -> bool {
66        let b = self.bounds();
67        local_pos.x >= 0.0
68            && local_pos.x <= b.width
69            && local_pos.y >= 0.0
70            && local_pos.y <= b.height
71    }
72
73    /// When `true`, `hit_test_subtree` stops recursing into this widget's
74    /// children and returns this widget as the hit target.  Used for floating
75    /// overlays (e.g. a scrollbar painted above its content) that must claim
76    /// the pointer before children that happen to share the same pixels.
77    /// Default: `false`.
78    fn claims_pointer_exclusively(&self, _local_pos: Point) -> bool {
79        false
80    }
81
82    /// Return true when `local_pos` hits an app-level overlay owned by this
83    /// widget. Unlike normal hit testing, ancestors may be missed because the
84    /// overlay is painted outside their bounds.
85    fn hit_test_global_overlay(&self, _local_pos: Point) -> bool {
86        false
87    }
88
89    /// Whether this widget currently owns an app-modal interaction layer.
90    ///
91    /// When true anywhere in the tree, [`App`](crate::App) routes pointer and
92    /// key events to that modal subtree before normal hit testing so content
93    /// underneath the modal backdrop cannot be interacted with.
94    fn has_active_modal(&self) -> bool {
95        false
96    }
97
98    /// Handle an event. The event's positions are already in **local** Y-up
99    /// coordinates. Return [`EventResult::Consumed`] to stop bubbling.
100    ///
101    /// # Invalidation contract
102    ///
103    /// If your handler mutates state that affects the next paint (hover
104    /// index, focus, button-pressed bool, animation phase, ...), call
105    /// [`crate::animation::request_draw`] from inside the handler.  That
106    /// bumps the invalidation epoch, which `dispatch_event` reads to mark
107    /// retained ancestor backbuffers dirty — without it, the cached
108    /// bitmap composites unchanged and your state mutation is invisible
109    /// until something else dirties the cache.
110    ///
111    /// Returning `Consumed` *also* dirties the ancestor path automatically,
112    /// so a consumed click handler that calls `request_draw` is belt-and-
113    /// suspenders.  But `MouseMove` handlers that return `Ignored` (the
114    /// usual case for hover-tracking) **only** invalidate via
115    /// `request_draw`'s epoch bump.  Forgetting it produces "hover only
116    /// works the first time after I drag the window resize edge" bugs.
117    ///
118    /// `request_draw_without_invalidation` is for the rare cases where
119    /// the visual change is in an app-overlay or a position-only
120    /// composite — see its rustdoc.
121    fn on_event(&mut self, event: &Event) -> EventResult;
122
123    /// Handle a key that was not consumed by the focused widget path.
124    ///
125    /// This is used for window/menu accelerators: focused controls get first
126    /// chance at the key, then visible widgets in paint order may claim it.
127    fn on_unconsumed_key(&mut self, _key: &Key, _modifiers: Modifiers) -> EventResult {
128        EventResult::Ignored
129    }
130
131    /// Whether this widget can receive keyboard focus. Default: false.
132    fn is_focusable(&self) -> bool {
133        false
134    }
135
136    /// Stable identifier for the programmatic focus channel
137    /// ([`crate::focus::request_focus`]).
138    ///
139    /// App code can't reach the [`App`](crate::widget::App)'s private focus
140    /// path to focus a widget the moment it appears (e.g. a search field
141    /// that should grab the keyboard when its overlay opens). A widget that
142    /// returns `Some(id)` here can be focused by calling
143    /// [`crate::focus::request_focus(id)`](crate::focus::request_focus); the
144    /// `App` services the request on its next `layout`, moving focus to the
145    /// matching focusable widget (which dispatches `FocusGained` and raises
146    /// the on-screen keyboard for text inputs). Default: `None`.
147    fn focus_id(&self) -> Option<crate::focus::FocusId> {
148        None
149    }
150
151    /// A static name for this widget type, used by the inspector. Default: "Widget".
152    fn type_name(&self) -> &'static str {
153        "Widget"
154    }
155
156    /// Optional human-readable identifier for this widget instance.
157    ///
158    /// Distinct from [`type_name`] (which is per-type and constant):
159    /// `id` lets external code look up a specific *instance* — used
160    /// today by the demo's z-order persistence to match a saved title
161    /// against a live `Window` in the canvas `Stack`.  Default
162    /// implementation returns `None`; widgets that want to be
163    /// identifiable (e.g. `Window` returning its title) override.
164    fn id(&self) -> Option<&str> {
165        None
166    }
167
168    /// Return `false` to suppress painting this widget **and all its children**.
169    /// The widget's own `paint()` will not be called.  Default: `true`.
170    fn is_visible(&self) -> bool {
171        true
172    }
173
174    /// Return type-specific properties for the inspector properties pane.
175    ///
176    /// Each entry is `(name, display_value)`.  The default returns an empty
177    /// list; widgets override this to expose their state to the inspector.
178    fn properties(&self) -> Vec<(&'static str, String)> {
179        vec![]
180    }
181
182    /// `true` when this widget accepts free-form character input (typing
183    /// arbitrary letters, numbers, punctuation). Used by the on-screen
184    /// software keyboard (`crate::widgets::on_screen_keyboard`) to decide
185    /// whether to slide up when this widget gains focus.
186    ///
187    /// Default is `false`. `TextField` and `TextArea` override to `true`.
188    /// `DragValue` and similar numeric editors should override to `true`
189    /// only when they are in their full-text-edit mode; otherwise the
190    /// keyboard would appear for transient drag interactions.
191    ///
192    /// This is independent of [`is_focusable`](Self::is_focusable) — a
193    /// `Button` is focusable but doesn't accept typed text.
194    fn accepts_text_input(&self) -> bool {
195        false
196    }
197
198    /// Current text contents of this widget if it is text-bearing.
199    /// Used by the on-screen software keyboard to apply the
200    /// sentence-start auto-capitalize heuristic: an empty field (or one
201    /// ending in `.`, `!`, `?`, newline) opens the keyboard with Shift
202    /// active.
203    ///
204    /// Default is `None`. `TextField` and `TextArea` override to return
205    /// their current text. Callers that only need to know *whether* the
206    /// widget accepts text input should use
207    /// [`accepts_text_input`](Self::accepts_text_input).
208    fn text_input_value(&self) -> Option<String> {
209        None
210    }
211
212    /// Preferred keyboard input mode for this widget — used by the
213    /// on-screen software keyboard to pick the initial layer when this
214    /// widget gains focus.  Default is
215    /// [`KeyboardInputMode::Text`](crate::widgets::on_screen_keyboard::KeyboardInputMode::Text);
216    /// numeric fields override to
217    /// [`Numeric`](crate::widgets::on_screen_keyboard::KeyboardInputMode::Numeric)
218    /// so the digit pad slides up instead of the letter row.
219    ///
220    /// Only consulted when [`accepts_text_input`](Self::accepts_text_input)
221    /// also returns `true`.
222    fn text_input_mode(&self) -> crate::widgets::on_screen_keyboard::KeyboardInputMode {
223        crate::widgets::on_screen_keyboard::KeyboardInputMode::Text
224    }
225
226    /// Try to lift this widget's visible content upward by `amount`
227    /// pixels.  Used by the on-screen-keyboard auto-scroll so a
228    /// focused text field doesn't end up hidden behind the keyboard
229    /// panel: the App walks UP the focus path and asks each ancestor
230    /// to absorb some of the deficit.
231    ///
232    /// Scrolling containers (notably
233    /// [`ScrollView`](crate::widgets::ScrollView)) override this and
234    /// increase their vertical scroll offset by up to `amount`,
235    /// returning how much they actually applied (clamped to their
236    /// remaining slack).  Negative `amount` reverses the operation
237    /// (used to restore scroll when focus leaves a text-input).
238    /// Default returns `0.0` — non-scrolling widgets contribute
239    /// nothing.
240    fn try_scroll_to_lift(&mut self, _amount: f64) -> f64 {
241        0.0
242    }
243
244    /// If this widget is text-bearing (e.g. `Label`), update its foreground
245    /// colour.  Default is a no-op.  Composite widgets call this on their
246    /// children to retint labels without rebuilding them — used by `Button`
247    /// when toggling between active (white text on accent) and inactive
248    /// (theme text on subtle bg) appearances.
249    fn set_label_color(&mut self, _color: crate::color::Color) {}
250
251    /// If this widget is text-bearing (e.g. `Label`), update its
252    /// displayed text.  Default is a no-op.  Composite widgets that
253    /// own a `Label` child use this to push live values (e.g. an FPS
254    /// counter) into the child without bypassing the standard
255    /// backbuffered glyph cache — calling this on a `Label` only
256    /// invalidates the cache when the text actually changed.
257    fn set_label_text(&mut self, _text: &str) {}
258
259    /// Opt-in reflection accessor for the inspector's typed property editors.
260    ///
261    /// Widgets that derive [`bevy_reflect::Reflect`] (via the `reflect`
262    /// cargo feature) override this to return `Some(self)` so the inspector
263    /// can walk their fields with type information — boolean toggles,
264    /// numeric sliders, color pickers, enum dropdowns — instead of falling
265    /// back to the read-only string [`properties`](Self::properties) list.
266    ///
267    /// Default returns `None`; the inspector then uses the string list.
268    /// Available only with the `reflect` feature so consumers without it
269    /// don't pay the dependency cost.
270    #[cfg(feature = "reflect")]
271    fn as_reflect(&self) -> Option<&dyn bevy_reflect::Reflect> {
272        None
273    }
274
275    /// Mutable counterpart of [`as_reflect`](Self::as_reflect).  Used by the
276    /// inspector to write edits back into the live widget.
277    #[cfg(feature = "reflect")]
278    fn as_reflect_mut(&mut self) -> Option<&mut dyn bevy_reflect::Reflect> {
279        None
280    }
281
282    /// Whether this widget renders into its own offscreen buffer before
283    /// compositing into the parent.
284    ///
285    /// When `true`, `paint_subtree` wraps the widget (and all its descendants)
286    /// in `ctx.push_layer` / `ctx.pop_layer`.  The widget and its children draw
287    /// into a fresh transparent framebuffer; when complete, the buffer is
288    /// SrcOver-composited back into the parent render target.  This enables
289    /// per-widget alpha compositing, caching, and isolation.
290    ///
291    /// Default: `false` (pass-through rendering).
292    fn has_backbuffer(&self) -> bool {
293        false
294    }
295
296    /// Request that this widget subtree be painted into a transient
297    /// transparent compositing layer before being blended into its parent.
298    ///
299    /// Renderers that do not implement real layers ignore this hook. The
300    /// method is mutable so widgets can advance visibility tweens at the
301    /// point where the traversal knows the layer will be painted.
302    fn compositing_layer(&mut self) -> Option<CompositingLayer> {
303        None
304    }
305
306    /// Unified widget-owned backbuffer request.
307    fn backbuffer_spec(&mut self) -> BackbufferSpec {
308        let mode = self.backbuffer_mode();
309        if self.backbuffer_cache_mut().is_some() {
310            BackbufferSpec {
311                kind: match mode {
312                    BackbufferMode::Rgba => BackbufferKind::SoftwareRgba,
313                    BackbufferMode::LcdCoverage => BackbufferKind::SoftwareLcd,
314                },
315                cached: true,
316                alpha: 1.0,
317                outsets: Insets::ZERO,
318                rounded_clip: None,
319            }
320        } else {
321            BackbufferSpec::none()
322        }
323    }
324
325    /// Mutable retained backbuffer state for widgets that request a
326    /// [`BackbufferSpec`] other than [`BackbufferKind::None`].
327    fn backbuffer_state_mut(&mut self) -> Option<&mut BackbufferState> {
328        None
329    }
330
331    /// Mark this widget's own retained surface dirty, if it owns one.
332    ///
333    /// Invalidates *both* the retained-layer [`BackbufferState`] and the
334    /// per-widget bitmap [`BackbufferCache`].  Inspector edits that mutate
335    /// a widget's reflected props bypass setters that normally invalidate
336    /// the cache (e.g. `Label::set_text`); calling `mark_dirty` after an
337    /// edit restores correct re-raster on the next frame.
338    fn mark_dirty(&mut self) {
339        if let Some(state) = self.backbuffer_state_mut() {
340            state.invalidate();
341        }
342        if let Some(cache) = self.backbuffer_cache_mut() {
343            cache.invalidate();
344        }
345    }
346
347    /// Opt into per-widget CPU bitmap caching with a dirty flag.
348    ///
349    /// Widgets that return `Some(&mut cache)` get their paint +
350    /// children cached as a `Vec<u8>` of RGBA8 pixels.  `paint_subtree`
351    /// re-rasterises via AGG only when `cache.dirty` is true; otherwise
352    /// it blits the existing bitmap.  GL backends key their texture
353    /// cache on the `Arc`'s pointer identity so the uploaded GPU
354    /// texture is also reused across frames.
355    ///
356    /// The widget is responsible for calling `cache.invalidate()` (or
357    /// setting `cache.dirty = true`) from any mutation that could
358    /// change the rendered output — text/color setters, focus/hover
359    /// state changes, layout size changes, etc.  The framework clears
360    /// the flag after a successful re-raster.
361    ///
362    /// Default: `None` (no caching — paint every frame directly).
363    fn backbuffer_cache_mut(&mut self) -> Option<&mut BackbufferCache> {
364        None
365    }
366
367    /// Storage format for this widget's backbuffer.  Ignored unless
368    /// [`backbuffer_cache_mut`] returns `Some`.  Default
369    /// [`BackbufferMode::Rgba`] — correct for any widget.
370    /// Opt into [`BackbufferMode::LcdCoverage`] only when the widget
371    /// paints opaque content covering its full bounds.
372    fn backbuffer_mode(&self) -> BackbufferMode {
373        BackbufferMode::Rgba
374    }
375
376    /// Whether the inspector should recurse into this widget's children.
377    ///
378    /// Returns `false` for widgets that are part of the inspector infrastructure
379    /// (e.g. the inspector's own `TreeView`) to prevent the inspector from
380    /// showing itself recursively, which would grow the node list every frame.
381    ///
382    /// The widget itself is still included in the inspector snapshot — only
383    /// its subtree is suppressed.
384    fn contributes_children_to_inspector(&self) -> bool {
385        true
386    }
387
388    /// Return `false` to hide this widget (and its subtree) from the inspector
389    /// node snapshot entirely.  Intended for zero-size utility widgets such
390    /// as layout-time watchers / tickers / invisible composers — they bloat
391    /// the inspector tree without providing user-relevant information and,
392    /// at scale, can make the inspector's per-frame tree rebuild expensive.
393    fn show_in_inspector(&self) -> bool {
394        true
395    }
396
397    /// Per-widget LCD subpixel preference for backbuffered text rendering.
398    ///
399    /// - `Some(true)`  — always raster text with LCD subpixel.
400    /// - `Some(false)` — always use grayscale AA.
401    /// - `None`        — defer to the global `font_settings::lcd_enabled()`.
402    ///
403    /// Only widgets that raster text into an offscreen backbuffer act on
404    /// this flag (today: `Label`).  Defaulting to `None` means every such
405    /// widget follows the global toggle unless the instance explicitly
406    /// opts in or out.
407    fn lcd_preference(&self) -> Option<bool> {
408        None
409    }
410
411    /// Paint decorations that must appear **on top of all children**.
412    ///
413    /// Called by [`paint_subtree`] after all children have been painted.
414    /// The default implementation is a no-op; override in widgets that need
415    /// to draw overlays (e.g. resize handles, drag previews) that must not
416    /// be occluded by child content.
417    fn paint_overlay(&mut self, _ctx: &mut dyn DrawCtx) {}
418
419    /// Called after `paint`, child painting, and optional overlay painting.
420    ///
421    /// Most widgets do not need this. It exists for widgets that intentionally
422    /// open a backend compositing scope in `paint` and must close it after all
423    /// descendants have rendered into that scope.
424    fn finish_paint(&mut self, _ctx: &mut dyn DrawCtx) {}
425
426    /// Paint app-level overlays after the entire widget tree has been painted.
427    ///
428    /// The traversal preserves this widget's local transform but skips ancestor
429    /// clips and retained parent redraw requirements. Use this for portal-style
430    /// UI that draws outside normal bounds while still participating in the
431    /// widget tree's Z order.
432    fn paint_global_overlay(&mut self, _ctx: &mut dyn DrawCtx) {}
433
434    /// Return a clip rectangle (in local coordinates) that constrains all child
435    /// painting.  `paint_subtree` applies this clip before recursing into
436    /// children, then restores the previous clip state afterward.  The clip does
437    /// **not** affect `paint_overlay`, which runs after the clip is removed.
438    ///
439    /// The default clips children to this widget's own bounds, preventing
440    /// overflow.  Override to return a narrower rect (e.g. Window clips to the
441    /// content area below the title bar, or an empty rect when collapsed).
442    fn clip_children_rect(&self) -> Option<(f64, f64, f64, f64)> {
443        let b = self.bounds();
444        Some((0.0, 0.0, b.width, b.height))
445    }
446
447    /// Affine transform applied between this widget and its children during
448    /// inspector traversal.  Mirrors what `paint()` does — e.g. a widget
449    /// that pushes pan/zoom in `paint()` and pops it in `finish_paint()`
450    /// makes the framework recurse into its children with pan/zoom active,
451    /// so `collect_inspector_nodes` must apply the same transform when
452    /// accumulating descendant screen bounds.  Without this hook the
453    /// inspector hover overlay lands at the un-transformed canvas position
454    /// when the widget sits inside a panning/zooming container.
455    ///
456    /// Default: identity (most widgets translate their children only
457    /// through `child.bounds()`, which `collect_inspector_nodes` already
458    /// accumulates separately).
459    fn inspector_child_transform(&self) -> crate::TransAffine {
460        crate::TransAffine::new()
461    }
462
463    // -------------------------------------------------------------------------
464    // Layout properties (universal — every widget carries these)
465    // -------------------------------------------------------------------------
466
467    /// Outer margin around this widget in logical units.
468    ///
469    /// The parent layout reads this to compute spacing and position.
470    /// Default: [`Insets::ZERO`].
471    fn margin(&self) -> Insets {
472        Insets::ZERO
473    }
474
475    /// Inner padding — space the widget reserves between its own bounds and
476    /// its child layout area.  Only container widgets carry padding; leaf
477    /// widgets default to [`Insets::ZERO`].
478    ///
479    /// The inspector reads this to draw a Chrome F12-style padding band on
480    /// the highlighted widget.  Containers that already store padding
481    /// internally (e.g. `FlexColumn::inner_padding`) override this to expose
482    /// it to the inspector.
483    fn padding(&self) -> Insets {
484        Insets::ZERO
485    }
486
487    /// Horizontal anchor: how this widget sizes/positions itself horizontally
488    /// within the slot the parent assigns.
489    /// Default: [`HAnchor::FIT`] (take natural content width).
490    fn h_anchor(&self) -> HAnchor {
491        HAnchor::FIT
492    }
493
494    /// Vertical anchor: how this widget sizes/positions itself vertically
495    /// within the slot the parent assigns.
496    /// Default: [`VAnchor::FIT`] (take natural content height).
497    fn v_anchor(&self) -> VAnchor {
498        VAnchor::FIT
499    }
500
501    /// Minimum size constraint (logical units).
502    ///
503    /// The parent will never assign a slot smaller than this.
504    /// Default: [`Size::ZERO`] (no minimum).
505    fn min_size(&self) -> Size {
506        Size::ZERO
507    }
508
509    /// Maximum size constraint (logical units).
510    ///
511    /// The parent will never assign a slot larger than this.
512    /// Default: [`Size::MAX`] (no maximum).
513    fn max_size(&self) -> Size {
514        Size::MAX
515    }
516
517    /// Direct read access to the widget's embedded [`WidgetBase`].
518    ///
519    /// Returns `Some` for every widget that embeds `WidgetBase` — effectively
520    /// all concrete widgets.  The inspector uses this to read margin, anchors,
521    /// and size constraints without going through the individual trait methods.
522    /// Default returns `None`; widgets that embed `WidgetBase` override.
523    fn widget_base(&self) -> Option<&WidgetBase> {
524        None
525    }
526
527    /// Mutable counterpart of [`widget_base`](Self::widget_base).
528    ///
529    /// The inspector calls this to apply live edits to margin, h_anchor,
530    /// v_anchor, min_size, and max_size from the properties pane.
531    fn widget_base_mut(&mut self) -> Option<&mut WidgetBase> {
532        None
533    }
534
535    /// Whether [`paint_subtree`] should snap this widget's incoming
536    /// translation to the physical pixel grid.
537    ///
538    /// Defaults to the process-wide
539    /// [`pixel_bounds::default_enforce_integer_bounds`](crate::pixel_bounds::default_enforce_integer_bounds)
540    /// flag so the common case — crisp UI text + strokes — works without
541    /// ceremony.  Widgets with a [`WidgetBase`] should delegate to
542    /// `self.base().enforce_integer_bounds` so per-instance overrides take
543    /// effect; widgets that genuinely want sub-pixel positioning (smooth
544    /// scroll markers, zoomed canvases) override to return `false`.
545    ///
546    /// Mirrors MatterCAD's `GuiWidget.EnforceIntegerBounds` accessor.
547    fn enforce_integer_bounds(&self) -> bool {
548        crate::pixel_bounds::default_enforce_integer_bounds()
549    }
550
551    /// Report the minimum height this widget needs to fully render
552    /// its content when given the supplied `available_w` for width.
553    ///
554    /// Used by parents whose layout strategy depends on a true
555    /// content-required height that's independent of the slot they
556    /// might hand the widget — most importantly by
557    /// `Window::with_tight_content_fit(true)` to enforce "no
558    /// clipping, no whitespace" on the height axis even when the
559    /// content tree contains a flex-fill widget that would
560    /// otherwise return `available.height` from `layout`.
561    ///
562    /// Default returns `min_size().height` — accurate for widgets
563    /// whose minimum doesn't depend on width.  Width-sensitive
564    /// widgets (wrapped text containers like `TextArea`, recursive
565    /// containers like `FlexColumn`) override and compute properly.
566    fn measure_min_height(&self, _available_w: f64) -> f64 {
567        self.min_size().height
568    }
569
570    /// Container widgets (notably [`crate::widgets::Stack`]) call this on each
571    /// child at the start of `layout()`.  A widget that returns `true` is
572    /// moved to the END of its parent's child list — painted last, i.e.
573    /// raised to the top of the z-order.  `take_` semantics: the call is
574    /// also expected to **clear** the request so the child doesn't keep
575    /// getting raised every frame.
576    ///
577    /// Default: no raise ever requested.  `Window` overrides to fire on the
578    /// false→true visibility transition (see its `with_visible_cell`), so
579    /// toggling a demo checkbox on in the sidebar automatically pops that
580    /// window to the front.
581    fn take_raise_request(&mut self) -> bool {
582        false
583    }
584
585    // -------------------------------------------------------------------------
586    // Visibility-gated scheduled draw propagation
587    // -------------------------------------------------------------------------
588    //
589    // The host render loop walks the widget tree from the root to decide
590    // whether a visible subtree has a scheduled draw need such as cursor blink.
591    // Ordinary visual invalidation should call `animation::request_draw`, which
592    // also advances the retained-layer invalidation epoch.  `needs_draw` stays
593    // for visibility-gated future/ongoing draw needs: invisible subtrees
594    // (collapsed Window, non-selected TabView tab, off-viewport content)
595    // must NOT keep the app in a continuous draw loop.
596
597    /// Return `true` if this widget, or any visible descendant, has an ongoing
598    /// draw need that should keep the host drawing.
599    ///
600    /// The default walks visible children.  Widgets with their own pending
601    /// state OR that state with the default walk — see `WidgetBase` helpers.
602    fn needs_draw(&self) -> bool {
603        if !self.is_visible() {
604            return false;
605        }
606        self.children().iter().any(|c| c.needs_draw())
607    }
608
609    /// Return the earliest wall-clock instant at which this widget (or any
610    /// visible descendant) wants the next draw.  `None` = no scheduled wake.
611    /// The host loop turns a `Some(t)` into `ControlFlow::WaitUntil(t)` so
612    /// e.g. a cursor blink fires without continuous polling.
613    ///
614    /// Same visibility contract as [`needs_draw`]: hidden subtrees return
615    /// `None` regardless of what the widget *would* ask for if shown.
616    fn next_draw_deadline(&self) -> Option<web_time::Instant> {
617        if !self.is_visible() {
618            return None;
619        }
620        let mut best: Option<web_time::Instant> = None;
621        for c in self.children() {
622            if let Some(t) = c.next_draw_deadline() {
623                best = Some(match best {
624                    Some(b) if b <= t => b,
625                    _ => t,
626                });
627            }
628        }
629        best
630    }
631}
632
633mod app;
634mod backbuffer;
635pub(crate) mod keyboard_scroll;
636mod paint;
637mod tree;
638
639pub use app::App;
640pub use backbuffer::{
641    BackbufferCache, BackbufferKind, BackbufferMode, BackbufferSpec, BackbufferState,
642    CompositingLayer,
643};
644pub use paint::{current_paint_clip, paint_global_overlays, paint_subtree};
645pub use tree::{
646    active_modal_path, apply_widget_base_edit, collect_inspector_nodes, current_mouse_world,
647    current_viewport, dispatch_event, dispatch_event_dyn, dispatch_unconsumed_key,
648    find_widget_by_id, find_widget_by_id_mut, find_widget_by_type, global_overlay_hit_path,
649    hit_test_subtree, mark_subtree_dirty, set_current_mouse_world, set_current_viewport,
650    walk_path_mut, InspectorNode, InspectorOverlay, WidgetBaseEdit, WidgetBaseField,
651};
652#[cfg(feature = "reflect")]
653pub use tree::{apply_inspector_edit, reflect_fields, InspectorEdit};