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
use std::panic::AssertUnwindSafe;
use std::path::PathBuf;

use cushy::figures::units::Px;
use cushy::figures::Size;
use cushy::widget::MakeWidget;
use cushy::widgets::container::ContainerShadow;
use cushy::window::{AnimationRecorder, Rgba8, VirtualRecorder, VirtualRecorderBuilder};

pub struct ExampleBuilder {
    name: &'static str,
    recorder: VirtualRecorderBuilder<Rgba8>,
}

impl ExampleBuilder {
    #[must_use]
    pub fn finish(self) -> Example {
        Example {
            name: self.name,
            recorder: self.recorder.finish().expect("error creating recorder"),
        }
    }

    pub fn untested_still_frame(self) {
        self.finish().untested_still_frame();
    }

    pub fn prepare_with<Prepare>(self, prepare: Prepare) -> Example
    where
        Prepare: FnOnce(&mut VirtualRecorder<Rgba8>),
    {
        self.finish().prepare_with(prepare)
    }

    pub fn still_frame<Test>(self, test: Test)
    where
        Test: FnOnce(&mut VirtualRecorder<Rgba8>),
    {
        self.finish().still_frame(test);
    }

    pub fn animated<Test>(self, test: Test)
    where
        Test: FnOnce(&mut AnimationRecorder<'_, Rgba8>),
    {
        self.finish().animated(test);
    }
}

fn target_dir() -> PathBuf {
    let current_dir = std::env::current_dir().expect("missing current dir");
    let mut target_dir = current_dir.join("guide").join("src").join("examples");
    if !target_dir.is_dir() {
        target_dir = current_dir
            .parent()
            .expect("missing guide folder")
            .join("src")
            .join("examples");
    }
    assert!(
        target_dir.is_dir(),
        "current directory is not guide-examples or the root directory"
    );

    target_dir
}

pub struct Example {
    name: &'static str,
    recorder: VirtualRecorder<Rgba8>,
}

impl Example {
    pub fn build(
        name: &'static str,
        interface: impl MakeWidget,
        width: u16,
        height: Option<u16>,
    ) -> ExampleBuilder {
        let mut contents = interface
            .contain()
            .shadow(ContainerShadow::drop(Px::new(16)))
            .width(Px::new(i32::from(width)));
        if let Some(height) = height {
            contents = contents.height(Px::new(i32::from(height)));
        }
        ExampleBuilder {
            name,
            recorder: contents
                .build_recorder()
                .with_alpha()
                .resize_to_fit()
                .size(Size::new(
                    u32::from(width),
                    u32::from(height.unwrap_or(432)),
                )),
        }
    }

    pub fn untested_still_frame(self) {
        self.still_frame(|_| {});
    }

    #[must_use]
    pub fn prepare_with<Prepare>(mut self, prepare: Prepare) -> Self
    where
        Prepare: FnOnce(&mut VirtualRecorder<Rgba8>),
    {
        prepare(&mut self.recorder);
        self
    }

    pub fn still_frame<Test>(mut self, test: Test)
    where
        Test: FnOnce(&mut VirtualRecorder<Rgba8>),
    {
        let capture = std::env::var("CAPTURE").is_ok();
        let errored =
            std::panic::catch_unwind(AssertUnwindSafe(|| test(&mut self.recorder))).is_err();
        if errored || capture {
            let path = target_dir().join(format!("{}.png", self.name));
            self.recorder
                .image()
                .save(&path)
                .expect("error saving file");
            println!("Wrote {}", path.display());

            if errored {
                std::process::exit(-1);
            }
        }
    }

    pub fn animated<Test>(mut self, test: Test)
    where
        Test: FnOnce(&mut AnimationRecorder<'_, Rgba8>),
    {
        self.recorder.refresh().expect("error refreshing recorder");
        let mut animation = self.recorder.record_animated_png(60);
        let capture = std::env::var("CAPTURE").is_ok();
        let errored = std::panic::catch_unwind(AssertUnwindSafe(|| test(&mut animation))).is_err();
        if errored || capture {
            let path = target_dir().join(format!("{}.png", self.name));
            animation.write_to(&path).expect("error saving file");
            println!("Wrote {}", path.display());

            if errored {
                std::process::exit(-1);
            }
        }
    }
}

#[macro_export]
macro_rules! example {
    ($name:ident) => {
        $crate::example!($name, 750)
    };
    ($name:ident, $width:expr) => {
        $crate::example::Example::build(stringify!($name), $name(), $width, None)
    };
    ($name:ident, $width:expr, $height:expr) => {
        $crate::example::Example::build(stringify!($name), $name(), $width, Some($height))
    };
}