macOS菜单栏应用程序:主菜单未显示 [英] macOS menubar application: main menu not being displayed

查看:134
本文介绍了macOS菜单栏应用程序:主菜单未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个状态栏应用程序,该应用程序在菜单栏中运行.因此,我在info.plst中将Application is agent (UIElement)设置为true.这将导致我的应用程序没有停靠图标,也没有菜单栏.

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

窗口将按预期显示,但是没有显示应用程序的主菜单栏,其中包含 File Edit 等.仅当我单击另一个应用程序并返回到我的应用程序时,菜单栏才会显示.

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.

我注意到,如果将info.plst中的值更改为false并在applicationDidFinishLaunching()中使用NSApp.setActivationPolicy(.accessory),则结果相同.但是,如果在调用applicationDidFinishLaunching()几毫秒后用计时器调用NSApp.setActivationPolicy(.accessory),它会起作用,并且按预期显示主菜单.但是,这样做的副作用是,应用程序图标会在扩展坞中弹出几秒钟(直到触发计时器为止),这不是很好的用户体验.

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?

我正在macOS 10.12.2(16C67)上使用版本8.2.1(8C1002)

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

谢谢!

推荐答案

这是我目前的解决方法解决方案:

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.

这篇关于macOS菜单栏应用程序:主菜单未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