rustmotion-components 0.7.0

Component library for rustmotion (51 components)
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
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use skia_safe::{Canvas, PaintStyle};

use rustmotion_core::css::CssStyle;
use rustmotion_core::engine::animator::AnimatedProperties;
use rustmotion_core::engine::layout_pass::BoxLayout;
use rustmotion_core::engine::renderer::{
    draw_text_with_fallback, emoji_typeface, measure_text_with_fallback, paint_from_hex,
    parse_hex_color, typeface_with_fallback,
};
use rustmotion_core::schema::TimelineStep;
use rustmotion_core::traits::{PaintCtx, Painter, TimingConfig};

fn default_active_step() -> u32 {
    0
}

fn default_transition_duration() -> f64 {
    0.5
}

/// Layout axis of the stepper. Closed set, matched exhaustively by the
/// painter; serde snake_case keeps the JSON values identical
/// ("horizontal"/"vertical") — an unknown value now fails the typed parse
/// (blocking validate error, by design).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum StepperOrientation {
    #[default]
    Horizontal,
    Vertical,
}

fn default_active_color() -> String {
    "#3B82F6".to_string()
}

fn default_completed_color() -> String {
    "#22C55E".to_string()
}

fn default_pending_color() -> String {
    "#6B7280".to_string()
}

