KMFullScreenWindow.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // KMFullScreenWindow.swift
  3. // PDF Master
  4. //
  5. // Created by liujiajie on 2024/1/16.
  6. //
  7. import Cocoa
  8. let DURATION = 0.3
  9. class KMFullScreenWindow: NSWindow, NSAnimationDelegate{
  10. var animation: NSViewAnimation?
  11. var isMain = false
  12. convenience init(screen: NSScreen, bgColor: NSColor, level: Int, isMain flag: Bool){
  13. self.init(contentRect: screen.frame, styleMask: .borderless, backing: .buffered, defer: false)
  14. isMain = flag
  15. self.backgroundColor = bgColor
  16. self.level = NSWindow.Level(rawValue: level)
  17. self.isReleasedWhenClosed = false
  18. self.displaysWhenScreenProfileChanges = flag
  19. self.acceptsMouseMovedEvents = flag
  20. self.isExcludedFromWindowsMenu = flag == false
  21. self.setFrame(screen.frame, display: false)
  22. let selector = NSSelectorFromString("setAnimationBehavior:")
  23. if self.responds(to: selector){
  24. self.animationBehavior = .none
  25. }
  26. }
  27. deinit {
  28. stopAnimation()
  29. }
  30. func stopAnimation() {
  31. animation?.stop()
  32. animation = nil
  33. }
  34. func canBecomeKeyWindow() -> Bool {
  35. return isMain
  36. }
  37. func canBecomeMainWindow() -> Bool {
  38. return isMain
  39. }
  40. override func orderFront(_ sender: Any?) {
  41. self.stopAnimation()
  42. self.alphaValue = 1.0
  43. super.orderFront(sender)
  44. }
  45. override func makeKeyAndOrderFront(_ sender: Any?) {
  46. self.stopAnimation()
  47. self.alphaValue = 1.0
  48. super.makeKeyAndOrderFront(sender)
  49. }
  50. override func orderOut(_ sender: Any?) {
  51. self.stopAnimation()
  52. self.alphaValue = 1.0
  53. super.orderOut(sender)
  54. }
  55. func fadeOutWithBlockingMode(blockingMode: NSAnimation.BlockingMode) {
  56. if UserDefaults.standard.bool(forKey: SKDisableAnimationsKey) {
  57. self.orderOut(nil)
  58. } else {
  59. let dict = NSMutableDictionary()
  60. dict.setObject(self as Any, forKey: NSViewAnimation.Key.target as NSCopying)
  61. dict.setObject(NSViewAnimation.EffectName.fadeOut, forKey: NSViewAnimation.Key.effect as NSCopying)
  62. animation = NSViewAnimation(viewAnimations: NSArray(object: dict) as! [[NSViewAnimation.Key : Any]])
  63. animation?.animationBlockingMode = blockingMode
  64. animation?.duration = DURATION
  65. animation?.delegate = self
  66. animation?.start()
  67. }
  68. }
  69. func fadeInWithBlockingMode(blockingMode: NSAnimation.BlockingMode) {
  70. if UserDefaults.standard.bool(forKey: SKDisableAnimationsKey) {
  71. self.orderFront(nil)
  72. } else {
  73. let dict = NSMutableDictionary()
  74. dict.setObject(self as Any, forKey: NSViewAnimation.Key.target as NSCopying)
  75. dict.setObject(NSViewAnimation.EffectName.fadeIn, forKey: NSViewAnimation.Key.effect as NSCopying)
  76. self.alphaValue = 0.0
  77. super.orderFront(nil)
  78. animation = NSViewAnimation(viewAnimations: NSArray(object: dict) as! [[NSViewAnimation.Key : Any]])
  79. animation?.animationBlockingMode = blockingMode
  80. animation?.duration = DURATION
  81. animation?.delegate = self
  82. animation?.start()
  83. }
  84. }
  85. func fadeOutBlocking() {
  86. self.fadeOutWithBlockingMode(blockingMode: .blocking)
  87. }
  88. func fadeOut() {
  89. self.fadeOutWithBlockingMode(blockingMode: .nonblockingThreaded)
  90. }
  91. func fadeInBlocking() {
  92. self.fadeInWithBlockingMode(blockingMode: .blocking)
  93. }
  94. func fadeIn() {
  95. self.fadeInWithBlockingMode(blockingMode: .nonblockingThreaded)
  96. }
  97. func animationDidEnd(_ anAnimation: NSAnimation) {
  98. let ani: NSViewAnimation = animation ?? NSViewAnimation()
  99. let arr: [Any] = ani.viewAnimations
  100. let dict: [NSViewAnimation.Key: Any] = arr.last as! [NSViewAnimation.Key : Any]
  101. let isFadeOut: Bool = dict[NSViewAnimation.Key.effect] as! NSViewAnimation.EffectName == NSViewAnimation.EffectName.fadeOut
  102. animation = nil
  103. Task { @MainActor in
  104. if isFadeOut {
  105. self.orderOut(nil)
  106. }
  107. self.alphaValue = 1.0
  108. }
  109. }
  110. func animationDidStop(_ anAnimation: NSAnimation) {
  111. animation = nil
  112. self.orderOut(nil)
  113. self.alphaValue = 1.0
  114. }
  115. override func sendEvent(_ event: NSEvent) {
  116. if event.type == .rightMouseDown || (event.type == .leftMouseDown && (event.modifierFlags.rawValue & NSEvent.ModifierFlags.control.rawValue) != 0) {
  117. if self.windowController?.responds(to: Selector(("handleRightMouseDown:"))) == true && self.windowController?.handleRightMouseDown(theEvent: event) == true {
  118. return
  119. }
  120. } else if event.type == .keyDown {
  121. if event.keyCode == 53 { // esc
  122. // let sel = NSSelectorFromString("cancelOperation")
  123. self.cancelOperation(nil)
  124. return
  125. }
  126. }
  127. super.sendEvent(event)
  128. }
  129. override func cancelOperation(_ sender: Any?) {
  130. if self.windowController?.responds(to: #selector(NSStandardKeyBindingResponding.cancelOperation(_:))) == true {
  131. self.windowController?.cancelOperation(self)
  132. }
  133. }
  134. }
  135. extension NSWindowController{
  136. func handleRightMouseDown(theEvent: NSEvent) -> Bool {
  137. return false
  138. }
  139. }