KMBrowserWindowController+Actions.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. // let backgroundColor = UserDefaults.standard.color(forKey: "SKBackgroundColorKey")
  68. var view: NSView?
  69. var contentView: NSView?
  70. // PDFPage *page = [[self pdfView] currentPage];
  71. //
  72. self.window?.isSwitchingFullScreen = true
  73. //
  74. // if ([[findController view] window])
  75. // [findController toggleAboveView:nil animate:NO];
  76. //
  77. if wasInteractionMode == .legacyFullScreen {
  78. let doc = self.document as? KMMainDocument
  79. view = doc?.mainViewController?.pdfSplitView
  80. contentView = doc?.mainViewController?.centerContentView
  81. } else {
  82. let doc = self.document as? KMMainDocument
  83. view = doc?.mainViewController?.listView
  84. contentView = doc?.mainViewController?.readContentView
  85. }
  86. self.hideLeftSideWindow()
  87. self.hideRightSideWindow()
  88. // do this first, otherwise the navigation window may be covered by fadeWindow and then reveiled again, which looks odd
  89. // [pdfView setInteractionMode:SKNormalMode];
  90. //
  91. // this should be done before exitPresentationMode to get a smooth transition
  92. if let v = view {
  93. self.fadeOutFullScreenView(v)
  94. view?.frame = contentView?.bounds ?? .zero
  95. contentView?.addSubview(v)
  96. v.mas_makeConstraints { make in
  97. make?.left.mas_equalTo()(0)
  98. make?.right.mas_equalTo()(0)
  99. make?.top.mas_equalTo()(0)
  100. make?.bottom.mas_equalTo()(0)
  101. }
  102. (v as? CPDFListView)?.layoutDocumentView()
  103. (v as? CPDFListView)?.requiresDisplay()
  104. }
  105. if let mainVc = (self.document as? KMMainDocument)?.mainViewController {
  106. mainVc.exitFullscreenMode()
  107. }
  108. self.window?.isSwitchingFullScreen = false
  109. self.forceSubwindowsOnTop(false)
  110. //
  111. self.window?.interactionMode = .normal
  112. // if (_isShowToolbar) {
  113. // [self.toolbarViewController hiddenToolbar:NO];
  114. // }
  115. self.fadeOutFullScreenWindow()
  116. // the page number may have changed
  117. self.synchronizeWindowTitleWithDocumentName()
  118. self.removeBlankingWindows()
  119. }
  120. func hideLeftSideWindow() {
  121. // if leftSideViewController.view.window == leftSideWindow {
  122. // self.leftView.remove()
  123. // if leftSideWindow.firstResponder()?.isDescendant(of: leftSideController.view()) {
  124. // leftSideWindow.makeFirstResponder(nil)
  125. // }
  126. // leftSideController.view().setFrame(SKShrinkRect(NSInsetRect(leftSideContentView.bounds, -1.0, -1.0), 1.0, .maxYEdge))
  127. // leftSideContentView.addSubview(leftSideController.view())
  128. //
  129. // if self.interactionMode() == SKPresentationMode {
  130. // self.setLeftSidePaneState(mwcFlags.savedLeftSidePaneState)
  131. // }
  132. //
  133. // leftSideWindow = nil
  134. // }
  135. }
  136. func hideRightSideWindow() {
  137. // if rightSideController.view().window() == rightSideWindow {
  138. // rightSideWindow.remove()
  139. // if rightSideWindow.firstResponder()?.isDescendant(of: rightSideController.view()) {
  140. // rightSideWindow.makeFirstResponder(nil)
  141. // }
  142. // rightSideController.view().setFrame(SKShrinkRect(NSInsetRect(rightSideContentView.bounds, -1.0, -1.0), 1.0, .maxYEdge))
  143. // rightSideContentView.addSubview(rightSideController.view())
  144. //
  145. // rightSideWindow = nil
  146. // }
  147. }
  148. func removeBlankingWindows() {
  149. // [blankingWindows makeObjectsPerformSelector:@selector(fadeOut)];
  150. // blankingWindows = nil;
  151. }
  152. // func fadeInFullScreenWindow(with backgroundColor: NSColor, level: Int) {
  153. // self._mainWindow = self.view.window
  154. // let fullScreenWindow = KMFullScreenWindow(screen: (self.mainWindow?.screen ?? NSScreen.main)!, bgColor: backgroundColor, level: NSWindow.Level.popUpMenu.rawValue, isMain: true)
  155. //
  156. // self.mainWindow?.delegate = nil
  157. // fullScreenWindow.fadeInBlocking()
  158. // self.browserWindowController?.window = fullScreenWindow
  159. // fullScreenWindow.makeKey()
  160. //
  161. //// NSApp.updatePresentationOptions(for: fullScreenWindow)
  162. //
  163. // let sel = NSSelectorFromString("setAnimationBehavior:")
  164. // if self.mainWindow?.responds(to: sel) ?? false{
  165. // self.mainWindow?.animationBehavior = .none
  166. // }
  167. // self.mainWindow?.orderOut(nil)
  168. // if self.mainWindow?.responds(to: sel) ?? false{
  169. // self.mainWindow?.animationBehavior = .default
  170. // }
  171. // fullScreenWindow.level = NSWindow.Level(rawValue: level)
  172. // fullScreenWindow.orderFront(nil)
  173. //// NSApp.addWindowsItem(fullScreenWindow, title: windowTitleForDocumentDisplayName(self.document?.displayName) ?? "", filename: false)
  174. // }
  175. func fadeOutFullScreenView(_ view: NSView) {
  176. guard let fullScreenWindow = self.window as? KMFullScreenWindow else {
  177. NSSound.beep()
  178. return
  179. }
  180. let fadeWindow = KMFullScreenWindow(screen: fullScreenWindow.screen ?? NSScreen.main!, bgColor: fullScreenWindow.backgroundColor, level: fullScreenWindow.level.rawValue, isMain: false)
  181. fadeWindow.alphaValue = 0
  182. fadeWindow.order(.above, relativeTo: fullScreenWindow.windowNumber)
  183. fadeWindow.fadeInBlocking()
  184. view.removeFromSuperview()
  185. fullScreenWindow.display()
  186. fullScreenWindow.delegate = nil
  187. fullScreenWindow.makeFirstResponder(nil)
  188. fadeWindow.orderOut(nil)
  189. }
  190. func fadeOutFullScreenWindow() {
  191. guard let fullScreenWindow = self.window as? KMFullScreenWindow else {
  192. NSSound.beep()
  193. return
  194. }
  195. let mainVc = (self.document as? KMMainDocument)?.mainViewController
  196. // let mainWindow = mainVc?.mainWindow
  197. let mainWindow = fullScreenWindow.interactionParent
  198. let collectionBehavior = mainWindow?.collectionBehavior
  199. self.window = mainWindow
  200. mainWindow?.alphaValue = 0
  201. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  202. mainWindow?.animationBehavior = .none
  203. }
  204. // trick to make sure the main window shows up in the same space as the fullscreen window
  205. fullScreenWindow.addChildWindow(mainWindow!, ordered: .below)
  206. fullScreenWindow.removeChildWindow(mainWindow!)
  207. fullScreenWindow.level = .popUpMenu
  208. // these can change due to the child window trick
  209. mainWindow?.level = .normal
  210. mainWindow?.alphaValue = 1.0
  211. mainWindow?.collectionBehavior = collectionBehavior!
  212. mainWindow?.display()
  213. mainWindow?.makeFirstResponder(mainVc?.listView)
  214. mainWindow?.recalculateKeyViewLoop()
  215. mainWindow?.delegate = self
  216. mainWindow?.makeKey()
  217. // [NSApp updatePresentationOptionsForWindow:mainWindow];
  218. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  219. mainWindow?.animationBehavior = .default
  220. }
  221. NSApp.removeWindowsItem(fullScreenWindow)
  222. fullScreenWindow.fadeOut()
  223. }
  224. }