Updates: opt-in launch check, Check for Updates…, install after a normal quit, from the Gitea release; dmgs carry an Applications link (UPD-1, REL-1)
This commit is contained in:
28 files changed
+896
-16
No files matched your search
@@ -18,6 +18,7 @@ actions!(
|
||||
CloseTab,
|
||||
ToggleCommandPalette,
|
||||
OpenSettings,
|
||||
CheckForUpdates,
|
||||
Quit
|
||||
]
|
||||
);
|
||||
@@ -43,7 +44,7 @@ pub fn bind_quit(cx: &mut App) {
|
||||
}
|
||||
|
||||
/// Quits through the main window's route, if one is set.
|
||||
fn quit(cx: &mut App) {
|
||||
pub(crate) fn quit(cx: &mut App) {
|
||||
match cx.try_global::<QuitRoute>().map(|route| Rc::clone(&route.0)) {
|
||||
Some(route) => route(cx),
|
||||
None => cx.quit(),
|
||||
@@ -171,6 +172,10 @@ pub fn menus(
|
||||
first.push(MenuItem::separator());
|
||||
}
|
||||
first.push(MenuItem::action(i18n::text(cx, "menu.app.settings", &[]), OpenSettings));
|
||||
// Only an installed app can update itself.
|
||||
if crate::updates::updater(cx).is_some() {
|
||||
first.push(MenuItem::action(i18n::text(cx, "menu.app.updates", &[]), CheckForUpdates));
|
||||
}
|
||||
first.push(MenuItem::separator());
|
||||
first.push(MenuItem::action(i18n::text(cx, "menu.app.quit", &[("app", app.name())]), Quit));
|
||||
let mut menus = vec![
|
||||
|
||||
@@ -24,6 +24,9 @@ use gpui::{App, SharedString};
|
||||
|
||||
const EN: &[(&str, &str)] = &[
|
||||
("menu.app.quit", "Quit {app}"),
|
||||
("updates.failed", "The update did not finish: {error}"),
|
||||
("updates.latest", "{app} {version} is the latest version."),
|
||||
("menu.app.updates", "Check for Updates…"),
|
||||
("menu.app.switch", "Open Ely App"),
|
||||
("toast.open", "Open"),
|
||||
("toast.open_failed", "The file did not open: {error}"),
|
||||
@@ -76,6 +79,9 @@ const EN: &[(&str, &str)] = &[
|
||||
|
||||
const ZH_CN: &[(&str, &str)] = &[
|
||||
("menu.app.quit", "退出 {app}"),
|
||||
("updates.failed", "更新没有完成:{error}"),
|
||||
("updates.latest", "{app} {version} 已是最新版本。"),
|
||||
("menu.app.updates", "检查更新…"),
|
||||
("menu.app.switch", "打开 Ely 应用"),
|
||||
("toast.open", "打开"),
|
||||
("toast.open_failed", "文件没能打开:{error}"),
|
||||
|
||||
@@ -9,6 +9,11 @@ pub(super) const EN: &[(&str, &str)] = &[
|
||||
("settings.appearance.light", "Light"),
|
||||
("settings.appearance.dark", "Dark"),
|
||||
("settings.omarchy", "Follow Omarchy"),
|
||||
("settings.updates", "Check for updates at launch"),
|
||||
(
|
||||
"settings.updates.hint",
|
||||
"Off, Ely asks its release server for a newer version only when you choose Check for Updates.",
|
||||
),
|
||||
("settings.language", "Language"),
|
||||
("settings.language.system", "System"),
|
||||
("settings.language.next", "Applies the next time each app starts"),
|
||||
@@ -64,6 +69,11 @@ pub(super) const ZH_CN: &[(&str, &str)] = &[
|
||||
("settings.appearance.light", "浅色"),
|
||||
("settings.appearance.dark", "深色"),
|
||||
("settings.omarchy", "跟随 Omarchy"),
|
||||
("settings.updates", "启动时检查更新"),
|
||||
(
|
||||
"settings.updates.hint",
|
||||
"关闭时,只在你选择“检查更新”时才向发布服务器询问新版本。",
|
||||
),
|
||||
("settings.language", "语言"),
|
||||
("settings.language.system", "跟随系统"),
|
||||
("settings.language.next", "各应用下次启动时生效"),
|
||||
|
||||
@@ -6,6 +6,7 @@ pub mod settings;
|
||||
mod shell;
|
||||
pub mod tasks;
|
||||
mod theme;
|
||||
pub mod updates;
|
||||
|
||||
pub use opening::{launch_files, on_open};
|
||||
pub use run::run;
|
||||
|
||||
@@ -15,6 +15,10 @@ pub fn run<V: Render>(
|
||||
app: AppId,
|
||||
root: impl FnOnce(AppId, &mut Window, &mut App) -> Entity<V> + 'static,
|
||||
) {
|
||||
// As the installer after an update, nothing else runs.
|
||||
if let Some(code) = crate::updates::installer(app) {
|
||||
std::process::exit(code);
|
||||
}
|
||||
// Before the log: a second copy must not trim it.
|
||||
if let Err(error) = ely_platform::instance::hold(app) {
|
||||
startup::fail(app, &format!("{error}"));
|
||||
@@ -77,6 +81,8 @@ fn open<V: Render>(
|
||||
}
|
||||
theme::install(cx);
|
||||
crate::i18n::install(cx);
|
||||
crate::updates::install(app, cx);
|
||||
cx.on_action(|_: &crate::commands::CheckForUpdates, cx| crate::updates::check_now(cx));
|
||||
crate::commands::bind_keys(cx);
|
||||
crate::commands::bind_quit(cx);
|
||||
cx.on_action(move |_: &crate::commands::OpenSettings, cx| crate::settings::open(app, cx));
|
||||
|
||||
@@ -9,7 +9,7 @@ use ely_gpui_component::{
|
||||
};
|
||||
use ely_platform::{
|
||||
AppId,
|
||||
settings::{APPEARANCE, AUTHOR, Appearance, LANGUAGE, Language, MOTION, Motion, OMARCHY_SYNC, Store, word},
|
||||
settings::{APPEARANCE, AUTHOR, Appearance, CHECK_UPDATES, LANGUAGE, Language, MOTION, Motion, OMARCHY_SYNC, Store, word},
|
||||
};
|
||||
use gpui::{
|
||||
AppContext as _, Context, Entity, FocusHandle, Global, IntoElement, ParentElement, Render, SharedString, Styled,
|
||||
@@ -222,7 +222,7 @@ impl SettingsView {
|
||||
Motion::Reduced => text(cx, "settings.motion.reduce", &[]),
|
||||
Motion::Full => text(cx, "settings.motion.full", &[]),
|
||||
}, MOTION, cx);
|
||||
let mut section = SettingsSection::new(text(cx, "settings.general", &[]))
|
||||
let section = SettingsSection::new(text(cx, "settings.general", &[]))
|
||||
.row(SettingsRow::new(text(cx, "settings.appearance", &[])).control(theme))
|
||||
.row(
|
||||
SettingsRow::new(text(cx, "settings.language", &[]))
|
||||
@@ -230,6 +230,14 @@ impl SettingsView {
|
||||
.control(language),
|
||||
)
|
||||
.row(SettingsRow::new(text(cx, "settings.motion", &[])).control(motion));
|
||||
let updates = Switch::new("settings-updates", shared.check_updates)
|
||||
.name(text(cx, "settings.updates", &[]))
|
||||
.on_change(|on, _, cx| set(CHECK_UPDATES, Value::Bool(on), cx));
|
||||
let mut section = section.row(
|
||||
SettingsRow::new(text(cx, "settings.updates", &[]))
|
||||
.description(text(cx, "settings.updates.hint", &[]))
|
||||
.control(updates),
|
||||
);
|
||||
// Only where Omarchy is installed.
|
||||
if ely_platform::omarchy_current_dir().is_some() {
|
||||
let follow = Switch::new("settings-omarchy", shared.omarchy_sync)
|
||||
|
||||
@@ -124,6 +124,7 @@ impl RenderOnce for Shell {
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(TitleBar::new("titlebar").title(self.title))
|
||||
.children(crate::updates::updater(cx))
|
||||
.children(menu_bar(cx))
|
||||
.children(self.tabs);
|
||||
// Panels give way so the content keeps its room.
|
||||
|
||||
@@ -102,6 +102,33 @@ fn build(
|
||||
if !document {
|
||||
window.focus(&focus, cx);
|
||||
}
|
||||
if let Some(updates) = crate::updates::updater(cx) {
|
||||
cx.subscribe(
|
||||
&updates,
|
||||
|root: &mut Root, _, news: &crate::updates::UpdateNews, cx| match news {
|
||||
crate::updates::UpdateNews::Failed(error) => {
|
||||
let text = crate::i18n::text(cx, "updates.failed", &[("error", error)]);
|
||||
root.toast(text, Severity::Danger, cx);
|
||||
}
|
||||
crate::updates::UpdateNews::Latest => {
|
||||
let args = [
|
||||
("app", root.app.name()),
|
||||
("version", env!("CARGO_PKG_VERSION")),
|
||||
];
|
||||
let text = crate::i18n::text(cx, "updates.latest", &args);
|
||||
root.toast(text, Severity::Success, cx);
|
||||
}
|
||||
},
|
||||
)
|
||||
.detach();
|
||||
// A failed install from the last quit says why.
|
||||
if let Some(note) = updates.update(cx, |updates, _| updates.note.take()) {
|
||||
let text = crate::i18n::text(cx, "updates.failed", &[("error", ¬e)]);
|
||||
cx.defer_in(window, move |root: &mut Root, _, cx| {
|
||||
root.toast(text, Severity::Danger, cx)
|
||||
});
|
||||
}
|
||||
}
|
||||
Root {
|
||||
app,
|
||||
focus,
|
||||
|
||||
@@ -0,0 +1,428 @@
|
||||
//! A newer release: offered, downloaded, installed after quitting.
|
||||
|
||||
use std::{
|
||||
io,
|
||||
path::{Path, PathBuf},
|
||||
process::{Command, Stdio},
|
||||
sync::atomic::AtomicBool,
|
||||
};
|
||||
|
||||
use ely_gpui_component::shell::{UpdateBanner, UpdateState};
|
||||
use ely_platform::{
|
||||
AppId,
|
||||
update::{self, Check, Release},
|
||||
};
|
||||
use futures::{StreamExt as _, channel::mpsc};
|
||||
use gpui::{
|
||||
App, AppContext as _, Context, Entity, EventEmitter, Global, IntoElement, Render, Window, div,
|
||||
};
|
||||
|
||||
/// The release server's newest release, listed by its API.
|
||||
const LATEST: &str = "https://git.zacharyzhang.com/api/v1/repos/ZacharyZhang-NY/ElyOffice/releases/latest";
|
||||
/// The argument that runs this binary as the installer.
|
||||
const INSTALL: &str = "--ely-install-update";
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
enum Phase {
|
||||
Offered,
|
||||
Fetching(f32),
|
||||
Fetched(PathBuf),
|
||||
}
|
||||
|
||||
pub struct Updates {
|
||||
app: AppId,
|
||||
bundle: PathBuf,
|
||||
offer: Option<(Release, Phase)>,
|
||||
dismissed: bool,
|
||||
/// The download to install once the app has quit.
|
||||
pending: Option<PathBuf>,
|
||||
/// Why the last install failed, for the window to say.
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
/// What an update says to the window.
|
||||
pub enum UpdateNews {
|
||||
Failed(String),
|
||||
/// A check the user asked for found nothing newer.
|
||||
Latest,
|
||||
}
|
||||
|
||||
impl EventEmitter<UpdateNews> for Updates {}
|
||||
|
||||
struct Updater(Entity<Updates>);
|
||||
|
||||
impl Global for Updater {}
|
||||
|
||||
/// The app's updater, if it runs from an installed bundle.
|
||||
pub fn updater(cx: &App) -> Option<Entity<Updates>> {
|
||||
cx.try_global::<Updater>().map(|updater| updater.0.clone())
|
||||
}
|
||||
|
||||
/// Checks now, as the user asked from the menu.
|
||||
pub fn check_now(cx: &mut App) {
|
||||
if let Some(updates) = updater(cx) {
|
||||
updates.update(cx, |updates, cx| updates.check(true, cx));
|
||||
}
|
||||
}
|
||||
|
||||
/// The quit an update asked for stopped: install nothing.
|
||||
pub fn quit_stopped(cx: &mut App) {
|
||||
if let Some(updates) = updater(cx) {
|
||||
updates.update(cx, |updates, _| {
|
||||
if updates.pending.take().is_some() {
|
||||
log::info!("update: the quit stopped; nothing installs");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Run as the installer: waits, installs, reopens; the exit code.
|
||||
pub fn installer(app: AppId) -> Option<i32> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let [_, flag, dmg, bundle, pid] = args.as_slice() else {
|
||||
return None;
|
||||
};
|
||||
if flag != INSTALL {
|
||||
return None;
|
||||
}
|
||||
let Ok(pid) = pid.parse() else {
|
||||
eprintln!("update: {pid} is not a process number");
|
||||
return Some(2);
|
||||
};
|
||||
ely_platform::install::wait_for_exit(pid);
|
||||
let installed = ely_platform::install::install(Path::new(dmg), Path::new(bundle));
|
||||
if let Err(error) = &installed {
|
||||
// To the app's log; the reopened app shows it.
|
||||
eprintln!("update: the install failed: {error}");
|
||||
let note = ely_platform::data_dir(app)
|
||||
.map_err(io::Error::other)
|
||||
.and_then(|dir| {
|
||||
std::fs::create_dir_all(dir.join("updates"))?;
|
||||
std::fs::write(dir.join("updates/failed.txt"), error.to_string())
|
||||
});
|
||||
if let Err(lost) = note {
|
||||
eprintln!("update: the failure note was not written: {lost}");
|
||||
}
|
||||
}
|
||||
// Last: the note is written before the app reads it.
|
||||
if let Err(error) = ely_platform::install::reopen(Path::new(bundle)) {
|
||||
eprintln!("update: the app did not reopen: {error}");
|
||||
return Some(1);
|
||||
}
|
||||
Some(i32::from(installed.is_err()))
|
||||
}
|
||||
|
||||
/// An installed app's updater; checks at launch if chosen.
|
||||
pub(crate) fn install(app: AppId, cx: &mut App) {
|
||||
let bundle = match ely_platform::install::running_bundle() {
|
||||
Ok(Some(bundle)) => bundle,
|
||||
Ok(None) => return log::info!("update: a dev build; no check"),
|
||||
Err(error) => return log::warn!("update: the running app is not found: {error}"),
|
||||
};
|
||||
match update::install_policy() {
|
||||
Ok(update::InstallPolicy::SelfManaged) => {}
|
||||
Ok(update::InstallPolicy::PackageManager) => {
|
||||
return log::info!("update: the package manager updates this app");
|
||||
}
|
||||
Err(error) => return log::warn!("update: {error:#}"),
|
||||
}
|
||||
let note = ely_platform::data_dir(app)
|
||||
.ok()
|
||||
.map(|dir| dir.join("updates/failed.txt"));
|
||||
let note = note.and_then(|path| {
|
||||
std::fs::read_to_string(&path)
|
||||
.ok()
|
||||
.inspect(|_| drop(std::fs::remove_file(&path)))
|
||||
});
|
||||
let updates = cx.new(|cx| {
|
||||
cx.on_app_quit(|this: &mut Updates, _| {
|
||||
this.start_installer();
|
||||
async {}
|
||||
})
|
||||
.detach();
|
||||
Updates {
|
||||
app,
|
||||
bundle,
|
||||
offer: None,
|
||||
dismissed: false,
|
||||
pending: None,
|
||||
note,
|
||||
}
|
||||
});
|
||||
cx.set_global(Updater(updates.clone()));
|
||||
// Off by default: no request unless the user chose it.
|
||||
if crate::settings::current(cx).check_updates {
|
||||
updates.update(cx, |updates, cx| updates.check(false, cx));
|
||||
} else {
|
||||
log::info!("update: no check at launch; it is off in settings");
|
||||
}
|
||||
}
|
||||
|
||||
/// The app's manifest in the newest release, or the test's.
|
||||
fn manifest(app: AppId) -> anyhow::Result<String> {
|
||||
match std::env::var("ELY_UPDATE_MANIFEST") {
|
||||
Ok(url) => Ok(url),
|
||||
Err(_) => ely_platform::release::asset_url(LATEST, &format!("{}.json", app.slug())),
|
||||
}
|
||||
}
|
||||
|
||||
impl Updates {
|
||||
/// Asks for a newer release; `asked` reports every outcome.
|
||||
fn check(&mut self, asked: bool, cx: &mut Context<Self>) {
|
||||
// A download under way or done stays the offer.
|
||||
if matches!(
|
||||
self.offer,
|
||||
Some((_, Phase::Fetching(_) | Phase::Fetched(_)))
|
||||
) {
|
||||
log::info!("update: a release is already on its way");
|
||||
if asked {
|
||||
self.dismissed = false;
|
||||
cx.notify();
|
||||
}
|
||||
return;
|
||||
}
|
||||
let app = self.app;
|
||||
let current =
|
||||
semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("the build's version");
|
||||
// The lookup is a request too: off the window's thread.
|
||||
let checked = cx.background_spawn(async move { manifest(app).and_then(|url| update::check(¤t, &url)) });
|
||||
cx.spawn(async move |this, cx| {
|
||||
let checked = checked.await;
|
||||
this.update(cx, |this, cx| match checked {
|
||||
Ok(Check::Available(_))
|
||||
if matches!(
|
||||
this.offer,
|
||||
Some((_, Phase::Fetching(_) | Phase::Fetched(_)))
|
||||
) => {}
|
||||
Ok(Check::Available(release)) => {
|
||||
(this.offer, this.dismissed) = (Some((release, Phase::Offered)), false);
|
||||
cx.notify();
|
||||
}
|
||||
Ok(Check::Current) if asked => cx.emit(UpdateNews::Latest),
|
||||
// At launch a failure only logs; offline is normal.
|
||||
Err(error) if asked => cx.emit(UpdateNews::Failed(format!("{error:#}"))),
|
||||
Err(error) => log::warn!("update: the launch check failed: {error:#}"),
|
||||
Ok(Check::Current) => log::info!("update: nothing newer at launch"),
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn later(&mut self, cx: &mut Context<Self>) {
|
||||
self.dismissed = true;
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn download(&mut self, cx: &mut Context<Self>) {
|
||||
let Some((release, phase)) = self
|
||||
.offer
|
||||
.as_mut()
|
||||
.filter(|(_, phase)| *phase == Phase::Offered)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
*phase = Phase::Fetching(0.0);
|
||||
cx.notify();
|
||||
let (release, app) = (release.clone(), self.app);
|
||||
let (sender, mut progress) = mpsc::unbounded();
|
||||
let fetching = release.clone();
|
||||
let fetched = cx.background_spawn(async move {
|
||||
let dir = ely_platform::data_dir(app)
|
||||
.map_err(|error| format!("{error:#}"))?
|
||||
.join("updates");
|
||||
if dir.exists() {
|
||||
std::fs::remove_dir_all(&dir).map_err(|error| error.to_string())?;
|
||||
}
|
||||
std::fs::create_dir_all(&dir).map_err(|error| error.to_string())?;
|
||||
let cancel = AtomicBool::new(false);
|
||||
let mut tell = |done: u64, total: Option<u64>| {
|
||||
let part = total
|
||||
.filter(|total| *total > 0)
|
||||
.map_or(0.0, |total| done as f32 / total as f32);
|
||||
sender.unbounded_send(part).ok();
|
||||
};
|
||||
let file = update::download(&fetching, &dir, &cancel, update::IDLE_TIMEOUT, &mut tell)
|
||||
.map_err(|error| format!("{error:#}"))?
|
||||
.ok_or("the download stopped")?;
|
||||
update::verify_file(&file, &fetching.sha256).map_err(|error| format!("{error:#}"))?;
|
||||
Ok::<PathBuf, String>(file)
|
||||
});
|
||||
cx.spawn(async move |this, cx| {
|
||||
while let Some(part) = progress.next().await {
|
||||
this.update(cx, |this, cx| this.fetching(part, cx)).ok();
|
||||
}
|
||||
let fetched = fetched.await;
|
||||
this.update(cx, |this, cx| this.fetched(&release, fetched, cx))
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn fetching(&mut self, part: f32, cx: &mut Context<Self>) {
|
||||
if let Some((_, phase @ Phase::Fetching(_))) = self.offer.as_mut() {
|
||||
*phase = Phase::Fetching(part);
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
/// A finished download counts only for the release it fetched.
|
||||
fn fetched(
|
||||
&mut self,
|
||||
release: &Release,
|
||||
fetched: Result<PathBuf, String>,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let Some((offered, phase)) = self
|
||||
.offer
|
||||
.as_mut()
|
||||
.filter(|(offered, _)| offered == release)
|
||||
else {
|
||||
return log::warn!("update: a download for another release was dropped");
|
||||
};
|
||||
match fetched {
|
||||
Ok(file) => *phase = Phase::Fetched(file),
|
||||
Err(error) => {
|
||||
*phase = Phase::Offered;
|
||||
cx.emit(UpdateNews::Failed(error));
|
||||
}
|
||||
}
|
||||
log::info!("update: {} is {:?}", offered.version, phase);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Quits as usual: unsaved work is asked about first.
|
||||
fn restart(&mut self, cx: &mut Context<Self>) {
|
||||
let Some((_, Phase::Fetched(file))) = self.offer.as_ref() else {
|
||||
return;
|
||||
};
|
||||
self.pending = Some(file.clone());
|
||||
crate::commands::quit(cx);
|
||||
}
|
||||
|
||||
/// At the real quit: the installer waits for this process.
|
||||
fn start_installer(&mut self) {
|
||||
let Some(file) = self.pending.take() else {
|
||||
return;
|
||||
};
|
||||
let started = std::env::current_exe().and_then(|exe| {
|
||||
let mut command = Command::new(exe);
|
||||
command
|
||||
.arg(INSTALL)
|
||||
.arg(&file)
|
||||
.arg(&self.bundle)
|
||||
.arg(std::process::id().to_string());
|
||||
// Its words go to the app's log.
|
||||
let log = ely_platform::logging::path()
|
||||
.map(|path| std::fs::OpenOptions::new().append(true).open(path));
|
||||
let said = match log {
|
||||
Some(file) => Stdio::from(file?),
|
||||
None => Stdio::null(),
|
||||
};
|
||||
command
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(said);
|
||||
#[cfg(unix)]
|
||||
std::os::unix::process::CommandExt::process_group(&mut command, 0);
|
||||
command.spawn()
|
||||
});
|
||||
match started {
|
||||
Ok(_) => log::info!("update: installs after this quit"),
|
||||
Err(error) => log::error!("update: the installer did not start: {error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Updates {
|
||||
/// The bar under the title while a release is offered.
|
||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let this = cx.entity();
|
||||
let Some((release, phase)) = self.offer.as_ref().filter(|_| !self.dismissed) else {
|
||||
return div().into_any_element();
|
||||
};
|
||||
let state = match phase {
|
||||
Phase::Offered => UpdateState::Available,
|
||||
Phase::Fetching(done) => UpdateState::Downloading(*done),
|
||||
Phase::Fetched(_) => UpdateState::Ready,
|
||||
};
|
||||
let (download, restart, later) = (this.clone(), this.clone(), this);
|
||||
let banner = UpdateBanner::new("update-banner", release.version.to_string(), state)
|
||||
.on_download(move |_, cx| download.update(cx, |this, cx| this.download(cx)))
|
||||
.on_later(move |_, cx| later.update(cx, |this, cx| this.later(cx)));
|
||||
match phase {
|
||||
Phase::Fetched(_) => banner
|
||||
.on_restart(move |_, cx| restart.update(cx, |this, cx| this.restart(cx)))
|
||||
.into_any_element(),
|
||||
_ => banner.into_any_element(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn release(version: &str, url: &str) -> Release {
|
||||
Release {
|
||||
version: semver::Version::parse(version).expect("a version"),
|
||||
url: url.into(),
|
||||
sha256: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_check_brings_back_a_dismissed_download(cx: &mut gpui::TestAppContext) {
|
||||
let updates = cx.new(|_| Updates {
|
||||
app: AppId::Tree,
|
||||
bundle: PathBuf::from("/Applications/ElyTree.app"),
|
||||
offer: Some((
|
||||
release("9.0.0", "https://b"),
|
||||
Phase::Fetched(PathBuf::from("B.dmg")),
|
||||
)),
|
||||
dismissed: true,
|
||||
pending: None,
|
||||
note: None,
|
||||
});
|
||||
updates.update(cx, |updates, cx| updates.check(true, cx));
|
||||
updates.read_with(cx, |updates, _| {
|
||||
assert!(!updates.dismissed, "the bar shows again");
|
||||
assert_eq!(
|
||||
updates.offer.as_ref().map(|(_, phase)| phase.clone()),
|
||||
Some(Phase::Fetched(PathBuf::from("B.dmg")))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_download_for_an_older_offer_is_dropped(cx: &mut gpui::TestAppContext) {
|
||||
let updates = cx.new(|_| Updates {
|
||||
app: AppId::Tree,
|
||||
bundle: PathBuf::from("/Applications/ElyTree.app"),
|
||||
offer: Some((release("9.0.0", "https://b"), Phase::Fetching(0.5))),
|
||||
dismissed: false,
|
||||
pending: None,
|
||||
note: None,
|
||||
});
|
||||
updates.update(cx, |updates, cx| {
|
||||
updates.fetched(
|
||||
&release("8.0.0", "https://a"),
|
||||
Ok(PathBuf::from("A.dmg")),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
updates.offer.as_ref().map(|(_, phase)| phase.clone()),
|
||||
Some(Phase::Fetching(0.5))
|
||||
);
|
||||
updates.fetched(
|
||||
&release("9.0.0", "https://b"),
|
||||
Ok(PathBuf::from("B.dmg")),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
updates.offer.as_ref().map(|(_, phase)| phase.clone()),
|
||||
Some(Phase::Fetched(PathBuf::from("B.dmg")))
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user