embedded_graphics_web_simulator/
output_settings.rs

1/* source:https://github.com/embedded-graphics/simulator/blob/master/src/output_settings.rs */
2
3/// Output settings.
4#[derive(Debug, PartialEq, Eq, Clone)]
5pub struct OutputSettings {
6    /// Pixel scale.
7    pub scale: u32,
8    /// Spacing between pixels.
9    pub pixel_spacing: u32,
10}
11
12impl Default for OutputSettings {
13    fn default() -> Self {
14        OutputSettingsBuilder::new().build()
15    }
16}
17
18/// Output settings builder.
19pub struct OutputSettingsBuilder {
20    scale: Option<u32>,
21    pixel_spacing: Option<u32>,
22}
23
24impl OutputSettingsBuilder {
25    /// Creates new output settings builder.
26    pub fn new() -> Self {
27        Self {
28            scale: None,
29            pixel_spacing: None,
30        }
31    }
32
33    /// Sets the pixel scale.
34    ///
35    /// A scale of `2` or higher is useful for viewing the simulator on high DPI displays.
36    ///
37    /// # Panics
38    ///
39    /// Panics if the scale is set to `0`.
40    pub fn scale(mut self, scale: u32) -> Self {
41        if scale == 0 {
42            panic!("scale must be >= 0");
43        }
44
45        self.scale = Some(scale);
46
47        self
48    }
49
50    /// Sets the gap between pixels.
51    ///
52    /// Most lower resolution displays have visible gaps between individual pixels.
53    /// This effect can be simulated by setting the pixel spacing to a value greater
54    /// than `0`.
55    pub fn pixel_spacing(mut self, pixel_spacing: u32) -> Self {
56        self.pixel_spacing = Some(pixel_spacing);
57
58        self
59    }
60
61    /// Builds the output settings.
62    pub fn build(self) -> OutputSettings {
63        OutputSettings {
64            scale: self.scale.unwrap_or(1),
65            pixel_spacing: self.pixel_spacing.unwrap_or(0),
66        }
67    }
68}