teksilo_core/widget/paint_context.rs
1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4use teksilo_canvas::{Rect, Size};
5
6use crate::arena::WidgetArena;
7use crate::widget_id::WidgetId;
8
9/// Context available during painting.
10pub struct PaintContext<'a> {
11 pub theme: &'a crate::styles::Theme,
12 /// Accumulated (quantized) scale of the transform scopes enclosing
13 /// this widget — `1.0` outside any scale transform, the zoom-derived
14 /// raster ladder value inside a `SceneView` / `Scale` wrapper. This
15 /// is the ambient text raster scale the walker has already set on
16 /// the shared `TextBackend`; widgets normally don't need it (text
17 /// drawn via `Canvas::draw_text` / `draw_paragraph` picks it up
18 /// automatically), but resolution-dependent custom paint can read
19 /// it to densify its own raster content. NOT the HiDPI device scale
20 /// — that lives on the renderer/text-service.
21 pub scale_factor: f32,
22 /// Combined user×OS text-scale factor (`1.0` = 100 %) — the *logical*
23 /// accessibility magnification, distinct from the raster `scale_factor`
24 /// above. Widgets that paint text via `Theme.typography` already scale
25 /// through the effective theme; this is for paint paths that size text
26 /// from another source (e.g. a scene `TextItem` that opts in). `1.0` when
27 /// no scale is active.
28 pub text_scale: f32,
29 /// Active layout direction. Used by widgets that have to resolve
30 /// Leading/Trailing semantics into geometric Left/Right at paint
31 /// time (e.g. attached-side shadow suppression on a popover that
32 /// opened off the trailing edge of its anchor).
33 pub layout_direction: crate::environment::LayoutDirection,
34 /// Whether the widget being painted is effectively enabled —
35 /// `false` iff this node or any ancestor has its arena-level
36 /// `enabled_state` resolved to `false`. Computed once per node by
37 /// the paint walker (start `true` at root, AND with each node's
38 /// `enabled_state` value as the walker descends).
39 ///
40 /// Leaf widgets that paint role-derived colors
41 /// ([`crate::color_prop::ColorProp::TextRole`] and the dynamic
42 /// variants) consult this to substitute `TextRole::Disabled`
43 /// automatically — the single hook that makes any descendant of a
44 /// disabled subtree dim without the composite parent doing
45 /// per-color bookkeeping. Static and bound color props are not
46 /// substituted (caller's literal wins).
47 pub effective_enabled: bool,
48 // TODO: Wire from platform accessibility settings (winit doesn't expose these yet)
49 pub prefers_high_contrast: bool,
50 pub prefers_reduced_motion: bool,
51 pub prefers_large_text: bool,
52 /// Whether the host window is currently active (`focused AND not
53 /// occluded`). Widgets that change appearance when the window loses focus
54 /// read this directly in `paint()` — the selection band in
55 /// `TableView`/`TreeTableView` desaturates, text engines swap their
56 /// selection colour, custom paint can dim. A window-active flip triggers a
57 /// global repaint ([`WidgetArena::mark_all_needs_paint_only`]), so no
58 /// per-widget signal binding is required to keep a paint-time read correct.
59 /// `true` in headless test contexts.
60 pub window_active: bool,
61 /// The accumulated clip rectangle this widget is painted within — the
62 /// intersection of every `clips_children` ancestor's bounds (a `ScrollArea`
63 /// viewport, a `MaxSize`, …), in the same screen space as the widget's own
64 /// `bounds`. `None` when no ancestor clips (the widget can paint anywhere).
65 ///
66 /// The paint walker already computes this to skip fully-offscreen subtrees;
67 /// surfacing it lets a widget that is laid out larger than its visible slot
68 /// — an editor at full document height inside an outer `ScrollArea`
69 /// ("dubious mode") — window its own expensive work to `clip ∩ bounds`
70 /// instead of processing the whole document. Correct under arbitrary
71 /// nesting, since it is the intersection of *all* clipping ancestors.
72 pub clip_bounds: Option<Rect>,
73}
74
75/// Read-only view of the widget tree's geometry passed to
76/// [`Widget::after_paint`](super::Widget::after_paint). Wraps a borrow of
77/// the arena so a parent widget can read the layout-resolved bounds of
78/// any descendant (typically by ids it memoised during `build`) once
79/// their paint pass has committed.
80///
81/// The view exposes only immutable queries; constructing one is
82/// crate-internal so the contract stays narrow.
83pub struct WidgetTreeView<'a> {
84 arena: &'a WidgetArena,
85 /// Screen rects of every interactive (non-fading) overlay this
86 /// frame — see [`overlay_rects`](Self::overlay_rects).
87 overlay_rects: &'a [Rect],
88}
89
90impl<'a> WidgetTreeView<'a> {
91 pub(crate) fn new(arena: &'a WidgetArena, overlay_rects: &'a [Rect]) -> Self {
92 Self {
93 arena,
94 overlay_rects,
95 }
96 }
97
98 /// Logical-pixel bounds of `id` after the most recent layout pass.
99 /// Returns `Rect::ZERO` for unknown ids.
100 pub fn bounds(&self, id: WidgetId) -> Rect {
101 self.arena.bounds(id)
102 }
103
104 /// Direct arena children of `id`, in order.
105 pub fn children(&self, id: WidgetId) -> &[WidgetId] {
106 self.arena.children(id)
107 }
108
109 /// Whether `id` is a gesture dead-zone boundary — see
110 /// [`WidgetNode::gesture_dead_zone`](crate::arena::WidgetNode::gesture_dead_zone)
111 /// and the `DeadZone` wrapper widget. Lets a parent classify its own
112 /// subtree from `after_paint`: `TitleBar` uses it to carve interactive
113 /// controls out of the OS caption region it publishes.
114 pub fn is_gesture_dead_zone(&self, id: WidgetId) -> bool {
115 self.arena
116 .get(id)
117 .map(|n| n.gesture_dead_zone)
118 .unwrap_or(false)
119 }
120
121 /// Whether `id` is active — i.e. not parked dormant by a `Switcher` or a
122 /// `visible_when` gate. A dormant node's [`bounds`](Self::bounds) are
123 /// stale, so callers walking a subtree must skip it.
124 pub fn is_active(&self, id: WidgetId) -> bool {
125 self.arena.is_active(id)
126 }
127
128 /// Screen rects of every overlay that is interactive this frame —
129 /// open and not fading out, the predicate the overlay manager's own
130 /// pointer routing uses. Overlay content floats *above* the widget
131 /// that anchors it, so an aggregator publishing geometry to the OS
132 /// must treat these rects as covering its own: `TitleBar` subtracts
133 /// them from the caption it publishes, or a revealed hamburger
134 /// `MenuBar` (any overlay over the title bar) would hit-test as
135 /// `HTCAPTION` on Windows and the OS would swallow its clicks as a
136 /// window drag.
137 pub fn overlay_rects(&self) -> &[Rect] {
138 self.overlay_rects
139 }
140}
141
142/// Placement of a child widget during layout.
143#[derive(Debug, Clone, Copy)]
144pub struct WidgetPlacement {
145 pub id: WidgetId,
146 pub origin: teksilo_canvas::Point,
147 pub size: Size,
148}