// // KMCoverButton.swift // PDF Reader Pro // // Created by wanjun on 2023/10/7. // import Cocoa @objc enum KMCoverAction: Int { case enter = 0 case move case exit } @objcMembers class KMCoverButton: NSButton { var coverAction: ((_ button: KMCoverButton, _ actionType: KMCoverAction) -> Void)? private var area: NSTrackingArea? deinit { if let trackingArea = area { self.removeTrackingArea(trackingArea) } } override func updateTrackingAreas() { super.updateTrackingAreas() if let existingArea = self.area { self.removeTrackingArea(existingArea) self.area = nil } let opts: NSTrackingArea.Options = [.mouseEnteredAndExited, .mouseMoved, .activeAlways] self.area = NSTrackingArea(rect: bounds, options: opts, owner: self, userInfo: nil) if let trackingArea = self.area { self.addTrackingArea(trackingArea) } } override func mouseEntered(with event: NSEvent) { if let coverAction = coverAction { coverAction(self, .enter) } } override func mouseExited(with event: NSEvent) { if let coverAction = coverAction { coverAction(self, .exit) } } override func mouseMoved(with event: NSEvent) { if let coverAction = coverAction { coverAction(self, .move) } } }