KMImageToolTipWindow.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.context = nil
  42. self.point = .zero
  43. super.fadeOut()
  44. }
  45. override func stopAnimation() {
  46. super.stopAnimation()
  47. Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showDelayed), object: nil)
  48. }
  49. // MARK: - Public Methods
  50. public func showForImageContext(_ aContext: KMImageToolTipContext, at aPoint: NSPoint) {
  51. if aContext != nil {
  52. if aContext.isEqual(self.context) == false {
  53. self.point = aPoint
  54. if aContext.isEqual(self.context) == false {
  55. self.stopAnimation()
  56. self.context = aContext
  57. self.perform(#selector(_showDelayed), with: nil, afterDelay: self.isVisible ? ALT_SHOW_DELAY: DEFAULT_SHOW_DELAY)
  58. }
  59. }
  60. }
  61. }
  62. @objc func newShowForImageContext(_ aContext: KMImageToolTipContext, at aPoint: NSPoint) {
  63. self.point = aPoint
  64. if aContext.isEqual(self.context) == false {
  65. self.stopAnimation()
  66. self.context = aContext
  67. self.perform(#selector(_showDelayed), with: nil, afterDelay: self.isVisible ? ALT_SHOW_DELAY: DEFAULT_SHOW_DELAY)
  68. }
  69. }
  70. }
  71. // MARK: - Private Methods
  72. extension KMImageToolTipWindow {
  73. @objc private func _showDelayed() {
  74. let thePoint = self.point == .zero ? NSEvent.mouseLocation : self.point
  75. var contentRect: NSRect = .zero
  76. let screenRect = NSScreen.screen(for: thePoint)?.frame ?? .zero
  77. let image = self.context?.toolTipImage()
  78. if (image != nil) {
  79. self.backgroundImage = image
  80. contentRect.size = image!.size
  81. contentRect.origin.x = fmin(thePoint.x, NSMaxX(screenRect) - NSWidth(contentRect))
  82. contentRect.origin.y = thePoint.y - WINDOW_OFFSET - NSHeight(contentRect)
  83. contentRect = self.frameRect(forContentRect: contentRect)
  84. if (NSMinY(contentRect) < NSMinX(screenRect)) {
  85. contentRect.origin.y = thePoint.y + WINDOW_OFFSET
  86. }
  87. self.setFrame(contentRect, display: false)
  88. if self.isVisible && self.alphaValue > CRITICAL_ALPHA_VALUE {
  89. self.orderFront(self)
  90. } else {
  91. self.fadeIn()
  92. }
  93. } else {
  94. self.fadeOut()
  95. }
  96. }
  97. }