// // CustomStampRightToolbar.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/6/20. // import Cocoa protocol CSRightToolbarDelegate { func csRightToolbar(_ toolbar: CustomStampRightToolbar, didAnnotationUpdate annotation: CPDFAnnotation?) func csRightToolbar(didPDFClip toolbar: CustomStampRightToolbar) func csRightToolbar(_ toolbar: CustomStampRightToolbar, didDeleteAnnotation annotation: CPDFAnnotation?) func csRightToolbar(_ toolbar: CustomStampRightToolbar, didAnnotationClear annotation: CPDFAnnotation?) } class CustomStampRightToolbar: NSView, NibLoadable { @IBOutlet var contendView: NSView! @IBOutlet var topContendView: NSView! @IBOutlet var infoContendView: NSView! @IBOutlet var titleLabel: NSTextField! @IBOutlet var clipBox: NSBox! @IBOutlet var clipButton: NSButton! @IBOutlet var deleteBox: NSBox! @IBOutlet var deleteButton: NSButton! @IBOutlet var clearBox: NSBox! @IBOutlet var clearButton: NSButton! @IBOutlet var emptyContendView: NSView! @IBOutlet var emptyLabel: NSTextField! @IBOutlet var emptyInfoView: NSView! @IBOutlet var emptyInfoLabel: NSTextField! var pdfView: CPDFListView! var _currentAnnotation: CPDFAnnotation? var delegate: CSRightToolbarDelegate? var textPropertyView: CSTextPropertyView! var imagePropertyView: CSImagePropertyView! override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } override func awakeFromNib() { super.awakeFromNib() self.infoContendView.wantsLayer = true self.textPropertyView = CSTextPropertyView.createFromNib() self.textPropertyView.frame = self.infoContendView.bounds self.textPropertyView.autoresizingMask = [.width, .height] self.infoContendView.addSubview(self.textPropertyView) self.imagePropertyView = CSImagePropertyView.createFromNib() self.imagePropertyView.frame = self.infoContendView.bounds self.imagePropertyView.autoresizingMask = [.width, .height] self.infoContendView.addSubview(self.imagePropertyView) self.imagePropertyView.updateHandle = {[weak self] annotation in } self.configUI() self.reloadData() self.updateViewColor() } //MARK: Setter var currentAnnotation: CPDFAnnotation? { get { return _currentAnnotation } set { _currentAnnotation = newValue self.reloadData() } } //MARK: ConfigUI func configUI() { self.contendView.wantsLayer = true self.titleLabel.stringValue = NSLocalizedString("Tools", comment: "") self.emptyLabel.stringValue = NSLocalizedString("Properties", comment: "") self.emptyInfoLabel.stringValue = NSLocalizedString("No Properties", comment: "") self.clipButton.title = NSLocalizedString("Crop Border", comment: "") self.deleteButton.title = NSLocalizedString("Delete", comment: "") self.clearButton.title = NSLocalizedString("Clear", comment: "") self.emptyInfoView.wantsLayer = true self.emptyInfoView.layer?.cornerRadius = 6 self.emptyInfoView.layer?.borderWidth = 1 self.emptyInfoView.layer?.masksToBounds = true for box in [self.clipBox, self.deleteBox, self.clearBox] { box?.borderWidth = 1 box?.cornerRadius = 4 } } func updateViewColor() { if KMAppearance.isDarkMode() { self.emptyInfoView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.05).cgColor self.emptyInfoView.layer?.borderColor = NSColor.white.withAlphaComponent(0.05).cgColor for box in [self.clipBox, self.deleteBox, self.clearBox] { box?.fillColor = NSColor(red: 110/255, green: 109/255, blue: 112/255, alpha: 1) box?.borderColor = NSColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.12) } self.emptyInfoLabel.textColor = KMAppearance.Layout.h1Color() self.titleLabel.textColor = NSColor(red: 200/255, green: 201/255, blue: 204/255, alpha: 1) self.emptyLabel.textColor = NSColor(red: 200/255, green: 201/255, blue: 204/255, alpha: 1) } else { self.emptyInfoView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.05).cgColor self.emptyInfoView.layer?.borderColor = NSColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1).cgColor for box in [self.clipBox, self.deleteBox, self.clearBox] { box?.fillColor = NSColor.white box?.borderColor = NSColor(red: 223/255, green: 225/255, blue: 229/255, alpha: 1) } self.emptyInfoLabel.textColor = NSColor.black.withAlphaComponent(0.85) self.titleLabel.textColor = NSColor.black.withAlphaComponent(0.85) self.emptyLabel.textColor = NSColor.black.withAlphaComponent(0.85) } } //MARK: func reloadData() { self.imagePropertyView.pdfView = self.pdfView self.textPropertyView.pdfView = self.pdfView self.emptyContendView.isHidden = false self.imagePropertyView.isHidden = true self.textPropertyView.isHidden = true self.updateButtonState() guard let annotation = self.currentAnnotation else { return } self.emptyContendView.isHidden = true if self.currentAnnotation is CPDFFreeTextAnnotation { self.textPropertyView.isHidden = false self.textPropertyView.currentAnnotation = annotation } else if self.currentAnnotation is CPDFStampAnnotation { self.imagePropertyView.isHidden = false self.imagePropertyView.currentAnnotation = annotation } } func updateButtonState() { var enableColor = NSColor.white var disableColor = NSColor(red: 237/255, green: 238/255, blue: 240/255, alpha: 1) var enableTextColor = NSColor(red: 37/255, green: 38/255, blue: 41/255, alpha: 1) var disableTextColor = NSColor(red: 177/255, green: 179/255, blue: 184/255, alpha: 1) if KMAppearance.isDarkMode() { enableColor = NSColor(red: 110/255, green: 109/255, blue: 112/255, alpha: 1) disableColor = NSColor(red: 110/255, green: 109/255, blue: 112/255, alpha: 1) enableTextColor = NSColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.85) disableTextColor = NSColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.25) } let page = self.pdfView?.document?.page(at: 0) if page?.annotations.isEmpty == false { self.clearButton.isEnabled = true self.clearButton.setTitleColor(enableTextColor) self.clearBox.fillColor = enableColor self.clipButton.isEnabled = true self.clipButton.setTitleColor(enableTextColor) self.clipBox.fillColor = enableColor } else { self.clearButton.isEnabled = false self.clearButton.setTitleColor(disableTextColor) self.clearBox.fillColor = disableColor self.clipButton.isEnabled = false self.clipButton.setTitleColor(disableTextColor) self.clipBox.fillColor = disableColor } self.deleteButton.isEnabled = false self.deleteButton.setTitleColor(disableTextColor) self.deleteBox.fillColor = disableColor guard self.currentAnnotation != nil else { return } self.deleteButton.isEnabled = true self.deleteButton.setTitleColor(enableTextColor) self.deleteBox.fillColor = enableColor } //MARK: IBAction @IBAction func clipAction(_ sender: Any) { if self.delegate != nil { self.delegate?.csRightToolbar(didPDFClip: self) } } @IBAction func deleteAction(_ sender: Any) { if self.delegate != nil { self.delegate?.csRightToolbar(self, didDeleteAnnotation: self.currentAnnotation) } } @IBAction func clearAction(_ sender: Any) { if self.delegate != nil { self.delegate?.csRightToolbar(self, didAnnotationClear: self.currentAnnotation) } } }