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
//! Functionality to work with gdk pixbufs.

use crate::{error, Result};
use gdk_pixbuf::Pixbuf;
use indexmap::IndexMap;
use snafu::ResultExt;
use std::path::Path;

/// A trait for providing pixbufs.
pub trait Pixbufs {
    /// Get a pixbuf.
    fn get_pixbuf(&self, k: &str) -> Option<Pixbuf>;

    /// Get a pixbuf, or if not present, a default pixbuf.
    fn get_pixbuf_or_default(&self, k: &str) -> Pixbuf;
}

impl Pixbufs for IndexMap<String, Pixbuf> {
    fn get_pixbuf(&self, k: &str) -> Option<Pixbuf> {
        self.get(&k.to_string()).cloned()
    }

    fn get_pixbuf_or_default(&self, k: &str) -> Pixbuf {
        self.get_pixbuf(k).unwrap_or_else(|| {
            Pixbuf::load_from_bytes_with_size(
                include_bytes!("../assets/default.svg").as_ref(),
                64u16,
            )
            .unwrap()
        })
    }
}

/// A trait for loading pixbufs from the icons of a source.
pub trait LoadPixbufs {
    /// The pixbufs type which gets loaded.
    type Loaded: Pixbufs;

    /// Load the pixbufs.
    fn load_pixbufs(&self) -> Result<Self::Loaded>;
}

impl<T: ommui_data::samplic::LoadableDevice> LoadPixbufs for T {
    type Loaded = IndexMap<String, Pixbuf>;

    fn load_pixbufs(&self) -> Result<Self::Loaded> {
        self.load_icon_index()
            .context(error::OmmuiData)?
            .into_iter()
            .map(|k| {
                let raw = self
                    .load_icon(k.as_str())
                    .context(error::OmmuiData)?;
                Ok((
                    k,
                    gdk_pixbuf::Pixbuf::load_from_bytes_with_size(
                        raw.as_bytes(),
                        64u16,
                    )
                    .unwrap(),
                ))
            })
            .collect::<Result<_>>()
    }
}

/// A trait for items loadable from in-memory byte arrays.
pub trait LoadFromBytes: Sized {
    /// Load the item with a maximum width and height.
    fn load_from_bytes_at_scale<B: Into<glib::Bytes>>(
        data: B,
        max_width: u16,
        max_height: u16,
    ) -> Result<Self>;

    /// Load the item with a defined pixel size.
    fn load_from_bytes_with_size<B: Into<glib::Bytes>>(
        data: B,
        pixelsize: u16,
    ) -> Result<Self> {
        Self::load_from_bytes_at_scale(data, pixelsize, pixelsize)
    }
}

impl LoadFromBytes for Pixbuf {
    fn load_from_bytes_at_scale<B: Into<glib::Bytes>>(
        data: B,
        max_width: u16,
        max_height: u16,
    ) -> Result<Self> {
        let b: glib::Bytes = data.into();
        let stream = gio::MemoryInputStream::new_from_bytes(&b);
        Ok(gdk_pixbuf::Pixbuf::new_from_stream_at_scale(
            &stream,
            i32::from(max_width),
            i32::from(max_height),
            true,
            None::<&gio::Cancellable>,
        )
        .context(error::GLib)?)
    }
}

/// A trait for items loadable from file.
pub trait LoadFromFile: Sized {
    /// Load the item with a maximum width and height.
    fn load_from_file_at_scale<P: AsRef<Path>>(
        filename: P,
        max_width: u16,
        max_height: u16,
    ) -> Result<Self>;

    /// Load the item with a defined pixel size.
    fn load_from_file_with_size<P: AsRef<Path>>(
        filename: P,
        pixelsize: u16,
    ) -> Result<Self> {
        Self::load_from_file_at_scale(filename, pixelsize, pixelsize)
    }
}

impl LoadFromFile for Pixbuf {
    fn load_from_file_at_scale<P: AsRef<Path>>(
        filename: P,
        max_width: u16,
        max_height: u16,
    ) -> Result<Self> {
        let file = gio::File::new_for_path(filename);
        use gio::FileExt;
        let stream =
            file.read(None::<&gio::Cancellable>).context(error::GLib)?;
        Ok(gdk_pixbuf::Pixbuf::new_from_stream_at_scale(
            &stream,
            i32::from(max_width),
            i32::from(max_height),
            true,
            None::<&gio::Cancellable>,
        )
        .context(error::GLib)?)
    }
}