NSApp.setActivationPolicy(.regular) does not activate app menu

Switching activation policy from accessory to regular does not activate the app menu in the menu bar. User have switch to another app and back to make app menu to show in the menu bar.

Even the call to activate is successful:
Code Block
assert(NSApp.setActivationPolicy(.regular))

the menu bar shows menu of the previously active app.

Filed feedback: FB7743313

Here is the project reproducing the issue with steps:
https://github.com/sparta-science/MacSwitchActivation

Below is README with step to reproduce to make it searchable in the developer forums:

MacSwitchActivation

Demo of SetActivationPolicy bug on Mac

Expected behaviour

When we click Show Window we expect the app to enter "Active State"

Actual behaviour


When we click Show Window we instead see the app enter "Unexpected State" (See Reproduction Steps)

To workaround the user has to switch to another app temporarily and switch back to our app to get menu showing.

App States


Active State:

  • App name appears in the menu bar

  • App Window is on top

  • App appears in dock

Active State is entered by:
House Icon -> Show Window which should run
Code Block
NSApp.activate(ignoringOtherApps: true)
NSApp.setActivationPolicy(.regular)


Accessory State:

  • App name does not appear in the menu bar

  • No App Window is visible

  • App does not appear in dock

Accessory State is entered by closing the app window which triggers NSApp.setActivationPolicy(.accessory)

Unexpected State:

  • The previous active app name appears in the menu bar

  • App Window is on top

  • App does not appear in dock


Steps to reproduce

  1. Launch the app to begin in "Active State"

  2. Close app window to enter "Accessory State"

  3. Restart the app to begin in "Accessory State"

  4. Attempt to enter "Active State" by selecting Show Window in the menu dropdown

  5. Notice the new "Unexpected State"

  6. Cmd-tab to another app and back brings the menu and name in top left corner

NSApp.setActivationPolicy(.regular) does not activate app menu
 
 
Q