liora-components 0.1.0

Enterprise-style native GPUI component library for Liora applications.
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use crate::chart::{ChartLineStyle, ChartPoint, downsample_index_range};
use crate::chart_shape::{
    area_path, line_path_with_style, smooth_area_path, smooth_line_path_with_style,
};
use gpui::{
    App, Background, Component, ElementId, Hsla, IntoElement, Pixels, RenderOnce, SharedString,
    Window, canvas, div, fill, point, prelude::*, px, size,
};
use liora_core::{Config, unique_id};

#[derive(Clone, Debug)]
pub struct Sparkline {
    id: SharedString,
    values: Vec<f64>,
    height: Pixels,
    width: Option<Pixels>,
    padding: Pixels,
    color: Option<Hsla>,
    positive_color: Option<Hsla>,
    negative_color: Option<Hsla>,
    fill_color: Option<Hsla>,
    baseline_color: Option<Hsla>,
    stroke_width: Pixels,
    smooth: bool,
    area_fill: bool,
    show_last_point: bool,
    show_baseline: bool,
    y_domain: Option<(f64, f64)>,
    line_style: ChartLineStyle,
    dash_pattern: Option<Vec<Pixels>>,
    max_render_points: Option<usize>,
}

impl Sparkline {
    pub fn new(values: impl IntoIterator<Item = f64>) -> Self {
        Self {
            id: unique_id("sparkline"),
            values: values.into_iter().collect(),
            height: px(56.0),
            width: None,
            padding: px(4.0),
            color: None,
            positive_color: None,
            negative_color: None,
            fill_color: None,
            baseline_color: None,
            stroke_width: px(2.0),
            smooth: true,
            area_fill: false,
            show_last_point: true,
            show_baseline: false,
            y_domain: None,
            line_style: ChartLineStyle::Solid,
            dash_pattern: None,
            max_render_points: Some(240),
        }
    }

    pub fn from_points(points: impl IntoIterator<Item = ChartPoint>) -> Self {
        Self::new(points.into_iter().map(|point| point.value))
    }

    pub fn id(mut self, id: impl Into<SharedString>) -> Self {
        self.id = id.into();
        self
    }

    pub fn height(mut self, height: impl Into<Pixels>) -> Self {
        self.height = height.into().max(px(12.0));
        self
    }

    pub fn width(mut self, width: impl Into<Pixels>) -> Self {
        self.width = Some(width.into().max(px(12.0)));
        self
    }

    pub fn padding(mut self, padding: impl Into<Pixels>) -> Self {
        self.padding = padding.into().max(px(0.0));
        self
    }

    pub fn color(mut self, color: Hsla) -> Self {
        self.color = Some(color);
        self
    }

    pub fn positive_color(mut self, color: Hsla) -> Self {
        self.positive_color = Some(color);
        self
    }

    pub fn negative_color(mut self, color: Hsla) -> Self {
        self.negative_color = Some(color);
        self
    }

    pub fn trend_colors(mut self, positive: Hsla, negative: Hsla) -> Self {
        self.positive_color = Some(positive);
        self.negative_color = Some(negative);
        self
    }

    pub fn fill_color(mut self, color: Hsla) -> Self {
        self.fill_color = Some(color);
        self.area_fill = true;
        self
    }

    pub fn baseline_color(mut self, color: Hsla) -> Self {
        self.baseline_color = Some(color);
        self.show_baseline = true;
        self
    }

    pub fn stroke_width(mut self, width: impl Into<Pixels>) -> Self {
        self.stroke_width = width.into().max(px(0.5));
        self
    }

    pub fn smooth(mut self, smooth: bool) -> Self {
        self.smooth = smooth;
        self
    }

    pub fn area_fill(mut self, enabled: bool) -> Self {
        self.area_fill = enabled;
        self
    }

    pub fn show_last_point(mut self, show: bool) -> Self {
        self.show_last_point = show;
        self
    }

    pub fn show_baseline(mut self, show: bool) -> Self {
        self.show_baseline = show;
        self
    }

    pub fn y_domain(mut self, min: f64, max: f64) -> Self {
        self.y_domain = Some((min, max));
        self
    }

    pub fn line_style(mut self, style: ChartLineStyle) -> Self {
        self.line_style = style;
        if !matches!(style, ChartLineStyle::Dashed) {
            self.dash_pattern = None;
        }
        self
    }

    pub fn dashed(mut self) -> Self {
        self.line_style = ChartLineStyle::Dashed;
        self
    }

    pub fn dotted(mut self) -> Self {
        self.line_style = ChartLineStyle::Dotted;
        self
    }

    pub fn dash_pattern(mut self, pattern: impl IntoIterator<Item = Pixels>) -> Self {
        self.dash_pattern = Some(
            pattern
                .into_iter()
                .map(|value| value.max(px(0.1)))
                .collect(),
        );
        self.line_style = ChartLineStyle::Dashed;
        self
    }

