forked from zed-industries/zed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
windows: Implement single instance (zed-industries#15371)
This PR implements a single instance mechanism using the `CreateEventW` function to create a mutex. If the identifier name begins with `Local`, the single instance applies only to processes under the same user. If the identifier begins with `Global`, it applies to all users. Additionally, I was thinking that perhaps we should integrate the single instance functionality into `gpui`. I believe applications developed using `gpui` would benefit from this feature. Furthermore, incorporating the single instance implementation into `gpui` would facilitate the `set_dock_menu` functionality. As I mentioned in zed-industries#12068, the implementation of `set_dock_menu` on Windows depends on the single instance feature. When a user clicks the "dock menu", Windows will open a new application instance. To achieve behavior similar to macOS, we need to prevent the new instance from launching and instead pass the parameters to the existing instance. Any advice and suggestions are welcome. https://github.com/user-attachments/assets/c46f7e92-4411-4fa9-830e-383798a9dd93 Release Notes: - N/A
- Loading branch information
1 parent
1eec601
commit 3c53832
Showing
5 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use release_channel::ReleaseChannel; | ||
use windows::{ | ||
core::HSTRING, | ||
Win32::{ | ||
Foundation::{GetLastError, ERROR_ALREADY_EXISTS}, | ||
System::Threading::CreateEventW, | ||
}, | ||
}; | ||
|
||
fn retrieve_app_instance_event_identifier() -> &'static str { | ||
match *release_channel::RELEASE_CHANNEL { | ||
ReleaseChannel::Dev => "Local\\Zed-Editor-Dev-Instance-Event", | ||
ReleaseChannel::Nightly => "Local\\Zed-Editor-Nightly-Instance-Event", | ||
ReleaseChannel::Preview => "Local\\Zed-Editor-Preview-Instance-Event", | ||
ReleaseChannel::Stable => "Local\\Zed-Editor-Stable-Instance-Event", | ||
} | ||
} | ||
|
||
pub fn check_single_instance() -> bool { | ||
if *db::ZED_STATELESS || *release_channel::RELEASE_CHANNEL == ReleaseChannel::Dev { | ||
return true; | ||
} | ||
|
||
check_single_instance_event() | ||
} | ||
|
||
fn check_single_instance_event() -> bool { | ||
unsafe { | ||
CreateEventW( | ||
None, | ||
false, | ||
false, | ||
&HSTRING::from(retrieve_app_instance_event_identifier()), | ||
) | ||
.expect("Unable to create instance sync event") | ||
}; | ||
let last_err = unsafe { GetLastError() }; | ||
last_err != ERROR_ALREADY_EXISTS | ||
} |