swift如何延迟mac菜单栏出现动画

gie*_*tal 5 macos delay nsmenu nswindow swift

问题:
在全屏 mac 应用程序中,当您将鼠标指针移到顶部时,mac 菜单栏会立即下拉。我想制作一个将菜单栏的显示延迟几秒钟的应用程序。

这应该是可能的,因为这是 VMWare Fusion 所做的确切行为。在全屏模式下,应用程序让鼠标指针在屏幕顶部停留几秒钟,然后下拉菜单栏。

我将如何着手解决这个问题?

---- 更新 ----
当我想使用 NSMenu.setMenuBarVisible() 时,我可以隐藏和显示菜单栏。
但是,如果我在全屏模式下有多个窗口应用程序,这似乎并不像我认为的那样工作。
它适用于全屏窗口之一,但如果我切换到另一个全屏窗口,我必须在调用 setMenuBarVisible(true) 之前移动鼠标似乎生效。

代码

public class CustomWindowController: NSWindowController, NSWindowDelegate {

    override public func windowDidLoad() {
        window!.delegate = self
    }

    public class func create() -> CustomWindowController {
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateControllerWithIdentifier("CustomWindowController") as! CustomWindowController
        controller.window?.collectionBehavior.insert(.FullScreenDisallowsTiling)

        return controller
    }

    // MARK: NSWindowDelegate protocol

    public func windowDidEnterFullScreen(notification: NSNotification) {
        print("window did enter fullscreen")
        NSMenu.setMenuBarVisible(false)
    }

    public func windowDidBecomeKey(notification: NSNotification) {
        print("window did become key")
        NSMenu.setMenuBarVisible(false)
    }
}

public class CustomWindow: NSWindow {

    // right now I'm using mouse click to trigger the showing of the menu bar
    // but my end goal is to use timer to trigger the showing of the menu bar
    // to delay the menu bar showing by a couple seconds
    public override func mouseUp(theEvent: NSEvent) {
        NSMenu.setMenuBarVisible(true)
    }
}

class CustomViewController: NSViewController {

    private var otherWindowControllers = [CustomWindowController]()

    // button to spawn a second identical window
    @IBAction func spawnWindowButton(sender: AnyObject) {
        let controller = CustomWindowController.create()
        controller.showWindow(self)
        // needed at least 1 strong reference to prevent the window to go away, 
        // so we save the controller to a list
        otherWindowControllers.append(controller) 
    }

    deinit {
        otherWindowControllers.removeAll()
    }
}
Run Code Online (Sandbox Code Playgroud)

复述:

  • 启动应用程序
  • 单击生成按钮以生成第二个窗口
  • 单击绿灯按钮使两个窗口都进入全屏
  • 转到全屏应用程序窗口之一
  • 将鼠标移动到屏幕顶部
  • 菜单栏不应该下拉(预期行为)
  • 左键单击
  • 菜单栏应立即下拉(预期行为)
  • 转到另一个全屏应用程序窗口
  • 将鼠标移动到屏幕顶部
  • 菜单栏不应该下拉(预期行为)
  • 左键单击
  • 菜单栏不下拉
  • 稍微移动鼠标指针
  • 现在它应该下降

知道为什么会这样吗?以及如何修复它以便全屏窗口都具有正确的行为