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
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! Default `AvatarStyle` impl driven by paint-recipe data.
//!
//! `RecipeAvatarStyle` ports the IntUI avatar chrome exactly: the
//! shape-aware background fill (with hash-derived palette pick when no
//! caller override is supplied), the optional border ring drawn over
//! the inner content to mask any image bleed at the rim, the keyboard
//! focus ring hugging the configured shape, and the presence
//! indicator dot positioned at one of the four corners with a
//! `surface_main`-coloured outline.
//!
//! The chrome helpers (`hash_pick_palette_color`, `auto_contrast_text`,
//! `paint_border`, `paint_focus_ring`, `fnv1a_64`, `avatar_pixel_size`)
//! live here so custom `AvatarStyle` implementations can reuse them
//! when they only want to swap one piece of the chrome.

use teksilo_canvas::{Canvas, Paint, Point, Rect, Size, SizeProposal, StrokeStyle};
use teksilo_core::accessibility::AccessNodeBuilder;
use teksilo_core::binding::BindingLevel;
use teksilo_core::build_context::BuildContext;
use teksilo_core::color_prop::ColorProp;
use teksilo_core::signal::Signal;
use teksilo_core::styles::{
    AvatarCorner, AvatarPresence, AvatarShape, AvatarSize, AvatarStyle, AvatarStyleConfig,
};
use teksilo_core::widget::{
    LayoutContext, LayoutResponse, PaintContext, PendingChild, Widget, WidgetPlacement,
};
use teksilo_core::widget_id::WidgetId;
use teksilo_tokens::{Color, CornerRadius};

// ─── IntUI design tokens for Avatar ────────────────────────────────
// The recipe owns its own dimensions. `Avatar` (and its `InitialsLeaf`
// sub-widget) reads the public `pub const`s below directly when it
// needs sizing data outside the chrome frame.

pub const AVATAR_SIZE_SMALL: f32 = 24.0;
pub const AVATAR_SIZE_MEDIUM: f32 = 32.0;
pub const AVATAR_SIZE_LARGE: f32 = 48.0;
pub const AVATAR_SIZE_X_LARGE: f32 = 64.0;

/// Default border (ring) thickness when `.border()` is called without
/// an explicit width override.
pub const AVATAR_BORDER_DEFAULT: f32 = 2.0;

/// Presence dot diameter as a fraction of avatar diameter.
pub const AVATAR_PRESENCE_DIAMETER_RATIO: f32 = 0.28;
pub const AVATAR_PRESENCE_DIAMETER_MIN: f32 = 8.0;
pub const AVATAR_PRESENCE_DIAMETER_MAX: f32 = 20.0;
/// Outline drawn around the presence dot.
pub const AVATAR_PRESENCE_OUTLINE_WIDTH: f32 = 1.5;
/// Inset of the presence dot from the avatar's bounding box edge.
pub const AVATAR_PRESENCE_INSET: f32 = 0.0;

/// Initials font-size as a fraction of avatar diameter.
pub const AVATAR_FONT_RATIO_1CHAR: f32 = 0.45;
pub const AVATAR_FONT_RATIO_2CHAR: f32 = 0.40;

/// Corner-radius ratio for `AvatarShape::RoundedSquare`.
pub const AVATAR_ROUNDED_RADIUS_RATIO: f32 = 0.25;

/// Resolve a discrete `AvatarSize` to a logical-pixel side length
/// using the recipe's size table. `Custom(px)` is clamped to at least
/// 1 px.
pub fn avatar_pixel_size(size: AvatarSize) -> f32 {
    match size {
        AvatarSize::Small => AVATAR_SIZE_SMALL,
        AvatarSize::Medium => AVATAR_SIZE_MEDIUM,
        AvatarSize::Large => AVATAR_SIZE_LARGE,
        AvatarSize::XLarge => AVATAR_SIZE_X_LARGE,
        AvatarSize::Custom(px) => px.max(1.0),
    }
}