    pub fn max_render_points(mut self, max_points: usize) -> Self {
        self.max_render_points = Some(max_points.max(3));
        self
    }

    pub fn disable_downsampling(mut self) -> Self {
        self.max_render_points = None;
        self
    }

    pub fn values(&self) -> &[f64] {
        &self.values
    }

    pub fn trend_delta(&self) -> Option<f64> {
        trend_delta(&self.values)
    }

    pub fn resolved_domain(&self) -> Option<(f64, f64)> {
        finite_stats(&self.values).and_then(|stats| sparkline_domain(self.y_domain, stats))
    }
}

impl IntoElement for Sparkline {
    type Element = Component<Self>;

    fn into_element(self) -> Self::Element {
        Component::new(self)
    }
}

impl RenderOnce for Sparkline {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.global::<Config>().theme.clone();
        let id = self.id.clone();
        let height = self.height;
        let width = self.width;
        let Some(stats) = finite_stats(&self.values) else {
            return div()
                .id(ElementId::from(id))
                .h(height)
                .when_some(width, |s, width| s.w(width))
                .when(width.is_none(), |s| s.w_full())
                .rounded_sm()
                .bg(theme.neutral.hover.opacity(0.28))
                .into_any_element();
        };

        let domain = sparkline_domain(self.y_domain, stats).unwrap_or((0.0, 1.0));
        let positive = self.positive_color.unwrap_or(theme.success.base);
        let negative = self.negative_color.unwrap_or(theme.danger.base);
        let trend_color = if stats.last - stats.first < 0.0 {
            negative
        } else {
            positive
        };
        let line_color = self.color.unwrap_or(trend_color);
        let fill_color = self.fill_color.unwrap_or(line_color.opacity(0.18));
        let baseline_color = self
            .baseline_color
            .unwrap_or(theme.neutral.border.opacity(0.72));
        let padding = self.padding;
        let stroke_width = self.stroke_width;
        let smooth = self.smooth;
        let area_fill_enabled = self.area_fill;
        let show_last_point = self.show_last_point;
        let show_baseline = self.show_baseline;
        let line_style = self.line_style;
        let dash_pattern = self.dash_pattern.clone();
        let values = self.values.clone();
        let max_render_points = self.max_render_points;

        let chart = canvas(
            |_, _, _| (),
            move |bounds, _, window, _cx| {
                let left = bounds.left() + padding;
                let right = bounds.right() - padding;
                let top = bounds.top() + padding;
                let bottom = bounds.bottom() - padding;
                let plot_width = (right - left).max(px(1.0));
                let plot_height = (bottom - top).max(px(1.0));
                let points = sparkline_points(
                    &values,
                    domain,
                    left,
                    top,
                    plot_width,
                    plot_height,
                    max_render_points,
                );
                if points.is_empty() {
                    return;
                }

                if show_baseline {
                    let baseline_y = y_for_value(0.0, domain, top, plot_height);
                    if let Some(path) = line_path_with_style(
                        &[point(left, baseline_y), point(right, baseline_y)],
                        px(1.0),
                        ChartLineStyle::Dashed,
                        None,
                    ) {
                        window.paint_path(path, baseline_color);
                    }
                }

                if area_fill_enabled {
                    let baseline_y = if domain.0 < 0.0 && domain.1 > 0.0 {
                        y_for_value(0.0, domain, top, plot_height)
                    } else if domain.1 <= 0.0 {
                        top
                    } else {
                        bottom
                    };
                    let area = if smooth {
                        smooth_area_path(&points, baseline_y)
                    } else {
                        area_path(&points, baseline_y)
                    };
                    if let Some(path) = area {
                        window.paint_path(path, Background::from(fill_color));
                    }
                }

                if let Some(path) = if smooth {
                    smooth_line_path_with_style(
                        &points,
                        stroke_width,
                        line_style,
                        dash_pattern.as_deref(),
                    )
                } else {
                    line_path_with_style(&points, stroke_width, line_style, dash_pattern.as_deref())
                } {
                    window.paint_path(path, line_color);
                }

                if show_last_point {
                    if let Some(last_point) = points.last().copied() {
                        let radius = (stroke_width.as_f32() + 2.0).max(3.0);
                        window.paint_quad(fill(
                            gpui::Bounds::new(
                                point(last_point.x - px(radius), last_point.y - px(radius)),
                                size(px(radius * 2.0), px(radius * 2.0)),
                            ),
                            Background::from(line_color),
                        ));
                    }
                }
            },
        )
        .h(height)
        .when_some(width, |s, width| s.w(width))
        .when(width.is_none(), |s| s.w_full());

