KMBrowserWindowController+Actions.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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._mwcFlags.isSwitchingFullScreen != 0) {
  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 == .SKFullScreenMode || mode == .SKLegacyFullScreenMode {
  27. return false
  28. }
  29. let cnt = self.window?.tabbedWindows?.count ?? 0
  30. return cnt < 2
  31. }
  32. func canEnterPresentation() -> Bool {
  33. // (interactionMode == .SKNormalMode || interactionMode == .SKFullScreenMode || interactionMode == .SKLegacyFullScreenMode)
  34. if self._interactionMode == .SKPresentationMode { // 幻灯片模式下
  35. return false
  36. }
  37. if self._mwcFlags.isSwitchingFullScreen == 1 {
  38. return false
  39. }
  40. if let mainVc = (self.document as? KMMainDocument)?.mainViewController {
  41. if mainVc.canEnterPresentation() == false {
  42. return false
  43. }
  44. }
  45. let cnt = self.window?.tabbedWindows?.count ?? 0
  46. return cnt < 2
  47. }
  48. func canExitFullscreen() -> Bool {
  49. if self._mwcFlags.isSwitchingFullScreen == 0 {
  50. return false
  51. }
  52. let mode = self._interactionMode
  53. if mode == .SKFullScreenMode || mode == .SKLegacyFullScreenMode {
  54. return true
  55. }
  56. return false
  57. }
  58. func canExitPresentation() -> Bool {
  59. if self._mwcFlags.isSwitchingFullScreen == 1 {
  60. return false
  61. }
  62. if self._interactionMode == .SKPresentationMode {
  63. return true
  64. }
  65. return false
  66. }
  67. func enterPresentation() {
  68. if self.canEnterPresentation() == false {
  69. return
  70. }
  71. let wasInteractionMode = self._interactionMode
  72. if wasInteractionMode == .SKFullScreenMode {
  73. self._mwcFlags.wantsPresentation = 1
  74. self.window?.toggleFullScreen(nil)
  75. return
  76. }
  77. let backgroundColor = NSColor.black
  78. let level = UserDefaults.standard.bool(forKey: "SKUseNormalLevelForPresentationKey") ? NSWindow.Level.normal : NSWindow.Level.popUpMenu
  79. // let page = self.listView.currentPage()
  80. if wasInteractionMode == .SKNormalMode {
  81. // savedNormalSetup.setDictionary(self.currentPDFSettings() as! [AnyHashable : Any])
  82. }
  83. // 设置切换中标识
  84. self._mwcFlags.isSwitchingFullScreen = 1
  85. // if findController.view().window() != nil {
  86. // findController.toggleAboveView(nil, animate: false)
  87. // }
  88. // 设置模式标识
  89. self._interactionMode = .SKPresentationMode
  90. if wasInteractionMode == .SKLegacyFullScreenMode {
  91. // self.enterPresentationMode()
  92. //
  93. //// updatePresentationOptions(for: self.view.window!)
  94. //
  95. // pdfSplitView.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(centerContentView.bounds), height: CGRectGetHeight(centerContentView.bounds)-1)
  96. // centerContentView.addSubview(pdfSplitView)
  97. // listView.frame = (self.view.window?.contentView?.bounds)!
  98. // self.view.window?.contentView?.addSubview(listView)
  99. //
  100. // self.view.window?.backgroundColor = backgroundColor
  101. // self.view.window?.level = level
  102. // listView.layoutDocumentView()
  103. // listView.requiresDisplay()
  104. //
  105. // self.forceSubwindowsOnTop(false)
  106. //
  107. // self.hideLeftSideWindow()
  108. // self.hideRightSideWindow()
  109. // self.removeBlankingWindows()
  110. } else {
  111. // self.fadeInFullScreenWindow(with: backgroundColor, level: level.rawValue)
  112. if let mainVc = (self.document as? KMMainDocument)?.mainViewController {
  113. mainVc.fadeInFullScreenWindow(with: backgroundColor, level: level.rawValue)
  114. mainVc.enterPresentationMode()
  115. mainVc.fadeInFullScreenView(mainVc.listView, inset: 0)
  116. }
  117. // self.fadeInFullScreenView(self.listView, inset: 0)
  118. }
  119. // if _isShowToolbar {
  120. // self.toolbarViewController.hiddenToolbar(true)
  121. // }
  122. //
  123. // if self.pdfView().currentPage()?.isEqual(page) == false {
  124. // self.pdfView().go(to: page)
  125. // }
  126. //
  127. // 恢复切换全屏标识
  128. self._mwcFlags.isSwitchingFullScreen = 0
  129. //
  130. // pdfView().setInteractionMode(SKPresentationMode)
  131. }
  132. func exitFullscreen() {
  133. let wasInteractionMode = self._interactionMode
  134. if self.canExitFullscreen() == false && self.canExitPresentation() == false {
  135. return
  136. }
  137. if wasInteractionMode == .SKFullScreenMode {
  138. self.window?.toggleFullScreen(nil)
  139. return
  140. }
  141. // let backgroundColor = UserDefaults.standard.color(forKey: "SKBackgroundColorKey")
  142. var view: NSView?
  143. var contentView: NSView?
  144. // PDFPage *page = [[self pdfView] currentPage];
  145. //
  146. self._mwcFlags.isSwitchingFullScreen = 1
  147. //
  148. // if ([[findController view] window])
  149. // [findController toggleAboveView:nil animate:NO];
  150. //
  151. if wasInteractionMode == .SKLegacyFullScreenMode {
  152. let doc = self.document as? KMMainDocument
  153. view = doc?.mainViewController?.pdfSplitView
  154. contentView = doc?.mainViewController?.centerContentView
  155. } else {
  156. let doc = self.document as? KMMainDocument
  157. view = doc?.mainViewController?.listView
  158. contentView = doc?.mainViewController?.readContentView
  159. }
  160. self.hideLeftSideWindow()
  161. self.hideRightSideWindow()
  162. // do this first, otherwise the navigation window may be covered by fadeWindow and then reveiled again, which looks odd
  163. // [pdfView setInteractionMode:SKNormalMode];
  164. //
  165. // this should be done before exitPresentationMode to get a smooth transition
  166. if let v = view {
  167. self.fadeOutFullScreenView(v)
  168. view?.frame = contentView?.bounds ?? .zero
  169. contentView?.addSubview(v)
  170. v.mas_makeConstraints { make in
  171. make?.left.mas_equalTo()(0)
  172. make?.right.mas_equalTo()(0)
  173. make?.top.mas_equalTo()(0)
  174. make?.bottom.mas_equalTo()(0)
  175. }
  176. (v as? CPDFListView)?.layoutDocumentView()
  177. (v as? CPDFListView)?.requiresDisplay()
  178. }
  179. if let mainVc = (self.document as? KMMainDocument)?.mainViewController {
  180. mainVc.exitFullscreenMode()
  181. }
  182. // [self applyPDFSettings:savedNormalSetup];
  183. // [savedNormalSetup removeAllObjects];
  184. //
  185. // [pdfView layoutDocumentView];
  186. // [pdfView requiresDisplay];
  187. //
  188. // if ([[[self pdfView] currentPage] isEqual:page] == NO)
  189. // [[self pdfView] goToPage:page];
  190. //
  191. self._mwcFlags.isSwitchingFullScreen = 0
  192. self.forceSubwindowsOnTop(false)
  193. //
  194. self._interactionMode = .SKNormalMode
  195. // if (_isShowToolbar) {
  196. // [self.toolbarViewController hiddenToolbar:NO];
  197. // }
  198. self.fadeOutFullScreenWindow()
  199. // the page number may have changed
  200. self.synchronizeWindowTitleWithDocumentName()
  201. self.removeBlankingWindows()
  202. }
  203. func hideLeftSideWindow() {
  204. // if leftSideViewController.view.window == leftSideWindow {
  205. // self.leftView.remove()
  206. // if leftSideWindow.firstResponder()?.isDescendant(of: leftSideController.view()) {
  207. // leftSideWindow.makeFirstResponder(nil)
  208. // }
  209. // leftSideController.view().setFrame(SKShrinkRect(NSInsetRect(leftSideContentView.bounds, -1.0, -1.0), 1.0, .maxYEdge))
  210. // leftSideContentView.addSubview(leftSideController.view())
  211. //
  212. // if self.interactionMode() == SKPresentationMode {
  213. // self.setLeftSidePaneState(mwcFlags.savedLeftSidePaneState)
  214. // }
  215. //
  216. // leftSideWindow = nil
  217. // }
  218. }
  219. func hideRightSideWindow() {
  220. // if rightSideController.view().window() == rightSideWindow {
  221. // rightSideWindow.remove()
  222. // if rightSideWindow.firstResponder()?.isDescendant(of: rightSideController.view()) {
  223. // rightSideWindow.makeFirstResponder(nil)
  224. // }
  225. // rightSideController.view().setFrame(SKShrinkRect(NSInsetRect(rightSideContentView.bounds, -1.0, -1.0), 1.0, .maxYEdge))
  226. // rightSideContentView.addSubview(rightSideController.view())
  227. //
  228. // rightSideWindow = nil
  229. // }
  230. }
  231. func removeBlankingWindows() {
  232. // [blankingWindows makeObjectsPerformSelector:@selector(fadeOut)];
  233. // blankingWindows = nil;
  234. }
  235. func fadeOutFullScreenView(_ view: NSView) {
  236. guard let fullScreenWindow = self.window as? KMFullScreenWindow else {
  237. NSSound.beep()
  238. return
  239. }
  240. let fadeWindow = KMFullScreenWindow(screen: fullScreenWindow.screen ?? NSScreen.main!, bgColor: fullScreenWindow.backgroundColor, level: fullScreenWindow.level.rawValue, isMain: false)
  241. fadeWindow.alphaValue = 0
  242. fadeWindow.order(.above, relativeTo: fullScreenWindow.windowNumber)
  243. fadeWindow.fadeInBlocking()
  244. view.removeFromSuperview()
  245. fullScreenWindow.display()
  246. fullScreenWindow.delegate = nil
  247. fullScreenWindow.makeFirstResponder(nil)
  248. fadeWindow.orderOut(nil)
  249. }
  250. func fadeOutFullScreenWindow() {
  251. guard let fullScreenWindow = self.window as? KMFullScreenWindow else {
  252. NSSound.beep()
  253. return
  254. }
  255. let mainVc = (self.document as? KMMainDocument)?.mainViewController
  256. let mainWindow = mainVc?.mainWindow
  257. let collectionBehavior = mainWindow?.collectionBehavior
  258. self.window = mainWindow
  259. mainWindow?.alphaValue = 0
  260. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  261. mainWindow?.animationBehavior = .none
  262. }
  263. // trick to make sure the main window shows up in the same space as the fullscreen window
  264. fullScreenWindow.addChildWindow(mainWindow!, ordered: .below)
  265. fullScreenWindow.removeChildWindow(mainWindow!)
  266. fullScreenWindow.level = .popUpMenu
  267. // these can change due to the child window trick
  268. mainWindow?.level = .normal
  269. mainWindow?.alphaValue = 1.0
  270. mainWindow?.collectionBehavior = collectionBehavior!
  271. mainWindow?.display()
  272. mainWindow?.makeFirstResponder(mainVc?.listView)
  273. mainWindow?.recalculateKeyViewLoop()
  274. mainWindow?.delegate = self
  275. mainWindow?.makeKey()
  276. // [NSApp updatePresentationOptionsForWindow:mainWindow];
  277. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  278. mainWindow?.animationBehavior = .default
  279. }
  280. NSApp.removeWindowsItem(fullScreenWindow)
  281. fullScreenWindow.fadeOut()
  282. }
  283. }