| 1 | //! Release check for every install kind. A Linux copy under `~/.local` asks, then |
| 2 | //! downloads and swaps. Windows downloads quietly from the shell poll. Every other |
| 3 | //! release build opens the release page and leaves the binary in place. Flatpak and |
| 4 | //! a build without a release tag stay quiet. Settings > General uses `updates.enabled`. |
| 5 | |
| 6 | use std::collections::HashMap; |
| 7 | use std::sync::Arc; |
| 8 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 9 | use std::time::Duration; |
| 10 | |
| 11 | use app_base::release_feed::{self, AvailableUpdate}; |
| 12 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 13 | use gpui_kit::component::dialog::{Dialog, DialogFooter}; |
| 14 | use gpui_kit::component::{WindowExt as _, v_flex}; |
| 15 | use gpui_kit::*; |
| 16 | |
| 17 | use crate::app_state::AppHandle; |
| 18 | use crate::i18n::{tr, tr_args}; |
| 19 | use crate::theme; |
| 20 | use crate::updater; |
| 21 | |
| 22 | const ENABLED_META_KEY: &str = "updates.enabled"; |
| 23 | /// How often an offer waits for a main window closed into the tray to come back. |
| 24 | const WINDOW_WAIT: Duration = Duration::from_secs(30); |
| 25 | /// How often the progress dialog redraws while downloading. |
| 26 | const PROGRESS_REDRAW: Duration = Duration::from_millis(250); |
| 27 | const DIALOG_WIDTH: f32 = 460.; |
| 28 | |
| 29 | #[derive(Clone)] |
| 30 | enum Stage { |
| 31 | Idle, |
| 32 | Offer(AvailableUpdate), |
| 33 | Downloading { |
| 34 | update: AvailableUpdate, |
| 35 | progress: Arc<AtomicU64>, |
| 36 | }, |
| 37 | Ready(String), |
| 38 | Notice(String), |
| 39 | Failed(String), |
| 40 | } |
| 41 | |
| 42 | struct LinuxUpdates { |
| 43 | stage: Stage, |
| 44 | dialog_window: Option<WindowId>, |
| 45 | /// "Not now" holds for this run only; the next start asks again. |
| 46 | declined: Option<String>, |
| 47 | } |
| 48 | |
| 49 | impl Global for LinuxUpdates {} |
| 50 | |
| 51 | fn stage(cx: &App) -> Stage { |
| 52 | cx.try_global::<LinuxUpdates>() |
| 53 | .map(|updates| updates.stage.clone()) |
| 54 | .unwrap_or(Stage::Idle) |
| 55 | } |
| 56 | |
| 57 | fn set_stage(stage: Stage, cx: &mut App) { |
| 58 | if cx.has_global::<LinuxUpdates>() { |
| 59 | cx.global_mut::<LinuxUpdates>().stage = stage; |
| 60 | } |
| 61 | cx.refresh_windows(); |
| 62 | } |
| 63 | |
| 64 | fn with_store<R>(cx: &App, read: impl FnOnce(&store::Store) -> R) -> Option<R> { |
| 65 | cx.try_global::<AppHandle>() |
| 66 | .map(|handle| read(&handle.0.read(cx).store)) |
| 67 | } |
| 68 | |
| 69 | /// The periodic check; on by default. |
| 70 | pub fn enabled(cx: &App) -> bool { |
| 71 | with_store(cx, |store| { |
| 72 | store.meta(ENABLED_META_KEY).ok().flatten().as_deref() != Some("0") |
| 73 | }) |
| 74 | .unwrap_or(true) |
| 75 | } |
| 76 | |
| 77 | pub fn set_enabled(enabled: bool, cx: &mut App) { |
| 78 | with_store(cx, |store| { |
| 79 | let _ = store.set_meta(ENABLED_META_KEY, if enabled { "1" } else { "0" }); |
| 80 | }); |
| 81 | cx.refresh_windows(); |
| 82 | } |
| 83 | |
| 84 | /// Flatpak updates through the package. A build without a release tag does not look. |
| 85 | pub fn checks_releases() -> bool { |
| 86 | // KVT: the AUR package (and any system package) updates through the package manager. |
| 87 | if app_base::kvt_feed::package_managed() { |
| 88 | return false; |
| 89 | } |
| 90 | #[cfg(target_os = "linux")] |
| 91 | { |
| 92 | let flatpak = std::env::var_os("FLATPAK_ID"); |
| 93 | if flatpak.is_some_and(|id| !id.is_empty()) { |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | app_base::version::release_tag().is_some() |
| 98 | } |
| 99 | |
| 100 | /// Linux under `~/.local` replaces itself after the user agrees. Windows does that |
| 101 | /// quietly from the shell poll, so this module does not download there. |
| 102 | fn installed_release() -> Option<&'static str> { |
| 103 | #[cfg(target_os = "linux")] |
| 104 | { |
| 105 | crate::linux_install::updatable_release() |
| 106 | } |
| 107 | #[cfg(not(target_os = "linux"))] |
| 108 | { |
| 109 | None |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn declined(cx: &App) -> Option<String> { |
| 114 | cx.try_global::<LinuxUpdates>() |
| 115 | .and_then(|updates| updates.declined.clone()) |
| 116 | } |
| 117 | |
| 118 | fn decline(tag: &str, cx: &mut App) { |
| 119 | if cx.has_global::<LinuxUpdates>() { |
| 120 | cx.global_mut::<LinuxUpdates>().declined = Some(tag.to_owned()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// A newer release a previous run downloaded but did not install ("Later"). |
| 125 | fn staged_update(running_tag: &str) -> Option<String> { |
| 126 | let paths = updater::current_paths()?; |
| 127 | if !updater::check(&paths) { |
| 128 | return None; |
| 129 | } |
| 130 | let tag = std::fs::read_to_string(&paths.version_file).ok()?; |
| 131 | let tag = tag.trim(); |
| 132 | if release_feed::is_newer(tag, running_tag) { |
| 133 | return Some(tag.to_owned()); |
| 134 | } |
| 135 | // Same or older than this build, e.g. after installing a release by hand. |
| 136 | let _ = std::fs::remove_file(&paths.version_file); |
| 137 | let _ = std::fs::remove_file(&paths.update_exe); |
| 138 | None |
| 139 | } |
| 140 | |
| 141 | fn args(name: &str, value: impl Into<String>) -> HashMap<String, String> { |
| 142 | HashMap::from([(name.to_owned(), value.into())]) |
| 143 | } |
| 144 | |
| 145 | fn notice(text: String, cx: &mut App) { |
| 146 | if let Some(handle) = cx.try_global::<AppHandle>() { |
| 147 | handle |
| 148 | .0 |
| 149 | .clone() |
| 150 | .update(cx, |state, cx| state.raise_notice(text, None, cx)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | fn ensure_state(cx: &mut App) { |
| 155 | if !cx.has_global::<LinuxUpdates>() { |
| 156 | cx.set_global(LinuxUpdates { |
| 157 | stage: Stage::Idle, |
| 158 | dialog_window: None, |
| 159 | declined: None, |
| 160 | }); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /// Starts the periodic check. Windows keeps its own quiet poll. |
| 165 | pub fn start(cx: &mut App) { |
| 166 | if !checks_releases() || cfg!(windows) { |
| 167 | return; |
| 168 | } |
| 169 | ensure_state(cx); |
| 170 | if let Some(running_tag) = installed_release() |
| 171 | && let Some(tag) = staged_update(running_tag) |
| 172 | { |
| 173 | set_stage(Stage::Ready(tag), cx); |
| 174 | show_when_window_is_open(cx); |
| 175 | } |
| 176 | cx.spawn(async move |cx| { |
| 177 | cx.background_executor() |
| 178 | .timer(release_feed::FIRST_CHECK_DELAY) |
| 179 | .await; |
| 180 | loop { |
| 181 | let waiting = cx.update(|cx| matches!(stage(cx), Stage::Ready(_))); |
| 182 | if !waiting && cx.update(|cx| enabled(cx)) { |
| 183 | cx.update(|cx| check(false, cx)); |
| 184 | } |
| 185 | cx.background_executor() |
| 186 | .timer(release_feed::CHECK_INTERVAL) |
| 187 | .await; |
| 188 | } |
| 189 | }) |
| 190 | .detach(); |
| 191 | } |
| 192 | |
| 193 | /// Looks for a newer release. `manual` (Settings > Check now) also offers a version |
| 194 | /// declined with "Not now" in this run and reports "up to date" and errors. |
| 195 | pub fn check(manual: bool, cx: &mut App) { |
| 196 | if !checks_releases() { |
| 197 | return; |
| 198 | } |
| 199 | if cfg!(windows) { |
| 200 | check_windows(manual, cx); |
| 201 | return; |
| 202 | } |
| 203 | let Some(running_tag) = installed_release() else { |
| 204 | check_notice(manual, cx); |
| 205 | return; |
| 206 | }; |
| 207 | ensure_state(cx); |
| 208 | match stage(cx) { |
| 209 | Stage::Downloading { .. } | Stage::Ready(_) => { |
| 210 | open_dialog(cx); |
| 211 | return; |
| 212 | } |
| 213 | Stage::Idle | Stage::Offer(_) | Stage::Notice(_) | Stage::Failed(_) => {} |
| 214 | } |
| 215 | if manual { |
| 216 | notice(tr("install.update_checking"), cx); |
| 217 | } |
| 218 | let find = release_feed::find_update(running_tag, cx); |
| 219 | cx.spawn(async move |cx| { |
| 220 | let found = find.await; |
| 221 | cx.update(|cx| match found { |
| 222 | Ok(Some(update)) if manual || declined(cx).as_deref() != Some(&update.tag) => { |
| 223 | offer(update, cx) |
| 224 | } |
| 225 | Ok(Some(_)) => {} |
| 226 | Ok(None) if manual => notice(tr("install.up_to_date"), cx), |
| 227 | Ok(None) => {} |
| 228 | Err(error) if manual => { |
| 229 | notice(tr_args("install.update_failed", &args("detail", error)), cx) |
| 230 | } |
| 231 | Err(error) => log::warn!("update check failed: {error}"), |
| 232 | }); |
| 233 | }) |
| 234 | .detach(); |
| 235 | } |
| 236 | |
| 237 | fn check_windows(manual: bool, cx: &mut App) { |
| 238 | if !manual && !enabled(cx) { |
| 239 | return; |
| 240 | } |
| 241 | let Some(running_tag) = app_base::version::release_tag() else { |
| 242 | return; |
| 243 | }; |
| 244 | let Some(paths) = updater::current_paths() else { |
| 245 | return; |
| 246 | }; |
| 247 | if manual { |
| 248 | notice(tr("install.update_checking"), cx); |
| 249 | } |
| 250 | let fetch = release_feed::fetch_update(running_tag, paths, cx); |
| 251 | cx.spawn(async move |cx| { |
| 252 | let found = fetch.await; |
| 253 | cx.update(|cx| match found { |
| 254 | Ok(Some(tag)) if manual => notice( |
| 255 | tr_args("install.update_ready_body", &args("version", tag)), |
| 256 | cx, |
| 257 | ), |
| 258 | Ok(_) if manual => notice(tr("install.up_to_date"), cx), |
| 259 | Ok(_) => {} |
| 260 | Err(error) if manual => { |
| 261 | notice(tr_args("install.update_failed", &args("detail", error)), cx) |
| 262 | } |
| 263 | Err(error) => log::warn!("update check failed: {error}"), |
| 264 | }); |
| 265 | }) |
| 266 | .detach(); |
| 267 | } |
| 268 | |
| 269 | fn check_notice(manual: bool, cx: &mut App) { |
| 270 | let Some(running_tag) = app_base::version::release_tag() else { |
| 271 | return; |
| 272 | }; |
| 273 | ensure_state(cx); |
| 274 | if matches!(stage(cx), Stage::Notice(_)) { |
| 275 | open_dialog(cx); |
| 276 | return; |
| 277 | } |
| 278 | if manual { |
| 279 | notice(tr("install.update_checking"), cx); |
| 280 | } |
| 281 | let find = release_feed::find_release(running_tag, cx); |
| 282 | cx.spawn(async move |cx| { |
| 283 | let found = find.await; |
| 284 | cx.update(|cx| match found { |
| 285 | Ok(Some(update)) if manual || declined(cx).as_deref() != Some(&update.tag) => { |
| 286 | set_stage(Stage::Notice(update.tag), cx); |
| 287 | show_when_window_is_open(cx); |
| 288 | } |
| 289 | Ok(Some(_)) => {} |
| 290 | Ok(None) if manual => notice(tr("install.up_to_date"), cx), |
| 291 | Ok(None) => {} |
| 292 | Err(error) if manual => { |
| 293 | notice(tr_args("install.update_failed", &args("detail", error)), cx) |
| 294 | } |
| 295 | Err(error) => log::warn!("update check failed: {error}"), |
| 296 | }); |
| 297 | }) |
| 298 | .detach(); |
| 299 | } |
| 300 | |
| 301 | fn offer(update: AvailableUpdate, cx: &mut App) { |
| 302 | set_stage(Stage::Offer(update), cx); |
| 303 | show_when_window_is_open(cx); |
| 304 | } |
| 305 | |
| 306 | /// Opens the dialog for the current stage, waiting for the main window if it is |
| 307 | /// closed into the tray. |
| 308 | fn show_when_window_is_open(cx: &mut App) { |
| 309 | cx.spawn(async move |cx| { |
| 310 | while cx.update(|cx| crate::main_window::current(cx).is_none()) { |
| 311 | cx.background_executor().timer(WINDOW_WAIT).await; |
| 312 | } |
| 313 | cx.update(|cx| { |
| 314 | if matches!( |
| 315 | stage(cx), |
| 316 | Stage::Offer(_) | Stage::Ready(_) | Stage::Notice(_) |
| 317 | ) { |
| 318 | open_dialog(cx); |
| 319 | } |
| 320 | }); |
| 321 | }) |
| 322 | .detach(); |
| 323 | } |
| 324 | |
| 325 | fn download(update: AvailableUpdate, cx: &mut App) { |
| 326 | let Some(paths) = updater::current_paths() else { |
| 327 | return; |
| 328 | }; |
| 329 | let progress = Arc::new(AtomicU64::new(0)); |
| 330 | set_stage( |
| 331 | Stage::Downloading { |
| 332 | update: update.clone(), |
| 333 | progress: progress.clone(), |
| 334 | }, |
| 335 | cx, |
| 336 | ); |
| 337 | let tag = update.tag.clone(); |
| 338 | let task = release_feed::download_update(update, paths, progress, cx); |
| 339 | cx.spawn(async move |cx| { |
| 340 | let redraw = cx.spawn(async move |cx| { |
| 341 | loop { |
| 342 | cx.background_executor().timer(PROGRESS_REDRAW).await; |
| 343 | cx.update(|cx| cx.refresh_windows()); |
| 344 | } |
| 345 | }); |
| 346 | let result = task.await; |
| 347 | drop(redraw); |
| 348 | cx.update(|cx| { |
| 349 | match result { |
| 350 | Ok(()) => set_stage(Stage::Ready(tag), cx), |
| 351 | Err(error) => { |
| 352 | log::warn!("update download failed: {error}"); |
| 353 | set_stage(Stage::Failed(error), cx); |
| 354 | } |
| 355 | } |
| 356 | open_dialog(cx); |
| 357 | }); |
| 358 | }) |
| 359 | .detach(); |
| 360 | } |
| 361 | |
| 362 | fn restart(cx: &mut App) { |
| 363 | let Some(paths) = updater::current_paths() else { |
| 364 | return; |
| 365 | }; |
| 366 | match updater::install_and_relaunch(&paths) { |
| 367 | Ok(()) => cx.quit(), |
| 368 | Err(error) => set_stage(Stage::Failed(error), cx), |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /// Opens the update dialog on the main window unless it is already open. It redraws |
| 373 | /// from the current stage, so one dialog walks through offer, progress and restart. |
| 374 | fn open_dialog(cx: &mut App) { |
| 375 | let Some(window) = crate::main_window::current(cx) else { |
| 376 | return; |
| 377 | }; |
| 378 | if !cx.has_global::<LinuxUpdates>() |
| 379 | || cx.global::<LinuxUpdates>().dialog_window == Some(window.window_id()) |
| 380 | { |
| 381 | return; |
| 382 | } |
| 383 | cx.global_mut::<LinuxUpdates>().dialog_window = Some(window.window_id()); |
| 384 | let opened = window.update(cx, |_, window, cx| { |
| 385 | window.open_dialog(cx, |dialog: Dialog, _, cx| build_dialog(dialog, cx)); |
| 386 | }); |
| 387 | if opened.is_err() { |
| 388 | cx.global_mut::<LinuxUpdates>().dialog_window = None; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | fn close_dialog(window: &mut Window, cx: &mut App) { |
| 393 | if cx.has_global::<LinuxUpdates>() { |
| 394 | cx.global_mut::<LinuxUpdates>().dialog_window = None; |
| 395 | } |
| 396 | window.close_dialog(cx); |
| 397 | } |
| 398 | |
| 399 | fn percent(progress: &AtomicU64, size: u64) -> u64 { |
| 400 | (progress.load(Ordering::Relaxed) * 100) |
| 401 | .checked_div(size) |
| 402 | .unwrap_or(0) |
| 403 | .min(100) |
| 404 | } |
| 405 | |
| 406 | fn build_dialog(dialog: Dialog, cx: &App) -> Dialog { |
| 407 | let running = app_base::version::release_tag().unwrap_or_default(); |
| 408 | let (title, body, footer) = match stage(cx) { |
| 409 | Stage::Offer(update) => { |
| 410 | let mut text_args = args("version", update.tag.clone()); |
| 411 | text_args.insert("current".to_owned(), running.to_owned()); |
| 412 | let tag = update.tag.clone(); |
| 413 | ( |
| 414 | tr("install.update_available_title"), |
| 415 | tr_args("install.update_available_body", &text_args), |
| 416 | DialogFooter::new() |
| 417 | .child( |
| 418 | Button::new("update-not-now") |
| 419 | .label(tr("install.not_now")) |
| 420 | .on_click(move |_, window, cx| { |
| 421 | decline(&tag, cx); |
| 422 | set_stage(Stage::Idle, cx); |
| 423 | close_dialog(window, cx); |
| 424 | }), |
| 425 | ) |
| 426 | .child( |
| 427 | Button::new("update-start") |
| 428 | .primary() |
| 429 | .label(tr("install.update_now")) |
| 430 | .on_click(move |_, _, cx| download(update.clone(), cx)), |
| 431 | ), |
| 432 | ) |
| 433 | } |
| 434 | Stage::Downloading { update, progress } => { |
| 435 | let mut text_args = args("version", update.tag.clone()); |
| 436 | text_args.insert( |
| 437 | "percent".to_owned(), |
| 438 | percent(&progress, update.asset.size).to_string(), |
| 439 | ); |
| 440 | ( |
| 441 | tr("install.update_available_title"), |
| 442 | tr_args("install.update_downloading", &text_args), |
| 443 | DialogFooter::new().child( |
| 444 | Button::new("update-hide") |
| 445 | .label(tr("install.hide")) |
| 446 | .on_click(|_, window, cx| close_dialog(window, cx)), |
| 447 | ), |
| 448 | ) |
| 449 | } |
| 450 | Stage::Notice(tag) => { |
| 451 | let mut text_args = args("version", tag.clone()); |
| 452 | text_args.insert("current".to_owned(), running.to_owned()); |
| 453 | let open = tag.clone(); |
| 454 | ( |
| 455 | tr("install.update_available_title"), |
| 456 | tr_args("install.update_available_body", &text_args), |
| 457 | DialogFooter::new() |
| 458 | .child( |
| 459 | Button::new("update-not-now") |
| 460 | .label(tr("install.not_now")) |
| 461 | .on_click(move |_, window, cx| { |
| 462 | decline(&tag, cx); |
| 463 | set_stage(Stage::Idle, cx); |
| 464 | close_dialog(window, cx); |
| 465 | }), |
| 466 | ) |
| 467 | .child( |
| 468 | Button::new("update-open-release") |
| 469 | .primary() |
| 470 | .label(tr("install.open_release")) |
| 471 | .on_click(move |_, window, cx| { |
| 472 | let url = release_feed::release_page_url(&open); |
| 473 | decline(&open, cx); |
| 474 | set_stage(Stage::Idle, cx); |
| 475 | close_dialog(window, cx); |
| 476 | app_base::open_link::open_url(cx, &url); |
| 477 | }), |
| 478 | ), |
| 479 | ) |
| 480 | } |
| 481 | Stage::Ready(tag) => ( |
| 482 | tr("install.update_ready_title"), |
| 483 | tr_args("install.update_ready_body", &args("version", tag)), |
| 484 | DialogFooter::new() |
| 485 | .child( |
| 486 | Button::new("update-later") |
| 487 | .label(tr("install.later")) |
| 488 | .on_click(|_, window, cx| close_dialog(window, cx)), |
| 489 | ) |
| 490 | .child( |
| 491 | Button::new("update-restart") |
| 492 | .primary() |
| 493 | .label(tr("install.restart_now")) |
| 494 | .on_click(|_, _, cx| restart(cx)), |
| 495 | ), |
| 496 | ), |
| 497 | Stage::Failed(error) => ( |
| 498 | tr("install.update_available_title"), |
| 499 | tr_args("install.update_failed", &args("detail", error)), |
| 500 | DialogFooter::new().child( |
| 501 | Button::new("update-close") |
| 502 | .label(tr("common.close")) |
| 503 | .on_click(|_, window, cx| { |
| 504 | set_stage(Stage::Idle, cx); |
| 505 | close_dialog(window, cx); |
| 506 | }), |
| 507 | ), |
| 508 | ), |
| 509 | Stage::Idle => (String::new(), String::new(), DialogFooter::new()), |
| 510 | }; |
| 511 | // Only the buttons close it, so the stage and the open flag stay in step. |
| 512 | dialog |
| 513 | .title(title) |
| 514 | .w(px(DIALOG_WIDTH)) |
| 515 | .keyboard(false) |
| 516 | .overlay_closable(false) |
| 517 | .close_button(false) |
| 518 | .child(v_flex().gap(px(8.)).text_color(theme::text()).child(body)) |
| 519 | .footer(footer) |
| 520 | } |
| 521 | |
| 522 | #[cfg(test)] |
| 523 | mod tests { |
| 524 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 525 | |
| 526 | use super::percent; |
| 527 | |
| 528 | #[test] |
| 529 | fn percent_is_clamped_and_safe_without_a_size() { |
| 530 | let progress = AtomicU64::new(50); |
| 531 | assert_eq!(percent(&progress, 200), 25); |
| 532 | assert_eq!(percent(&progress, 0), 0); |
| 533 | progress.store(500, Ordering::Relaxed); |
| 534 | assert_eq!(percent(&progress, 200), 100); |
| 535 | } |
| 536 | } |