KMPresentationTopViewController.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // KMPresentationTopViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/9/23.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc public protocol KMPresentationTopViewControllerDelegate: AnyObject {
  10. @objc optional func presentationTopViewExit(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
  11. @objc optional func presentationTopViewUndo(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
  12. @objc optional func presentationTopViewClear(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
  13. @objc optional func presentationTopViewType(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton,isSeletion:Bool)
  14. @objc optional func presentationTopViewDrawColor(_ presentationTopViewController: KMPresentationTopViewController, withView: NSView,color:NSColor?)
  15. }
  16. public class KMPresentationTopViewController: NSViewController, KMDrawViewDelegate {
  17. @IBOutlet var pageNubLabel: NSTextField!
  18. @IBOutlet var backButton: NSButton!
  19. @IBOutlet var forwardButton: NSButton!
  20. @IBOutlet var undoButton: NSButton!
  21. @IBOutlet var exitButton: NSButton!
  22. @IBOutlet var deleteButton: NSButton!
  23. @IBOutlet var typeButton: NSButton!
  24. @IBOutlet var colorGroup: ComponentCColorGroup!
  25. public weak var pdfView: CPDFListView?
  26. public var isSelectionPre: Bool = true
  27. weak var delegate: KMPresentationTopViewControllerDelegate?
  28. var keyEventMonitor: Any?
  29. deinit {
  30. NSColorPanel.shared.setTarget(nil)
  31. NSColorPanel.shared.setAction(nil)
  32. removeKeyEventMonitor()
  33. }
  34. public override func viewDidLoad() {
  35. super.viewDidLoad()
  36. pageNubLabel.stringValue = "\((pdfView?.currentPageIndex ?? 0) + 1)/\(pdfView?.document.pageCount ?? 0)"
  37. NotificationCenter.default.addObserver(self, selector: #selector(pageChangedNotification(_:)), name: NSNotification.Name.CPDFViewPageChanged, object: self.pdfView)
  38. self.view.wantsLayer = true
  39. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  40. updateViewColor()
  41. if(isSelectionPre == false) {
  42. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  43. } else {
  44. typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
  45. }
  46. updatePageState()
  47. pdfView?.presentationDrawView.delegate = self
  48. undoButton.isEnabled = false
  49. (self.view as? KMHoverView)?.hoverAction = { view, act in
  50. if act == .enter {
  51. NSCursor.arrow.set()
  52. } else if act == .exit {
  53. } else {
  54. NSCursor.arrow.set()
  55. }
  56. }
  57. let colors = KMAnnotationPropertiesColorManager.manager.pptColors
  58. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  59. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  60. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  61. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  62. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  63. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  64. colorGroup.delegate = self
  65. addKeyEventMonitor()
  66. }
  67. func addKeyEventMonitor() {
  68. if (self.keyEventMonitor != nil) {
  69. self.removeKeyEventMonitor()
  70. }
  71. keyEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
  72. guard let self = self else { return event}
  73. if let mainVC = self.delegate as? KMMainViewController {
  74. if event.keyCode == 53 {
  75. self.buttonItemClicked_Exit(self.exitButton)
  76. }
  77. if mainVC.interactionMode == .presentation { // 幻灯片模式下
  78. self.pdfView!.keyDown(with: event)
  79. return nil
  80. }
  81. }
  82. return event
  83. }
  84. }
  85. func removeKeyEventMonitor() {
  86. if (self.keyEventMonitor != nil) {
  87. KMPrint("removeKeyEventMonitor 已移除事件监听")
  88. NSEvent.removeMonitor(self.keyEventMonitor as Any)
  89. self.keyEventMonitor = nil
  90. }
  91. }
  92. // MARK: - Private
  93. private func updatePageState() {
  94. if(pdfView?.canGoToNextPage() == true) {
  95. backButton.isEnabled = true
  96. } else {
  97. backButton.isEnabled = false
  98. }
  99. if(pdfView?.canGoToPreviousPage() == true) {
  100. forwardButton.isEnabled = true
  101. } else {
  102. forwardButton.isEnabled = false
  103. }
  104. }
  105. private func updateViewColor() {
  106. let isDark = KMAppearance.isDarkMode()
  107. if isDark {
  108. self.view.layer?.backgroundColor = NSColor(hex: "#464646").cgColor
  109. } else {
  110. self.view.layer?.backgroundColor = NSColor.white.cgColor
  111. }
  112. }
  113. // MARK: - Action
  114. @IBAction func buttonItemClicked_BackPage(_ sender: NSButton) {
  115. updatePageState()
  116. if(pdfView?.canGoToNextPage() == true) {
  117. pdfView?.goToNextPage(sender)
  118. }
  119. }
  120. @IBAction func buttonItemClicked_ForwardPage(_ sender: NSButton) {
  121. updatePageState()
  122. if(pdfView?.canGoToPreviousPage() == true) {
  123. pdfView?.goToPreviousPage(sender)
  124. }
  125. }
  126. @IBAction func buttonItemClicked_Undo(_ sender: NSButton) {
  127. delegate?.presentationTopViewUndo?(self, withButton: sender)
  128. }
  129. @IBAction func buttonItemClicked_Exit(_ sender: NSButton) {
  130. delegate?.presentationTopViewExit?(self, withButton: sender)
  131. }
  132. @IBAction func buttonItemClicked_Delete(_ sender: NSButton) {
  133. delegate?.presentationTopViewClear?(self, withButton: sender)
  134. }
  135. @IBAction func buttonItemClicked_Type(_ sender: NSButton) {
  136. if(isSelectionPre == true) {
  137. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  138. isSelectionPre = false
  139. } else {
  140. typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
  141. isSelectionPre = true
  142. }
  143. undoButton.isEnabled = false
  144. colorGroup.currentColor = nil
  145. colorGroup.refreshUI()
  146. delegate?.presentationTopViewType?(self, withButton: sender, isSeletion: isSelectionPre)
  147. }
  148. // MARK: - Notification
  149. @objc func pageChangedNotification(_ notification: Notification) {
  150. guard let pdfview = notification.object as? CPDFView else {
  151. return
  152. }
  153. if pdfview.document == self.pdfView?.document {
  154. pageNubLabel.stringValue = "\((pdfView?.currentPageIndex ?? 0) + 1)/\(pdfView?.document.pageCount ?? 0)"
  155. updatePageState()
  156. let presentationDrawView = pdfView?.presentationDrawView
  157. presentationDrawView?.clear() //
  158. presentationDrawView?.resetUndoManager()
  159. }
  160. }
  161. @objc func themeChanged(notification: Notification) {
  162. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  163. self.updateViewColor()
  164. }
  165. }
  166. // MARK: - KMDrawView
  167. public func drawView(_ drawView: KMDrawView!, didUpdateUndoStatus enable: Bool) {
  168. undoButton.isEnabled = enable
  169. }
  170. public func drawView(_ drawView: KMDrawView!, didDrawEnd point: CGPoint) {
  171. undoButton.isEnabled = ((pdfView?.presentationDrawView.canUndo()) == true)
  172. }
  173. }
  174. // MARK: - ComponentCColorDelegate
  175. extension KMPresentationTopViewController: ComponentCColorDelegate {
  176. public func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  177. if let borderColor = color {
  178. if (isSelectionPre == true) {
  179. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  180. isSelectionPre = false
  181. }
  182. undoButton.isEnabled = false
  183. delegate?.presentationTopViewDrawColor?(self, withView: view, color: borderColor)
  184. }
  185. }
  186. }