KMPresentationTopViewController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. self.view.layer?.backgroundColor = .white
  44. if(isSelectionPre == false) {
  45. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  46. } else {
  47. typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
  48. }
  49. updatePageState()
  50. pdfView?.presentationDrawView.delegate = self
  51. undoButton.isEnabled = false
  52. }
  53. // MARK: - Private
  54. private func updatePageState() {
  55. if(pdfView?.canGoToNextPage() == true) {
  56. backButton.isEnabled = true
  57. } else {
  58. backButton.isEnabled = false
  59. }
  60. if(pdfView?.canGoToPreviousPage() == true) {
  61. forwardButton.isEnabled = true
  62. } else {
  63. forwardButton.isEnabled = false
  64. }
  65. }
  66. // MARK: - Action
  67. @IBAction func buttonItemClicked_BackPage(_ sender: NSButton) {
  68. updatePageState()
  69. if(pdfView?.canGoToNextPage() == true) {
  70. pdfView?.goToNextPage(sender)
  71. let presentationDrawView = pdfView?.presentationDrawView
  72. presentationDrawView?.clear()
  73. }
  74. }
  75. @IBAction func buttonItemClicked_ForwardPage(_ sender: NSButton) {
  76. updatePageState()
  77. if(pdfView?.canGoToPreviousPage() == true) {
  78. pdfView?.goToPreviousPage(sender)
  79. let presentationDrawView = pdfView?.presentationDrawView
  80. presentationDrawView?.clear()
  81. }
  82. }
  83. @IBAction func buttonItemClicked_Undo(_ sender: NSButton) {
  84. delegate?.presentationTopViewUndo?(self, withButton: sender)
  85. }
  86. @IBAction func buttonItemClicked_Exit(_ sender: NSButton) {
  87. delegate?.presentationTopViewExit?(self, withButton: sender)
  88. }
  89. @IBAction func buttonItemClicked_Delete(_ sender: NSButton) {
  90. delegate?.presentationTopViewClear?(self, withButton: sender)
  91. }
  92. @IBAction func buttonItemClicked_Type(_ sender: NSButton) {
  93. if(isSelectionPre == true) {
  94. typeButton.image = NSImage(named: "KMPresentationImageNameType")
  95. isSelectionPre = false
  96. } else {
  97. typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
  98. isSelectionPre = true
  99. }
  100. textColorPickerView.decSelectionColorView()
  101. delegate?.presentationTopViewType?(self, withButton: sender, isSeletion: isSelectionPre)
  102. }
  103. @IBAction func drawColorPickerViewAction(_ sender: Any) {
  104. let borderColor = textColorPickerView.color
  105. delegate?.presentationTopViewDrawColor?(self, withView: textColorPickerView, color: borderColor)
  106. }
  107. // MARK: - Notification
  108. @objc func pageChangedNotification(_ notification: Notification) {
  109. guard let pdfview = notification.object as? CPDFView else {
  110. return
  111. }
  112. if pdfview.document == self.pdfView?.document {
  113. pageNubLabel.stringValue = "\((pdfView?.currentPageIndex ?? 0) + 1)/\(pdfView?.document.pageCount ?? 0)"
  114. updatePageState()
  115. }
  116. }
  117. // MARK: - KMDrawView
  118. public func drawView(_ drawView: KMDrawView!, didUpdateUndoStatus enable: Bool) {
  119. undoButton.isEnabled = enable
  120. }
  121. public func drawView(_ drawView: KMDrawView!, didDrawEnd point: CGPoint) {
  122. }
  123. }