diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index d12dcd13c..02131cb42 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -558,7 +558,26 @@ pub fn install(release: &Release, progress: impl Fn(f32)) -> Result<(), String> } pub fn remove(tag: &str) -> Result<(), String> { - fs::remove_dir_all(version_dir(tag)).map_err(|e| e.to_string()) + let dir = version_dir(tag); + // Windows keeps a handle on a file for a moment after the game, a scan or the launcher's own + // reader lets go of it, and remove_dir_all stops at the first one: a removal that already + // took most of the folder reported os error 5 while the rest of it did go. Retry, and treat a + // folder that is already gone as done. + let mut last = None; + for attempt in 0..5 { + match fs::remove_dir_all(&dir) { + Ok(()) => return Ok(()), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(e) => last = Some(e), + } + if attempt < 4 { + std::thread::sleep(std::time::Duration::from_millis(250)); + } + } + Err(match last { + Some(e) => e.to_string(), + None => format!("could not remove {}", dir.display()), + }) } /// The Minecraft version a Horizon build targets, from its research-lock.json. @@ -2170,6 +2189,8 @@ pub enum Msg { Links(Links), Progress(String, f32), Installed(String, Result<(), String>), + /// A removal finished off the UI thread. + Removed(String, Result<(), String>), Line(Job, String), Exit(Job, Result<(), String>), Updates(Result), diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 4a68ecdef..fd7e3d343 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -144,6 +144,9 @@ struct Launcher { releases: Load>, installed: Vec, installing: Option<(String, f32)>, + /// The version being deleted: the removal runs off the UI thread, so the window has to know + /// which row is busy until `Msg::Removed` arrives. + removing: Option, install_error: Option<(String, String)>, confirm_remove: Option, picker_open: bool, @@ -286,6 +289,7 @@ impl Launcher { releases: Load::Loading, installed, installing: None, + removing: None, install_error: None, confirm_remove: None, picker_open: false, @@ -516,6 +520,16 @@ impl Launcher { self.bar = (self.bar.1, p); self.installing = Some((tag, p)); } + Msg::Removed(tag, result) => { + self.removing = None; + self.installed = backend::installed(); + self.settings.selected = Settings::load() + .selected + .or_else(|| self.installed.first().cloned()); + if let Err(e) = result { + self.install_error = Some((tag, e)); + } + } Msg::Installed(tag, result) => { self.installing = None; self.bar = (0., 0.); @@ -725,14 +739,16 @@ impl Launcher { } fn remove(&mut self, tag: &str) { - if let Err(e) = backend::remove_version(tag) { - self.install_error = Some((tag.to_string(), e)); - } + // Same shape as install: the folder is hundreds of megabytes and Windows may hold a file + // for a moment, so the deletion goes to a thread instead of stalling the window. + let (tag, tx) = (tag.to_string(), self.tx.clone()); + self.removing = Some(tag.clone()); self.confirm_remove = None; - self.installed = backend::installed(); - self.settings.selected = Settings::load() - .selected - .or_else(|| self.installed.first().cloned()); + self.install_error = None; + std::thread::spawn(move || { + let result = backend::remove_version(&tag); + let _ = tx.send(Msg::Removed(tag, result)); + }); } fn prepare(&mut self) { @@ -2921,6 +2937,7 @@ impl Launcher { let keep = |i: usize| { let tag = &releases[i].tag; self.installing.as_ref().is_some_and(|(t, _)| t == tag) + || self.removing.as_deref() == Some(tag.as_str()) || self.install_error.as_ref().is_some_and(|(t, _)| t == tag) || self.confirm_remove.as_ref() == Some(tag) };