diff --git a/src/app/headless.rs b/src/app/headless.rs index a9fadc8..f7f0b42 100644 --- a/src/app/headless.rs +++ b/src/app/headless.rs @@ -2279,6 +2279,28 @@ fn an_idle_window_is_not_drawn_again_for_nothing() { assert!(redraws <= 1, "{redraws} frames redrew an idle window"); } +/// Check now in Settings looks for an update even with background checks off, and says what it found. A test build +/// isn't installed by the Claudiu installer, so it says that. +#[test] +fn check_now_says_what_it_found() { + let mut h = Harness::new(|_| vec![]); + h.step(|ws| ws.store.data.settings.update_url = Some(String::new())); + h.wait("the check at launch to finish", |ws| ws.update.status != crate::updater::Status::Checking); + h.step(|ws| ws.act(Act::OpenSettings)); + h.settle(); + h.click("settings-check-updates"); + h.wait("an answer", |ws| ws.toast.is_some()); + let toast = h.ws().toast.as_ref().unwrap(); + assert_eq!(toast.text, "Could not check for updates: updates work only in a copy installed with the Claudiu installer"); + assert!(toast.error); + + // A downloaded update isn't thrown away by another check; the button says it's waiting instead. + h.step(|ws| ws.update.status = crate::updater::Status::Ready { version: "9.9.9".into() }); + h.click("settings-check-updates"); + assert_eq!(h.ws().toast.as_ref().map(|t| t.text.as_str()), Some("Claudiu 9.9.9 is ready. Restart to update.")); + assert_eq!(h.ws().update.status, crate::updater::Status::Ready { version: "9.9.9".into() }); +} + /// Settings lists the projects to move, rename and remove; a removal is undone from its toast, sessions and all. #[test] fn the_projects_page_moves_renames_and_removes_projects() { diff --git a/src/app/mod.rs b/src/app/mod.rs index 24ddc0b..ed68045 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -185,6 +185,8 @@ pub enum Act { ConfirmInstallUpdate, Dismiss, InstallUpdate, + /// The Check now button in Settings. + CheckForUpdates, /// New sessions start with this account; running ones keep theirs. SwitchAccount(Id), /// The shown session runs under another account: restart it under the active one (asks first, see `handoff.rs`). @@ -960,6 +962,7 @@ impl Workspace { crate::updater::install(self); } } + Act::CheckForUpdates => crate::updater::check_now(self), Act::ConfirmInstallUpdate => { self.modal = None; crate::updater::install(self); diff --git a/src/panels/settings_view.rs b/src/panels/settings_view.rs index d59eb4a..3ca7e9f 100644 --- a/src/panels/settings_view.rs +++ b/src/panels/settings_view.rs @@ -317,6 +317,14 @@ impl Workspace { .child(settings_toggle("settings-updates", "Check for updates", *updates, |m| match m { Modal::Settings { updates, .. } => Some(updates), _ => None, + })) + .child(row().child(if self.update.status == crate::updater::Status::Checking { + button_disabled("Checking…").key("settings-check-updates") + } else { + button("Check now", false).key("settings-check-updates").on_press_tx(|ws: &mut Workspace, tx: &mut Tx| { + ws.act(Act::CheckForUpdates); + ws.after_input(tx); + }) })), SettingsPage::Sessions => { let mut c = col() diff --git a/src/updater.rs b/src/updater.rs index e7f67d2..2f9f53c 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -3,7 +3,8 @@ //! * Only active when the app was installed by the Velopack installer; dev builds and portable //! copies without an install silently skip it. //! * The only network request is the public GitHub Releases API call Velopack makes. No identifiers -//! or usage data are sent. Set `settings.update_url` to `""` in state.json to disable entirely. +//! or usage data are sent. Set `settings.update_url` to `""` in state.json to turn off background +//! checks; Check now in Settings still looks once, at the default feed. use std::time::Duration; @@ -44,32 +45,80 @@ fn repo_url(ws: &Workspace) -> Option { pub fn check(ws: &mut Workspace) { ws.rt.after(Duration::from_secs(6 * 3600), check); let Some(url) = repo_url(ws) else { return }; + look(ws, url, false); +} + +/// The Check now button in Settings: one look, made even with background checks off, that says what it found. +pub fn check_now(ws: &mut Workspace) { + let url = repo_url(ws).unwrap_or_else(|| DEFAULT_REPO.to_string()); + look(ws, url, true); +} + +fn look(ws: &mut Workspace, url: String, asked: bool) { // Once an update is downloading or downloaded, a re-check would only throw that away. - if matches!(ws.update.status, Status::Checking | Status::Downloading | Status::Ready { .. }) { + let busy = match &ws.update.status { + Status::Checking => Some(None), + Status::Downloading => Some(Some("An update is downloading".to_string())), + Status::Ready { version } => Some(Some(format!("Claudiu {version} is ready. Restart to update."))), + _ => None, + }; + if let Some(said) = busy { + if let Some(text) = said.filter(|_| asked) { + ws.toast(text, false); + } return; } let previous = std::mem::replace(&mut ws.update.status, Status::Checking); ws.rt.spawn( move || { - let manager = UpdateManager::new(GithubSource::new(&url, None, false), None, None).map_err(|e| e.to_string())?; + let manager = UpdateManager::new(GithubSource::new(&url, None, false), None, None).map_err(|e| match e { + velopack::Error::NotInstalled(_) => "updates work only in a copy installed with the Claudiu installer".to_string(), + e => e.to_string(), + })?; let check = manager.check_for_updates().map_err(|e| e.to_string())?; Ok::<_, String>((manager, check)) }, - move |ws: &mut Workspace, result| match result { - Ok((manager, UpdateCheck::UpdateAvailable(info))) => { - ws.update.status = Status::Available { version: info.TargetFullRelease.Version.clone() }; - ws.update.manager = Some(manager); - ws.update.info = Some(*info); + move |ws: &mut Workspace, result| { + let found = match result { + Ok((manager, UpdateCheck::UpdateAvailable(info))) => { + let version = info.TargetFullRelease.Version.clone(); + ws.update.manager = Some(manager); + ws.update.info = Some(*info); + Ok(Found::Newer(version)) + } + Ok((manager, UpdateCheck::NoUpdateAvailable)) => Ok(Found::Latest(manager.get_current_version_as_string())), + Ok((_, UpdateCheck::RemoteIsEmpty)) => Err("the update feed has no releases".to_string()), + Err(e) => Err(e), + }; + let (status, said) = outcome(previous, found, asked); + ws.update.status = status; + if let Some((text, error)) = said { + ws.toast(text, error); } - Ok(_) => ws.update.status = Status::UpToDate, - // A failed re-check (e.g. offline) keeps an update already offered. - Err(_) if matches!(previous, Status::Available { .. }) => ws.update.status = previous, - // Not installed via Velopack (dev build / portable): nothing to do, and nothing to show. - Err(_) => ws.update.status = Status::Idle, }, ); } +/// What a check found: a newer release, or that the installed version is the latest. +#[derive(Debug)] +enum Found { + Newer(String), + Latest(String), +} + +/// The status a check leaves behind, and, when the user asked for the check, what to tell them (text, is an error). +fn outcome(previous: Status, found: Result, asked: bool) -> (Status, Option<(String, bool)>) { + let (status, said) = match found { + Ok(Found::Newer(version)) => (Status::Available { version: version.clone() }, (format!("Claudiu {version} is available"), false)), + Ok(Found::Latest(installed)) => (Status::UpToDate, (format!("Claudiu {installed} is the latest version"), false)), + // A failed re-check (e.g. offline) keeps an update already offered. + Err(e) if matches!(previous, Status::Available { .. }) => (previous, (format!("Could not check for updates: {e}"), true)), + // Not installed via Velopack (dev build / portable): nothing to do, and nothing to show unless asked. + Err(e) => (Status::Idle, (format!("Could not check for updates: {e}"), true)), + }; + (status, asked.then_some(said)) +} + /// Download (first click) then apply and restart (second click). pub fn install(ws: &mut Workspace) { let (Some(manager), Some(info)) = (ws.update.manager.clone(), ws.update.info.clone()) else { return }; @@ -100,3 +149,26 @@ pub fn install(ws: &mut Workspace) { _ => {} } } + +#[cfg(test)] +mod tests { + use super::{Found, Status, outcome}; + + #[test] + fn only_a_check_the_user_asked_for_says_what_it_found() { + let available = Status::Available { version: "9.9.9".into() }; + assert_eq!(outcome(Status::Idle, Ok(Found::Newer("9.9.9".into())), false), (available.clone(), None)); + assert_eq!(outcome(Status::Idle, Ok(Found::Newer("9.9.9".into())), true), (available, Some(("Claudiu 9.9.9 is available".into(), false)))); + let latest = "Claudiu 1.2.3 is the latest version".to_string(); + assert_eq!(outcome(Status::Idle, Ok(Found::Latest("1.2.3".into())), true), (Status::UpToDate, Some((latest, false)))); + assert_eq!(outcome(Status::Idle, Err("offline".into()), false), (Status::Idle, None)); + assert_eq!(outcome(Status::Idle, Err("offline".into()), true), (Status::Idle, Some(("Could not check for updates: offline".into(), true)))); + } + + #[test] + fn a_failed_check_keeps_an_update_already_offered() { + let available = Status::Available { version: "9.9.9".into() }; + assert_eq!(outcome(available.clone(), Err("offline".into()), false), (available.clone(), None)); + assert_eq!(outcome(available.clone(), Err("offline".into()), true).0, available); + } +}