我正在编码一个Mac应用程序,它是菜单栏右侧的NSPopover
(Application is agent(UIElement)
设置为YES
)。我允许用户通过单击并向下拖动弹出窗口来分离弹出窗口,这会将应用程序置于窗口内。这很好。但是,当将应用程序从菜单栏拖到窗口中时,我希望我的应用程序图标出现在停靠栏中,并且希望在菜单栏的左侧显示特定于应用程序的菜单,就像Application is agent(UIElement)
设置为NO
。相反,当关闭窗口并将应用程序返回到菜单栏中的弹出框时,我希望我的应用程序图标从停靠栏中消失,并且不再在菜单栏的左侧显示应用程序专用菜单({{ 1}}重新设置为Application is agent(UIElement)
)。
根据this question,我知道无法在运行时更改YES
。但是,给出的答案是在Objective-C中进行的,自OS X 10.9起,最后一个功能似乎已被淘汰。如何使我的应用具有与在运行时使用Swift更改Application is agent(UIElement)
相同的行为?
我知道在Application is agent(UIElement)
中显示应用程序图标/菜单栏菜单,而在windowDidBecomeMain
中隐藏应用程序图标/菜单栏菜单。
谢谢。
答案 0 :(得分:3)
花费了大量的尝试和错误,但是我终于弄明白了。您可以使用Application is agent(UIElement)
来代替NSApp.setActivationPolicy
。现在这是我的代码。在应用程序委托中:
var isWindow = false
class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
let popover = NSPopover()
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
NSApp.setActivationPolicy(.accessory)
if let button = statusItem.button {
button.image = NSImage(named: "StatusBarImage")
button.action = #selector(togglePopover(_:))
}
popover.contentViewController = MainViewController.loadController()
popover.delegate = self
popover.animates = false
popover.behavior = .transient
}
@objc func togglePopover(_ sender: Any?) {
if popover.isShown == true {
popover.performClose(sender)
} else if detachedWindowController.window!.isVisible {
detachedWindowController.window?.setIsVisible(false)
isWindow = true
} else if isWindow == true {
detachedWindowController.window?.setIsVisible(true)
} else {
if let button = statusItem.button {
popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
}
}
}
lazy var detachedWindowController: DetachedWindowController = {
let detachedWindowController = DetachedWindowController(windowNibName: "DetachedWindowController")
detachedWindowController.contentViewController = MainViewController.loadController()
return detachedWindowController
}()
func popoverShouldDetach(_ popover: NSPopover) -> Bool {
return true
}
func detachableWindow(for popover: NSPopover) -> NSWindow? {
return detachedWindowController.window
}
}
在DetachedWindowController
中:
class DetachedWindowController: NSWindowController, NSWindowDelegate {
@IBOutlet var detachedWindow: NSWindow!
override func windowDidLoad() {
super.windowDidLoad()
detachedWindow.delegate = self
}
func windowWillClose(_ notification: Notification) {
isWindow = false
NSApp.setActivationPolicy(.accessory)
}
func windowDidBecomeMain(_ notification: Notification) {
if NSApp.activationPolicy() == .accessory {
NSApp.setActivationPolicy(.regular)
}
}
}
答案 1 :(得分:1)
此外,如果在切换到常规菜单后无法显示菜单和停靠图标,则可能必须执行以下操作。对于较旧的Mac OS版本,这绝对是必需的。
// needed to activate menu
NSArray *dockAppA = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.loginwindow"];
NSRunningApplication *dock = [dockAppA firstObject];
[dock activateWithOptions:NSApplicationActivateIgnoringOtherApps];
NSArray *dockAppB = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"myAppBundleIdentifier"];
NSRunningApplication *myApp = [dockAppB firstObject];
[myApp activateWithOptions:NSApplicationActivateIgnoringOtherApps];