-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
windows: Implement single instance #15371
windows: Implement single instance #15371
Conversation
8775e59
to
cffcd5d
Compare
A quick demo of dock menu on windows with changes of this PR: Screen.Recording.2024-08-16.221846.mp4I will open a new PR when this PR is merged. |
Is this PR ready to merge (after resolving conflicts)? Or is it waiting on some more development? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello!
I think we'd prefer for this to be implemented entirely in the zed
crate if possible. GPUI should be laser focused on windows and rendering, and platform specific app architecture is probably outside of it's purview.
This PR requires discussions about its design before it can move forward.
The situation here is quite complex. Since Windows and Linux handle dock menus very differently compared to macOS, and both heavily rely on the single instance feature, separating the Currently, my approach to implementing the dock menu on Windows is as follows:
In this process, the |
let single_instance_event = unsafe { | ||
Owned::new(OpenEventW( | ||
SYNCHRONIZATION_ACCESS_RIGHTS(SYNCHRONIZE.0), | ||
false, | ||
&HSTRING::from(retrieve_app_instance_event_identifier()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When initializing GPUI
, the name provided for the single_instance_event
during creation must match the name passed to CreateEventW
when checking for an existing single instance. If we separate the single instance
functionality from gpui
, then GPUI
would require additional information at startup to create this Event
, for example, WindowsPlatform::new(event_name: &str, shared_memory_name: &str)
.
Furthermore, other apps developed using gpui
that wishes to use the dock menu
functionality would need to implement the single instance logic themselves. This code would likely be identical across different apps, so including the single instance implementation within gpui
helps to avoid unnecessary code duplication and simplifies the process for developers.
pub(crate) fn check_single_instance<F>(f: F) -> bool | ||
where | ||
F: FnOnce(bool) -> bool, | ||
{ | ||
unsafe { | ||
CreateEventW( | ||
None, | ||
false, | ||
false, | ||
&HSTRING::from(retrieve_app_instance_event_identifier()), | ||
) | ||
.expect("Unable to create instance sync event") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name (the HSTRING
here) provided when calling CreateEventW
here must match the name used in the OpenEventW
call mentioned in the previous comment.
I just noticed that the discussion seems to be focused primarily on the |
cffcd5d
to
2901466
Compare
2901466
to
3ea94f4
Compare
Thanks for splitting those up, looks good! |
This PR implements a single instance mechanism using the
CreateEventW
function to create a mutex. If the identifier name begins withLocal
, the single instance applies only to processes under the same user. If the identifier begins withGlobal
, it applies to all users.Additionally, I was thinking that perhaps we should integrate the single instance functionality into
gpui
. I believe applications developed usinggpui
would benefit from this feature. Furthermore, incorporating the single instance implementation intogpui
would facilitate theset_dock_menu
functionality. As I mentioned in #12068, the implementation ofset_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.
Screen.Recording.2024-08-15.234336.mp4
Release Notes: