1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: MIT |
3 | |
4 | #![no_std ] |
5 | #![cfg_attr (not(feature = "simulator" ), no_main)] |
6 | |
7 | extern crate alloc; |
8 | |
9 | use alloc::vec::Vec; |
10 | |
11 | use alloc::rc::Rc; |
12 | use slint::Model; |
13 | |
14 | slint::include_modules!(); |
15 | |
16 | struct PrinterQueueData { |
17 | data: Rc<slint::VecModel<PrinterQueueItem>>, |
18 | print_progress_timer: slint::Timer, |
19 | } |
20 | |
21 | impl PrinterQueueData { |
22 | fn push_job(&self, title: slint::SharedString) { |
23 | self.data.push(PrinterQueueItem { |
24 | status: JobStatus::Waiting, |
25 | progress: 0, |
26 | title, |
27 | owner: env!("CARGO_PKG_AUTHORS" ).into(), |
28 | pages: 1, |
29 | size: "100kB" .into(), |
30 | submission_date: "" .into(), |
31 | }) |
32 | } |
33 | } |
34 | |
35 | #[mcu_board_support::entry ] |
36 | fn main() -> ! { |
37 | mcu_board_support::init(); |
38 | let main_window = MainWindow::new().unwrap(); |
39 | main_window.set_ink_levels( |
40 | [ |
41 | InkLevel { color: slint::Color::from_rgb_u8(0, 255, 255), level: 0.40 }, |
42 | InkLevel { color: slint::Color::from_rgb_u8(255, 0, 255), level: 0.20 }, |
43 | InkLevel { color: slint::Color::from_rgb_u8(255, 255, 0), level: 0.50 }, |
44 | InkLevel { color: slint::Color::from_rgb_u8(0, 0, 0), level: 0.80 }, |
45 | ] |
46 | .into(), |
47 | ); |
48 | |
49 | let default_queue: Vec<PrinterQueueItem> = |
50 | main_window.global::<PrinterQueue>().get_printer_queue().iter().collect(); |
51 | let printer_queue = Rc::new(PrinterQueueData { |
52 | data: Rc::new(slint::VecModel::from(default_queue.clone())), |
53 | print_progress_timer: Default::default(), |
54 | }); |
55 | main_window.global::<PrinterQueue>().set_printer_queue(printer_queue.data.clone().into()); |
56 | |
57 | main_window.on_quit(move || { |
58 | #[cfg (not(target_arch = "wasm32" ))] |
59 | slint::quit_event_loop().unwrap(); |
60 | }); |
61 | |
62 | let printer_queue_copy = printer_queue.clone(); |
63 | main_window.global::<PrinterQueue>().on_start_job(move |title| { |
64 | printer_queue_copy.push_job(title); |
65 | }); |
66 | |
67 | let printer_queue_copy = printer_queue.clone(); |
68 | main_window.global::<PrinterQueue>().on_cancel_job(move |idx| { |
69 | printer_queue_copy.data.remove(idx as usize); |
70 | }); |
71 | |
72 | let printer_queue_weak = Rc::downgrade(&printer_queue); |
73 | printer_queue.print_progress_timer.start( |
74 | slint::TimerMode::Repeated, |
75 | core::time::Duration::from_secs(1), |
76 | move || { |
77 | if let Some(printer_queue) = printer_queue_weak.upgrade() { |
78 | if printer_queue.data.row_count() > 0 { |
79 | let mut top_item = printer_queue.data.row_data(0).unwrap(); |
80 | top_item.progress += 1; |
81 | top_item.status = JobStatus::Printing; |
82 | if top_item.progress > 100 { |
83 | printer_queue.data.remove(0); |
84 | if printer_queue.data.row_count() == 0 { |
85 | return; |
86 | } |
87 | top_item = printer_queue.data.row_data(0).unwrap(); |
88 | } |
89 | printer_queue.data.set_row_data(0, top_item); |
90 | } else { |
91 | printer_queue.data.set_vec(default_queue.clone()); |
92 | } |
93 | } |
94 | }, |
95 | ); |
96 | |
97 | main_window.run().unwrap(); |
98 | |
99 | panic!("The MCU demo should not quit" ) |
100 | } |
101 | |