NSStatusBarButton keep highlighted

后端 未结 9 2381
一个人的身影
一个人的身影 2020-12-16 16:05

As of OS X 10.10 most of NSStatusItem has been deprecated in favour of the button property, which consists of an NSStatusBarButton. It should work like a norma

9条回答
  •  [愿得一人]
    2020-12-16 16:54

    Anton's solution is perfect. Here it is in Swift 3:

    NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { [weak self] event in
        if event.window == self?.statusItem.button?.window {
            // Your action:
            self?.togglePopover(self?.statusItem.button)
            return nil
        }
    
        return event
    }
    

    I added an observer for NSApplicationWillResignActive to close the popover and set the button's isHighlighted to false.

提交回复
热议问题