cosmic_freedesktop_icons/theme/
directories.rs

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
#[derive(Debug)]
pub struct Directory<'a> {
    pub name: &'a str,
    pub size: i16,
    pub scale: i16,
    pub context: Option<&'a str>,
    pub type_: DirectoryType,
    pub maxsize: i16,
    pub minsize: i16,
    pub threshold: i16,
}

impl Directory<'_> {
    pub fn match_size(&self, size: u16, scale: u16) -> bool {
        let scale = scale as i16;
        let size = size as i16;

        if self.scale != scale {
            false
        } else {
            match self.type_ {
                DirectoryType::Fixed => self.size == size,
                DirectoryType::Scalable => self.minsize <= size && size <= self.maxsize,
                DirectoryType::Threshold => {
                    self.size - self.threshold <= size && size <= self.size + self.threshold
                }
            }
        }
    }

    pub fn directory_size_distance(&self, size: u16, scale: u16) -> i16 {
        let scaled_size = self.size * self.scale;
        let min_scaled_size = self.minsize * self.scale;
        let max_scaled_size = self.maxsize * self.scale;
        let scale = scale as i16;
        let size = size as i16;
        let scaled_requested_size = size * scale;

        match self.type_ {
            DirectoryType::Fixed => scaled_size - scaled_requested_size,
            DirectoryType::Scalable => {
                if scaled_requested_size < min_scaled_size {
                    min_scaled_size - scaled_requested_size
                } else if scaled_requested_size < max_scaled_size {
                    scaled_requested_size - max_scaled_size
                } else {
                    0
                }
            }
            DirectoryType::Threshold => {
                if scaled_requested_size < (self.size - self.threshold) * scale {
                    min_scaled_size - scaled_requested_size
                } else if scaled_requested_size > (self.size + self.threshold) * scale {
                    scaled_requested_size - max_scaled_size
                } else {
                    0
                }
            }
        }
    }
}

#[derive(Debug)]
pub enum DirectoryType {
    Fixed,
    Scalable,
    Threshold,
}

impl Default for DirectoryType {
    fn default() -> Self {
        Self::Threshold
    }
}

impl From<&str> for DirectoryType {
    fn from(value: &str) -> Self {
        match value {
            "Fixed" => DirectoryType::Fixed,
            "Scalable" => DirectoryType::Scalable,
            _ => DirectoryType::Threshold,
        }
    }
}