From a11bc277f2eac753d3e3fabb4d9db7c9c29d7f45 Mon Sep 17 00:00:00 2001
From: Nessar
Date: Thu, 19 Nov 2026 22:40:58 +0100
Subject: [PATCH] Restore the existing window on relaunch (#50)
Elect one process per Windows session before starting GPUI or opening the database. Let the existing window handle reopen requests and preserve its current state.
---
crates/app/Cargo.toml | 2 +
crates/app/src/main.rs | 9 ++
crates/app/src/single_instance.rs | 217 ++++++++++++++++++++++++++++++
crates/app/src/tray.rs | 52 ++++---
4 files changed, 265 insertions(+), 15 deletions(-)
create mode 100644 crates/app/src/single_instance.rs
diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml
index b170172..73f0247 100644
--- a/crates/app/Cargo.toml
+++ b/crates/app/Cargo.toml
@@ -14,7 +14,9 @@ gpui-kit = "=0.9.1"
[target.'cfg(windows)'.dependencies]
raw-window-handle = "0.6"
windows = { version = "0.62", features = [
+ "Win32_Security",
"Win32_System_LibraryLoader",
+ "Win32_System_Threading",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
] }
diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs
index 0f3959d..7c9b11c 100644
--- a/crates/app/src/main.rs
+++ b/crates/app/src/main.rs
@@ -4,6 +4,7 @@ mod champion_table;
#[cfg(feature = "profile")]
mod profile;
mod settings;
+mod single_instance;
mod stats_page;
mod text;
mod title_bar;
@@ -261,6 +262,11 @@ fn init_theme(cx: &mut App) {
}
fn main() {
+ let Some((_instance, starting)) =
+ single_instance::launch().expect("Failed to check for a running instance")
+ else {
+ return;
+ };
let app = gpui_kit::application().with_assets(gpui_kit::assets::Assets);
app.run(move |cx| {
gpui_kit::init(cx);
@@ -282,10 +288,12 @@ fn main() {
};
match startup() {
Ok(setup) => gpui_kit::open_window(options, cx, |window, cx| {
+ single_instance::attach(window);
cx.new(|cx| RootView::new(setup, window, cx))
})
.map(drop),
Err((lines, preferences)) => gpui_kit::open_window(options, cx, |window, cx| {
+ single_instance::attach(window);
apply_theme(preferences.theme, window, cx);
window
.observe_window_appearance(move |window, cx| {
@@ -302,6 +310,7 @@ fn main() {
.map(drop),
}
.expect("Failed to open window");
+ drop(starting);
});
}
diff --git a/crates/app/src/single_instance.rs b/crates/app/src/single_instance.rs
new file mode 100644
index 0000000..77102f7
--- /dev/null
+++ b/crates/app/src/single_instance.rs
@@ -0,0 +1,217 @@
+//! Elects one primary process per Windows session and relays launches to its window.
+
+use std::marker::PhantomData;
+use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
+
+use gpui_kit::Window;
+use raw_window_handle::{HasWindowHandle, RawWindowHandle};
+use windows::Win32::{
+ Foundation::{
+ ERROR_TIMEOUT, HANDLE, HWND, LPARAM, LRESULT, WAIT_ABANDONED, WAIT_EVENT, WAIT_OBJECT_0,
+ WAIT_TIMEOUT, WPARAM,
+ },
+ System::Threading::{
+ CreateEventW, CreateMutexW, EVENT_MODIFY_STATE, INFINITE, OpenEventW, ReleaseMutex,
+ SetEvent, WaitForMultipleObjects, WaitForSingleObject,
+ },
+ UI::{
+ Shell::{DefSubclassProc, RemoveWindowSubclass, SetWindowSubclass},
+ WindowsAndMessaging::*,
+ },
+};
+use windows::core::{BOOL, Error, HSTRING, PCWSTR, Result, w};
+
+const STARTING: PCWSTR = w!("Local\\nessar.arena-tracker.starting");
+const INSTANCE: PCWSTR = w!("Local\\nessar.arena-tracker.instance");
+const WINDOW_MARKER: PCWSTR = w!("nessar.arena-tracker.main-window");
+const RELAY_TIMEOUT_MS: u32 = 5_000;
+const REOPEN: u32 = WM_APP + 2;
+// The acknowledgement event is the second object in the wait list.
+const REOPENED: WAIT_EVENT = WAIT_EVENT(WAIT_OBJECT_0.0 + 1);
+
+// Windows requires a mutex to be released on the thread that acquired it.
+pub struct SessionLock(OwnedHandle, PhantomData<*mut ()>);
+
+impl SessionLock {
+ fn from_wait(handle: OwnedHandle, status: WAIT_EVENT) -> Result {
+ match status {
+ WAIT_OBJECT_0 | WAIT_ABANDONED => Ok(Self(handle, PhantomData)),
+ WAIT_TIMEOUT => Err(Error::from_hresult(ERROR_TIMEOUT.to_hresult())),
+ _ => Err(Error::from_thread()),
+ }
+ }
+}
+
+impl Drop for SessionLock {
+ fn drop(&mut self) {
+ let _ = unsafe { ReleaseMutex(HANDLE(self.0.as_raw_handle())) };
+ }
+}
+
+pub fn launch() -> Result