KMBrowserWindowController+Actions.swift 9.6 KB

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