MacOS menu: main menu does not appear

I have a status bar application that runs on a menu bar. Therefore, I installed Application is agent (UIElement)in truec info.plst. This results in a missing dock icon and menu bar for my application.

However, I also have a preference window that the user can open from the status menu. Here is how I open it:

if (!NSApp.setActivationPolicy(.regular)) {
    print("unable to set regular activation policy")
}
NSApp.activate(ignoringOtherApps: true)
if let window = preferencesWindowController.window {
    window.makeKeyAndOrderFront(nil)
}

The window is displayed as expected, but the main menu bar of the application with the file, editing, etc. not displayed. Only if I click on another application and return to my application, a menu will be displayed.

I noticed that if I changed the value in info.plstto falseand used NSApp.setActivationPolicy(.accessory)in applicationDidFinishLaunching(), it has the same result. However, if I call NSApp.setActivationPolicy(.accessory)with a timer a few milliseconds after the call applicationDidFinishLaunching(), it works, and the main menu is displayed as expected. This, however, has a side effect that the application icon appears on the dock for several seconds (until the timer starts), which is not very nice.

Does anyone have an idea what else I could try? What happens when I switch an active application that I don’t do in code?

I am using Version 8.2.1 (8C1002) for macOS 10.12.2 (16C67)

Thank!

+4
source share
1 answer

:

, , . , :

    NSApp.setActivationPolicy(.regular)
    NSApp.activate(ignoringOtherApps: true)
    window.makeKeyAndOrderFront(nil)

   if (NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.dock").first?.activate(options: []))! {
       let deadlineTime = DispatchTime.now() + .milliseconds(200)
       DispatchQueue.main.asyncAfter(deadline: deadlineTime) {                 
       NSApp.setActivationPolicy(.regular)
            NSApp.activate(ignoringOtherApps: true)
       }
   }

. , .

0

Source: https://habr.com/ru/post/1664968/


All Articles