KMBrowserWindowController+PPTMode.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. } else {
  67. let doc = self.document as? KMMainDocument
  68. view = doc?.mainViewController?.listView
  69. }
  70. let doc = self.document as? KMMainDocument
  71. let mainViewController = doc?.mainViewController
  72. mainViewController?.presentationTopViewController?.view.removeFromSuperview()
  73. if let v = view {
  74. self.fadeOutFullScreenView(v)
  75. view?.frame = contentView?.bounds ?? .zero
  76. view?.autoresizingMask = [.width, .height]
  77. contentView?.addSubview(v)
  78. (v as? CPDFListView)?.isPresentationMode = false
  79. (v as? CPDFListView)?.layoutDocumentView()
  80. (v as? CPDFListView)?.requiresDisplay()
  81. if (v as? CPDFListView)?.isEnterPresentationDrawMode() == true {
  82. (v as? CPDFListView)?.exitPresentationDrawMode()
  83. }
  84. }
  85. if let mainVc = (self.document as? KMMainDocument)?.mainViewController {
  86. mainVc.exitFullscreenMode()
  87. }
  88. self.window?.isSwitchingFullScreen = false
  89. self.forceSubwindowsOnTop(false)
  90. self.window?.interactionMode = .normal
  91. self.fadeOutFullScreenWindow()
  92. self.synchronizeWindowTitleWithDocumentName()
  93. }
  94. func fadeOutFullScreenView(_ view: NSView) {
  95. guard let fullScreenWindow = self.window as? KMFullScreenWindow else {
  96. NSSound.beep()
  97. return
  98. }
  99. let fadeWindow = KMFullScreenWindow(screen: fullScreenWindow.screen ?? NSScreen.main!, bgColor: fullScreenWindow.backgroundColor, level: fullScreenWindow.level.rawValue, isMain: false)
  100. fadeWindow.alphaValue = 0
  101. fadeWindow.order(.above, relativeTo: fullScreenWindow.windowNumber)
  102. fadeWindow.fadeInBlocking()
  103. view.removeFromSuperview()
  104. fullScreenWindow.display()
  105. fullScreenWindow.delegate = nil
  106. fullScreenWindow.makeFirstResponder(nil)
  107. fadeWindow.orderOut(nil)
  108. }
  109. func fadeOutFullScreenWindow() {
  110. guard let fullScreenWindow = self.window as? KMFullScreenWindow else {
  111. NSSound.beep()
  112. return
  113. }
  114. let mainVc = (self.document as? KMMainDocument)?.mainViewController
  115. let mainWindow = fullScreenWindow.interactionParent
  116. let collectionBehavior = mainWindow?.collectionBehavior
  117. self.window = mainWindow
  118. mainWindow?.alphaValue = 0
  119. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  120. mainWindow?.animationBehavior = .none
  121. }
  122. // trick to make sure the main window shows up in the same space as the fullscreen window
  123. fullScreenWindow.addChildWindow(mainWindow!, ordered: .below)
  124. fullScreenWindow.removeChildWindow(mainWindow!)
  125. fullScreenWindow.level = .popUpMenu
  126. // these can change due to the child window trick
  127. mainWindow?.level = .normal
  128. mainWindow?.alphaValue = 1.0
  129. mainWindow?.collectionBehavior = collectionBehavior!
  130. mainWindow?.display()
  131. mainWindow?.makeFirstResponder(mainVc?.listView)
  132. mainWindow?.recalculateKeyViewLoop()
  133. mainWindow?.delegate = self
  134. mainWindow?.makeKey()
  135. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  136. mainWindow?.animationBehavior = .default
  137. }
  138. NSApp.removeWindowsItem(fullScreenWindow)
  139. fullScreenWindow.fadeOut()
  140. }
  141. }