KMBrowserWindowController+Actions.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // KMBrowserWindowController+Actions.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/2/20.
  6. //
  7. import Foundation
  8. // MARK: - Actions
  9. @objc extension KMBrowserWindowController {
  10. }
  11. // MARK: - 幻灯片
  12. extension KMBrowserWindowController {
  13. func canEnterFullscreen() -> Bool {
  14. if self.isSwitchingFullScreen {
  15. return false
  16. }
  17. // if useNativeFullScreen() {
  18. // return interactionMode == .SKNormalMode || interactionMode == .SKPresentationMode
  19. // } else {
  20. if let mainVc = (self.document as? KMMainDocument)?.mainViewController {
  21. if mainVc.canEnterFullscreen() == false {
  22. return false
  23. }
  24. }
  25. let mode = self.interactionMode
  26. if mode == .fullScreen || mode == .legacyFullScreen {
  27. return false
  28. }
  29. let cnt = self.window?.tabbedWindows?.count ?? 0
  30. return cnt < 2
  31. }
  32. override func canEnterPresentation() -> Bool {
  33. let can = super.canEnterPresentation()
  34. if can == false {
  35. return false
  36. }
  37. if let mainVc = (self.document as? KMMainDocument)?.mainViewController {
  38. if mainVc.canEnterPresentation() == false {
  39. return false
  40. }
  41. }
  42. return can
  43. }
  44. func canExitFullscreen() -> Bool {
  45. if self.isSwitchingFullScreen == false {
  46. return false
  47. }
  48. let mode = self.interactionMode
  49. if mode == .fullScreen || mode == .legacyFullScreen {
  50. return true
  51. }
  52. return false
  53. }
  54. // 进入幻灯片
  55. func enterPresentation() {
  56. self.window?.enterPresentation()
  57. }
  58. func exitFullscreen() {
  59. let wasInteractionMode = self.interactionMode
  60. if self.canExitFullscreen() == false && self.canExitPresentation() == false {
  61. return
  62. }
  63. if wasInteractionMode == .fullScreen {
  64. self.window?.toggleFullScreen(nil)
  65. return
  66. }
  67. var view: NSView?
  68. var contentView: NSView?
  69. self.window?.isSwitchingFullScreen = true
  70. if wasInteractionMode == .legacyFullScreen {
  71. let doc = self.document as? KMMainDocument
  72. view = doc?.mainViewController?.pdfSplitView
  73. contentView = doc?.mainViewController?.centerContentView
  74. } else {
  75. let doc = self.document as? KMMainDocument
  76. view = doc?.mainViewController?.listView
  77. contentView = doc?.mainViewController?.readContentView
  78. }
  79. self.hideLeftSideWindow()
  80. self.hideRightSideWindow()
  81. let doc = self.document as? KMMainDocument
  82. let mainViewController = doc?.mainViewController
  83. mainViewController?.presentationTopViewController?.view.removeFromSuperview()
  84. if let v = view {
  85. self.fadeOutFullScreenView(v)
  86. view?.frame = contentView?.bounds ?? .zero
  87. contentView?.addSubview(v)
  88. v.mas_makeConstraints { make in
  89. make?.left.mas_equalTo()(0)
  90. make?.right.mas_equalTo()(0)
  91. make?.top.mas_equalTo()(0)
  92. make?.bottom.mas_equalTo()(0)
  93. }
  94. (v as? CPDFListView)?.isPresentationMode = false
  95. (v as? CPDFListView)?.layoutDocumentView()
  96. (v as? CPDFListView)?.requiresDisplay()
  97. if (v as? CPDFListView)?.isEnterPresentationDrawMode() == true {
  98. (v as? CPDFListView)?.exitPresentationDrawMode()
  99. }
  100. }
  101. if let mainVc = (self.document as? KMMainDocument)?.mainViewController {
  102. mainVc.exitFullscreenMode()
  103. }
  104. self.window?.isSwitchingFullScreen = false
  105. self.forceSubwindowsOnTop(false)
  106. self.window?.interactionMode = .normal
  107. self.fadeOutFullScreenWindow()
  108. self.synchronizeWindowTitleWithDocumentName()
  109. self.removeBlankingWindows()
  110. }
  111. func hideLeftSideWindow() {
  112. }
  113. func hideRightSideWindow() {
  114. }
  115. func removeBlankingWindows() {
  116. }
  117. func fadeOutFullScreenView(_ view: NSView) {
  118. guard let fullScreenWindow = self.window as? KMFullScreenWindow else {
  119. NSSound.beep()
  120. return
  121. }
  122. let fadeWindow = KMFullScreenWindow(screen: fullScreenWindow.screen ?? NSScreen.main!, bgColor: fullScreenWindow.backgroundColor, level: fullScreenWindow.level.rawValue, isMain: false)
  123. fadeWindow.alphaValue = 0
  124. fadeWindow.order(.above, relativeTo: fullScreenWindow.windowNumber)
  125. fadeWindow.fadeInBlocking()
  126. view.removeFromSuperview()
  127. fullScreenWindow.display()
  128. fullScreenWindow.delegate = nil
  129. fullScreenWindow.makeFirstResponder(nil)
  130. fadeWindow.orderOut(nil)
  131. }
  132. func fadeOutFullScreenWindow() {
  133. guard let fullScreenWindow = self.window as? KMFullScreenWindow else {
  134. NSSound.beep()
  135. return
  136. }
  137. let mainVc = (self.document as? KMMainDocument)?.mainViewController
  138. let mainWindow = fullScreenWindow.interactionParent
  139. let collectionBehavior = mainWindow?.collectionBehavior
  140. self.window = mainWindow
  141. mainWindow?.alphaValue = 0
  142. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  143. mainWindow?.animationBehavior = .none
  144. }
  145. // trick to make sure the main window shows up in the same space as the fullscreen window
  146. fullScreenWindow.addChildWindow(mainWindow!, ordered: .below)
  147. fullScreenWindow.removeChildWindow(mainWindow!)
  148. fullScreenWindow.level = .popUpMenu
  149. // these can change due to the child window trick
  150. mainWindow?.level = .normal
  151. mainWindow?.alphaValue = 1.0
  152. mainWindow?.collectionBehavior = collectionBehavior!
  153. mainWindow?.display()
  154. mainWindow?.makeFirstResponder(mainVc?.listView)
  155. mainWindow?.recalculateKeyViewLoop()
  156. mainWindow?.delegate = self
  157. mainWindow?.makeKey()
  158. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  159. mainWindow?.animationBehavior = .default
  160. }
  161. NSApp.removeWindowsItem(fullScreenWindow)
  162. fullScreenWindow.fadeOut()
  163. }
  164. }