NSWindowController+KMExtension.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // NSWindowController+KMExtension.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/7/27.
  6. //
  7. import Foundation
  8. @objc protocol KMForceProtocol: NSObjectProtocol {
  9. @objc optional func setForceOnTop(_ flag: Bool)
  10. }
  11. extension NSWindowController {
  12. override func km_window() -> NSWindow? {
  13. return self.window
  14. }
  15. func forceSubwindowsOnTop(_ flag: Bool) {
  16. guard let doc = self.document as? NSDocument else {
  17. NSSound.beep()
  18. return
  19. }
  20. for wc in doc.windowControllers {
  21. if wc.responds(to: #selector(setForceOnTop)) {
  22. wc.setForceOnTop(flag)
  23. }
  24. }
  25. }
  26. }
  27. extension NSWindowController: KMForceProtocol {
  28. func setForceOnTop(_ flag: Bool) {
  29. // no things.
  30. }
  31. }
  32. // MARK: - 互动模式
  33. @objc enum KMInteractionMode: Int {
  34. case normal = 0 // 常规模式
  35. case fullScreen
  36. case presentation // 幻灯片模式
  37. case legacyFullScreen
  38. }
  39. @objc protocol KMInteractionModeProtocol: NSObjectProtocol {
  40. @objc optional var interactionMode: KMInteractionMode { get }
  41. @objc optional var isSwitchingFullScreen: Bool { get }
  42. @objc optional func canEnterPresentation() -> Bool
  43. @objc optional func canExitPresentation() -> Bool
  44. @objc optional func enterPresentation()
  45. @objc optional func exitPresentation()
  46. }
  47. @objc protocol KMInteractionProviderProtocol: NSObjectProtocol {
  48. @objc optional func providerContentView(fullScreenWindow: NSWindow, inset: CGFloat) -> NSView?
  49. }
  50. extension NSWindowController: KMInteractionModeProtocol {
  51. var interactionMode: KMInteractionMode {
  52. get {
  53. if let win = self.window?.interactionParent {
  54. return win.interactionMode
  55. }
  56. return self.window?.interactionMode ?? .normal
  57. }
  58. }
  59. var isSwitchingFullScreen: Bool {
  60. get {
  61. if let win = self.window?.interactionParent {
  62. return win.isSwitchingFullScreen
  63. }
  64. return self.window?.isSwitchingFullScreen ?? false
  65. }
  66. }
  67. func canEnterPresentation() -> Bool {
  68. if let win = self.window?.interactionParent {
  69. return win.canEnterPresentation()
  70. }
  71. return self.window?.canEnterPresentation() ?? false
  72. }
  73. func canExitPresentation() -> Bool {
  74. if let win = self.window?.interactionParent {
  75. return win.canExitPresentation()
  76. }
  77. return self.window?.canExitPresentation() ?? false
  78. }
  79. }
  80. @objc extension NSView: KMInteractionModeProtocol {
  81. var interactionMode: KMInteractionMode {
  82. get {
  83. if let win = self.window?.interactionParent {
  84. return win.interactionMode
  85. }
  86. return self.window?.interactionMode ?? .normal
  87. }
  88. }
  89. var isSwitchingFullScreen: Bool {
  90. get {
  91. if let win = self.window?.interactionParent {
  92. return win.isSwitchingFullScreen
  93. }
  94. return self.window?.isSwitchingFullScreen ?? false
  95. }
  96. }
  97. func canEnterPresentation() -> Bool {
  98. if let win = self.window?.interactionParent {
  99. return win.canEnterPresentation()
  100. }
  101. return self.window?.canEnterPresentation() ?? false
  102. }
  103. func canExitPresentation() -> Bool {
  104. if let win = self.window?.interactionParent {
  105. return win.canExitPresentation()
  106. }
  107. return self.window?.canExitPresentation() ?? false
  108. }
  109. }
  110. @objc extension NSViewController: KMInteractionModeProtocol {
  111. var interactionMode: KMInteractionMode {
  112. get {
  113. if let win = self.view.window?.interactionParent {
  114. return win.interactionMode
  115. }
  116. return self.view.window?.interactionMode ?? .normal
  117. }
  118. }
  119. var isSwitchingFullScreen: Bool {
  120. get {
  121. if let win = self.view.window?.interactionParent {
  122. return win.isSwitchingFullScreen
  123. }
  124. return self.view.window?.isSwitchingFullScreen ?? false
  125. }
  126. }
  127. func canEnterPresentation() -> Bool {
  128. if let win = self.view.window?.interactionParent {
  129. return win.canEnterPresentation()
  130. }
  131. return self.view.window?.canEnterPresentation() ?? false
  132. }
  133. func canExitPresentation() -> Bool {
  134. if let win = self.view.window?.interactionParent {
  135. return win.canExitPresentation()
  136. }
  137. return self.view.window?.canExitPresentation() ?? false
  138. }
  139. }