Skip to main content

teksilo_preview/
variant.rs

1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4//! Preview variant — a named instance of a widget.
5//!
6//! Two flavours:
7//! - `Knobs` — the variant supplies preset overrides for the
8//!   widget's `KnobSpec`. The widget is built from a
9//!   `KnobValues` populated with those overrides.
10//! - `Scenario` — the variant ignores the spec entirely and runs a
11//!   hand-authored builder function. Used by composites
12//!   (Wizard, Dialog, ListView with sample data) where a
13//!   flat knob surface doesn't describe the shape.
14
15use crate::knob::KnobOverrides;
16use teksilo_core::widget::Widget;
17
18/// Builder fn used by `PreviewVariant::Scenario`. Returns a freshly
19/// constructed widget instance — the previewer wraps it for layout
20/// and paint just like any other root child.
21pub type ScenarioBuilder = fn() -> Box<dyn Widget>;
22
23#[derive(Debug, Clone)]
24pub enum PreviewVariant {
25    /// Knob preset — `WidgetCatalog::build` runs and consults
26    /// `knobs()` to build the widget; the supplied overrides are
27    /// applied on top of the spec's defaults.
28    Knobs {
29        name: &'static str,
30        overrides: KnobOverrides,
31    },
32    /// Hand-authored scenario — `WidgetCatalog::build` ignores its
33    /// `KnobValues` argument when this variant is selected and instead
34    /// dispatches to the captured builder fn.
35    Scenario {
36        name: &'static str,
37        builder: ScenarioBuilder,
38    },
39}
40
41impl PreviewVariant {
42    pub fn name(&self) -> &'static str {
43        match self {
44            PreviewVariant::Knobs { name, .. } => name,
45            PreviewVariant::Scenario { name, .. } => name,
46        }
47    }
48
49    pub fn knobs(name: &'static str, overrides: KnobOverrides) -> Self {
50        PreviewVariant::Knobs { name, overrides }
51    }
52
53    pub fn defaults(name: &'static str) -> Self {
54        PreviewVariant::Knobs {
55            name,
56            overrides: KnobOverrides::new(),
57        }
58    }
59
60    pub fn scenario(name: &'static str, builder: ScenarioBuilder) -> Self {
61        PreviewVariant::Scenario { name, builder }
62    }
63}