KMAnimatedBorderlessWindow.swift 4.9 KB

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