KMImageToolTipWindow.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // KMImageToolTipWindow.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/23.
  6. //
  7. import Cocoa
  8. class KMImageToolTipWindow: KMAnimatedBorderlessWindow {
  9. var context: KMImageToolTipContext?
  10. var point: NSPoint = .zero
  11. private let WINDOW_OFFSET = 18.0
  12. private let ALPHA_VALUE = 0.95
  13. private let CRITICAL_ALPHA_VALUE = 0.9
  14. private let AUTO_HIDE_TIME_INTERVAL = 10.0
  15. private let DEFAULT_SHOW_DELAY = 1.5
  16. private let ALT_SHOW_DELAY = 0.2
  17. static let shared = KMImageToolTipWindow()
  18. deinit {
  19. KMPrint("KMImageToolTipWindow deinit.")
  20. NotificationCenter.default.removeObserver(self)
  21. }
  22. convenience init() {
  23. self.init(contentRect: .zero)
  24. self.hidesOnDeactivate = false
  25. self.ignoresMouseEvents = true
  26. self.isOpaque = true
  27. self.backgroundColor = .white
  28. self.hasShadow = true
  29. self.level = NSWindow.Level(104)
  30. self.defaultAlphaValue = ALPHA_VALUE
  31. self.autoHideTimeInterval = AUTO_HIDE_TIME_INTERVAL
  32. self.context = nil
  33. self.point = .zero
  34. NotificationCenter.default.addObserver(self, selector: #selector(orderOut), name: NSApplication.willResignActiveNotification, object: NSApp)
  35. }
  36. override func orderOut(_ sender: Any?) {
  37. self.point = .zero
  38. super.orderOut(sender)
  39. }
  40. override func fadeOut() {
  41. self.point = .zero
  42. super.fadeOut()
  43. }
  44. override func stopAnimation() {
  45. super.stopAnimation()
  46. Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showDelayed), object: nil)
  47. }
  48. // MARK: - Public Methods
  49. public func showForImageContext(_ aContext: KMImageToolTipContext, at aPoint: NSPoint) {
  50. self.point = aPoint
  51. if aContext.isEqual(self.context) == false {
  52. self.stopAnimation()
  53. self.context = aContext
  54. self.perform(#selector(_showDelayed), with: nil, afterDelay: self.isVisible ? ALT_SHOW_DELAY: DEFAULT_SHOW_DELAY)
  55. }
  56. }
  57. }
  58. // MARK: - Private Methods
  59. extension KMImageToolTipWindow {
  60. @objc private func _showDelayed() {
  61. let thePoint = self.point == .zero ? NSEvent.mouseLocation : self.point
  62. var contentRect: NSRect = .zero
  63. var screenRect = NSScreen.screen(for: thePoint)?.frame ?? .zero
  64. var image = (self.context as? KMImageToolTipContext)?.toolTipImage()
  65. if (image != nil) {
  66. self.backgroundImage = image
  67. contentRect.size = image!.size
  68. contentRect.origin.x = fmin(thePoint.x, NSMaxX(screenRect) - NSWidth(contentRect))
  69. contentRect.origin.y = thePoint.y - WINDOW_OFFSET - NSHeight(contentRect)
  70. contentRect = self.frameRect(forContentRect: contentRect)
  71. if (NSMinY(contentRect) < NSMinX(screenRect)) {
  72. contentRect.origin.y = thePoint.y + WINDOW_OFFSET
  73. }
  74. self.setFrame(contentRect, display: false)
  75. if self.isVisible && self.alphaValue > CRITICAL_ALPHA_VALUE {
  76. self.orderFront(self)
  77. } else {
  78. self.fadeIn()
  79. }
  80. } else {
  81. self.fadeOut()
  82. }
  83. }
  84. }