123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- //
- // KMPresentationTopViewController.swift
- // PDF Reader Pro
- //
- // Created by 丁林圭 on 2024/9/23.
- //
- import Cocoa
- @objc public protocol KMPresentationTopViewControllerDelegate: AnyObject {
- @objc optional func presentationTopViewExit(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
-
- @objc optional func presentationTopViewUndo(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
- @objc optional func presentationTopViewClear(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton)
-
- @objc optional func presentationTopViewType(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton,isSeletion:Bool)
-
- @objc optional func presentationTopViewDrawColor(_ presentationTopViewController: KMPresentationTopViewController, withView: NSView,color:NSColor?)
- }
- public class KMPresentationTopViewController: NSViewController, KMDrawViewDelegate {
-
- @IBOutlet var pageNubLabel: NSTextField!
- @IBOutlet var backButton: NSButton!
- @IBOutlet var forwardButton: NSButton!
- @IBOutlet var undoButton: NSButton!
- @IBOutlet var exitButton: NSButton!
- @IBOutlet var deleteButton: NSButton!
- @IBOutlet var typeButton: NSButton!
-
- public weak var pdfView: CPDFListView?
-
- public var isSelectionPre: Bool = true
- weak var delegate: KMPresentationTopViewControllerDelegate?
- @IBOutlet private weak var textColorPickerView: KMColorPickerView!
- deinit {
- textColorPickerView.target = nil
- textColorPickerView.action = nil
-
- NSColorPanel.shared.setTarget(nil)
- NSColorPanel.shared.setAction(nil)
- }
- public override func viewDidLoad() {
- super.viewDidLoad()
- pageNubLabel.stringValue = "\((pdfView?.currentPageIndex ?? 0) + 1)/\(pdfView?.document.pageCount ?? 0)"
-
- NotificationCenter.default.addObserver(self, selector: #selector(pageChangedNotification(_:)), name: NSNotification.Name.CPDFViewPageChanged, object: self.pdfView)
-
- textColorPickerView.isCallColorPanelAction = true
- textColorPickerView.noContentString = true
- textColorPickerView.annotationType = .inkColors
- textColorPickerView.isFillColor = true
- textColorPickerView.isOrderWindowAbove = true
-
- self.view.wantsLayer = true
- DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
- updateViewColor()
-
- if(isSelectionPre == false) {
- typeButton.image = NSImage(named: "KMPresentationImageNameType")
- } else {
- typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
- }
- updatePageState()
-
- pdfView?.presentationDrawView.delegate = self
- undoButton.isEnabled = false
- (self.view as? KMHoverView)?.hoverAction = { view, act in
- if act == .enter {
- NSCursor.arrow.set()
- } else if act == .exit {
-
- } else {
- NSCursor.arrow.set()
- }
- }
- }
-
- // MARK: - Private
- private func updatePageState() {
- if(pdfView?.canGoToNextPage() == true) {
- backButton.isEnabled = true
- } else {
- backButton.isEnabled = false
- }
-
- if(pdfView?.canGoToPreviousPage() == true) {
- forwardButton.isEnabled = true
- } else {
- forwardButton.isEnabled = false
- }
-
- }
-
- private func updateViewColor() {
- let isDark = KMAppearance.isDarkMode()
- if isDark {
- self.view.layer?.backgroundColor = NSColor(hex: "#464646").cgColor
- } else {
- self.view.layer?.backgroundColor = NSColor.white.cgColor
- }
- }
-
- // MARK: - Action
- @IBAction func buttonItemClicked_BackPage(_ sender: NSButton) {
- updatePageState()
- if(pdfView?.canGoToNextPage() == true) {
- pdfView?.goToNextPage(sender)
- }
- }
-
- @IBAction func buttonItemClicked_ForwardPage(_ sender: NSButton) {
- updatePageState()
- if(pdfView?.canGoToPreviousPage() == true) {
- pdfView?.goToPreviousPage(sender)
- }
- }
-
- @IBAction func buttonItemClicked_Undo(_ sender: NSButton) {
- delegate?.presentationTopViewUndo?(self, withButton: sender)
- }
-
- @IBAction func buttonItemClicked_Exit(_ sender: NSButton) {
- delegate?.presentationTopViewExit?(self, withButton: sender)
- }
-
- @IBAction func buttonItemClicked_Delete(_ sender: NSButton) {
- delegate?.presentationTopViewClear?(self, withButton: sender)
- }
-
- @IBAction func buttonItemClicked_Type(_ sender: NSButton) {
- if(isSelectionPre == true) {
- typeButton.image = NSImage(named: "KMPresentationImageNameType")
- isSelectionPre = false
- } else {
- typeButton.image = NSImage(named: "KMPresentationImageNameTypeSeletion")
- isSelectionPre = true
- }
- undoButton.isEnabled = false
- textColorPickerView.decSelectionColorView()
- delegate?.presentationTopViewType?(self, withButton: sender, isSeletion: isSelectionPre)
- }
-
- @IBAction func drawColorPickerViewAction(_ sender: Any) {
- let borderColor = textColorPickerView.color
- if(isSelectionPre == true) {
- typeButton.image = NSImage(named: "KMPresentationImageNameType")
- isSelectionPre = false
- }
- undoButton.isEnabled = false
-
- delegate?.presentationTopViewDrawColor?(self, withView: textColorPickerView, color: borderColor)
- }
-
- // MARK: - Notification
- @objc func pageChangedNotification(_ notification: Notification) {
- guard let pdfview = notification.object as? CPDFView else {
- return
- }
- if pdfview.document == self.pdfView?.document {
- pageNubLabel.stringValue = "\((pdfView?.currentPageIndex ?? 0) + 1)/\(pdfView?.document.pageCount ?? 0)"
- updatePageState()
- let presentationDrawView = pdfView?.presentationDrawView
- presentationDrawView?.clear() //
- presentationDrawView?.resetUndoManager()
- }
- }
-
- @objc func themeChanged(notification: Notification) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
- self.updateViewColor()
- }
- }
-
- // MARK: - KMDrawView
- public func drawView(_ drawView: KMDrawView!, didUpdateUndoStatus enable: Bool) {
- undoButton.isEnabled = enable
- }
-
- public func drawView(_ drawView: KMDrawView!, didDrawEnd point: CGPoint) {
- undoButton.isEnabled = ((pdfView?.presentationDrawView.canUndo()) == true)
- }
- }
|