Skip to content
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

macOS window launch behaviour #5714

Closed
Kees-van-Beilen opened this issue Aug 16, 2022 · 7 comments
Closed

macOS window launch behaviour #5714

Kees-van-Beilen opened this issue Aug 16, 2022 · 7 comments
Labels
A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior O-MacOS Specific to the MacOS (Apple) desktop operating system

Comments

@Kees-van-Beilen
Copy link
Contributor

Kees-van-Beilen commented Aug 16, 2022

Bevy version

0.8.0

[Optional] Relevant system information

  • affects all versions of macos

What you did

Running any bevy proccess that launches a window

What went wrong

The window ignores macOS application rules (i.e. floating above fullscreen apps).
It should follow macOS application rules.

Additional information

This is quite an easy fix please make sure that

//the proccess is registered as an sharedApplication
[NSApplication sharedApplication];
//with regular activation policy
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
//make key window
[theNSWindowRef makeKeyAndOrderFront:nil];
//and importantly actually activating the process as an app
[NSApp activateIgnoringOtherApps:YES];
@Kees-van-Beilen Kees-van-Beilen added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Aug 16, 2022
@bjorn3
Copy link
Contributor

bjorn3 commented Aug 16, 2022

We don't directly interact with the respective api's. Instead we use winit. I think rust-windowing/winit#2051 describes the same issue as you are having.

@bjorn3 bjorn3 added A-Windowing Platform-agnostic interface layer to run your app in O-MacOS Specific to the MacOS (Apple) desktop operating system labels Aug 16, 2022
@Kees-van-Beilen
Copy link
Contributor Author

We don't directly interact with the respective api's. Instead we use winit. I think rust-windowing/winit#2051 describes the same issue as you are having.

Well actually winit already made there stance on it so really what you can do and what I would like to see is delay the logic triggered by app.run() until the ApplicationDidFinishLauncing event fires on mac or by registering the activation policy on the current porcess before making the window. This is really Something that has to be fixed because this invites unexpected app behaviour on macos

@Weibye Weibye removed the S-Needs-Triage This issue needs to be labelled label Aug 18, 2022
@Kees-van-Beilen
Copy link
Contributor Author

Kees-van-Beilen commented Aug 18, 2022

There are two ways to fix this:

  1. by adding:
#[cfg(target_os = "macos")]
unsafe {
    use cocoa::{appkit::*,base::{NO,nil}};
    let nsapp = NSApp();
    NSApplication::setActivationPolicy_(nsapp, NSApplicationActivationPolicyRegular);
}

to bevy_winit's plugin build functions, or

  1. by delaying window creation until the Event::NewEvents(StartCause::Init) has been fired

I'll have to know which one is preferred, than I'll submit a pr with the fix

@mockersf
Copy link
Member

To someone not used to cocoa or apple APIs, the code snippet you posted looks a little like dark magic.

I think waiting for the event could be preferred

@MillionIntegrals
Copy link

This PR (#5883) seems to be fixing the issue and is live on current main branch. Depending on when the next bevy version is meant to be released, it may be worth to bacport it to a bug fix 0.8.2 release, as current 0.8.1 is quite broken for on macOS.

@mockersf
Copy link
Member

0.9 should be in roughly one month.

I'm running on macOS and wouldn't call it quite broken

@maaku
Copy link

maaku commented May 2, 2023

For what it's worth, @Kees-van-Beilen's suggested code seems quite reasonable. Calling Cocoa from Rust does indeed look like scary unsafe {} black magic, but this call is pretty straight forward. It is simply getting the running process' NSApplication instance and sending it the setActivationPolicy:NSApplicationActivationPolicyRegular message, both of which are technically unsafe operations but should never fail on macOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior O-MacOS Specific to the MacOS (Apple) desktop operating system
Projects
None yet
Development

No branches or pull requests

6 participants