如何让我的应用程序表现得像我在运行时使用 Swift 更改 Application is agent(UIElement)?

How can I make my app behave like I'm changing Application is agent(UIElement) at runtime using Swift?

我正在编写一个 Mac 应用程序,它是菜单栏右侧的 NSPopoverApplication is agent(UIElement) 设置为 YES)。我允许用户通过单击并向下拖动来分离弹出窗口,这会将应用置于 window 内。这工作正常;但是,当应用程序被拖出菜单栏并变成 window 时,我希望我的应用程序图标出现在停靠栏中,并在菜单左侧显示特定于应用程序的菜单栏,就好像 Application is agent(UIElement) 设置为 NO。相反,当 window 关闭并且应用程序返回到菜单栏中的弹出窗口时,我希望我的应用程序图标从停靠栏中消失并且不再在菜单左侧显示特定于应用程序的菜单栏(Application is agent(UIElement) 设置回 YES)。

根据 this question,我了解到无法在运行时更改 Application is agent(UIElement)。然而,给出的答案是在Objective-C中,最后一个函数似乎从OS X 10.9开始贬值了。如何使我的应用程序具有与在运行时使用 Swift 更改 Application is agent(UIElement) 相同的行为?

我知道显示应用 icon/menu 栏菜单会在 windowDidBecomeMain 中发生,隐藏应用 icon/menu 栏菜单会在 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)
        }
    }
}

此外,如果您在切换到常规后无法显示菜单和停靠图标,则可能需要执行如下操作。对于旧的 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];