KMPresentationTopViewController.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. self.view.appearance = NSAppearance(named: .aqua)
  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. textColorPickerView.isCallColorPanelAction = true
  39. textColorPickerView.noContentString = true
  40. textColorPickerView.annotationType = .inkColors
  41. textColorPickerView.isFillColor = true
  42. textColorPickerView.isOrderWindowAbove = true
  43. self.view.wantsLayer = true
  44. self.view.layer?.backgroundColor = .white
  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. // MARK: - Action
  76. @IBAction func buttonItemClicked_BackPage(_ sender: NSButton) {
  77. updatePageState()
  78. if(pdfView?.canGoToNextPage() == true) {
  79. pdfView?.goToNextPage(sender)
  80. }
  81. }
  82. @IBAction func buttonItemClicked_ForwardPage(_ sender: NSButton) {
  83. updatePageState()
  84. if(pdfView?.canGoToPreviousPage() == true) {
  85. pdfView?.goToPreviousPage(sender)
  86. }
  87. }
  88. @IBAction func buttonItemClicked_Undo(_ sender: NSButton) {
  89. delegate?.presentationTopViewUndo?(self, withButton: sender)
  90. }
  91. @IBAction func buttonItemClicked_Exit(_ sender: NSButton) {
  92. delegate?.presentationTopViewExit?(self, withButton: sender)
  93. }
  94. @IBAction func buttonItemClicked_Delete(_ sender: NSButton) {
  95. delegate?.presentationTopViewClear?(self, withButton: sender)
  96. }
  97. @IBAction func buttonItemClicked_Type(_ sender: NSButton) {
  98. if(isSelectionPre == true) {
  99. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  100. isSelectionPre = false
  101. } else {
  102. typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
  103. isSelectionPre = true
  104. }
  105. undoButton.isEnabled = false
  106. textColorPickerView.decSelectionColorView()
  107. delegate?.presentationTopViewType?(self, withButton: sender, isSeletion: isSelectionPre)
  108. }
  109. @IBAction func drawColorPickerViewAction(_ sender: Any) {
  110. let borderColor = textColorPickerView.color
  111. delegate?.presentationTopViewDrawColor?(self, withView: textColorPickerView, color: borderColor)
  112. }
  113. // MARK: - Notification
  114. @objc func pageChangedNotification(_ notification: Notification) {
  115. guard let pdfview = notification.object as? CPDFView else {
  116. return
  117. }
  118. if pdfview.document == self.pdfView?.document {
  119. pageNubLabel.stringValue = "\((pdfView?.currentPageIndex ?? 0) + 1)/\(pdfView?.document.pageCount ?? 0)"
  120. updatePageState()
  121. let presentationDrawView = pdfView?.presentationDrawView
  122. presentationDrawView?.resetUndoManager()
  123. presentationDrawView?.clear()
  124. }
  125. }
  126. // MARK: - KMDrawView
  127. public func drawView(_ drawView: KMDrawView!, didUpdateUndoStatus enable: Bool) {
  128. undoButton.isEnabled = enable
  129. }
  130. public func drawView(_ drawView: KMDrawView!, didDrawEnd point: CGPoint) {
  131. }
  132. }