icon_loader/icon/
icon_dir.rs

1use std::path::{Path, PathBuf};
2
3/// Struct that holds information about a directory containing a set of icons
4/// with a particular size.
5#[derive(Clone, Debug, Hash, PartialEq, Eq)]
6pub struct IconDir {
7    path: PathBuf,
8    size: u16,
9    scale: u16,
10    context: Option<String>,
11    size_type: IconSizeType,
12    max_size: Option<u16>,
13    min_size: Option<u16>,
14    threshold: Option<u16>,
15}
16
17impl IconDir {
18    pub(crate) fn new(path: PathBuf, properties: &ini::Properties) -> Self {
19        let mut dir_info = Self {
20            path,
21            size: 0,
22            scale: 1,
23            context: None,
24            size_type: IconSizeType::Threshold,
25            max_size: None,
26            min_size: None,
27            threshold: None,
28        };
29
30        for (key, value) in properties.iter() {
31            match key {
32                "Size" => {
33                    if let Ok(size) = value.parse() {
34                        dir_info.size = size;
35                    }
36                }
37                "Scale" => {
38                    if let Ok(scale) = value.parse() {
39                        dir_info.scale = scale;
40                    }
41                }
42                "Context" => {
43                    dir_info.context = Some(String::from(value));
44                }
45                "Type" => dir_info.size_type = value.into(),
46                "Threshold" => {
47                    if let Ok(threshold) = value.parse() {
48                        dir_info.threshold = Some(threshold);
49                    }
50                }
51                "MinSize" => {
52                    if let Ok(min_size) = value.parse() {
53                        dir_info.min_size = Some(min_size);
54                    }
55                }
56                "MaxSize" => {
57                    if let Ok(max_size) = value.parse() {
58                        dir_info.max_size = Some(max_size);
59                    }
60                }
61                _ => {}
62            }
63        }
64
65        dir_info
66    }
67
68    /// Returns the path of this icon dir.
69    pub fn path(&self) -> &Path {
70        &self.path
71    }
72
73    /// Returns the size of the icons contained.
74    pub const fn size(&self) -> u16 {
75        self.size
76    }
77
78    /// Returns the scale of the icons contained.
79    pub const fn scale(&self) -> u16 {
80        self.scale
81    }
82
83    /// Returns the context of the icons contained.
84    pub fn context(&self) -> Option<&str> {
85        self.context.as_deref()
86    }
87
88    /// Returns the type of icon sizes contained.
89    pub const fn size_type(&self) -> IconSizeType {
90        self.size_type
91    }
92
93    /// Returns the max size of icons contained.
94    pub fn max_size(&self) -> u16 {
95        self.max_size.unwrap_or_else(|| self.size())
96    }
97
98    /// Returns the min size of icons contained.
99    pub fn min_size(&self) -> u16 {
100        self.min_size.unwrap_or_else(|| self.size())
101    }
102
103    /// Returns the threshold of icons contained.
104    pub fn threshold(&self) -> u16 {
105        self.threshold.unwrap_or(2)
106    }
107
108    pub(crate) const fn is_valid(&self) -> bool {
109        self.size != 0
110    }
111}
112
113#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
114/// The size type of icons contained in an [`IconDir`](crate::icon::IconDir).
115pub enum IconSizeType {
116    /// Icons with a fixed size.
117    Fixed,
118
119    /// Scalable icons.
120    Scalable,
121
122    /// Icons with a fixed size and a threshold.
123    Threshold,
124}
125
126impl<S: AsRef<str>> From<S> for IconSizeType {
127    fn from(s: S) -> Self {
128        match s.as_ref() {
129            "Fixed" => IconSizeType::Fixed,
130            "Scalable" => IconSizeType::Scalable,
131            _ => IconSizeType::Threshold,
132        }
133    }
134}