KMBrowserWindowController+PPTMode.swift 5.7 KB

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