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
#![doc = include_str!("../README.md")]
use fltk::{prelude::*, *};
use notify::{
event::{AccessKind, AccessMode, EventKind},
Event, RecursiveMode, Watcher,
};
use serde_derive::{Deserialize, Serialize};
use std::{
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
mod utils;
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Widget {
widget: String,
label: Option<String>,
id: Option<String>,
fixed: Option<i32>,
color: Option<String>,
labelcolor: Option<String>,
children: Option<Vec<Widget>>,
hide: Option<bool>,
deactivate: Option<bool>,
visible: Option<bool>,
resizable: Option<bool>,
selectioncolor: Option<String>,
tooltip: Option<String>,
image: Option<String>,
deimage: Option<String>,
labelfont: Option<u32>,
labelsize: Option<i32>,
align: Option<i32>,
when: Option<i32>,
frame: Option<u32>,
downframe: Option<u32>,
shortcut: Option<String>,
pad: Option<i32>,
minimum: Option<f64>,
maximum: Option<f64>,
step: Option<f64>,
slidersize: Option<f64>,
}
#[derive(Debug, Clone)]
pub struct DeclarativeApp {
a: app::App,
w: i32,
h: i32,
label: String,
#[allow(dead_code)]
path: PathBuf,
widget: Option<Widget>,
}
impl DeclarativeApp {
pub fn new<P: AsRef<Path>>(w: i32, h: i32, label: &str, path: P) -> Self {
let json = utils::load(path.as_ref()).expect("Failed to load widget data!");
let a = app::App::default().with_scheme(app::Scheme::Gtk);
Self {
a,
w,
h,
label: label.to_string(),
path: PathBuf::from(path.as_ref()),
widget: Some(json),
}
}
pub fn run<F: FnMut(&mut window::Window) + 'static>(
&self,
hot_reload: bool,
mut run_cb: F,
) -> Result<(), Box<dyn std::error::Error>> {
let mut win = window::Window::default()
.with_size(self.w, self.h)
.with_label(&self.label);
if let Some(widget) = &self.widget {
utils::transform(widget);
}
win.end();
win.show();
if let Some(mut frst) = win.child(0) {
frst.resize(0, 0, win.w(), win.h());
win.resizable(&frst);
}
if hot_reload {
let flag = Arc::new(AtomicBool::new(true));
app::add_timeout3(0.0, {
let flag = flag.clone();
let mut win = win.clone();
move |_t| {
if flag.load(Ordering::Relaxed) {
run_cb(&mut win);
flag.store(false, Ordering::Relaxed);
}
app::repeat_timeout3(0.1, _t);
}
});
let path = self.path.clone();
let mut watcher =
notify::recommended_watcher(move |res: Result<Event, notify::Error>| match res {
Ok(event) => {
if let EventKind::Access(AccessKind::Close(mode)) = event.kind {
if mode == AccessMode::Write {
if let Some(wid) = utils::load(&path) {
win.clear();
win.begin();
utils::transform(&wid);
win.end();
if let Some(mut frst) = win.child(0) {
frst.resize(0, 0, win.w(), win.h());
win.resizable(&frst);
}
flag.store(true, Ordering::Relaxed);
}
}
}
}
Err(e) => eprintln!("{}", e),
})?;
watcher.watch(&self.path, RecursiveMode::NonRecursive)?;
}
self.a.run()?;
Ok(())
}
}