teksilo-widgets 0.9.0

Widget library for Teksilo — over a hundred widgets and layout primitives, from Button to TreeTableView.
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! RectWidget — a leaf widget that paints a filled and/or stroked rounded rectangle.
//!
//! `RectWidget` has no intrinsic content: it fills whatever space its parent
//! proposes (or reports `0×0` when unconstrained) and draws a fill (solid color
//! or gradient), an optional border (a uniform stroke positioned inside / center
//! / outside, or per-side edge fills for an underline), and an optional corner
//! radius. It is the low-level building block for card backgrounds, focus rings,
//! dividers, underlined fields, and highlight overlays.
//!
//! The fill accepts `impl Into<PaintProp>` — anything `Into<ColorProp>` (a raw
//! `Color`, a theme role such as `SurfaceRole::Hover`, or a `Signal<Color>`) for
//! a solid, plus `PaintProp::Linear` / `Radial` for a gradient. Border color
//! accepts `impl Into<ColorProp>`, so reactive interaction-driven colors require
//! no extra wiring.
//!
//! ```rust
//! # use teksilo_tokens::{Color, CornerRadius};
//! # use teksilo_widgets::primitives::RectWidget;
//! // A pill-shaped accent badge background:
//! let _w = RectWidget::new()
//!     .background(Color::from_rgba(0.2, 0.5, 1.0, 1.0))
//!     .corner_radius(CornerRadius::uniform(12.0));
//! ```

use teksilo_canvas::{Canvas, Paint, Rect, Size, SizeProposal};
use teksilo_tokens::{Color, CornerRadius};

use teksilo_core::accessibility::AccessNodeBuilder;
use teksilo_core::color_prop::ColorProp;
use teksilo_core::paint_prop::PaintProp;
use teksilo_core::signal::Prop;
use teksilo_core::styles::{BorderPosition, BorderSides, apply_border_position};
use teksilo_core::widget::{LayoutContext, PaintContext, Widget};

/// A leaf widget that paints a filled and/or stroked rounded rectangle.
///
/// See the [module documentation](self) for the full feature description.
/// All visual properties accept `impl Into<ColorProp>` (colors/roles/signals) or
/// `impl Into<Prop<f32>>` / `impl Into<Prop<CornerRadius>>` (static or reactive)
/// — so the common "fill with theme surface, border with theme border" setup is
/// just `.background(SurfaceRole::Main).border_color(BorderRole::Default)`.
pub struct RectWidget {
    background: PaintProp,
    border_color: ColorProp,
    border_width: Prop<f32>,
    corner_radius: Prop<CornerRadius>,
    /// `None` = a uniform stroke on all four sides (honouring
    /// `border_position`). `Some(..)` = per-side edge fills (e.g. a
    /// bottom-only underline), drawn with `border_color`.
    border_sides: Prop<Option<BorderSides>>,
    /// Where a uniform stroke sits relative to the rect edge. Default
    /// `Center` matches the SDF stroke's native behaviour.
    border_position: BorderPosition,
}

impl RectWidget {
    /// Create a fully transparent, zero-border rectangle with no corner radius.
    pub fn new() -> Self {
        Self {
            background: PaintProp::Solid(ColorProp::Static(Color::TRANSPARENT)),
            border_color: ColorProp::Static(Color::TRANSPARENT),
            border_width: Prop::Static(0.0),
            corner_radius: Prop::Static(CornerRadius::ZERO),
            border_sides: Prop::Static(None),
            border_position: BorderPosition::Center,
        }
    }

    /// Fill. Accepts `Color`, a theme role (`SurfaceRole`, etc.), a
    /// `Signal<Color>`, or a [`PaintProp`] (e.g. a gradient).
    pub fn background(mut self, paint: impl Into<PaintProp>) -> Self {
        self.background = paint.into();
        self
    }

    /// Per-side border widths (e.g. [`BorderSides::bottom`] for an
    /// underline). When set, overrides the uniform stroke; sides are
    /// drawn as edge fills in `border_color`.
    pub fn border_sides(mut self, sides: impl Into<Prop<Option<BorderSides>>>) -> Self {
        self.border_sides = sides.into();
        self
    }

    /// Where a uniform stroke sits relative to the rect edge
    /// (inside / center / outside). Ignored when `border_sides` is set.
    pub fn border_position(mut self, position: BorderPosition) -> Self {
        self.border_position = position;
        self
    }

    /// Border color. Accepts `Color`, a theme role (`BorderRole`, etc.),
    /// or a `Signal<Color>`.
    pub fn border_color(mut self, color: impl Into<ColorProp>) -> Self {
        self.border_color = color.into();
        self
    }

    /// Stroke width, in logical pixels. Accepts a static value or a reactive `Signal<f32>`.
    pub fn border_width(mut self, width: impl Into<Prop<f32>>) -> Self {
        self.border_width = width.into();
        self
    }

