From ae06bec3ed7f90a846842fa081097cc054bfb2c6 Mon Sep 17 00:00:00 2001 From: Fiery Imp Date: Wed, 23 Apr 2025 17:20:57 -0400 Subject: [PATCH] Claude: Cancel download and disabled back and next button while downloading --- rust/src/app.rs | 48 +++++++++++++++++++++++++++----- rust/src/ui/download_progress.rs | 9 ++++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/rust/src/app.rs b/rust/src/app.rs index e38c0bb..cb88f6e 100644 --- a/rust/src/app.rs +++ b/rust/src/app.rs @@ -32,6 +32,7 @@ pub struct DownloadState { pub completed: bool, pub files: Option>, pub error: Option, + pub cancelled: bool, } pub struct SinfarInstallerApp<> { @@ -96,9 +97,12 @@ impl<> eframe::App for SinfarInstallerApp<> { if self.state == InstallerState::Download { if let Ok(state) = self.download_state.lock() { - self.download_progress = state.progress; - self.download_speed = state.speed; - self.estimated_remaining = state.estimated_remaining; + // Only update progress if not cancelled + if !state.cancelled { + self.download_progress = state.progress; + self.download_speed = state.speed; + self.estimated_remaining = state.estimated_remaining; + } if let Some(error) = &state.error { self.download_error = Some(error.clone()); @@ -150,6 +154,13 @@ impl<> eframe::App for SinfarInstallerApp<> { None => false, } }, + InstallerState::Download => { + if let Ok(state) = self.download_state.lock() { + state.completed && state.files.is_some() + } else { + false + } + }, _ => true, }; @@ -163,10 +174,20 @@ impl<> eframe::App for SinfarInstallerApp<> { } } - if self.state != InstallerState::GameSelection { - if ui.button("Back").clicked() { - self.previous_state(); - } + let can_go_back = match self.state { + InstallerState::Download => { + if let Ok(state) = self.download_state.lock() { + state.progress == 0.0 || state.completed || state.error.is_some() + } else { + true + } + }, + InstallerState::GameSelection => false, + _ => true + }; + + if ui.add_enabled(can_go_back, egui::Button::new("Back")).clicked() { + self.previous_state(); } } }); @@ -232,6 +253,19 @@ impl SinfarInstallerApp<> { }); } + pub fn cancel_download(&mut self) { + if let Ok(mut state) = self.download_state.lock() { + state.error = Some("Download cancelled".to_string()); + state.progress = 0.0; + state.cancelled = true; + state.speed = 0.0; + state.estimated_remaining = std::time::Duration::from_secs(0); + } + self.download_progress = 0.0; + self.download_speed = 0.0; + self.estimated_remaining = std::time::Duration::from_secs(0); + } + pub fn start_installation(&mut self) { diff --git a/rust/src/ui/download_progress.rs b/rust/src/ui/download_progress.rs index 0ad9e6e..284a994 100644 --- a/rust/src/ui/download_progress.rs +++ b/rust/src/ui/download_progress.rs @@ -43,6 +43,15 @@ pub mod download_progress { }); } }); + + ui.add_space(10.0); + + // Add cancel button during active download + if app.download_progress > 0.0 && app.download_progress < 1.0 { + if ui.button("Cancel Download").clicked() { + app.cancel_download(); + } + } if let Some(error) = &app.download_error { ui.colored_label(eframe::egui::Color32::RED, format!("Error: {}", error));