        div()
            .id(ElementId::from(id))
            .child(chart)
            .into_any_element()
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
struct FiniteStats {
    first: f64,
    last: f64,
    min: f64,
    max: f64,
}

fn finite_stats(values: &[f64]) -> Option<FiniteStats> {
    let mut first = None;
    let mut last = 0.0;
    let mut min = f64::INFINITY;
    let mut max = f64::NEG_INFINITY;
    for value in values.iter().copied().filter(|value| value.is_finite()) {
        first.get_or_insert(value);
        last = value;
        min = min.min(value);
        max = max.max(value);
    }
    Some(FiniteStats {
        first: first?,
        last,
        min,
        max,
    })
}

fn trend_delta(values: &[f64]) -> Option<f64> {
    let stats = finite_stats(values)?;
    Some(stats.last - stats.first)
}

fn sparkline_domain(domain: Option<(f64, f64)>, stats: FiniteStats) -> Option<(f64, f64)> {
    if let Some((min, max)) = domain.filter(|(min, max)| min.is_finite() && max.is_finite()) {
        if max > min {
            return Some((min, max));
        }
    }
    let mut min = stats.min;
    let mut max = stats.max;
    if (max - min).abs() < f64::EPSILON {
        let pad = if max.abs() < f64::EPSILON {
            1.0
        } else {
            max.abs() * 0.1
        };
        min -= pad;
        max += pad;
    }
    Some((min, max))
}

fn y_for_value(value: f64, domain: (f64, f64), top: Pixels, plot_height: Pixels) -> Pixels {
    let ratio = ((value - domain.0) / (domain.1 - domain.0)).clamp(0.0, 1.0) as f32;
    top + px(plot_height.as_f32() * (1.0 - ratio))
}

fn sparkline_points(
    values: &[f64],
    domain: (f64, f64),
    left: Pixels,
    top: Pixels,
    plot_width: Pixels,
    plot_height: Pixels,
    max_render_points: Option<usize>,
) -> Vec<gpui::Point<Pixels>> {
    let last_index = values.len().saturating_sub(1).max(1) as f32;
    downsample_index_range(values.len(), |index| values[index], max_render_points)
        .into_iter()
        .map(|(index, value)| {
            let x = left + px(plot_width.as_f32() * (index as f32 / last_index));
            let y = y_for_value(value, domain, top, plot_height);
            point(x, y)
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sparkline_tracks_builder_options() {
        let chart = Sparkline::new([1.0, 2.0, 3.0])
            .id("revenue-spark")
            .height(px(72.0))
            .width(px(180.0))
            .padding(px(8.0))
            .color(gpui::blue())
            .trend_colors(gpui::green(), gpui::red())
            .fill_color(gpui::blue().opacity(0.2))
            .baseline_color(gpui::black().opacity(0.2))
            .stroke_width(px(3.0))
            .smooth(false)
            .area_fill(true)
            .show_last_point(false)
            .show_baseline(true)
            .y_domain(0.0, 10.0)
            .dashed()
            .max_render_points(120);

        assert_eq!(chart.id, SharedString::from("revenue-spark"));
        assert_eq!(chart.height, px(72.0));
        assert_eq!(chart.width, Some(px(180.0)));
        assert_eq!(chart.padding, px(8.0));
        assert_eq!(chart.stroke_width, px(3.0));
        assert!(!chart.smooth);
        assert!(chart.area_fill);
        assert!(!chart.show_last_point);
        assert!(chart.show_baseline);
        assert_eq!(chart.y_domain, Some((0.0, 10.0)));
        assert_eq!(chart.line_style, ChartLineStyle::Dashed);
        assert_eq!(chart.max_render_points, Some(120));
    }

    #[test]
    fn sparkline_domain_ignores_invalid_values_and_expands_flat_data() {
        assert_eq!(
            finite_stats(&[f64::NAN]).and_then(|stats| sparkline_domain(None, stats)),
            None
        );
        let domain = sparkline_domain(None, finite_stats(&[4.0, f64::NAN, 4.0]).unwrap()).unwrap();
        assert!(domain.0 < 4.0);
        assert!(domain.1 > 4.0);
    }

    #[test]
    fn sparkline_trend_delta_uses_finite_values() {
        let chart = Sparkline::new([f64::NAN, 10.0, 7.0]);
        assert_eq!(chart.trend_delta(), Some(-3.0));
    }

    #[test]
    fn sparkline_points_keep_single_value_visible() {
        let points = sparkline_points(
            &[5.0],
            (0.0, 10.0),
            px(0.0),
            px(0.0),
            px(100.0),
            px(50.0),
            Some(240),
        );
        assert_eq!(points.len(), 1);
        assert_eq!(points[0].x, px(0.0));
    }

    #[test]
    fn sparkline_downsamples_dense_values() {
        let values = (0..1000).map(|index| index as f64).collect::<Vec<_>>();
        let points = sparkline_points(
            &values,
            (0.0, 1000.0),
            px(0.0),
            px(0.0),
            px(100.0),
            px(50.0),
            Some(80),
        );
        assert!(points.len() <= 80);
    }
}