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
#![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::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
mod frames;
mod utils;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub 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<String>,
downframe: Option<String>,
shortcut: Option<String>,
pad: Option<i32>,
minimum: Option<f64>,
maximum: Option<f64>,
step: Option<f64>,
slidersize: Option<f64>,
textfont: Option<i32>,
textsize: Option<i32>,
textcolor: Option<String>,
}
#[derive(Debug, Clone)]
pub struct DeclarativeApp {
a: app::App,
w: i32,
h: i32,
label: String,
#[allow(dead_code)]
path: Option<&'static str>,
widget: Option<Widget>,
load_fn: fn(&'static str) -> Option<Widget>,
}
impl DeclarativeApp {
pub fn new(
w: i32,
h: i32,
label: &str,
path: &'static str,
load_fn: fn(&'static str) -> Option<Widget>,
) -> Self {
let widget = load_fn(path);
let a = app::App::default().with_scheme(app::Scheme::Gtk);
Self {
a,
w,
h,
label: label.to_string(),
path: Some(path),
widget,
load_fn,
}
}
#[cfg(feature = "json")]
pub fn new_json(w: i32, h: i32, label: &str, path: &'static str) -> Self {
fn load_fn(path: &'static str) -> Option<Widget> {
let s = std::fs::read_to_string(path).ok()?;
serde_json::from_str(&s).map_err(|e| eprintln!("{e}")).ok()
}
Self::new(w, h, label, path, load_fn)
}
#[cfg(feature = "json5")]
pub fn new_json5(w: i32, h: i32, label: &str, path: &'static str) -> Self {
fn load_fn(path: &'static str) -> Option<Widget> {
let s = std::fs::read_to_string(path).ok()?;
serde_json5::from_str(&s).map_err(|e| eprintln!("{e}")).ok()
}
Self::new(w, h, label, path, load_fn)
}
#[cfg(feature = "xml")]
pub fn new_xml(w: i32, h: i32, label: &str, path: &'static str) -> Self {
fn load_fn(path: &'static str) -> Option<Widget> {
let s = std::fs::read_to_string(path).ok()?;
serde_xml_rs::from_str(&s)
.map_err(|e| eprintln!("{e}"))
.ok()
}
Self::new(w, h, label, path, load_fn)
}
#[cfg(feature = "yaml")]
pub fn new_yaml(w: i32, h: i32, label: &str, path: &'static str) -> Self {
fn load_fn(path: &'static str) -> Option<Widget> {
let s = std::fs::read_to_string(path).ok()?;
serde_yaml::from_str(&s).map_err(|e| eprintln!("{e}")).ok()
}
Self::new(w, h, label, path, load_fn)
}
pub fn new_inline(w: i32, h: i32, label: &str, widget: Option<Widget>) -> Self {
let a = app::App::default().with_scheme(app::Scheme::Gtk);
Self {
a,
w,
h,
label: label.to_string(),
path: None,
widget,
load_fn: |_| None,
}
}
pub fn run<F: FnMut(&mut window::Window) + 'static>(
&self,
mut run_cb: F,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(path) = &self.path {
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);
}
run_cb(&mut win);
let flag = Arc::new(AtomicBool::new(false));
app::add_timeout3(0.1, {
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 load_fn = self.load_fn;
let mut watcher = notify::recommended_watcher({
let path = <&str>::clone(path);
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) = (load_fn)(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);
}
app::redraw();
flag.store(true, Ordering::Relaxed);
}
}
}
}
Err(e) => eprintln!("{}", e),
}
})?;
watcher.watch(&PathBuf::from(path), RecursiveMode::NonRecursive)?;
self.a.run()?;
} else {
self.run_once(run_cb)?;
}
Ok(())
}
pub fn run_once<F: FnMut(&mut window::Window) + 'static>(
&self,
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);
}
run_cb(&mut win);
self.a.run()?;
Ok(())
}
pub fn dump_image(&self) {
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);
}
let sur = surface::SvgFileSurface::new(win.w(), win.h(), "temp.svg");
surface::SvgFileSurface::push_current(&sur);
draw::set_draw_color(enums::Color::White);
draw::draw_rectf(0, 0, win.w(), win.h());
sur.draw(&win, 0, 0);
surface::SvgFileSurface::pop_current();
}
}