SDL icon indicating copy to clipboard operation
SDL copied to clipboard

(macOS) Opening a window in windowed mode unhides the dock sometimes

Open dortamiguel opened this issue 1 year ago • 4 comments

Since SDL2 and in the latest SDL3 build I keep seeing this happening, when opening a window in windowed mode the dock will unhide sometimes.

This are the flags I'm using SDL_WINDOW_VULKAN, SDL_WINDOW_HIGH_PIXEL_DENSITY, SDL_WINDOW_RESIZABLE, SDL_WINDOW_FULLSCREEN

Is something I can't always reproduce, it will happen randomly a few times during the day.

Where I could start looking in the code to fix this? Is this something that might be happening to someone else?

dortamiguel avatar Jul 22 '24 02:07 dortamiguel

I'm seeing this issue too. It happens for me consistently if I'm moving the mouse while the app launches but doesn't happen if the mouse is still.

EDIT: Seems to be caused by this piece of code focusing the dock: https://github.com/libsdl-org/SDL/blob/8c733d1f7bdb03b2082028349e97137c0bdea13b/src/video/cocoa/SDL_cocoaevents.m#L322-L330

I was able to work around the issue by setting SDL_MAC_BACKGROUND_APP to 1 after SDL_Init which is super hacky but seems to avoid running that specific piece of code while also not doing the normal background app stuff that's supposed to happen if you correctly enable SDL_MAC_BACKGROUND_APP before calling SDL_Init.

Zorbn avatar Apr 27 '25 02:04 Zorbn

I also see the dock appear when launching a .app-bundled game while moving the mouse under macOS 15.5 with SDL 3 on main at 36936cbf3.

From https://github.com/libsdl-org/SDL/issues/1913 and https://github.com/libsdl-org/SDL/commit/fb071a4cd9a06cf1c9d5ed40aef7c5bf4e8430ce, it looks like dock activation was only added to make the menu appear in macOS games that don't run inside an .app bundle. The first link also notes that DOTA just switched to shipping an app bundle to work around this.

I compiled SDL without this block from SDL/src/video/cocoa/SDL_cocoaevents.m

    /* The menu bar of SDL apps which don't have the typical .app bundle
     * structure fails to work the first time a window is created (until it's
     * de-focused and re-focused), if this call is in Cocoa_RegisterApp instead
     * of here. https://bugzilla.libsdl.org/show_bug.cgi?id=3051
     */
    if (!SDL_GetHintBoolean(SDL_HINT_MAC_BACKGROUND_APP, false)) {
        // Get more aggressive for Catalina: activate the Dock first so we definitely reset all activation state.
        for (NSRunningApplication *i in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) {
            [i activateWithOptions:NSApplicationActivateIgnoringOtherApps];
            break;
        }
        SDL_Delay(300); // !!! FIXME: this isn't right.
        [NSApp activateIgnoringOtherApps:YES];
    }

And ran my game without bundling it in a .app. I see the menu bar under macOS 15.5 (as well as when switching away and back to the app):

Image

When bundling as an app, I also see the menu bar instantly (and when switching away and back), and the dock does not appear when moving the mouse on launch. In both cases the game also launches faster due to the removed 300ms delay.

Image

So it looks like the hack/workaround to activate the dock might not be needed any more? Perhaps we could just remove it? Happy to submit a PR if that approach is ok.

nickcernis avatar May 28 '25 21:05 nickcernis

Let's not risk breaking things at the last moment for 3.2.16. I'm bumping this to 3.2.18, but we can put a patch in revision control right now, as long as we don't cherry-pick it to the release-3.2.x branch until 3.2.16 is out the door.

I'd say this code likely needs a check to still run on older macOS releases, though. If someone is able to decide where this started working without it, that'd be helpful, otherwise, let's just call it unneeded on anything >= 15.0.0 and tweak the specific version if people complain.

icculus avatar Jun 01 '25 07:06 icculus

If someone is able to decide where this started working without it, that'd be helpful,

It started working without it in macOS 14.0 based on my tests with UTM and old images from https://ipsw.me/.

I can repro no focus on launch ('menu activation failure') under 13.6 but not 14.0.

  • 13.6 test run (only gains focus on launch with dylib using dock hack): https://share.cleanshot.com/rMc9XYWD
  • 14.0 test run (gains focus on launch with or without dock hack): https://share.cleanshot.com/h8KlQjjT

nickcernis avatar Jun 01 '25 19:06 nickcernis

Okay, I've pushed a fix for this that makes it go from completely reproducable on macOS 15.4.1 to 100% fixed, but I don't have a convenient older macOS to verify it doesn't explode there.

I'm 90% confident we're good to go here, but I'd love it if someone could verify the latest in revision control against macOS 13.x.

icculus avatar Jun 26 '25 18:06 icculus

(This also needs to be cherry-picked to release-3.2.x and SDL2, but confirmation first would be really helpful.)

icculus avatar Jun 26 '25 18:06 icculus

Confirming that 279dabf:

  1. Prevents hidden docks appearing under macOS 15.5 if you move the mouse during launch.
  2. Still makes the app focus on launch under macOS 13.6 if launched outside a packaged .app.

Thanks for your work!

Readers should be aware that for both (1) and (2) to be true if you're compiling SDL on macOS >= 14 you need to set MACOSX_DEPLOYMENT_TARGET to the lowest version of macOS you want to support.

When I first compiled SDL under macOS 15.5 checked out at 279dabf, the app did not focus on launch under macOS 13.6. When I recompiled with export MACOSX_DEPLOYMENT_TARGET=13.6 && mkdir build && cd build && cmake .. && make, that version works on 15.5 and 13.6. For more see:

  • https://github.com/libsdl-org/SDL/issues/11332
  • https://travis-ci.community/t/objective-c-api-availability-checks-give-wrong-results/6144/3

nickcernis avatar Jun 27 '25 21:06 nickcernis

Cherry-picked to release-3.2.x in 17656d051b750b945fdd8b4608f9524c7879bc1b, and merged into SDL2 in b2c3e6fadec4be4f16febf1b5046fe0cc950addb. We're good to go here!

icculus avatar Jul 10 '25 19:07 icculus