diff --git a/src/main.rs b/src/main.rs index 34d2e6d..2351768 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,18 @@ -use ::rand::seq::SliceRandom; -use ::slint::include_modules; -use ::slint::{Model, Timer, VecModel}; -use std::rc::Rc; -use std::time::Duration; +::slint::include_modules!(); -include_modules!(); +fn main() -> Result<(), slint::PlatformError> { + use ::rand::seq::SliceRandom; + use ::slint::{Model, Timer, VecModel}; + use std::rc::Rc; + use std::time::Duration; -fn main() { - let main_window = MainWindow::new().unwrap(); + let main_window = MainWindow::new()?; - // Fetch the tiles from the model + // Fetch the tiles from the model, and then duplicate them so we have pairs let mut tiles: Vec = main_window.get_memory_tiles().iter().collect(); - // Duplicate the list of tiles so we have pairs tiles.extend(tiles.clone()); + // Randomize the tiles let mut rng = rand::thread_rng(); tiles.shuffle(&mut rng); @@ -22,6 +21,7 @@ fn main() { main_window.set_memory_tiles(tiles_model.clone().into()); let main_window_weak = main_window.as_weak(); + main_window.on_check_if_pair_solved(move || { let mut flipped_tiles = tiles_model .iter() @@ -34,23 +34,28 @@ fn main() { let is_pair_solved = t1 == t2; if is_pair_solved { t1.solved = true; - tiles_model.set_row_data(t1_idx, t1); t2.solved = true; + tiles_model.set_row_data(t1_idx, t1); tiles_model.set_row_data(t2_idx, t2); } else { let main_window = main_window_weak.unwrap(); - main_window.set_disable_tiles(true); let tiles_model = tiles_model.clone(); + + main_window.set_disable_tiles(true); + Timer::single_shot(Duration::from_secs(1), move || { main_window.set_disable_tiles(false); + t1.image_visible = false; - tiles_model.set_row_data(t1_idx, t1); t2.image_visible = false; + tiles_model.set_row_data(t1_idx, t1); tiles_model.set_row_data(t2_idx, t2); }); } } }); - main_window.run().unwrap(); + main_window.run()?; + + Ok(()) }