/// FNV-1a 64-bit hash — deterministic palette-bucket selection.
pub fn fnv1a_64(bytes: &[u8]) -> u64 {
    let mut h: u64 = 0xcbf29ce484222325;
    for &b in bytes {
        h ^= b as u64;
        h = h.wrapping_mul(0x100000001b3);
    }
    h
}

/// Pick a colour from the theme's chart palette deterministically
/// from `seed`. Empty palette falls back to a neutral grey so the
/// widget still renders.
pub fn hash_pick_palette_color(seed: &str, theme: &teksilo_core::Theme) -> Color {
    let palette = &theme.colors.chart_palette;
    if palette.is_empty() {
        return Color::from_rgb(0.5, 0.5, 0.5);
    }
    let h = fnv1a_64(seed.as_bytes());
    let idx = (h as usize) % palette.len();
    palette[idx]
}

/// Auto-contrast foreground for a given background.
pub fn auto_contrast_text(bg: Color) -> Color {
    if bg.relative_luminance() < 0.5 {
        Color::WHITE
    } else {
        Color::from_rgb(0.121, 0.121, 0.121)
    }
}

/// Stroke a focus ring outside the avatar's content bounds. Hugs the
/// configured shape so a square avatar gets a square ring.
pub fn paint_focus_ring(
    canvas: &mut Canvas,
    bounds: Rect,
    shape: AvatarShape,
    rounded_radius_ratio: f32,
    offset: f32,
    width: f32,
    color: Color,
) {
    let outset = offset + width / 2.0;
    let outer = Rect::new(
        bounds.x - outset,
        bounds.y - outset,
        bounds.width + outset * 2.0,
        bounds.height + outset * 2.0,
    );
    match shape {
        AvatarShape::Circle => {
            let radius = outer.width.min(outer.height) / 2.0;
            let center = Point::new(outer.x + outer.width / 2.0, outer.y + outer.height / 2.0);
            canvas.stroke_circle(
                center,
                radius,
                Paint::from(color),
                StrokeStyle::solid(width),
            );
        }
        AvatarShape::RoundedSquare => {
            let r = bounds.width.min(bounds.height) * rounded_radius_ratio + outset;
            canvas.stroke_rounded_rect(
                outer,
                CornerRadius::uniform(r),
                color,
                StrokeStyle::solid(width),
            );
        }
        AvatarShape::Square => {
            canvas.stroke_rounded_rect(
                outer,
                CornerRadius::uniform(0.0),
                color,
                StrokeStyle::solid(width),
            );
        }
    }
}

/// Stroke a border ring inside the avatar's content bounds.
pub fn paint_border(
    canvas: &mut Canvas,
    bounds: Rect,
    shape: AvatarShape,
    rounded_radius_ratio: f32,
    width: f32,
    color: Color,
) {
    let half = width / 2.0;
    let inner = Rect::new(
        bounds.x + half,
        bounds.y + half,
        (bounds.width - width).max(0.0),
        (bounds.height - width).max(0.0),
    );
    match shape {
        AvatarShape::Circle => {
            let radius = inner.width.min(inner.height) / 2.0;
            let center = Point::new(inner.x + inner.width / 2.0, inner.y + inner.height / 2.0);
            canvas.stroke_circle(
                center,
                radius,
                Paint::from(color),
                StrokeStyle::solid(width),
            );
        }
        AvatarShape::RoundedSquare => {
            let r = inner.width.min(inner.height) * rounded_radius_ratio;
            canvas.stroke_rounded_rect(
                inner,
                CornerRadius::uniform(r),
                color,
                StrokeStyle::solid(width),
            );
        }
        AvatarShape::Square => {
            canvas.stroke_rounded_rect(
                inner,
                CornerRadius::uniform(0.0),
                color,
                StrokeStyle::solid(width),
            );
        }
    }
}