    /// Corner radius for the fill and stroke. Accepts a `CornerRadius` (per-corner
    /// control) or a reactive `Signal<CornerRadius>`.
    pub fn corner_radius(mut self, radius: impl Into<Prop<CornerRadius>>) -> Self {
        self.corner_radius = radius.into();
        self
    }
}

impl Default for RectWidget {
    fn default() -> Self {
        Self::new()
    }
}

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

impl Widget for RectWidget {
    fn build(
        &mut self,
        ctx: &mut teksilo_core::build_context::BuildContext,
    ) -> Vec<teksilo_core::widget_id::WidgetId> {
        let self_id = ctx.self_id();
        let registry = ctx.binding_registry();
        self.background.register_if_bound(
            self_id,
            registry,
            teksilo_core::binding::BindingLevel::RepaintOnly,
        );
        self.border_color.register_if_bound(
            self_id,
            registry,
            teksilo_core::binding::BindingLevel::RepaintOnly,
        );
        self.border_width.register_if_bound(
            self_id,
            registry,
            teksilo_core::binding::BindingLevel::RepaintOnly,
        );
        self.corner_radius.register_if_bound(
            self_id,
            registry,
            teksilo_core::binding::BindingLevel::RepaintOnly,
        );
        self.border_sides.register_if_bound(
            self_id,
            registry,
            teksilo_core::binding::BindingLevel::RepaintOnly,
        );
        Vec::new()
    }

    fn layout_response(
        &self,
        proposal: SizeProposal,
        _ctx: &LayoutContext,
    ) -> teksilo_core::widget::LayoutResponse {
        // RectWidget has no intrinsic content — it accepts whatever space is offered.
        // With an exact proposal it fills the space; with unspecified it reports 0x0.
        proposal.resolve(0.0, 0.0).into()
    }

    fn paint(&self, bounds: Rect, canvas: &mut Canvas, ctx: &PaintContext) {
        let radius = self.corner_radius.get();

        // Fill — a solid color or a gradient. Gradient endpoints are
        // rect-local, so only the size is needed.
        let paint = self.background.resolve(
            ctx.theme,
            ctx.effective_enabled,
            Size::new(bounds.width, bounds.height),
        );
        let skip_fill = matches!(&paint, Paint::Solid(c) if c.a() <= 0.0);
        if !skip_fill {
            canvas.fill_rounded_rect(bounds, radius, paint);
        }

        // Border — per-side edge fills, or a uniform stroke.
        let bc = self.border_color.resolve(ctx.theme, ctx.effective_enabled);
        if bc.a() <= 0.0 {
            return;
        }
        match self.border_sides.get() {
            Some(sides) => paint_border_sides(canvas, bounds, sides, bc),
            None => {
                let bw = self.border_width.get();
                if bw > 0.0 {
                    let stroke_rect = apply_border_position(bounds, bw, self.border_position);
                    canvas.stroke_rounded_rect(stroke_rect, radius, bc, bw);
                }
            }
        }
    }

    fn accessibility(&self, _builder: &mut AccessNodeBuilder) {}
}

