How activate window correctly using activationPolicy

I have the following code:

+ (BOOL)activateWindow:(NSWindow*)window
{
    if (NSApp.activationPolicy != NSApplicationActivationPolicyRegular)
        [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

    if (window)
    {
        [NSApp activate];
        //if (window.isMiniaturized) [window deminiaturize:nil];
        [window makeKeyAndOrderFront:nil];
    }
    return YES;
}

+ (BOOL)hideWindowFromDock:(NSWindow*)window
{
    if (NSApp.activationPolicy != NSApplicationActivationPolicyProhibited)
        [NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];

    window.isVisible = NO;
    return YES;
}

I hide app main window by setting NSApplicationActivationPolicyProhibited if it is minimized or being closed.

The code worked most of the time. But since upgrading to Sonoma, sometimes it won't correctly activate main window. The window icon reappears in the dock, but the window won't show up. I have to click on the window icon again to let it 'order front'.

Sometimes, I observe a very weird behavior of the window being activated. It 'order front' and then disappears and re-appears.

Sonoma made some changes to the app activation process that are designed to prevent one app from stealing focus from each other. The article "Passing control from one app to another with cooperative activation" has a full description of what's changed.

However, what you're describing here:

The code worked most of the time. But since upgrading to Sonoma, sometimes it won't correctly activate main window. The window icon reappears in the dock, but the window won't show up. I have to click on the window icon again to let it 'order front'.

...is basically what I'd expect from an incomplete activation. Your app became visible from the dock perspective (so your dock window became visible) but was not allowed to activate (so you couldn't foreground and the docked window did not expand).

-Kevin Elliott
DTS Engineer, CoreOS/Hardware

How activate window correctly using activationPolicy
 
 
Q