macOS menubar application: main menu not being displayed

与世无争的帅哥 提交于 2019-12-03 09:41:08

问题


I have a statusbar application, which runs in the menu bar. Therefore I set Application is agent (UIElement) to true in the info.plst. That results in no dock icon and no menu bar for my application.

However, I also have a preference window that the user can open from the statusbar 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 shows up as expected, but the application's main menu bar with File, Edit and so on, does not show up. Only if I click on another app and come back to my app, the menubar is being displayed.

I noticed, that if I change the value in the info.plst to false and use NSApp.setActivationPolicy(.accessory) in applicationDidFinishLaunching(), it has the same result. However, if I call NSApp.setActivationPolicy(.accessory) with a timer a few milliseconds after applicationDidFinishLaunching() is being called, it works and the main menu is being displayed as expected. This however has the side effect that the app icon pops up in the dock for a few seconds (until the timer is being fired), which is not a nice user experience.

Does anyone have an idea what else I could try? What is happening when I switch the active app, that I am not doing in code?

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

Thanks!


回答1:


This is my workaround solution for now:

As I wrote in the question, if I click on another app and come back to my app, the menubar is being displayed. I am simulating this when I try to show the preference window:

    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)
       }
   }

This is not a perfect solution. If I don't find a better solution, I will file a bug.



来源:https://stackoverflow.com/questions/41340071/macos-menubar-application-main-menu-not-being-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!