slintplot-gallery 0.4.3

Sample browser for SlintPlot: every chart type, with live data editing, themes and dark mode
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! The code panel's files: a sample's Slint code from ui/gallery.slint (see
//! build.rs), in a window that declares the gallery properties it reads, with
//! the values they have now, and the rest of a Rust project that runs it.

use slint::{Color, Model, ModelRc, SharedString, VecModel};

use crate::{
    ChartCandle, ChartDataset, ChartGanttTask, ChartLegendPosition, ChartSeriesType, ChartSlice,
    ChartSunburstItem, ChartTreemapItem, ChartWaterfallStep, ChartXY, CodeFile, Gallery,
    SAMPLE_CODE, SLINTPLOT_NAMES,
};

/// Lines are wrapped at this width.
const WIDTH: usize = 100;
/// Lists longer than this show their first values and a comment.
const MAX_ITEMS: usize = 1000;
/// List items this short fill the lines; longer ones get a line each.
const SHORT: usize = 40;

/// The code panel's tabs for the sample `id`: its Slint code with the
/// gallery's current data, and the Rust project around it.
pub fn sample_files(ui: &Gallery, id: &str) -> ModelRc<CodeFile> {
    let slint = sample_code(ui, id);
    let files = [
        ("ui/app.slint", slint.clone()),
        ("src/main.rs", MAIN_RS.to_string()),
        ("build.rs", BUILD_RS.to_string()),
        ("Cargo.toml", cargo_toml(id)),
        (
            "How to run",
            how_to_run(slint.contains("debug(self.status)")),
        ),
    ];
    let files = files.map(|(name, code)| CodeFile {
        name: name.into(),
        code: code.into(),
    });
    ModelRc::new(VecModel::from(files.to_vec()))
}

const MAIN_RS: &str = "\
slint::include_modules!();

fn main() -> Result<(), slint::PlatformError> {
    let app = Example::new()?;
    // Connects the charts to SlintPlot's Rust code.
    slintplot::register!(app);
    app.run()
}
";

const BUILD_RS: &str = "\
// Compiles ui/app.slint, with SlintPlot's charts importable as \"@slintplot\".
fn main() {
    let config = slint_build::CompilerConfiguration::new().with_library_paths(
        std::collections::HashMap::from([(\"slintplot\".into(), slintplot::library_path())]),
    );
    slint_build::compile_with_config(\"ui/app.slint\", config).unwrap();
}
";

/// The project's Cargo.toml, with SlintPlot of the gallery's version: its
/// major and minor number.
fn cargo_toml(id: &str) -> String {
    let version = env!("CARGO_PKG_VERSION")
        .rsplit_once('.')
        .map_or("", |(minor, _)| minor);
    format!(
        "[package]\n\
         name = \"{id}-example\"\n\
         version = \"0.1.0\"\n\
         edition = \"2021\"\n\n\
         [dependencies]\n\
         slint = \"1.18\"\n\
         slintplot = \"{version}\"\n\n\
         [build-dependencies]\n\
         slint-build = \"1.18\"\n\
         slintplot = \"{version}\"\n"
    )
}

/// The steps from the other tabs to a running chart.
fn how_to_run(prints_clicks: bool) -> String {
    let mut text = String::from(
        "\
This chart runs as a small Rust app made of the files in the other tabs.

1. Make a new project:

       cargo new my-chart
       cd my-chart

2. Replace Cargo.toml with the Cargo.toml tab.
3. Save the build.rs tab as build.rs, next to Cargo.toml.
4. Save the ui/app.slint tab as ui/app.slint.
5. Replace src/main.rs with the src/main.rs tab.
6. Run it:

       cargo run
",
    );
    if prints_clicks {
        text.push_str("\nClicks on the chart are printed in the terminal.\n");
    }
    text.push_str(
        "\n\
On Linux, Slint needs the fontconfig and xkbcommon development packages first\n\
(libfontconfig-dev and libxkbcommon-dev on Debian and Ubuntu).

More in Getting started: https://slintplot.dev/#getting-started
",
    );
    text
}

/// The Slint code of the sample `id`, with the gallery's current data.
fn sample_code(ui: &Gallery, id: &str) -> String {
    let Some(body) = template(id) else {
        return String::new();
    };
    let mut properties = String::new();
    for name in properties_read(body) {
        if let Some(callback) = callback_declaration(&name) {
            properties.push_str(callback);
            continue;
        }
        let (ty, value) = property(ui, &name);
        let start = format!("    property <{ty}> {name}: ");
        properties.push_str(&format!("{start}{};\n", value.render(start.len())));
    }
    if body.contains("root.status") {
        properties.push_str("    // What was clicked, written by the chart's callbacks.\n");
        properties.push_str("    property <string> status;\n");
        properties.push_str("    changed status => {\n        debug(self.status);\n    }\n");
    }
    let mut window = String::from("export component Example inherits Window {\n");
    window.push_str("    preferred-width: 720px;\n    preferred-height: 480px;\n");
    if !properties.is_empty() {
        window.push_str(&properties);
    }
    // In a layout, the chart fills the window.
    window.push_str("\n    VerticalLayout {\n        padding: 16px;\n\n");
    for line in body.lines() {
        if !line.is_empty() {
            window.push_str("        ");
        }
        window.push_str(line);
        window.push('\n');
    }
    window.push_str("    }\n}\n");
    // ChartEngine is exported so that slintplot::register! can connect it.
    let code = format!("export {{ ChartEngine }}\n\n{window}");
    format!("{}\n{code}", import_line(&code))
}

/// The sample's code from ui/gallery.slint, if it has any.
fn template(id: &str) -> Option<&'static str> {
    SAMPLE_CODE
        .iter()
        .find(|(sample, _)| *sample == id)
        .map(|(_, code)| *code)
}

/// The gallery properties `body` reads, `root.status` aside, in order.
pub fn properties_read(body: &str) -> Vec<String> {
    let mut names = Vec::new();
    for (i, _) in body.match_indices("root.") {
        let name: String = body[i + 5..]
            .chars()
            .take_while(|c| c.is_ascii_alphanumeric() || *c == '-')
            .collect();
        if name != "status" && !names.contains(&name) {
            names.push(name);
        }
    }
    names
}

/// The words of `code`.
fn words(code: &str) -> Vec<&str> {
    code.split(|c: char| !c.is_ascii_alphanumeric())
        .filter(|w| !w.is_empty())
        .collect()
}

/// The import statement of the SlintPlot names `code` uses.
fn import_line(code: &str) -> String {
    let words = words(code);
    // The gallery doesn't import the enum the dataset values may use.
    let mut used: Vec<&str> = SLINTPLOT_NAMES
        .iter()
        .chain(&["ChartSeriesType"])
        .copied()
        .filter(|name| words.contains(name))
        .collect();
    used.dedup();
    used.sort_unstable();
    let line = format!("import {{ {} }} from \"@slintplot\";\n", used.join(", "));
    if line.len() <= WIDTH {
        return line;
    }
    format!(
        "import {{\n    {},\n}} from \"@slintplot\";\n",
        used.join(",\n    ")
    )
}

/// The declaration of the gallery callback `name`, if it is one, so that the
/// code that calls it compiles.
fn callback_declaration(name: &str) -> Option<&'static str> {
    match name {
        "edit-task" => Some(
            "    // This is simplified code: in the gallery, clicking a task opens a dialog\n\
             \x20   // that edits it. The full example code, with the dialog (TaskEditor), is on\n\
             \x20   // GitHub: https://github.com/trondegil/slintplot/blob/main/examples/gallery/ui/gallery.slint\n\
             \x20   callback edit-task(index: int);\n",
        ),
        _ => None,
    }
}

/// The type of the gallery property `name`, and its value now.
fn property(ui: &Gallery, name: &str) -> (&'static str, Lit) {
    let value = match name {
        // The gallery's layout for phones, below 720 pixels wide.
        "narrow" => false.lit(),
        "labels" => list(ui.get_labels()),
        "row-labels" => list(ui.get_row_labels()),
        "datasets" => list(ui.get_datasets()),
        "slices" => list(ui.get_slices()),
        "channels" => list(ui.get_channels()),
        "grid" => list(ui.get_grid()),
        "growth" => list(ui.get_growth()),
        "bullet-values" => list(ui.get_bullet_values()),
        "tree" => list(ui.get_tree()),
        "sun" => list(ui.get_sun()),
        "steps" => list(ui.get_steps()),
        "candles" => list(ui.get_candles()),
        "matrix-values" => list(ui.get_matrix_values()),
        "matrix-x-labels" => list(ui.get_matrix_x_labels()),
        "matrix-y-labels" => list(ui.get_matrix_y_labels()),
        "matrix-totals" => list(ui.get_matrix_totals()),
        "ring-class-colors" => list(ui.get_ring_class_colors()),
        "tile-color" => ui.get_tile_color().lit(),
        "gantt-tasks" => list(ui.get_gantt_tasks()),
        "gauge-value" => ui.get_gauge_value().lit(),
        "view-start" => ui.get_view_start().lit(),
        "view-end" => ui.get_view_end().lit(),
        "ohlc" => ui.get_ohlc().lit(),
        "skip-gaps" => ui.get_skip_gaps().lit(),
        "decimate" => ui.get_decimate().lit(),
        "calendar-colors" => ui.get_calendar_colors().lit(),
        "legend-position" => ui.get_legend_position().lit(),
        "sample-title" => ui.get_sample_title().lit(),
        "language-title" => ui.get_language_title().lit(),
        "language-y-title" => ui.get_language_y_title().lit(),
        "language-y2-title" => ui.get_language_y2_title().lit(),
        _ => panic!("the code panel doesn't know the gallery property {name}"),
    };
    (property_type(name).unwrap(), value)
}

/// The Slint type of the gallery property `name`.
pub fn property_type(name: &str) -> Option<&'static str> {
    Some(match name {
        "narrow" | "ohlc" | "skip-gaps" | "decimate" => "bool",
        "labels" | "row-labels" | "matrix-totals" => "[string]",
        "matrix-values" => "[[[float]]]",
        "ring-class-colors" => "[[color]]",
        "tile-color" => "color",
        "matrix-x-labels" | "matrix-y-labels" => "[[string]]",
        "datasets" => "[ChartDataset]",
        "slices" | "channels" => "[ChartSlice]",
        "grid" | "growth" | "bullet-values" => "[float]",
        "tree" => "[ChartTreemapItem]",
        "sun" => "[ChartSunburstItem]",
        "steps" => "[ChartWaterfallStep]",
        "candles" => "[ChartCandle]",
        "gantt-tasks" => "[ChartGanttTask]",
        "gauge-value" | "view-start" | "view-end" => "float",
        "calendar-colors" => "int",
        "legend-position" => "ChartLegendPosition",
        "sample-title" | "language-title" | "language-y-title" | "language-y2-title" => "string",
        _ => return None,
    })
}

/// A Slint value, to be written within a width.
enum Lit {
    Atom(String),
    /// The items shown, and how many more there are.
    List(Vec<Lit>, usize),
    Struct(Vec<(&'static str, Lit)>),
}

impl Lit {
    /// The value on one line.
    fn flat(&self) -> String {
        match self {
            Lit::Atom(atom) => atom.clone(),
            Lit::List(items, _) => {
                let items: Vec<String> = items.iter().map(Lit::flat).collect();
                format!("[{}]", items.join(", "))
            }
            Lit::Struct(fields) => {
                let fields: Vec<String> = fields
                    .iter()
                    .map(|(name, value)| format!("{name}: {}", value.flat()))
                    .collect();
                format!("{{ {} }}", fields.join(", "))
            }
        }
    }

    /// The value starting `column` characters in, on one line if it fits,
    /// else over lines indented by `indent`.
    fn render_at(&self, indent: usize, column: usize) -> String {
        let flat = self.flat();
        let more = matches!(self, Lit::List(_, more) if *more > 0);
        if column + flat.len() <= WIDTH && !more {
            return flat;
        }
        let inner = " ".repeat(indent + 4);
        let close = " ".repeat(indent);
        match self {
            Lit::Atom(_) => flat,
            Lit::List(items, more) => {
                let mut out = String::from("[\n");
                let flats: Vec<String> = items.iter().map(Lit::flat).collect();
                if flats.iter().all(|item| item.len() <= SHORT) {
                    // Values fill the lines.
                    let mut line = inner.clone();
                    for item in &flats {
                        let item = format!("{item},");
                        if line.len() + 1 + item.len() > WIDTH && !line.trim().is_empty() {
                            out.push_str(line.trim_end());
                            out.push('\n');
                            line = inner.clone();
                        } else if !line.trim().is_empty() {
                            line.push(' ');
                        }
                        line.push_str(&item);
                    }
                    if !line.trim().is_empty() {
                        out.push_str(&line);
                        out.push('\n');
                    }
                } else {
                    for item in items {
                        out.push_str(&format!(
                            "{inner}{},\n",
                            item.render_at(indent + 4, inner.len())
                        ));
                    }
                }
                if *more > 0 {
                    out.push_str(&format!(
                        "{inner}// ... and {more} more, made in examples/gallery/src/main.rs\n"
                    ));
                }
                out + &close + "]"
            }
            Lit::Struct(fields) => {
                let mut out = String::from("{\n");
                for (name, value) in fields {
                    let start = inner.len() + name.len() + 2;
                    out.push_str(&format!(
                        "{inner}{name}: {},\n",
                        value.render_at(indent + 4, start)
                    ));
                }
                out + &close + "}"
            }
        }
    }

    /// The value as a property's, which starts `column` characters in.
    fn render(&self, column: usize) -> String {
        self.render_at(4, column)
    }
}

/// A Slint value of a Rust one.
trait ToLit {
    fn lit(&self) -> Lit;
}

/// The items of `model`, up to `MAX_ITEMS`.
fn list<T: ToLit + Clone + 'static>(model: ModelRc<T>) -> Lit {
    let shown = model.row_count().min(MAX_ITEMS);
    let items = (0..shown)
        .filter_map(|i| model.row_data(i))
        .map(|item| item.lit());
    Lit::List(items.collect(), model.row_count() - shown)
}

impl ToLit for f32 {
    fn lit(&self) -> Lit {
        // Slint has no NaN literal, but 0 / 0 is NaN.
        Lit::Atom(if self.is_nan() {
            "0 / 0".into()
        } else if self.is_infinite() {
            format!("{}1 / 0", if *self < 0.0 { "-" } else { "" })
        } else {
            // + 0.0 turns -0 into 0.
            format!("{}", self + 0.0)
        })
    }
}

impl ToLit for i32 {
    fn lit(&self) -> Lit {
        Lit::Atom(self.to_string())
    }
}

impl ToLit for bool {
    fn lit(&self) -> Lit {
        Lit::Atom(self.to_string())
    }
}

impl ToLit for SharedString {
    fn lit(&self) -> Lit {
        let mut out = String::from("\"");
        for c in self.chars() {
            match c {
                '"' => out.push_str("\\\""),
                '\\' => out.push_str("\\\\"),
                '\n' => out.push_str("\\n"),
                c => out.push(c),
            }
        }
        out.push('"');
        Lit::Atom(out)
    }
}

impl ToLit for Color {
    fn lit(&self) -> Lit {
        let (r, g, b, a) = (self.red(), self.green(), self.blue(), self.alpha());
        Lit::Atom(if a == 255 {
            format!("#{r:02x}{g:02x}{b:02x}")
        } else {
            format!("#{r:02x}{g:02x}{b:02x}{a:02x}")
        })
    }
}

impl<T: ToLit + Clone + 'static> ToLit for ModelRc<T> {
    fn lit(&self) -> Lit {
        list(self.clone())
    }
}

impl ToLit for ChartSeriesType {
    fn lit(&self) -> Lit {
        Lit::Atom(match self {
            ChartSeriesType::Line => "ChartSeriesType.line".into(),
            ChartSeriesType::Bar => "ChartSeriesType.bar".into(),
            _ => "ChartSeriesType.auto".into(),
        })
    }
}

impl ToLit for ChartLegendPosition {
    fn lit(&self) -> Lit {
        Lit::Atom(match self {
            ChartLegendPosition::Bottom => "ChartLegendPosition.bottom".into(),
            ChartLegendPosition::Left => "ChartLegendPosition.left".into(),
            ChartLegendPosition::Right => "ChartLegendPosition.right".into(),
            _ => "ChartLegendPosition.top".into(),
        })
    }
}

/// Gantt task times in whole days are written as days of 86400 seconds.
impl ToLit for ChartGanttTask {
    fn lit(&self) -> Lit {
        let time = |seconds: f32| {
            let days = seconds / 86400.0;
            if seconds != 0.0 && days.fract() == 0.0 {
                Lit::Atom(format!("{days} * 86400"))
            } else {
                seconds.lit()
            }
        };
        let mut fields = vec![("label", self.label.lit())];
        if !self.group.is_empty() {
            fields.push(("group", self.group.lit()));
        }
        fields.push(("start", time(self.start)));
        fields.push(("end", time(self.end)));
        if self.progress != 0.0 {
            fields.push(("progress", self.progress.lit()));
        }
        if self.color.alpha() != 0 {
            fields.push(("color", self.color.lit()));
        }
        Lit::Struct(fields)
    }
}

/// Struct literals with the fields that differ from their defaults.
macro_rules! struct_lit {
    ($($ty:ident { $($field:ident: $name:literal),* })*) => {$(
        impl ToLit for $ty {
            fn lit(&self) -> Lit {
                let default = $ty::default();
                let mut fields = Vec::new();
                $(
                    let value = self.$field.lit();
                    if value.flat() != default.$field.lit().flat() {
                        fields.push(($name, value));
                    }
                )*
                Lit::Struct(fields)
            }
        }
    )*};
}

struct_lit! {
    ChartXY { x: "x", y: "y", r: "r" }
    ChartDataset {
        label: "label", series_type: "series-type", values: "values", points: "points",
        color: "color", hidden: "hidden", fill: "fill", tension: "tension",
        stepped: "stepped", right_axis: "right-axis"
    }
    ChartSlice { label: "label", value: "value", color: "color", hidden: "hidden" }
    ChartTreemapItem { label: "label", value: "value", group: "group", color: "color" }
    ChartSunburstItem {
        label: "label", value: "value", parent: "parent", color: "color", hidden: "hidden"
    }
    ChartWaterfallStep { label: "label", value: "value", total: "total" }

    ChartCandle {
        x: "x", open: "open", high: "high", low: "low", close: "close", volume: "volume"
    }
}

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

    #[test]
    fn every_sample_has_code_and_known_properties() {
        for sample in crate::SAMPLES {
            let body = template(sample.id).unwrap_or_else(|| panic!("no code for {}", sample.id));
            for name in properties_read(body) {
                assert!(
                    property_type(&name).is_some() || callback_declaration(&name).is_some(),
                    "{}: root.{name}",
                    sample.id
                );
            }
        }
    }

    /// The code panel writes every sample's properties without panicking,
    /// with the data the sample has when shown.
    #[test]
    fn every_sample_code_panel_opens() {
        crate::snapshot::install();
        let (ui, state) = crate::gallery().unwrap();
        for sample in crate::SAMPLES {
            state.borrow_mut().select(sample);
            let files = sample_files(&ui, sample.id);
            let slint = files.row_data(0).unwrap().code;
            assert!(slint.contains("export component Example"), "{}", sample.id);
            if let Some(dir) = option_env!("GALLERY_CODE_DIR") {
                std::fs::write(format!("{dir}/{}.slint", sample.id), slint.as_str()).unwrap();
            }
        }
    }

    #[test]
    fn cargo_toml_takes_slintplot_of_the_gallerys_version() {
        let version = env!("CARGO_PKG_VERSION");
        let minor = &version[..version.rfind('.').unwrap()];
        assert!(cargo_toml("bar").contains(&format!("slintplot = \"{minor}\"\n")));
    }

    #[test]
    fn values_are_slint_literals() {
        assert_eq!(f32::NAN.lit().flat(), "0 / 0");
        assert_eq!(2.5f32.lit().flat(), "2.5");
        assert_eq!(
            SharedString::from("a \"b\"").lit().flat(),
            "\"a \\\"b\\\"\""
        );
        let slice = ChartSlice {
            label: "Web".into(),
            value: 12.0,
            ..Default::default()
        };
        assert_eq!(slice.lit().flat(), "{ label: \"Web\", value: 12 }");
    }
}