fn default_node_size() -> f32 {
    32.0
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct StepItem {
    pub label: String,
    #[serde(default)]
    pub description: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct Stepper {
    /// The steps to display.
    pub steps: Vec<StepItem>,
    /// The currently active step (0-indexed).
    #[serde(default = "default_active_step")]
    pub active_step: u32,
    /// Animate active step to this index.
    #[serde(default)]
    pub animate_to: Option<u32>,
    /// Time at which the animation starts.
    #[serde(default)]
    pub animate_at: Option<f64>,
    /// Duration of the step transition animation.
    #[serde(default = "default_transition_duration")]
    pub transition_duration: f64,
    /// Layout direction: "horizontal" or "vertical".
    #[serde(default)]
    pub orientation: StepperOrientation,
    /// Color of the active step node.
    #[serde(default = "default_active_color")]
    pub active_color: String,
    /// Color of completed step nodes.
    #[serde(default = "default_completed_color")]
    pub completed_color: String,
    /// Color of pending step nodes.
    #[serde(default = "default_pending_color")]
    pub pending_color: String,
    /// Diameter of step nodes.
    #[serde(default = "default_node_size")]
    pub node_size: f32,
    #[serde(flatten)]
    pub timing: TimingConfig,
    #[serde(default)]
    pub style: CssStyle,
    #[serde(default)]
    pub timeline: Vec<TimelineStep>,
    #[serde(default)]
    pub stagger: Option<f32>,
}

rustmotion_core::impl_traits!(Stepper {
    Animatable => animation,
    Timed => timing,
    Styled => style,
});

impl Stepper {
    fn current_step_at(&self, time: f64) -> f32 {
        if let (Some(target), Some(start_at)) = (self.animate_to, self.animate_at) {
            let elapsed = (time - start_at).max(0.0);
            let p = (elapsed / self.transition_duration).clamp(0.0, 1.0) as f32;
            let eased = 1.0 - (1.0 - p).powi(3);
            let from = self.active_step as f32;
            let to = target as f32;
            from + (to - from) * eased
        } else {
            self.active_step as f32
        }
    }
}

impl Stepper {
    fn paint(&self, canvas: &Canvas, layout_w: f32, layout_h: f32, time: f64) {
        let w = layout_w;
        let h = layout_h;
        let n = self.steps.len();
        if n == 0 {
            return;
        }

        let current = self.current_step_at(time);
        let r = self.node_size / 2.0;

        let font_style = skia_safe::FontStyle::normal();
        let Ok(typeface) = typeface_with_fallback("Inter", font_style) else {
            return;
        };

        let bold_style = skia_safe::FontStyle::bold();
        let Ok(bold_typeface) = typeface_with_fallback("Inter", bold_style) else {
            return;
        };

        let number_font_size = r * 0.9;
        let number_font = skia_safe::Font::from_typeface(&bold_typeface, number_font_size);
        let emoji_number_font =
            emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, number_font_size));

        let label_font_size = 14.0;
        let label_font = skia_safe::Font::from_typeface(&typeface, label_font_size);
        let emoji_label_font =
            emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, label_font_size));

        let desc_font_size = 11.0;
        let desc_font = skia_safe::Font::from_typeface(&typeface, desc_font_size);
        let emoji_desc_font =
            emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, desc_font_size));

        let is_horizontal = self.orientation == StepperOrientation::Horizontal;

        if is_horizontal {
            let padding = r + 8.0;
            let available = w - padding * 2.0;
            let spacing = if n > 1 {
                available / (n - 1) as f32
            } else {
                0.0
            };
            let cy = r + 4.0;

            // Draw connector lines between nodes
            for i in 0..(n - 1) {
                let x1 = padding + i as f32 * spacing + r;
                let x2 = padding + (i + 1) as f32 * spacing - r;

                let step_f = i as f32;
                let is_completed = current > step_f + 0.5;

                let (cr, cg, cb, _) = if is_completed {
                    parse_hex_color(&self.completed_color)
                } else {
                    parse_hex_color(&self.pending_color)
                };

                let alpha = if is_completed { 255u8 } else { 80u8 };
                let color = skia_safe::Color::from_argb(alpha, cr, cg, cb);
                let mut line_paint = skia_safe::Paint::default();
                line_paint.set_color(color);
                line_paint.set_style(PaintStyle::Stroke);
                line_paint.set_stroke_width(2.0);
                line_paint.set_anti_alias(true);

                canvas.draw_line((x1, cy), (x2, cy), &line_paint);
            }

            // Draw nodes and labels
            for (i, step) in self.steps.iter().enumerate() {
                let cx = padding + i as f32 * spacing;
                let step_f = i as f32;

                let (node_color, is_active) = if step_f < current.floor() {
                    // Completed
                    (&self.completed_color, false)
                } else if (step_f - current).abs() < 0.5 {
                    // Active
                    (&self.active_color, true)
                } else {
                    // Pending
                    (&self.pending_color, false)
                };

                // Node circle
                let mut node_paint = paint_from_hex(node_color);
                node_paint.set_style(PaintStyle::Fill);
                node_paint.set_anti_alias(true);

                if is_active {
                    // Filled circle for active
                    canvas.draw_circle((cx, cy), r, &node_paint);
                } else if step_f < current.floor() {
                    // Filled circle for completed
                    canvas.draw_circle((cx, cy), r, &node_paint);
                } else {
                    // Ring for pending
                    node_paint.set_style(PaintStyle::Stroke);
                    node_paint.set_stroke_width(2.0);
                    canvas.draw_circle((cx, cy), r - 1.0, &node_paint);
                }

                // Step number inside circle
                let num_text = format!("{}", i + 1);
                let num_w =
                    measure_text_with_fallback(&num_text, &number_font, &emoji_number_font, 0.0);
                let (_, num_metrics) = number_font.metrics();
                let num_x = cx - num_w / 2.0;
                let num_y = cy + (-num_metrics.ascent - num_metrics.descent) / 2.0;

                let mut num_paint = paint_from_hex("#FFFFFF");
                num_paint.set_anti_alias(true);
                draw_text_with_fallback(
                    canvas,
                    &num_text,
                    &number_font,
                    &emoji_number_font,
                    0.0,
                    num_x,
                    num_y,
                    &num_paint,
                );

                // Label below circle
                let label_w =
                    measure_text_with_fallback(&step.label, &label_font, &emoji_label_font, 0.0);
                let label_x = cx - label_w / 2.0;
                let (_, label_metrics) = label_font.metrics();
                let label_y = cy + r + 12.0 + (-label_metrics.ascent);

                let mut label_paint = paint_from_hex("#FFFFFF");
                label_paint.set_anti_alias(true);
                draw_text_with_fallback(
                    canvas,
                    &step.label,
                    &label_font,
                    &emoji_label_font,
                    0.0,
                    label_x,
                    label_y,
                    &label_paint,
                );

                // Description below label
                if let Some(desc) = &step.description {
                    let desc_w =
                        measure_text_with_fallback(desc, &desc_font, &emoji_desc_font, 0.0);
                    let desc_x = cx - desc_w / 2.0;
                    let desc_y = label_y + label_font_size + 4.0;

                    let mut desc_paint = paint_from_hex("#8B949E");
                    desc_paint.set_anti_alias(true);
                    draw_text_with_fallback(
                        canvas,
                        desc,
                        &desc_font,
                        &emoji_desc_font,
                        0.0,
                        desc_x,
                        desc_y,
                        &desc_paint,
                    );
                }
            }
        } else {
            // Vertical layout
            let padding = r + 8.0;
            let available = h - padding * 2.0;
            let spacing = if n > 1 {
                available / (n - 1) as f32
            } else {
                0.0
            };
            let cx = r + 4.0;

            // Draw connector lines
            for i in 0..(n - 1) {
                let y1 = padding + i as f32 * spacing + r;
                let y2 = padding + (i + 1) as f32 * spacing - r;

                let step_f = i as f32;
                let is_completed = current > step_f + 0.5;

                let (cr, cg, cb, _) = if is_completed {
                    parse_hex_color(&self.completed_color)
                } else {
                    parse_hex_color(&self.pending_color)
                };

                let alpha = if is_completed { 255u8 } else { 80u8 };
                let color = skia_safe::Color::from_argb(alpha, cr, cg, cb);
                let mut line_paint = skia_safe::Paint::default();
                line_paint.set_color(color);
                line_paint.set_style(PaintStyle::Stroke);
                line_paint.set_stroke_width(2.0);
                line_paint.set_anti_alias(true);

                canvas.draw_line((cx, y1), (cx, y2), &line_paint);
            }

            // Draw nodes and labels
            for (i, step) in self.steps.iter().enumerate() {
                let cy = padding + i as f32 * spacing;
                let step_f = i as f32;

                let (node_color, is_active) = if step_f < current.floor() {
                    (&self.completed_color, false)
                } else if (step_f - current).abs() < 0.5 {
                    (&self.active_color, true)
                } else {
                    (&self.pending_color, false)
                };

                let mut node_paint = paint_from_hex(node_color);
                node_paint.set_style(PaintStyle::Fill);
                node_paint.set_anti_alias(true);

                if is_active || step_f < current.floor() {
                    canvas.draw_circle((cx, cy), r, &node_paint);
                } else {
                    node_paint.set_style(PaintStyle::Stroke);
                    node_paint.set_stroke_width(2.0);
                    canvas.draw_circle((cx, cy), r - 1.0, &node_paint);
                }

                // Step number
                let num_text = format!("{}", i + 1);
                let num_w =
                    measure_text_with_fallback(&num_text, &number_font, &emoji_number_font, 0.0);
                let (_, num_metrics) = number_font.metrics();
                let num_x = cx - num_w / 2.0;
                let num_y = cy + (-num_metrics.ascent - num_metrics.descent) / 2.0;

                let mut num_paint = paint_from_hex("#FFFFFF");
                num_paint.set_anti_alias(true);
                draw_text_with_fallback(
                    canvas,
                    &num_text,
                    &number_font,
                    &emoji_number_font,
                    0.0,
                    num_x,
                    num_y,
                    &num_paint,
                );

                // Label to the right
                let lx = cx + r + 12.0;
                let (_, lm) = label_font.metrics();
                let ly = cy + (-lm.ascent - lm.descent) / 2.0;
                let mut label_paint = paint_from_hex("#FFFFFF");
                label_paint.set_anti_alias(true);
                draw_text_with_fallback(
                    canvas,
                    &step.label,
                    &label_font,
                    &emoji_label_font,
                    0.0,
                    lx,
                    ly,
                    &label_paint,
                );

                // Description below label
                if let Some(desc) = &step.description {
                    let desc_y = ly + label_font_size + 2.0;
                    let mut desc_paint = paint_from_hex("#8B949E");
                    desc_paint.set_anti_alias(true);
                    draw_text_with_fallback(
                        canvas,
                        desc,
                        &desc_font,
                        &emoji_desc_font,
                        0.0,
                        lx,
                        desc_y,
                        &desc_paint,
                    );
                }
            }
        }
    }
}

impl Painter for Stepper {
    fn paint_content(
        &self,
        canvas: &Canvas,
        layout: &BoxLayout,
        _props: &AnimatedProperties,
        ctx: &PaintCtx,
    ) {
        self.paint(canvas, layout.width, layout.height, ctx.time);
    }
}