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