forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
419 lines (378 loc) · 15.7 KB
/
main.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
#![doc = include_str!("README.md")]
use i_slint_core::model::{Model, ModelRc};
use i_slint_core::SharedVector;
use slint_interpreter::{
ComponentDefinition, ComponentHandle, ComponentInstance, SharedString, Value,
};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use clap::Parser;
use itertools::Itertools;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[derive(Clone, clap::Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg(short = 'I', name = "include path for other .slint files", number_of_values = 1, action)]
include_paths: Vec<std::path::PathBuf>,
/// The first argument is the library name, and the second argument is the path to the library.
#[arg(short = 'L', name = "library path for @library imports", number_of_values = 1, action)]
library_paths: Vec<String>,
/// The .slint file to load ('-' for stdin)
#[arg(name = "path to .slint file", action)]
path: std::path::PathBuf,
/// The style name ('native' or 'fluent')
#[arg(long, name = "style name", action)]
style: Option<String>,
/// The rendering backend
#[arg(long, name = "backend", action)]
backend: Option<String>,
/// Automatically watch the file system, and reload when it changes
#[arg(long, action)]
auto_reload: bool,
/// Load properties from a json file ('-' for stdin)
#[arg(long, name = "load data file", action)]
load_data: Option<std::path::PathBuf>,
/// Store properties values in a json file at exit ('-' for stdout)
#[arg(long, name = "save data file", action)]
save_data: Option<std::path::PathBuf>,
/// Specify callbacks handler.
/// The first argument is the callback name, and the second argument is a string that is going
/// to be passed to the shell to be executed. Occurrences of `$1` will be replaced by the first argument,
/// and so on.
#[arg(long, value_names(&["callback", "handler"]), number_of_values = 2, action)]
on: Vec<String>,
#[cfg(feature = "gettext")]
/// Translation domain
#[arg(long = "translation-domain", action)]
translation_domain: Option<String>,
#[cfg(feature = "gettext")]
/// Translation directory where the translation files are searched for
#[arg(long = "translation-dir", action)]
translation_dir: Option<std::path::PathBuf>,
}
thread_local! {static CURRENT_INSTANCE: std::cell::RefCell<Option<ComponentInstance>> = Default::default();}
static EXIT_CODE: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
fn main() -> Result<()> {
env_logger::init();
let args = Cli::parse();
if args.auto_reload && args.save_data.is_some() {
eprintln!("Cannot pass both --auto-reload and --save-data");
std::process::exit(-1);
}
if let Some(backend) = &args.backend {
std::env::set_var("SLINT_BACKEND", backend);
}
#[cfg(feature = "gettext")]
if let Some(dirname) = args.translation_dir.clone() {
i_slint_core::translations::gettext_bindtextdomain(
args.translation_domain.as_ref().map(String::as_str).unwrap_or_default(),
dirname,
)?;
};
let fswatcher = if args.auto_reload { Some(start_fswatch_thread(args.clone())?) } else { None };
let mut compiler = init_compiler(&args, fswatcher);
let c = spin_on::spin_on(compiler.build_from_path(args.path));
slint_interpreter::print_diagnostics(compiler.diagnostics());
let c = match c {
Some(c) => c,
None => std::process::exit(-1),
};
let component = c.create().unwrap();
init_dialog(&component);
if let Some(data_path) = args.load_data {
load_data(&c, &component, &data_path)?;
}
install_callbacks(&component, &args.on);
if args.auto_reload {
CURRENT_INSTANCE.with(|current| current.replace(Some(component.clone_strong())));
}
component.run().unwrap();
if let Some(data_path) = args.save_data {
let mut obj = serde_json::Map::new();
for (name, _) in c.properties() {
fn to_json(val: slint_interpreter::Value) -> Option<serde_json::Value> {
match val {
slint_interpreter::Value::Number(x) => Some(x.into()),
slint_interpreter::Value::String(x) => Some(x.as_str().into()),
slint_interpreter::Value::Bool(x) => Some(x.into()),
slint_interpreter::Value::Model(model) => {
let mut res = Vec::with_capacity(model.row_count());
for i in 0..model.row_count() {
res.push(to_json(model.row_data(i).unwrap())?);
}
Some(serde_json::Value::Array(res))
}
slint_interpreter::Value::Struct(st) => {
let mut obj = serde_json::Map::new();
for (k, v) in st.iter() {
obj.insert(k.into(), to_json(v.clone())?);
}
Some(obj.into())
}
slint_interpreter::Value::EnumerationValue(_class, value) => {
Some(value.as_str().into())
}
_ => None,
}
}
if let Some(v) = to_json(component.get_property(&name).unwrap()) {
obj.insert(name, v);
}
}
if data_path == std::path::Path::new("-") {
serde_json::to_writer_pretty(std::io::stdout(), &obj)?;
} else {
serde_json::to_writer_pretty(std::fs::File::create(data_path)?, &obj)?;
}
}
std::process::exit(EXIT_CODE.load(std::sync::atomic::Ordering::Relaxed))
}
fn init_compiler(
args: &Cli,
fswatcher: Option<Arc<Mutex<notify::RecommendedWatcher>>>,
) -> slint_interpreter::ComponentCompiler {
let mut compiler = slint_interpreter::ComponentCompiler::default();
#[cfg(feature = "gettext")]
if let Some(domain) = args.translation_domain.clone() {
compiler.set_translation_domain(domain);
}
compiler.set_include_paths(args.include_paths.clone());
compiler.set_library_paths(
args.library_paths
.iter()
.filter_map(|entry| entry.split('=').collect_tuple().map(|(k, v)| (k.into(), v.into())))
.collect(),
);
if let Some(style) = &args.style {
compiler.set_style(style.clone());
}
if let Some(watcher) = fswatcher {
notify::Watcher::watch(
&mut *watcher.lock().unwrap(),
&args.path,
notify::RecursiveMode::NonRecursive,
)
.unwrap_or_else(|err| {
eprintln!("Warning: error while watching {}: {:?}", args.path.display(), err)
});
if let Some(data_path) = &args.load_data {
notify::Watcher::watch(
&mut *watcher.lock().unwrap(),
data_path,
notify::RecursiveMode::NonRecursive,
)
.unwrap_or_else(|err| {
eprintln!("Warning: error while watching {}: {:?}", data_path.display(), err)
});
}
compiler.set_file_loader(move |path| {
notify::Watcher::watch(
&mut *watcher.lock().unwrap(),
path,
notify::RecursiveMode::NonRecursive,
)
.unwrap_or_else(|err| {
eprintln!("Warning: error while watching {}: {:?}", path.display(), err)
});
Box::pin(async { None })
})
}
compiler
}
fn init_dialog(instance: &ComponentInstance) {
for cb in instance.definition().callbacks() {
let exit_code = match cb.as_str() {
"ok-clicked" | "yes-clicked" | "close-clicked" => 0,
"cancel-clicked" | "no-clicked" => 1,
_ => continue,
};
// this is a dialog, so clicking the "x" should cancel
EXIT_CODE.store(1, std::sync::atomic::Ordering::Relaxed);
instance
.set_callback(&cb, move |_| {
EXIT_CODE.store(exit_code, std::sync::atomic::Ordering::Relaxed);
i_slint_core::api::quit_event_loop().unwrap();
Default::default()
})
.unwrap();
}
}
static PENDING_EVENTS: AtomicU32 = AtomicU32::new(0);
fn start_fswatch_thread(args: Cli) -> Result<Arc<Mutex<notify::RecommendedWatcher>>> {
let (tx, rx) = std::sync::mpsc::channel();
let w = Arc::new(Mutex::new(notify::recommended_watcher(tx)?));
let w2 = w.clone();
std::thread::spawn(move || {
while let Ok(event) = rx.recv() {
use notify::EventKind::*;
if let Ok(event) = event {
if (matches!(event.kind, Modify(_) | Remove(_) | Create(_)))
&& PENDING_EVENTS.load(Ordering::SeqCst) == 0
{
PENDING_EVENTS.fetch_add(1, Ordering::SeqCst);
let args = args.clone();
let w2 = w2.clone();
i_slint_core::api::invoke_from_event_loop(move || {
i_slint_core::future::spawn_local(reload(args, w2)).unwrap();
})
.unwrap();
}
}
}
});
Ok(w)
}
async fn reload(args: Cli, fswatcher: Arc<Mutex<notify::RecommendedWatcher>>) {
let mut compiler = init_compiler(&args, Some(fswatcher));
let c = compiler.build_from_path(&args.path).await;
slint_interpreter::print_diagnostics(compiler.diagnostics());
if let Some(c) = c {
CURRENT_INSTANCE.with(|current| {
let mut current = current.borrow_mut();
if let Some(handle) = current.take() {
let window = handle.window();
let new_handle = c.create_with_existing_window(window).unwrap();
init_dialog(&new_handle);
current.replace(new_handle);
} else {
let handle = c.create().unwrap();
init_dialog(&handle);
handle.show().unwrap();
current.replace(handle);
}
if let Some(data_path) = args.load_data {
let _ = load_data(&c, current.as_ref().unwrap(), &data_path);
}
eprintln!("Successful reload of {}", args.path.display());
});
}
PENDING_EVENTS.fetch_sub(1, Ordering::SeqCst);
}
fn load_data(
c: &ComponentDefinition,
instance: &ComponentInstance,
data_path: &std::path::Path,
) -> Result<()> {
let json: serde_json::Value = if data_path == std::path::Path::new("-") {
serde_json::from_reader(std::io::stdin())?
} else {
serde_json::from_reader(std::fs::File::open(data_path)?)?
};
let types = c.properties_and_callbacks().collect::<HashMap<_, _>>();
let obj = json.as_object().ok_or("The data is not a JSON object")?;
for (name, v) in obj {
fn from_json(
t: &i_slint_compiler::langtype::Type,
v: &serde_json::Value,
) -> slint_interpreter::Value {
match v {
serde_json::Value::Null => slint_interpreter::Value::Void,
serde_json::Value::Bool(b) => (*b).into(),
serde_json::Value::Number(n) => {
slint_interpreter::Value::Number(n.as_f64().unwrap_or(f64::NAN))
}
serde_json::Value::String(s) => match t {
i_slint_compiler::langtype::Type::Enumeration(e) => {
if e.values.contains(s) {
slint_interpreter::Value::EnumerationValue(
e.name.to_string(),
s.to_string(),
)
} else {
eprintln!("Warning: Unexpected value for enum '{}': {}", e.name, s);
slint_interpreter::Value::Void
}
}
i_slint_compiler::langtype::Type::String => {
SharedString::from(s.as_str()).into()
}
_ => slint_interpreter::Value::Void,
},
serde_json::Value::Array(array) => match t {
i_slint_compiler::langtype::Type::Array(it) => slint_interpreter::Value::Model(
ModelRc::new(i_slint_core::model::SharedVectorModel::from(
array.iter().map(|v| from_json(it, v)).collect::<SharedVector<Value>>(),
)),
),
_ => slint_interpreter::Value::Void,
},
serde_json::Value::Object(obj) => match t {
i_slint_compiler::langtype::Type::Struct { fields, .. } => obj
.iter()
.filter_map(|(k, v)| match fields.get(k) {
Some(t) => Some((k.clone(), from_json(t, v))),
None => {
eprintln!("Warning: ignoring unknown property: {}", k);
None
}
})
.collect::<slint_interpreter::Struct>()
.into(),
_ => slint_interpreter::Value::Void,
},
}
}
match types.get(name) {
Some(t) => {
match instance.set_property(name, from_json(t, v)) {
Ok(()) => (),
Err(e) => {
eprintln!("Warning: cannot set property '{}' from data file: {:?}", name, e)
}
};
}
None => eprintln!("Warning: ignoring unknown property: {}", name),
}
}
Ok(())
}
fn install_callbacks(instance: &ComponentInstance, callbacks: &[String]) {
assert!(callbacks.len() % 2 == 0);
for chunk in callbacks.chunks(2) {
if let [callback, cmd] = chunk {
let cmd = cmd.clone();
match instance.set_callback(callback, move |args| {
match execute_cmd(&cmd, args) {
Ok(()) => (),
Err(e) => eprintln!("Error: {}", e),
}
Value::Void
}) {
Ok(()) => (),
Err(e) => {
eprintln!("Warning: cannot set callback handler for '{}': {}", callback, e)
}
}
}
}
}
fn execute_cmd(cmd: &str, callback_args: &[Value]) -> Result<()> {
let cmd_args = shlex::split(cmd).ok_or("Could not parse the command string")?;
let program_name = cmd_args.first().ok_or("Missing program name")?;
let mut command = std::process::Command::new(program_name);
let callback_args = callback_args
.iter()
.map(|v| {
Ok(match v {
Value::Number(x) => x.to_string(),
Value::String(x) => x.to_string(),
Value::Bool(x) => x.to_string(),
Value::Image(img) => {
img.path().map(|p| p.to_string_lossy()).unwrap_or_default().into()
}
_ => return Err(format!("Cannot convert argument to string: {:?}", v).into()),
})
})
.collect::<Result<Vec<String>>>()?;
for mut a in cmd_args.into_iter().skip(1) {
for (idx, cb_a) in callback_args.iter().enumerate() {
a = a.replace(&format!("${}", idx + 1), cb_a);
}
command.arg(a);
}
command.spawn()?;
Ok(())
}