123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- //
- // KMCoverButton.swift
- // PDF Master
- //
- // 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: ((KMCoverButton, KMCoverAction) -> Void)?
- 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)
- }
- }
- }
|