heic2jpg

git clone git://archive.git.mtrnord.blog/MTRNord/heic2jpg.git
Log | Files | Refs | README | LICENSE

commit df1da446fab1c4e33d56258b9caa7397680d0b94
parent 7bb24fa5088a1cb19f5c9dad9d21c066a6ffbed4
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sun, 10 Nov 2024 23:07:55 +0100

Remove debug code and add some info on how many files it is processing

Diffstat:
Msrc/app.rs | 62+++++++++++++++++++++++++++++++++++++++++++++++++-------------
Msrc/conversion_worker.rs | 37+++++++++++++------------------------
2 files changed, 62 insertions(+), 37 deletions(-)

diff --git a/src/app.rs b/src/app.rs @@ -31,6 +31,7 @@ pub(super) struct App { mode: Mode, progress: f64, failure: Option<String>, + file_count: usize, } #[derive(Debug)] @@ -40,6 +41,7 @@ pub(super) enum AppMsg { DeselectInputFolder, DeselectOutputFolder, Convert, + ConversionStarted(usize), ProgressUpdate(f64), ConversionComplete, ConversionFailed(String), @@ -109,6 +111,7 @@ impl SimpleComponent for App { } }, + #[transition = "SlideRight"] match model.mode { Mode::Progressing => { adw::StatusPage { @@ -116,10 +119,26 @@ impl SimpleComponent for App { set_vexpand: true, set_title: &gettext("Converting"), set_description: Some(&gettext("Please wait while the conversion is in progress")), - - gtk::ProgressBar { - #[watch] - set_fraction: model.progress, + gtk::Box { + set_halign: gtk::Align::Center, + set_orientation: gtk::Orientation::Vertical, + set_spacing: 24, + gtk::ProgressBar { + #[watch] + set_fraction: model.progress, + }, + + gtk::Text { + set_halign: gtk::Align::Center, + #[watch] + set_visible: model.file_count > 0, + #[watch] + set_text: &format!( + "{} / {}", + model.file_count as u32 * model.progress as u32, + model.file_count + ), + } } } } @@ -129,11 +148,16 @@ impl SimpleComponent for App { set_vexpand: true, set_title: &gettext("Conversion Complete"), set_description: Some(&gettext("The conversion was successful")), + gtk::Box { + set_halign: gtk::Align::Center, + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 24, - gtk::Button { - set_label: "Close", - connect_clicked[sender] => move |_| { - sender.input(AppMsg::Quit); + gtk::Button { + set_label: "Close", + connect_clicked[sender] => move |_| { + sender.input(AppMsg::Quit); + } } } } @@ -146,10 +170,15 @@ impl SimpleComponent for App { #[watch] set_description: model.failure.as_deref(), - gtk::Button { - set_label: &gettext("Close"), - connect_clicked[sender] => move |_| { - sender.input(AppMsg::Quit); + gtk::Box { + set_halign: gtk::Align::Center, + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 24, + gtk::Button { + set_label: &gettext("Close"), + connect_clicked[sender] => move |_| { + sender.input(AppMsg::Quit); + } } } } @@ -221,7 +250,9 @@ impl SimpleComponent for App { ConversionWorker::builder() .detach_worker(()) .forward(sender.input_sender(), |msg| match msg { - ConversionWorkerMsg::ConversionStarted => AppMsg::Noop, + ConversionWorkerMsg::ConversionStarted(number) => { + AppMsg::ConversionStarted(number) + } ConversionWorkerMsg::ProgressUpdate(number) => AppMsg::ProgressUpdate(number), ConversionWorkerMsg::ConversionComplete => AppMsg::ConversionComplete, ConversionWorkerMsg::ConversionFailed(e) => AppMsg::ConversionFailed(e), @@ -237,6 +268,7 @@ impl SimpleComponent for App { mode: Mode::InputSelection, progress: 0.0, failure: None, + file_count: 0, }; let widgets = view_output!(); @@ -268,6 +300,10 @@ impl SimpleComponent for App { fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) { match message { + AppMsg::ConversionStarted(number) => { + self.file_count = number; + self.mode = Mode::Progressing; + } AppMsg::InputFolderSelected(path) => { self.input_folder = Some(path); self.mode = Mode::OutputSelection diff --git a/src/conversion_worker.rs b/src/conversion_worker.rs @@ -13,7 +13,7 @@ pub enum ConversionWorkerInputMsg { #[derive(Debug)] pub enum ConversionWorkerMsg { - ConversionStarted, + ConversionStarted(usize), ProgressUpdate(f64), ConversionComplete, ConversionFailed(String), @@ -33,19 +33,9 @@ impl Worker for ConversionWorker { fn update(&mut self, msg: ConversionWorkerInputMsg, sender: ComponentSender<Self>) { match msg { ConversionWorkerInputMsg::ConvertFolder(input_path, output_path) => { - // Start the conversion - info!("Starting conversion of folder {:?}", input_path); - sender - .output(ConversionWorkerMsg::ConversionStarted) - .unwrap(); - // Walk directory, find all heic files, convert them to jpg and update progress info!("Converting folder {:?}", input_path); - let result = self.convert_folder(input_path, output_path, |progress| { - sender - .output(ConversionWorkerMsg::ProgressUpdate(progress)) - .unwrap(); - }); + let result = self.convert_folder(input_path, output_path, &sender); // Send the result of the conversion back match result { @@ -62,21 +52,14 @@ impl Worker for ConversionWorker { } impl ConversionWorker { - fn convert_folder<F: Fn(f64)>( + fn convert_folder( &self, input_path: PathBuf, output_path: PathBuf, - progress_callback: F, + sender: &ComponentSender<Self>, ) -> Result<(), MagickError> { + // Start the conversion info!("Converting folder {:?} to {:?}", input_path, output_path); - // List files in folder for debugging - for entry in WalkDir::new(&input_path) - .follow_links(true) - .same_file_system(false) - { - let entry = entry.unwrap(); - info!("Found {:?}", entry.path()); - } // Use walkdir to find all heic files in the input directory let heic_files: Vec<PathBuf> = WalkDir::new(input_path) @@ -86,7 +69,6 @@ impl ConversionWorker { .filter_map(|entry| { let entry = entry.ok()?; let path = entry.path(); - info!("Checking file {:?}", path); if path.is_file() && path .extension() @@ -99,6 +81,9 @@ impl ConversionWorker { }) .collect(); info!("Found {} heic files", heic_files.len()); + sender + .output(ConversionWorkerMsg::ConversionStarted(heic_files.len())) + .unwrap(); // Convert each heic file to jpg for (index, heic_file) in heic_files.iter().enumerate() { @@ -111,7 +96,11 @@ impl ConversionWorker { self.convert_heic_to_jpg(heic_file.to_path_buf(), output_file)?; // Update the progress - progress_callback((index + 1) as f64 / heic_files.len() as f64); + sender + .output(ConversionWorkerMsg::ProgressUpdate( + (index + 1) as f64 / heic_files.len() as f64, + )) + .unwrap(); } info!("Conversion complete"); Ok(())