diff --git a/src/app.rs b/src/app.rs index b611426..266e585 100644 --- a/src/app.rs +++ b/src/app.rs @@ -227,6 +227,7 @@ pub struct AppState { pub transform_data: TransformData, pub filters: FileFilters, pub selected_image: ImageData, + pub loaded_thumbnail: bool, } impl AppState { @@ -298,7 +299,6 @@ impl AppState { let scale_factor = 1.0; let (image_texture, bind_group, render_pipeline, transform_buffer) = - // setup_texture(&device, surface_config.clone(), 6000, 4000); setup_texture(&device, surface_config.clone(), 8192, 8192); let transform_data = TransformData { @@ -325,6 +325,7 @@ impl AppState { transform_data, filters: FileFilters::default(), selected_image, + loaded_thumbnail: false, } } @@ -383,7 +384,7 @@ impl App { self.state.get_or_insert(state); self.reset_transform(); - self.update_texture(); + self.update_texture(true); } fn handle_resized(&mut self, width: u32, height: u32) { @@ -393,20 +394,30 @@ impl App { self.pan_zoom(0.0, 0.0, 0.0); } - pub fn update_texture(&mut self) { + pub fn update_texture(&mut self, force: bool) { let state = self.state.as_mut().unwrap(); + if !force { - let store = state.store.read().unwrap(); - if state.selected_image == store.current_image_path { + let mut store = state.store.write().unwrap(); + store.check_loaded_images(); + let current_image_selected = state.selected_image == store.current_image_path; + let current_quality_loaded = + state.loaded_thumbnail == store.get_current_image().is_none(); + println!( + "check {} {}", + current_quality_loaded, current_image_selected + ); + if current_image_selected && current_quality_loaded { return; } } { let mut store = state.store.write().unwrap(); - store.check_loaded_images(); let imbuf = if let Some(full) = store.get_current_image() { + state.loaded_thumbnail = false; full } else { + state.loaded_thumbnail = true; store.get_thumbnail() }; let width = imbuf.width as u32; @@ -441,6 +452,7 @@ impl App { depth_or_array_layers: 1, }, ); + state.selected_image = store.current_image_path.clone(); } self.update_transform(); @@ -710,6 +722,7 @@ impl App { image_widget.scroll_to_me(Some(Align::Center)); } if image_widget.clicked() { + println!("{}", image.get_hash_str()); selected_image = Some(image); } } @@ -746,7 +759,7 @@ impl App { state.queue.submit(Some(encoder.finish())); surface_texture.present(); - self.update_texture(); + self.update_texture(false); } } @@ -843,7 +856,7 @@ impl ApplicationHandler for App { } if updated_image { - self.update_texture(); + self.update_texture(false); } if reset_transform { self.reset_transform(); diff --git a/src/image.rs b/src/image.rs index f1be53b..7e0bdb5 100644 --- a/src/image.rs +++ b/src/image.rs @@ -30,6 +30,8 @@ use std::io::Read; use std::io::Write; use std::path::PathBuf; use std::str::FromStr; +use std::thread::sleep; +use std::time::Duration; use std::time::Instant; #[derive(Clone, Eq, Hash, PartialEq, PartialOrd)] @@ -145,6 +147,7 @@ fn get_format(path: &PathBuf) -> Option { } pub fn load_image(image: &ImageData) -> ImflowImageBuffer { + // sleep(Duration::from_millis(500)); let total_start = Instant::now(); match image.format { @@ -291,11 +294,18 @@ pub fn check_embedded_thumbnail(path: &PathBuf) -> bool { } pub fn get_embedded_thumbnail(image: &ImageData) -> Option> { - Metadata::new_from_path(&image.path) - .ok()? - .get_preview_images()? - .first() - .and_then(|preview| preview.get_data().ok()) + let meta = Metadata::new_from_path(&image.path).ok()?; + + let width = meta.get_pixel_width(); + let height = meta.get_pixel_height(); + println!("image: {}", width as f32 / height as f32); + + meta.get_preview_images()?.first().and_then(|preview| { + let width = preview.get_width(); + let height = preview.get_height(); + println!("thumbnail: {}", width as f32 / height as f32); + preview.get_data().ok() + }) } pub fn load_thumbnail(path: &ImageData) -> ImflowImageBuffer { diff --git a/src/store.rs b/src/store.rs index d9301ca..f070ca9 100644 --- a/src/store.rs +++ b/src/store.rs @@ -56,11 +56,11 @@ impl ImageStore { let currently_loading = HashSet::new(); - let first_image_path = available_images[0].clone(); - let first_image_thread = std::thread::spawn(move || { - let image = load_image(&first_image_path); - (first_image_path, image) - }); + // let first_image_path = available_images[0].clone(); + // let first_image_thread = std::thread::spawn(move || { + // let image = load_image(&first_image_path); + // (first_image_path, image) + // }); let total_start = Instant::now(); let (sender, receiver) = unbounded(); @@ -80,8 +80,9 @@ impl ImageStore { loaded_thumbnails.len() ); - let (path, image) = first_image_thread.join().unwrap(); - loaded_images.insert(path, image); + let image = load_image(&new_path.clone()); + // let (path, image) = first_image_thread.join().unwrap(); + loaded_images.insert(new_path.clone(), image); let mut state = Self { current_image_id, loaded_images,