Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): clear the hand-off marker when Windows ends the session

Cursor Agent6f95827ad318
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index fda2c4de5..a29d75e3b 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -528,11 +528,17 @@ fn main() {
.detach();
#[cfg(target_os = "windows")]
cx.spawn(async move |_| {
- if let Some(native) = native_main
- && let Err(error) = platform::install_main_window_frame_policy(&native)
- {
+ let Some(native) = native_main else {
+ return;
+ };
+ if let Err(error) = platform::install_main_window_frame_policy(&native) {
tracing::warn!(%error, "could not install Main window frame policy");
}
+ if let Err(error) =
+ platform::install_session_end_handler(&native, crate::store::clear_handoff_marker)
+ {
+ tracing::warn!(%error, "could not install the Windows session end handler");
+ }
})
.detach();
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
diff --git a/apps/desktop-gpui/src/platform/windows.rs b/apps/desktop-gpui/src/platform/windows.rs
index 207465cc8..a1733ed4f 100644
--- a/apps/desktop-gpui/src/platform/windows.rs
+++ b/apps/desktop-gpui/src/platform/windows.rs
@@ -17,6 +17,7 @@ use super::{ForcedAppearance, MaterialKind, PanelBehavior};
mod capture_exclusion;
mod hidden_frame;
+mod session_end;
mod url_scheme;
#[derive(Clone, Copy)]
@@ -69,6 +70,13 @@ pub fn install_main_window_frame_policy(native: &NativeWindow) -> std::io::Resul
hidden_frame::install(native.hwnd())
}
+pub fn install_session_end_handler(
+ native: &NativeWindow,
+ on_session_end: fn(),
+) -> std::io::Result<()> {
+ session_end::install(native.hwnd(), on_session_end)
+}
+
#[link(name = "dwmapi")]
unsafe extern "system" {
fn DwmSetWindowAttribute(hwnd: HWND, attribute: u32, value: *const c_void, size: u32) -> i32;
diff --git a/apps/desktop-gpui/src/platform/windows/session_end.rs b/apps/desktop-gpui/src/platform/windows/session_end.rs
new file mode 100644
index 000000000..630266304
--- /dev/null
+++ b/apps/desktop-gpui/src/platform/windows/session_end.rs
@@ -0,0 +1,92 @@
+use std::io;
+
+use windows_sys::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM};
+use windows_sys::Win32::UI::Shell::{DefSubclassProc, RemoveWindowSubclass, SetWindowSubclass};
+use windows_sys::Win32::UI::WindowsAndMessaging::{WM_ENDSESSION, WM_NCDESTROY};
+
+const SUBCLASS_ID: usize = 0x4341_5053;
+
+// gpui's Windows platform ignores WM_ENDSESSION, so a shutdown, restart or sign-off
+// terminates the process without running `on_app_quit`. The Tauri app then reads the
+// leftover hand-off marker as a crash and turns the native app off.
+pub fn install(hwnd: HWND, on_session_end: fn()) -> io::Result<()> {
+ let callback = Box::into_raw(Box::new(on_session_end));
+ if unsafe { SetWindowSubclass(hwnd, Some(subclass), SUBCLASS_ID, callback as usize) } == 0 {
+ drop(unsafe { Box::from_raw(callback) });
+ return Err(io::Error::other(
+ "could not install the session end subclass",
+ ));
+ }
+ Ok(())
+}
+
+unsafe extern "system" fn subclass(
+ hwnd: HWND,
+ message: u32,
+ wparam: WPARAM,
+ lparam: LPARAM,
+ _subclass_id: usize,
+ reference_data: usize,
+) -> LRESULT {
+ let callback = reference_data as *mut fn();
+ if message == WM_NCDESTROY {
+ unsafe { RemoveWindowSubclass(hwnd, Some(subclass), SUBCLASS_ID) };
+ drop(unsafe { Box::from_raw(callback) });
+ } else if message == WM_ENDSESSION && wparam != 0 {
+ unsafe { (*callback)() };
+ }
+ unsafe { DefSubclassProc(hwnd, message, wparam, lparam) }
+}
+
+#[cfg(test)]
+mod tests {
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
+ use windows_sys::Win32::UI::WindowsAndMessaging::{
+ CreateWindowExW, DestroyWindow, ENDSESSION_CLOSEAPP, ENDSESSION_LOGOFF, HWND_MESSAGE,
+ SendMessageW, WM_QUERYENDSESSION,
+ };
+
+ use super::*;
+
+ static ENDED: AtomicUsize = AtomicUsize::new(0);
+
+ fn record_session_end() {
+ ENDED.fetch_add(1, Ordering::SeqCst);
+ }
+
+ #[test]
+ fn runs_the_handler_only_when_the_session_really_ends() {
+ let class: Vec<u16> = "STATIC\0".encode_utf16().collect();
+ let hwnd = unsafe {
+ CreateWindowExW(
+ 0,
+ class.as_ptr(),
+ std::ptr::null(),
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ HWND_MESSAGE,
+ std::ptr::null_mut(),
+ std::ptr::null_mut(),
+ std::ptr::null(),
+ )
+ };
+ assert!(!hwnd.is_null());
+ install(hwnd, record_session_end).unwrap();
+
+ assert_ne!(
+ unsafe { SendMessageW(hwnd, WM_QUERYENDSESSION, 0, ENDSESSION_CLOSEAPP as LPARAM) },
+ 0
+ );
+ unsafe { SendMessageW(hwnd, WM_ENDSESSION, 0, 0) };
+ assert_eq!(ENDED.load(Ordering::SeqCst), 0);
+
+ unsafe { SendMessageW(hwnd, WM_ENDSESSION, 1, ENDSESSION_LOGOFF as LPARAM) };
+ assert_eq!(ENDED.load(Ordering::SeqCst), 1);
+
+ assert_ne!(unsafe { DestroyWindow(hwnd) }, 0);
+ }
+}