/// Dimension recipe for `RecipeAvatarStyle`.
///
/// All tunable measurements in one place. The recipe is `Copy` so it
/// can be stored inside the internal `AvatarChromeFrame` body widget
/// without any allocation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AvatarRecipe {
    pub size_small: f32,
    pub size_medium: f32,
    pub size_large: f32,
    pub size_x_large: f32,
    pub border_default: f32,
    pub presence_diameter_ratio: f32,
    pub presence_diameter_min: f32,
    pub presence_diameter_max: f32,
    pub presence_outline_width: f32,
    pub presence_inset: f32,
    pub font_ratio_1char: f32,
    pub font_ratio_2char: f32,
    pub rounded_radius_ratio: f32,
}

impl Default for AvatarRecipe {
    fn default() -> Self {
        Self {
            size_small: AVATAR_SIZE_SMALL,
            size_medium: AVATAR_SIZE_MEDIUM,
            size_large: AVATAR_SIZE_LARGE,
            size_x_large: AVATAR_SIZE_X_LARGE,
            border_default: AVATAR_BORDER_DEFAULT,
            presence_diameter_ratio: AVATAR_PRESENCE_DIAMETER_RATIO,
            presence_diameter_min: AVATAR_PRESENCE_DIAMETER_MIN,
            presence_diameter_max: AVATAR_PRESENCE_DIAMETER_MAX,
            presence_outline_width: AVATAR_PRESENCE_OUTLINE_WIDTH,
            presence_inset: AVATAR_PRESENCE_INSET,
            font_ratio_1char: AVATAR_FONT_RATIO_1CHAR,
            font_ratio_2char: AVATAR_FONT_RATIO_2CHAR,
            rounded_radius_ratio: AVATAR_ROUNDED_RADIUS_RATIO,
        }
    }
}

/// Default `AvatarStyle` shipped with Teksilo.
#[derive(Debug, Default, Clone, Copy)]
pub struct RecipeAvatarStyle {
    pub recipe: AvatarRecipe,
}

impl RecipeAvatarStyle {
    pub fn new(recipe: AvatarRecipe) -> Self {
        Self { recipe }
    }
}

impl AvatarStyle for RecipeAvatarStyle {
    fn make_body(&self, cfg: &AvatarStyleConfig, ctx: &mut BuildContext) -> WidgetId {
        ctx.add(AvatarChromeFrame {
            child_id: None,
            pending_child: Some(PendingChild::Id(cfg.content)),
            shape: cfg.shape,
            presence: cfg.presence.clone(),
            presence_corner: cfg.presence_corner,
            is_focused: cfg.is_focused.clone(),
            background: cfg.background_override.clone(),
            border_color: cfg.border_color_override.clone(),
            border_width: cfg.border_width_override,
            seed: cfg.seed.clone(),
            recipe: self.recipe,
        })
    }
}

/// Internal container that paints the avatar chrome (shape-aware
/// background fill, border ring, keyboard focus ring, presence dot)
/// around the pre-built content child. Mirrors the pre-migration
/// `Avatar::paint` exactly.
struct AvatarChromeFrame {
    child_id: Option<WidgetId>,
    pending_child: Option<PendingChild>,
    shape: AvatarShape,
    presence: Option<AvatarPresence>,
    presence_corner: AvatarCorner,
    is_focused: Signal<bool>,
    background: Option<ColorProp>,
    border_color: Option<ColorProp>,
    border_width: Option<f32>,
    seed: String,
    recipe: AvatarRecipe,
}

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

impl Widget for AvatarChromeFrame {
    fn build(&mut self, ctx: &mut BuildContext) -> Vec<WidgetId> {
        if let Some(pending) = self.pending_child.take() {
            self.child_id = Some(match pending {
                PendingChild::Id(id) => id,
                PendingChild::Deferred(w) => ctx.add_boxed(w),
            });
        }
        // Repaint when focus changes — the ring appears / disappears.
        let id = ctx.self_id();
        let registry = ctx.binding_registry();
        self.is_focused
            .bind_to(id, registry, BindingLevel::RepaintOnly);
        self.child_id.into_iter().collect()
    }

