GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: create `SingleInstance` util
Lars Bergerba875943edac
diff --git a/Cargo.toml b/Cargo.tomlindex b7b034a5..d0f060e8 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -12,6 +12,7 @@ windows = { version = "0.52", features = ["Win32_Foundation","Win32_Graphics_Dwm","Win32_Graphics_Gdi",+ "Win32_Security","Win32_System_Com","Win32_System_LibraryLoader","Win32_System_RemoteDesktop",diff --git a/packages/wm/src/common/platform/mod.rs b/packages/wm/src/common/platform/mod.rsindex e5275a2f..c391fb59 100644--- a/packages/wm/src/common/platform/mod.rs+++ b/packages/wm/src/common/platform/mod.rs@@ -3,9 +3,11 @@ mod event_window;mod native_monitor;mod native_window;mod platform;+mod single_instance;pub use event_listener::*;pub use event_window::*;pub use native_monitor::*;pub use native_window::*;pub use platform::*;+pub use single_instance::*;diff --git a/packages/wm/src/common/platform/single_instance.rs b/packages/wm/src/common/platform/single_instance.rsnew file mode 100644index 00000000..0ce4d67a--- /dev/null+++ b/packages/wm/src/common/platform/single_instance.rs@@ -0,0 +1,52 @@+use anyhow::{Context, Result};+use windows::{+ core::{w, PCWSTR},+ Win32::{+ Foundation::{+ CloseHandle, GetLastError, ERROR_ALREADY_EXISTS, HANDLE,+ },+ System::Threading::{CreateMutexW, ReleaseMutex},+ },+};++pub struct SingleInstance {+ handle: Option<HANDLE>,+}++const APP_GUID: PCWSTR =+ w!("Global\\325d0ed7-7f60-4925-8d1b-aa287b26b218");++impl SingleInstance {+ /// Creates a new instance of `SingleInstance` struct.+ pub fn new() -> Result<Self> {+ let handle = unsafe { CreateMutexW(None, true, APP_GUID) }+ .context("Failed to create single instance mutex.")?;++ if let Err(error) = unsafe { GetLastError() } {+ if error == ERROR_ALREADY_EXISTS.into() {+ // Another instance of the application is already running.+ return Ok(Self { handle: None });+ }+ }++ Ok(Self {+ handle: Some(handle),+ })+ }++ /// Gets whether this is the only active instance of the application.+ pub fn is_single(&self) -> bool {+ self.handle.is_some()+ }+}++impl Drop for SingleInstance {+ fn drop(&mut self) {+ if let Some(handle) = self.handle.take() {+ unsafe {+ let _ = ReleaseMutex(handle);+ let _ = CloseHandle(handle);+ }+ }+ }+}