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
use std::env::var;
use std::path::PathBuf;
use ini::Ini;
pub fn theme_search_paths() -> Vec<PathBuf> {
let mut res: Vec<PathBuf> = Vec::new();
res.push([var("HOME").unwrap(), String::from(".icons")].iter().collect());
for i in var("XDG_DATA_DIRS").unwrap_or("/usr/local/share/:/usr/share/".to_string()).split(':') {
res.push([i, "icons"].iter().collect());
}
res.push(PathBuf::from("/usr/share/pixmaps"));
res
}
#[derive(Debug)]
pub struct XCursorTheme {
name: String,
dirs: Vec<PathBuf>,
inherits: String,
search_paths: Vec<PathBuf>,
}
impl XCursorTheme {
pub fn load(name: &str, search_paths: &Vec<PathBuf>) -> Self {
let mut dirs = Vec::new();
let mut inherits = String::from("default");
for mut path in search_paths.clone() {
path.push(name);
if path.is_dir() {
dirs.push(path.clone());
}
}
for mut path in dirs.clone() {
path.push("index.theme");
if let Some(i) = theme_inherits(&path) {
inherits = i;
}
}
Self {
name: String::from(name),
dirs,
inherits,
search_paths: search_paths.clone(),
}
}
pub fn load_icon(&self, icon_name: &str) -> Option<PathBuf> {
for mut icon_path in self.dirs.clone() {
icon_path.push("cursors");
icon_path.push(icon_name);
if icon_path.is_file() {
return Some(icon_path);
}
}
if self.name == self.inherits {
return None;
}
XCursorTheme::load(&self.inherits, &self.search_paths).load_icon(icon_name)
}
}
fn theme_inherits(file_path: &PathBuf) -> Option<String> {
let ini = Ini::load_from_file(file_path).ok()?;
ini
.section(Some("Icon Theme"))?
.get("Inherits")
.map(|i| { i.to_string() })
}