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
use std::{fs::File, path::PathBuf};

use anyhow::{Context, Result};
use cairo::ImageSurface;
use derive_builder::Builder;

use crate::{
    get_table_from_config, parser, remove_float_from_config,
    remove_string_from_config,
};

/// An image to be rendered on the bar
#[derive(Debug, Builder, Clone)]
#[builder_struct_attr(allow(missing_docs))]
#[builder_impl_attr(allow(missing_docs))]
pub struct Image {
    surface: ImageSurface,
    #[builder(default)]
    x: f64,
    #[builder(default)]
    y: f64,
}

impl Image {
    /// Creates a new instance
    pub fn new(path: PathBuf, x: f64, y: f64) -> Result<Self> {
        let mut file = File::open(path)?;
        Ok(Self {
            surface: ImageSurface::create_from_png(&mut file)?,
            x,
            y,
        })
    }

    /// Attempts to parse a new instance from the global config
    ///
    /// Configuration options:
    /// - `path`: the file path of the image (PNG only)
    /// - `x`: the x coordinate of the image, relative to the panel
    /// - `y`: the y coordinate of the image, relative to the panel
    pub fn parse(name: &str) -> Result<Self> {
        let images_table = parser::IMAGES.get().unwrap();

        let mut table = get_table_from_config(name, images_table)
            .with_context(|| format!("No subtable found with name {name}"))?;

        let mut builder = ImageBuilder::default();

        let path = remove_string_from_config("path", &mut table)
            .context("No path specified")?;
        let mut file = File::open(path)?;

        builder.surface(ImageSurface::create_from_png(&mut file)?);

        if let Some(x) = remove_float_from_config("x", &mut table) {
            builder.x(x);
        }

        if let Some(y) = remove_float_from_config("y", &mut table) {
            builder.y(y);
        }

        Ok(builder.build()?)
    }

    /// Draws the image on the bar. `cr`'s (0, 0) should be at the top left
    /// corner of the panel.
    pub fn draw(&self, cr: &cairo::Context) -> Result<()> {
        cr.save()?;

        cr.set_source_surface(self.surface.as_ref(), 0.0, 0.0)?;
        cr.rectangle(
            self.x,
            self.y,
            self.surface.width() as f64,
            self.surface.height() as f64,
        );

        cr.fill()?;
        cr.restore()?;

        Ok(())
    }
}