    fn layout_response(&self, proposal: SizeProposal, _ctx: &LayoutContext) -> LayoutResponse {
        // Fill the parent's proposal — the parent `Avatar` widget owns
        // the size policy (`avatar_pixel_size(self.size)`).
        Size::new(
            proposal.width.unwrap_or(0.0),
            proposal.height.unwrap_or(0.0),
        )
        .into()
    }

    fn place_children(
        &self,
        bounds: Rect,
        _proposal: SizeProposal,
        children: &mut [WidgetPlacement],
        _ctx: &LayoutContext,
    ) {
        for child in children.iter_mut() {
            child.origin = bounds.origin();
            child.size = bounds.size();
        }
    }

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

        // Background fill — shape-aware. Default is hash-picked from
        // the chart palette using `seed`.
        let bg = match &self.background {
            Some(prop) => prop.resolve(theme, ctx.effective_enabled),
            None => hash_pick_palette_color(&self.seed, theme),
        };
        match self.shape {
            AvatarShape::Circle => {
                let radius = bounds.width.min(bounds.height) / 2.0;
                let center = Point::new(
                    bounds.x + bounds.width / 2.0,
                    bounds.y + bounds.height / 2.0,
                );
                canvas.fill_circle(center, radius, bg);
            }
            AvatarShape::RoundedSquare => {
                let r = bounds.width.min(bounds.height) * self.recipe.rounded_radius_ratio;
                canvas.fill_rounded_rect(bounds, CornerRadius::uniform(r), bg);
            }
            AvatarShape::Square => {
                canvas.fill_rounded_rect(bounds, CornerRadius::uniform(0.0), bg);
            }
        }

        // Border (outer ring) — drawn over content to mask image
        // bleed. Half-stroke inset so the ring sits inside `bounds`.
        if let Some(width) = self.border_width
            && width > 0.0
        {
            let color = match &self.border_color {
                Some(prop) => prop.resolve(theme, ctx.effective_enabled),
                None => theme.colors.surface_main,
            };
            paint_border(
                canvas,
                bounds,
                self.shape,
                self.recipe.rounded_radius_ratio,
                width,
                color,
            );
        }

        // Focus ring — outside the avatar bounds, hugging the shape.
        if self.is_focused.get() {
            paint_focus_ring(
                canvas,
                bounds,
                self.shape,
                self.recipe.rounded_radius_ratio,
                theme.shape.focus_ring_offset,
                theme.shape.focus_ring_width,
                theme.colors.focus_ring,
            );
        }

        // Presence dot — on top of everything, with a surface_main
        // outline that "punches" it out of the avatar.
        if let Some(presence) = &self.presence {
            let color = presence.color(theme);
            let dot_diameter =
                (bounds.width.min(bounds.height) * self.recipe.presence_diameter_ratio).clamp(
                    self.recipe.presence_diameter_min,
                    self.recipe.presence_diameter_max,
                );
            let dot_radius = dot_diameter / 2.0;
            let (xf, yf) = self.presence_corner.offset();
            let cx = if xf < 0.0 {
                bounds.x + dot_radius + self.recipe.presence_inset
            } else {
                bounds.x + bounds.width - dot_radius - self.recipe.presence_inset
            };
            let cy = if yf < 0.0 {
                bounds.y + dot_radius + self.recipe.presence_inset
            } else {
                bounds.y + bounds.height - dot_radius - self.recipe.presence_inset
            };
            let center = Point::new(cx, cy);
            let outline_radius = dot_radius + self.recipe.presence_outline_width;
            canvas.fill_circle(center, outline_radius, theme.colors.surface_main);
            canvas.fill_circle(center, dot_radius, color);
        }
    }

    fn accessibility(&self, builder: &mut AccessNodeBuilder) {
        // Presentational — the parent `Avatar` emits the user-facing
        // Image / Label / Button node.
        builder.set_hidden();
    }

    fn children(&self) -> Vec<WidgetId> {
        self.child_id.into_iter().collect()
    }
}