KMImageToolTipWindow.swift 3.4 KB

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