/// Draw the non-zero edges of a per-side border as filled rects.
/// `Leading`/`Trailing` map to left/right (LTR); RTL flipping is a
/// follow-up — the common case (a bottom underline) is direction-neutral.
fn paint_border_sides(canvas: &mut Canvas, bounds: Rect, sides: BorderSides, color: Color) {
    if sides.top > 0.0 {
        canvas.fill_rect(
            Rect::new(bounds.x, bounds.y, bounds.width, sides.top),
            color,
        );
    }
    if sides.bottom > 0.0 {
        canvas.fill_rect(
            Rect::new(
                bounds.x,
                bounds.y + bounds.height - sides.bottom,
                bounds.width,
                sides.bottom,
            ),
            color,
        );
    }
    if sides.leading > 0.0 {
        canvas.fill_rect(
            Rect::new(bounds.x, bounds.y, sides.leading, bounds.height),
            color,
        );
    }
    if sides.trailing > 0.0 {
        canvas.fill_rect(
            Rect::new(
                bounds.x + bounds.width - sides.trailing,
                bounds.y,
                sides.trailing,
                bounds.height,
            ),
            color,
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use teksilo_core::signal::Signal;
    use teksilo_core::widget_tree::WidgetTree;

    #[test]
    fn static_background_paints_correctly() {
        let mut tree = WidgetTree::new();
        tree.add(
            RectWidget::new()
                .background(Color::RED)
                .corner_radius(CornerRadius::uniform(4.0)),
        );
        tree.layout(SizeProposal::exact(100.0, 40.0));
        let frame = tree.render();
        assert_eq!(frame.shapes.len(), 1);
        assert_eq!(frame.shapes[0].color, Color::RED.to_array());
    }

    #[test]
    fn accent_role_desaturates_when_window_inactive() {
        use teksilo_tokens::SurfaceRole;

        let theme = teksilo_core::presets::intui::light();
        let accent = theme.colors.accent.to_array();
        let inactive_accent = theme.colors.for_inactive_window().accent.to_array();
        assert_ne!(accent, inactive_accent);

        let mut tree = WidgetTree::new().with_theme(theme);
        tree.add(
            RectWidget::new()
                .background(SurfaceRole::Accent)
                .corner_radius(CornerRadius::uniform(4.0)),
        );
        tree.layout(SizeProposal::exact(100.0, 40.0));

        // Active: the role resolves to the vivid accent.
        let frame = tree.render();
        assert_eq!(frame.shapes.len(), 1);
        assert_eq!(
            frame.shapes[0].color, accent,
            "active window paints the vivid accent"
        );

        // Inactive: the paint walker swaps in the accent-desaturated theme
        // projection, so the *same* SurfaceRole::Accent resolves to the muted
        // colour — the systemic theme-side path that greys every accent control
        // (Toggle, Button, Tab, Segment, Checkbox/Radio, Slider, ProgressBar)
        // with no per-widget code.
        tree.set_window_active(false);
        let frame = tree.render();
        assert_eq!(frame.shapes.len(), 1);
        assert_eq!(
            frame.shapes[0].color, inactive_accent,
            "inactive window desaturates the accent"
        );

        // Reactivate: vivid accent returns.
        tree.set_window_active(true);
        let frame = tree.render();
        assert_eq!(frame.shapes[0].color, accent);
    }

    #[test]
    fn background_reads_from_state() {
        let color = Signal::new(Color::BLUE);
        let mut tree = WidgetTree::new();
        let w = tree.add(
            RectWidget::new()
                .background(color.clone())
                .corner_radius(CornerRadius::uniform(4.0)),
        );
        color.bind_to(
            w,
            tree.binding_registry(),
            teksilo_core::binding::BindingLevel::RepaintOnly,
        );
        tree.layout(SizeProposal::exact(100.0, 40.0));
        let frame = tree.render();
        assert_eq!(frame.shapes[0].color, Color::BLUE.to_array());
    }

    #[test]
    fn underline_draws_a_bottom_decoration() {
        let mut tree = WidgetTree::new();
        tree.add(
            RectWidget::new()
                .border_color(Color::RED)
                .border_sides(Some(BorderSides::bottom(2.0))),
        );
        tree.layout(SizeProposal::exact(100.0, 40.0));
        let frame = tree.render();
        // The bottom underline is an edge-fill decoration in the border color.
        let underline = frame
            .decorations
            .iter()
            .find(|d| d.color == Color::RED.to_array())
            .expect("underline decoration present");
        // rect = [x, y, w, h]; bottom edge sits at y = height - width.
        assert_eq!(underline.rect[1], 38.0);
        assert_eq!(underline.rect[3], 2.0);
        // No uniform stroke shape was emitted.
        assert!(frame.shapes.iter().all(|s| s.stroke_width == 0.0));
    }

    #[test]
    fn gradient_background_emits_linear_gradient_paint() {
        use teksilo_canvas::render_frame::PaintData;
        use teksilo_core::paint_prop::{GradientStopProp, PaintProp};

        let mut tree = WidgetTree::new();
        tree.add(RectWidget::new().background(PaintProp::Linear {
            stops: vec![
                GradientStopProp {
                    offset: 0.0,
                    color: Color::RED.into(),
                },
                GradientStopProp {
                    offset: 1.0,
                    color: Color::BLUE.into(),
                },
            ],
            angle_deg: 90.0,
        }));
        tree.layout(SizeProposal::exact(100.0, 40.0));
        let frame = tree.render();
        assert_eq!(frame.shapes.len(), 1);
        assert!(matches!(
            frame.shapes[0].paint_data,
            PaintData::LinearGradient { .. }
        ));
    }

    #[test]
    fn background_updates_on_state_change() {
        let color = Signal::new(Color::RED);
        let mut tree = WidgetTree::new();
        let w = tree.add(
            RectWidget::new()
                .background(color.clone())
                .corner_radius(CornerRadius::uniform(4.0)),
        );
        color.bind_to(
            w,
            tree.binding_registry(),
            teksilo_core::binding::BindingLevel::RepaintOnly,
        );

        tree.layout(SizeProposal::exact(100.0, 40.0));
        let frame = tree.render();
        assert_eq!(frame.shapes[0].color, Color::RED.to_array());

        // Change the state
        color.set(Color::GREEN);
        tree.layout(SizeProposal::exact(100.0, 40.0));
        let frame = tree.render();
        assert_eq!(frame.shapes[0].color, Color::GREEN.to_array());
    }
}