1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: MIT
3
4#[cfg(target_arch = "wasm32")]
5use wasm_bindgen::prelude::*;
6
7use slint::Model;
8use std::rc::Rc;
9
10slint::include_modules!();
11
12/// Returns the current time formatted as a string
13fn current_time() -> slint::SharedString {
14 #[cfg(not(target_arch = "wasm32"))]
15 return chrono::Local::now().format(fmt:"%H:%M:%S %d/%m/%Y").to_string().into();
16 #[cfg(target_arch = "wasm32")]
17 return "".into();
18}
19
20struct PrinterQueueData {
21 data: Rc<slint::VecModel<PrinterQueueItem>>,
22 print_progress_timer: slint::Timer,
23}
24
25impl PrinterQueueData {
26 fn push_job(&self, title: slint::SharedString) {
27 self.data.push(PrinterQueueItem {
28 status: "waiting".into(),
29 progress: 0,
30 title,
31 owner: env!("CARGO_PKG_AUTHORS").into(),
32 pages: 1,
33 size: "100kB".into(),
34 submission_date: current_time(),
35 })
36 }
37}
38
39#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
40pub fn main() {
41 // This provides better error messages in debug mode.
42 // It's disabled in release mode so it doesn't bloat up the file size.
43 #[cfg(all(debug_assertions, target_arch = "wasm32"))]
44 console_error_panic_hook::set_once();
45
46 let main_window = MainWindow::new().unwrap();
47 main_window.set_ink_levels(
48 [
49 InkLevel { color: slint::Color::from_rgb_u8(0, 255, 255), level: 0.40 },
50 InkLevel { color: slint::Color::from_rgb_u8(255, 0, 255), level: 0.20 },
51 InkLevel { color: slint::Color::from_rgb_u8(255, 255, 0), level: 0.50 },
52 InkLevel { color: slint::Color::from_rgb_u8(0, 0, 0), level: 0.80 },
53 ]
54 .into(),
55 );
56
57 let default_queue: Vec<PrinterQueueItem> =
58 main_window.global::<PrinterQueue>().get_printer_queue().iter().collect();
59 let printer_queue = Rc::new(PrinterQueueData {
60 data: Rc::new(slint::VecModel::from(default_queue)),
61 print_progress_timer: Default::default(),
62 });
63 main_window.global::<PrinterQueue>().set_printer_queue(printer_queue.data.clone().into());
64
65 main_window.on_quit(move || {
66 #[cfg(not(target_arch = "wasm32"))]
67 std::process::exit(0);
68 });
69
70 let printer_queue_copy = printer_queue.clone();
71 main_window.global::<PrinterQueue>().on_start_job(move |title| {
72 printer_queue_copy.push_job(title);
73 });
74
75 let printer_queue_copy = printer_queue.clone();
76 main_window.global::<PrinterQueue>().on_cancel_job(move |idx| {
77 printer_queue_copy.data.remove(idx as usize);
78 });
79
80 let printer_queue_weak = Rc::downgrade(&printer_queue);
81 printer_queue.print_progress_timer.start(
82 slint::TimerMode::Repeated,
83 std::time::Duration::from_secs(1),
84 move || {
85 if let Some(printer_queue) = printer_queue_weak.upgrade() {
86 if printer_queue.data.row_count() > 0 {
87 let mut top_item = printer_queue.data.row_data(0).unwrap();
88 top_item.progress += 1;
89 top_item.status = "printing".into();
90 if top_item.progress > 100 {
91 printer_queue.data.remove(0);
92 if printer_queue.data.row_count() == 0 {
93 return;
94 }
95 top_item = printer_queue.data.row_data(0).unwrap();
96 }
97 printer_queue.data.set_row_data(0, top_item);
98 } else {
99 // FIXME: stop this timer?
100 }
101 }
102 },
103 );
104
105 main_window.run().unwrap();
106}
107
108#[cfg(target_os = "android")]
109#[no_mangle]
110fn android_main(app: slint::android::AndroidApp) {
111 slint::android::init(app).unwrap();
112 main()
113}
114