KMAnimatedBorderlessWindow.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // KMAnimatedBorderlessWindow.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/23.
  6. //
  7. import Cocoa
  8. @objcMembers class KMAnimatedBorderlessWindow: NSPanel {
  9. var defaultAlphaValue: CGFloat = 0
  10. var autoHideTimeInterval: TimeInterval = 0
  11. var fadeInDuration: TimeInterval {
  12. get {
  13. return FADE_IN_DURATION
  14. }
  15. }
  16. var fadeOutDuration: TimeInterval {
  17. get {
  18. return FADE_OUT_DURATION
  19. }
  20. }
  21. var backgroundImage: NSImage? {
  22. get {
  23. if let _ = self.contentView?.responds(to: NSSelectorFromString("image")) {
  24. return (self.contentView as? NSImageView)?.image
  25. }
  26. return nil
  27. }
  28. set {
  29. var imageView: NSImageView?
  30. if let data = self.contentView?.responds(to: NSSelectorFromString("setImage:")), data {
  31. imageView = self.contentView as? NSImageView
  32. } else if newValue != nil {
  33. imageView = NSImageView()
  34. imageView?.isEditable = false
  35. imageView?.imageFrameStyle = .none
  36. imageView?.imageScaling = .scaleProportionallyDown
  37. self.contentView = imageView
  38. }
  39. imageView?.image = newValue
  40. }
  41. }
  42. private let ALPHA_VALUE = 1.0
  43. private let FADE_IN_DURATION = 0.3
  44. private let FADE_OUT_DURATION = 1.0
  45. private let AUTO_HIDE_TIME_INTERVAL = 0.0
  46. deinit {
  47. KMPrint("KMAnimatedBorderlessWindow deinit.")
  48. self.stopAnimation()
  49. }
  50. convenience init(contentRect: NSRect) {
  51. self.init(contentRect: contentRect, styleMask: [.borderless], backing: .buffered, defer: false)
  52. self.defaultAlphaValue = ALPHA_VALUE
  53. self.autoHideTimeInterval = AUTO_HIDE_TIME_INTERVAL
  54. self.backgroundColor = .clear
  55. self.isOpaque = false
  56. self.alphaValue = self.defaultAlphaValue
  57. self.isReleasedWhenClosed = false
  58. self.hidesOnDeactivate = false
  59. if self.responds(to: NSSelectorFromString("setAnimationBehavior:")) {
  60. self.animationBehavior = .none
  61. }
  62. }
  63. override var canBecomeKey: Bool {
  64. return false
  65. }
  66. override var canBecomeMain: Bool {
  67. return false
  68. }
  69. override func accessibilityIsIgnored() -> Bool {
  70. return true
  71. }
  72. // MARK: - Public Methods
  73. public func fadeIn() {
  74. self.stopAnimation()
  75. if self.isVisible == false {
  76. self.alphaValue = 0.0
  77. }
  78. super.orderFront(self)
  79. if UserDefaults.standard.bool(forKey: SKDisableAnimationsKey) {
  80. self.alphaValue = self.defaultAlphaValue
  81. } else {
  82. NSAnimationContext.runAnimationGroup { context in
  83. context.duration = self.fadeInDuration
  84. self.animator().alphaValue = self.defaultAlphaValue
  85. }
  86. }
  87. self._fadeOutAfterTimeout()
  88. }
  89. @objc public func fadeOut() {
  90. self.stopAnimation()
  91. self.alphaValue = self.defaultAlphaValue
  92. if UserDefaults.standard.bool(forKey: SKDisableAnimationsKey) {
  93. self.remove()
  94. } else {
  95. NSAnimationContext.runAnimationGroup { context in
  96. context.duration = self.fadeOutDuration
  97. self.animator().alphaValue = 0.0
  98. }
  99. // don't put this in the completionHandler, because we want to be able to stop this using stopAnimation
  100. self.perform(#selector(remove), with: nil, afterDelay: self.fadeOutDuration)
  101. }
  102. }
  103. @objc public func remove() {
  104. self.orderOut(nil)
  105. }
  106. override func orderOut(_ sender: Any?) {
  107. self.stopAnimation()
  108. super.orderOut(sender)
  109. self.alphaValue = self.defaultAlphaValue
  110. }
  111. override func orderFront(_ sender: Any?) {
  112. self.stopAnimation()
  113. self.alphaValue = self.defaultAlphaValue
  114. super.orderFront(sender)
  115. self._fadeOutAfterTimeout()
  116. }
  117. override func orderFrontRegardless() {
  118. self.stopAnimation()
  119. self.alphaValue = self.defaultAlphaValue
  120. super.orderFrontRegardless()
  121. self._fadeOutAfterTimeout()
  122. }
  123. public func stopAnimation() {
  124. Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(fadeOut), object: nil)
  125. Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(remove), object: nil)
  126. }
  127. }
  128. // MARK: - Private Methods
  129. extension KMAnimatedBorderlessWindow {
  130. private func _fadeOutAfterTimeout() {
  131. if self.autoHideTimeInterval > 0.0 {
  132. self.perform(#selector(fadeOut), with: nil, afterDelay: self.autoHideTimeInterval)
  133. }
  134. }
  135. }