123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- //
- // 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)
- }
- }
- }
|