我正在编写一个 Mac 应用程序,它是菜单栏右侧的 NSPopover
(Application is agent(UIElement)
设置为 YES
)。我允许用户通过单击并向下拖动来分离弹出窗口,这会将应用程序置于窗口内。这工作正常;但是,当应用程序被拖出菜单栏并进入窗口时,我希望我的应用程序的图标出现在停靠栏中,并在菜单栏的左侧显示特定于应用程序的菜单,就好像Application is agent(UIElement)
设置为 NO
。相反,当窗口关闭并且应用程序返回到菜单栏中的弹出窗口时,我希望我的应用程序图标从停靠栏中消失并且不再在菜单栏左侧显示特定于应用程序的菜单(Application is agent(UIElement)
设置回 YES
)。
来自 this question ,我知道在运行时更改 Application is agent(UIElement)
是不可能的。然而,给出的答案是在 Objective-C 中,最后一个函数似乎从 OS X 10.9 开始贬值。如何使我的应用程序具有与使用 Swift 在运行时更改 Application is agent(UIElement)
相同的行为?
我知道显示应用程序图标/菜单栏菜单会在 windowDidBecomeMain
中发生,隐藏应用程序图标/菜单栏菜单会在 windowWillClose
中发生。
谢谢。
最佳答案
经过反复试验,我终于弄明白了。您不使用 Application is agent(UIElement)
,而是使用 NSApp.setActivationPolicy
。现在这是我的代码。在 App Delegate 中:
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)
}
}
}
关于swift - 如何让我的应用程序表现得像我在运行时使用 Swift 更改 Application is agent(UIElement)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51036106/