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

标签 swift macos cocoa nspopover lsuielement

我正在编写一个 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/

相关文章:

arrays - swift 3 : Remove specific string from array without knowing the indexPath?

swift - 通过达到分数激事件作

arrays - 如何打印变量的名称而不是 Swift 中的值?

ios - 在 CollectionView 上滑动删除

c++ - 全屏模式下未触发 KeyRelease 事件

macos - 为什么在发送 ACK 之前等待 SIFS 时间?

c - 尝试在 Mac OSX 的 OpenGL 中使用鼠标外观相机

cocoa - 设置 NSWindow 在显示前的位置

macos - Mac 操作系统雪豹

iphone - Objective-C++ 支持多少 C++