-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathlayout.rs
More file actions
286 lines (239 loc) · 8.18 KB
/
Copy pathlayout.rs
File metadata and controls
286 lines (239 loc) · 8.18 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use crate::fl;
use cosmic::iced::{Alignment, Length};
use cosmic::{cosmic_theme, widget};
use kdl::{KdlDocument, KdlValue};
use std::any::Any;
use std::collections::{BTreeMap, VecDeque};
use std::io::Read;
use std::path::{Path, PathBuf};
struct Layout {
id: i32,
names: BTreeMap<String, String>,
path: PathBuf,
icon_path: PathBuf,
}
#[derive(Default)]
pub struct Page {
locale: String,
layouts: Vec<Layout>,
selected: Option<usize>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Message {
Selected(usize),
}
impl From<Message> for super::Message {
fn from(message: Message) -> Self {
super::Message::Layout(message)
}
}
impl Page {
pub fn update(&mut self, message: Message) -> cosmic::Task<super::Message> {
match message {
Message::Selected(id) => {
if let Some(layout) = self.layouts.get(id) {
self.selected = Some(id);
apply_layout(&layout.path);
}
}
}
cosmic::Task::none()
}
}
impl super::Page for Page {
fn title(&self) -> String {
fl!("layout-page")
}
fn as_any(&mut self) -> &mut dyn Any {
self
}
fn skippable(&self) -> bool {
true
}
fn init(&mut self) -> cosmic::Task<super::Message> {
#[cfg(feature = "nixos")]
let layouts_dir_path = "/run/current-system/sw/share/cosmic-layouts/";
#[cfg(not(feature = "nixos"))]
let layouts_dir_path = "/usr/share/cosmic-layouts/";
let Ok(layouts_dir) = std::fs::read_dir(layouts_dir_path) else {
return cosmic::Task::none();
};
self.layouts.clear();
let mut buffer = String::new();
for entry in layouts_dir.filter_map(Result::ok) {
let path = entry.path();
let metadata_path = path.join("layout.kdl");
let Ok(mut metadata) = std::fs::File::open(&metadata_path) else {
tracing::error!(?metadata_path, "failed to open layout file");
continue;
};
buffer.clear();
if metadata.read_to_string(&mut buffer).is_err() {
tracing::error!(?metadata_path, "failed to read layout file");
continue;
}
let document = match buffer.parse::<KdlDocument>() {
Ok(document) => document,
Err(why) => {
tracing::error!(?metadata_path, ?why, "failed to parse layout file");
continue;
}
};
let mut names = BTreeMap::new();
let mut id = -1;
for node in document.nodes() {
match node.name().value() {
"name" => {
for entry in node.entries() {
let locale = entry.name().map_or("en", |ident| ident.value());
if let KdlValue::String(name) = entry.value() {
names.insert(locale.to_owned(), name.to_owned());
}
}
}
"id" => {
if let Some(KdlValue::Integer(value)) = node.get(0) {
id = *value as i32;
}
}
_ => (),
}
}
let icon_path = path.join("icon.png");
if !icon_path.exists() {
tracing::error!(?metadata_path, "missing icon.png in layout dir");
continue;
}
self.layouts.push(Layout {
id,
names,
path,
icon_path,
})
}
self.layouts.sort_by_key(|a| a.id);
cosmic::Task::none()
}
fn open(&mut self) -> cosmic::Task<super::Message> {
if let Ok(lang) = std::env::var("LANG") {
self.locale = lang.split('.').next().unwrap_or("en").to_owned();
}
cosmic::Task::none()
}
fn view(&self) -> cosmic::Element<'_, super::Message> {
let cosmic_theme::Spacing {
space_s, space_m, ..
} = cosmic::theme::spacing();
let description = widget::text::body(fl!("layout-page", "description"))
.align_x(cosmic::iced::Alignment::Center)
.width(Length::Fill);
let mut grid = widget::grid().column_spacing(space_m).row_spacing(space_m);
for (id, layout) in self.layouts.iter().enumerate() {
if id > 0 && id % 3 == 0 {
grid = grid.insert_row();
}
grid = grid.push(layout_button(
&self.locale,
layout,
id,
self.selected,
space_s,
));
}
widget::column::with_capacity(2)
.push(widget::container(grid))
.push(description)
.align_x(Alignment::Center)
.spacing(space_s)
.into()
}
}
fn layout_button<'a>(
locale: &str,
layout: &'a Layout,
id: usize,
current: Option<usize>,
spacing: u16,
) -> cosmic::Element<'a, super::Message> {
let name = layout
.names
.get(locale)
.or_else(|| {
locale
.split('_')
.next()
.and_then(|short| layout.names.get(short))
.or_else(|| layout.names.get("en"))
})
.expect("layout does not have a name");
let thumbnail = widget::image(&layout.icon_path).width(144).height(81);
let button = widget::button::custom_image_button(thumbnail, None)
.description(name.as_str())
.class(cosmic::theme::Button::Image)
.selected(current == Some(id))
.on_press(Message::Selected(id).into());
widget::column::with_capacity(2)
.push(button)
.push(widget::text::body(name.as_str()))
.spacing(spacing)
.align_x(Alignment::Center)
.into()
}
fn copy_dir(src: &Path, dst: &Path) {
let mut dirs_to_copy = VecDeque::new();
dirs_to_copy.push_back((src.to_path_buf(), dst.to_path_buf()));
while let Some((src_dir, dst_dir)) = dirs_to_copy.pop_front() {
if let Err(why) = std::fs::create_dir_all(&dst_dir) {
tracing::error!(?dst_dir, ?why, "failed to create dir");
continue;
}
let Ok(dir) = src_dir.read_dir() else {
tracing::error!(?src_dir, "failed to read dir");
continue;
};
for entry in dir.filter_map(Result::ok) {
let src_path = entry.path();
let dst_path = dst_dir.join(entry.file_name());
if let Ok(meta) = entry.metadata() {
if meta.is_dir() {
dirs_to_copy.push_back((src_path, dst_path));
} else if meta.is_file() {
// We read and then write the contents to not preserve any metadata.
let copy_result =
std::fs::read(&src_path).and_then(|data| std::fs::write(&dst_path, &data));
if let Err(why) = copy_result {
tracing::error!(?src_path, ?dst_path, ?why, "failed to copy file");
}
}
}
}
}
}
fn apply_layout(path: &Path) {
let Ok(layout_dir) = path.read_dir() else {
tracing::error!(?path, "failed to read layout directory");
return;
};
for entry in layout_dir.filter_map(Result::ok) {
let Ok(meta) = entry.metadata() else {
continue;
};
if meta.is_dir() {
let path = entry.path();
let Some(config_name) = path.file_name() else {
continue;
};
let config_dest_path = std::env::home_dir()
.unwrap()
.join(".config/cosmic")
.join(config_name);
// Delete any existing config
_ = std::fs::remove_dir_all(&config_dest_path);
// Copy layout to local config
copy_dir(&path, &config_dest_path);
}
}
_ = std::process::Command::new("killall")
.arg("cosmic-panel")
.status();
}