KMPresentationTopViewController.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // KMPresentationTopViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/9/23.
  6. //
  7. import Cocoa
  8. @objc public protocol KMPresentationTopViewControllerDelegate: AnyObject {
  9. @objc optional func presentationTopViewExit(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
  10. @objc optional func presentationTopViewUndo(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
  11. @objc optional func presentationTopViewClear(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
  12. @objc optional func presentationTopViewType(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton,isSeletion:Bool)
  13. @objc optional func presentationTopViewDrawColor(_ presentationTopViewController: KMPresentationTopViewController, withView: NSView,color:NSColor?)
  14. }
  15. public class KMPresentationTopViewController: NSViewController, KMDrawViewDelegate {
  16. @IBOutlet var pageNubLabel: NSTextField!
  17. @IBOutlet var backButton: NSButton!
  18. @IBOutlet var forwardButton: NSButton!
  19. @IBOutlet var undoButton: NSButton!
  20. @IBOutlet var exitButton: NSButton!
  21. @IBOutlet var deleteButton: NSButton!
  22. @IBOutlet var typeButton: NSButton!
  23. public weak var pdfView: CPDFListView?
  24. public var isSelectionPre: Bool = true
  25. weak var delegate: KMPresentationTopViewControllerDelegate?
  26. @IBOutlet private weak var textColorPickerView: KMColorPickerView!
  27. deinit {
  28. textColorPickerView.target = nil
  29. textColorPickerView.action = nil
  30. NSColorPanel.shared.setTarget(nil)
  31. NSColorPanel.shared.setAction(nil)
  32. }
  33. public override func viewDidLoad() {
  34. super.viewDidLoad()
  35. pageNubLabel.stringValue = "\((pdfView?.currentPageIndex ?? 0) + 1)/\(pdfView?.document.pageCount ?? 0)"
  36. NotificationCenter.default.addObserver(self, selector: #selector(pageChangedNotification(_:)), name: NSNotification.Name.CPDFViewPageChanged, object: self.pdfView)
  37. textColorPickerView.isCallColorPanelAction = true
  38. textColorPickerView.noContentString = true
  39. textColorPickerView.annotationType = .inkColors
  40. textColorPickerView.isFillColor = true
  41. textColorPickerView.isOrderWindowAbove = true
  42. self.view.wantsLayer = true
  43. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  44. updateViewColor()
  45. if(isSelectionPre == false) {
  46. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  47. } else {
  48. typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
  49. }
  50. updatePageState()
  51. pdfView?.presentationDrawView.delegate = self
  52. undoButton.isEnabled = false
  53. (self.view as? KMHoverView)?.hoverAction = { view, act in
  54. if act == .enter {
  55. NSCursor.arrow.set()
  56. } else if act == .exit {
  57. } else {
  58. NSCursor.arrow.set()
  59. }
  60. }
  61. }
  62. // MARK: - Private
  63. private func updatePageState() {
  64. if(pdfView?.canGoToNextPage() == true) {
  65. backButton.isEnabled = true
  66. } else {
  67. backButton.isEnabled = false
  68. }
  69. if(pdfView?.canGoToPreviousPage() == true) {
  70. forwardButton.isEnabled = true
  71. } else {
  72. forwardButton.isEnabled = false
  73. }
  74. }
  75. private func updateViewColor() {
  76. let isDark = KMAppearance.isDarkMode()
  77. if isDark {
  78. self.view.layer?.backgroundColor = NSColor(hex: "#464646").cgColor
  79. } else {
  80. self.view.layer?.backgroundColor = NSColor.white.cgColor
  81. }
  82. }
  83. // MARK: - Action
  84. @IBAction func buttonItemClicked_BackPage(_ sender: NSButton) {
  85. updatePageState()
  86. if(pdfView?.canGoToNextPage() == true) {
  87. pdfView?.goToNextPage(sender)
  88. }
  89. }
  90. @IBAction func buttonItemClicked_ForwardPage(_ sender: NSButton) {
  91. updatePageState()
  92. if(pdfView?.canGoToPreviousPage() == true) {
  93. pdfView?.goToPreviousPage(sender)
  94. }
  95. }
  96. @IBAction func buttonItemClicked_Undo(_ sender: NSButton) {
  97. delegate?.presentationTopViewUndo?(self, withButton: sender)
  98. }
  99. @IBAction func buttonItemClicked_Exit(_ sender: NSButton) {
  100. delegate?.presentationTopViewExit?(self, withButton: sender)
  101. }
  102. @IBAction func buttonItemClicked_Delete(_ sender: NSButton) {
  103. delegate?.presentationTopViewClear?(self, withButton: sender)
  104. }
  105. @IBAction func buttonItemClicked_Type(_ sender: NSButton) {
  106. if(isSelectionPre == true) {
  107. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  108. isSelectionPre = false
  109. } else {
  110. typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
  111. isSelectionPre = true
  112. }
  113. undoButton.isEnabled = false
  114. textColorPickerView.decSelectionColorView()
  115. delegate?.presentationTopViewType?(self, withButton: sender, isSeletion: isSelectionPre)
  116. }
  117. @IBAction func drawColorPickerViewAction(_ sender: Any) {
  118. let borderColor = textColorPickerView.color
  119. if(isSelectionPre == true) {
  120. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  121. isSelectionPre = false
  122. }
  123. undoButton.isEnabled = false
  124. delegate?.presentationTopViewDrawColor?(self, withView: textColorPickerView, color: borderColor)
  125. }
  126. // MARK: - Notification
  127. @objc func pageChangedNotification(_ notification: Notification) {
  128. guard let pdfview = notification.object as? CPDFView else {
  129. return
  130. }
  131. if pdfview.document == self.pdfView?.document {
  132. pageNubLabel.stringValue = "\((pdfView?.currentPageIndex ?? 0) + 1)/\(pdfView?.document.pageCount ?? 0)"
  133. updatePageState()
  134. let presentationDrawView = pdfView?.presentationDrawView
  135. presentationDrawView?.clear() //
  136. presentationDrawView?.resetUndoManager()
  137. }
  138. }
  139. @objc func themeChanged(notification: Notification) {
  140. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  141. self.updateViewColor()
  142. }
  143. }
  144. // MARK: - KMDrawView
  145. public func drawView(_ drawView: KMDrawView!, didUpdateUndoStatus enable: Bool) {
  146. undoButton.isEnabled = enable
  147. }
  148. public func drawView(_ drawView: KMDrawView!, didDrawEnd point: CGPoint) {
  149. undoButton.isEnabled = ((pdfView?.presentationDrawView.canUndo()) == true)
  150. }
  151. }