windows: Fix auto update failure when launching from the cli (#34303)

Release Notes:

- N/A

---------

Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
张小白 2025-08-13 08:04:30 +08:00 committed by GitHub
parent 658d56bd72
commit 32975c4208
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 250 additions and 131 deletions

View file

@ -277,6 +277,8 @@ pub struct App {
pub(crate) release_listeners: SubscriberSet<EntityId, ReleaseListener>,
pub(crate) global_observers: SubscriberSet<TypeId, Handler>,
pub(crate) quit_observers: SubscriberSet<(), QuitHandler>,
pub(crate) restart_observers: SubscriberSet<(), Handler>,
pub(crate) restart_path: Option<PathBuf>,
pub(crate) window_closed_observers: SubscriberSet<(), WindowClosedHandler>,
pub(crate) layout_id_buffer: Vec<LayoutId>, // We recycle this memory across layout requests.
pub(crate) propagate_event: bool,
@ -349,6 +351,8 @@ impl App {
keyboard_layout_observers: SubscriberSet::new(),
global_observers: SubscriberSet::new(),
quit_observers: SubscriberSet::new(),
restart_observers: SubscriberSet::new(),
restart_path: None,
window_closed_observers: SubscriberSet::new(),
layout_id_buffer: Default::default(),
propagate_event: true,
@ -832,8 +836,16 @@ impl App {
}
/// Restarts the application.
pub fn restart(&self, binary_path: Option<PathBuf>) {
self.platform.restart(binary_path)
pub fn restart(&mut self) {
self.restart_observers
.clone()
.retain(&(), |observer| observer(self));
self.platform.restart(self.restart_path.take())
}
/// Sets the path to use when restarting the application.
pub fn set_restart_path(&mut self, path: PathBuf) {
self.restart_path = Some(path);
}
/// Returns the HTTP client for the application.
@ -1466,6 +1478,21 @@ impl App {
subscription
}
/// Register a callback to be invoked when the application is about to restart.
///
/// These callbacks are called before any `on_app_quit` callbacks.
pub fn on_app_restart(&self, mut on_restart: impl 'static + FnMut(&mut App)) -> Subscription {
let (subscription, activate) = self.restart_observers.insert(
(),
Box::new(move |cx| {
on_restart(cx);
true
}),
);
activate();
subscription
}
/// Register a callback to be invoked when a window is closed
/// The window is no longer accessible at the point this callback is invoked.
pub fn on_window_closed(&self, mut on_closed: impl FnMut(&mut App) + 'static) -> Subscription {

View file

@ -164,6 +164,20 @@ impl<'a, T: 'static> Context<'a, T> {
subscription
}
/// Register a callback to be invoked when the application is about to restart.
pub fn on_app_restart(
&self,
mut on_restart: impl FnMut(&mut T, &mut App) + 'static,
) -> Subscription
where
T: 'static,
{
let handle = self.weak_entity();
self.app.on_app_restart(move |cx| {
handle.update(cx, |entity, cx| on_restart(entity, cx)).ok();
})
}
/// Arrange for the given function to be invoked whenever the application is quit.
/// The future returned from this callback will be polled for up to [crate::SHUTDOWN_TIMEOUT] until the app fully quits.
pub fn on_app_quit<Fut>(
@ -175,20 +189,15 @@ impl<'a, T: 'static> Context<'a, T> {
T: 'static,
{
let handle = self.weak_entity();
let (subscription, activate) = self.app.quit_observers.insert(
(),
Box::new(move |cx| {
let future = handle.update(cx, |entity, cx| on_quit(entity, cx)).ok();
async move {
if let Some(future) = future {
future.await;
}
self.app.on_app_quit(move |cx| {
let future = handle.update(cx, |entity, cx| on_quit(entity, cx)).ok();
async move {
if let Some(future) = future {
future.await;
}
.boxed_local()
}),
);
activate();
subscription
}
.boxed_local()
})
}
/// Tell GPUI that this entity has changed and observers of it should be notified.

View file

@ -370,9 +370,9 @@ impl Platform for WindowsPlatform {
.detach();
}
fn restart(&self, _: Option<PathBuf>) {
fn restart(&self, binary_path: Option<PathBuf>) {
let pid = std::process::id();
let Some(app_path) = self.app_path().log_err() else {
let Some(app_path) = binary_path.or(self.app_path().log_err()) else {
return;
};
let script = format!(