KMButton.swift 907 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // KMButton.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/9.
  6. //
  7. import Cocoa
  8. @objcMembers class KMButton: NSButton {
  9. @objc var mouseMoveCallback: ((Bool) -> Void)?
  10. override func viewDidMoveToWindow() {
  11. super.viewDidMoveToWindow()
  12. if window != nil {
  13. addTrackingRect(bounds, owner: self, userData: nil, assumeInside: false)
  14. }
  15. }
  16. override func setFrameSize(_ newSize: NSSize) {
  17. super.setFrameSize(newSize)
  18. addTrackingRect(NSMakeRect(0, 0, newSize.width, newSize.height), owner: self, userData: nil, assumeInside: false)
  19. }
  20. override func mouseEntered(with theEvent: NSEvent) {
  21. if let callback = mouseMoveCallback {
  22. callback(true)
  23. }
  24. }
  25. override func mouseExited(with theEvent: NSEvent) {
  26. if let callback = mouseMoveCallback {
  27. callback(false)
  28. }
  29. }
  30. }