// // KMHoverView.swift // PDF Reader Pro // // Created by tangchao on 2023/6/7. // import Cocoa enum KMHoverAction: Int { case enter = 1 case move = 2 case exit = 3 } // hover 基类 class KMHoverView: NSView { private var _area: NSTrackingArea? var hoverAction: ((KMHoverView, KMHoverAction)->Void)? deinit { if let area_ = self._area { self.removeTrackingArea(area_) } } override func updateTrackingAreas() { super.updateTrackingAreas() if let area_ = self._area { self.removeTrackingArea(area_) self._area = nil } self._area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self) if let area_ = self._area { self.addTrackingArea(area_) } } override func mouseEntered(with event: NSEvent) { guard let callback = self.hoverAction else { return } callback(self, .enter) } override func mouseExited(with event: NSEvent) { guard let callback = self.hoverAction else { return } callback(self, .exit) } override func mouseMoved(with event: NSEvent) { guard let callback = self.hoverAction else { return } callback(self, .move) } }