forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
111 lines (97 loc) · 3.93 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
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2021 Olivier Goffart <[email protected]>
Copyright (c) 2021 Simon Hausmann <[email protected]>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact [email protected] for more information.
LICENSE END */
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
use sixtyfps::Model;
use std::rc::Rc;
sixtyfps::include_modules!();
/// Returns the current time formated as a string
fn current_time() -> sixtyfps::SharedString {
#[cfg(not(target_arch = "wasm32"))]
return chrono::Local::now().format("%H:%M:%S %d/%m/%Y").to_string().into();
#[cfg(target_arch = "wasm32")]
return "".into();
}
struct PrinterQueueData {
data: Rc<sixtyfps::VecModel<PrinterQueueItem>>,
print_progress_timer: sixtyfps::Timer,
}
impl PrinterQueueData {
fn push_job(&self, title: sixtyfps::SharedString) {
self.data.push(PrinterQueueItem {
status: "WAITING...".into(),
progress: 0,
title,
owner: env!("CARGO_PKG_AUTHORS").into(),
pages: 1,
size: "100kB".into(),
submission_date: current_time(),
})
}
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn main() {
// This provides better error messages in debug mode.
// It's disabled in release mode so it doesn't bloat up the file size.
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
console_error_panic_hook::set_once();
let main_window = MainWindow::new();
main_window.set_ink_levels(sixtyfps::VecModel::from_slice(&[
InkLevel { color: sixtyfps::Color::from_rgb_u8(0, 255, 255), level: 0.40 },
InkLevel { color: sixtyfps::Color::from_rgb_u8(255, 0, 255), level: 0.20 },
InkLevel { color: sixtyfps::Color::from_rgb_u8(255, 255, 0), level: 0.50 },
InkLevel { color: sixtyfps::Color::from_rgb_u8(0, 0, 0), level: 0.80 },
]));
let default_queue: Vec<PrinterQueueItem> =
main_window.global::<PrinterQueue>().get_printer_queue().iter().collect();
let printer_queue = Rc::new(PrinterQueueData {
data: Rc::new(sixtyfps::VecModel::from(default_queue)),
print_progress_timer: Default::default(),
});
main_window
.global::<PrinterQueue>()
.set_printer_queue(sixtyfps::ModelHandle::new(printer_queue.data.clone()));
main_window.on_quit(move || {
#[cfg(not(target_arch = "wasm32"))]
std::process::exit(0);
});
let printer_queue_copy = printer_queue.clone();
main_window.global::<PrinterQueue>().on_start_job(move |title| {
printer_queue_copy.push_job(title);
});
let printer_queue_copy = printer_queue.clone();
main_window.global::<PrinterQueue>().on_cancel_job(move |idx| {
printer_queue_copy.data.remove(idx as usize);
});
let printer_queue_weak = Rc::downgrade(&printer_queue);
printer_queue.print_progress_timer.start(
sixtyfps::TimerMode::Repeated,
std::time::Duration::from_secs(1),
move || {
if let Some(printer_queue) = printer_queue_weak.upgrade() {
if printer_queue.data.row_count() > 0 {
let mut top_item = printer_queue.data.row_data(0);
top_item.progress += 1;
top_item.status = "PRINTING".into();
if top_item.progress > 100 {
printer_queue.data.remove(0);
if printer_queue.data.row_count() == 0 {
return;
}
top_item = printer_queue.data.row_data(0);
}
printer_queue.data.set_row_data(0, top_item);
} else {
// FIXME: stop this timer?
}
}
},
);
main_window.run();
}