CustomStampRightToolbar.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // CustomStampRightToolbar.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/6/20.
  6. //
  7. import Cocoa
  8. protocol CSRightToolbarDelegate {
  9. func csRightToolbar(_ toolbar: CustomStampRightToolbar, didAnnotationUpdate annotation: CPDFAnnotation?)
  10. func csRightToolbar(didPDFClip toolbar: CustomStampRightToolbar)
  11. func csRightToolbar(_ toolbar: CustomStampRightToolbar, didDeleteAnnotation annotation: CPDFAnnotation?)
  12. func csRightToolbar(_ toolbar: CustomStampRightToolbar, didAnnotationClear annotation: CPDFAnnotation?)
  13. }
  14. class CustomStampRightToolbar: NSView, NibLoadable {
  15. @IBOutlet var contendView: NSView!
  16. @IBOutlet var topContendView: NSView!
  17. @IBOutlet var infoContendView: NSView!
  18. @IBOutlet var titleLabel: NSTextField!
  19. @IBOutlet var clipBox: NSBox!
  20. @IBOutlet var clipButton: NSButton!
  21. @IBOutlet var deleteBox: NSBox!
  22. @IBOutlet var deleteButton: NSButton!
  23. @IBOutlet var clearBox: NSBox!
  24. @IBOutlet var clearButton: NSButton!
  25. @IBOutlet var emptyContendView: NSView!
  26. @IBOutlet var emptyLabel: NSTextField!
  27. @IBOutlet var emptyInfoView: NSView!
  28. @IBOutlet var emptyInfoLabel: NSTextField!
  29. var pdfView: CPDFListView!
  30. var _currentAnnotation: CPDFAnnotation?
  31. var delegate: CSRightToolbarDelegate?
  32. var textPropertyView: CSTextPropertyView!
  33. var imagePropertyView: CSImagePropertyView!
  34. override func draw(_ dirtyRect: NSRect) {
  35. super.draw(dirtyRect)
  36. // Drawing code here.
  37. }
  38. override func awakeFromNib() {
  39. super.awakeFromNib()
  40. self.infoContendView.wantsLayer = true
  41. self.textPropertyView = CSTextPropertyView.createFromNib()
  42. self.textPropertyView.frame = self.infoContendView.bounds
  43. self.textPropertyView.autoresizingMask = [.width, .height]
  44. self.infoContendView.addSubview(self.textPropertyView)
  45. self.imagePropertyView = CSImagePropertyView.createFromNib()
  46. self.imagePropertyView.frame = self.infoContendView.bounds
  47. self.imagePropertyView.autoresizingMask = [.width, .height]
  48. self.infoContendView.addSubview(self.imagePropertyView)
  49. self.imagePropertyView.updateHandle = {[weak self] annotation in
  50. }
  51. self.configUI()
  52. self.reloadData()
  53. self.updateViewColor()
  54. }
  55. //MARK: Setter
  56. var currentAnnotation: CPDFAnnotation? {
  57. get {
  58. return _currentAnnotation
  59. }
  60. set {
  61. _currentAnnotation = newValue
  62. self.reloadData()
  63. }
  64. }
  65. //MARK: ConfigUI
  66. func configUI() {
  67. self.contendView.wantsLayer = true
  68. self.titleLabel.stringValue = NSLocalizedString("Tools", comment: "")
  69. self.emptyLabel.stringValue = NSLocalizedString("Properties", comment: "")
  70. self.emptyInfoLabel.stringValue = NSLocalizedString("No Properties", comment: "")
  71. self.clipButton.title = NSLocalizedString("Crop Border", comment: "")
  72. self.deleteButton.title = NSLocalizedString("Delete", comment: "")
  73. self.clearButton.title = NSLocalizedString("Clear", comment: "")
  74. self.emptyInfoView.wantsLayer = true
  75. self.emptyInfoView.layer?.cornerRadius = 6
  76. self.emptyInfoView.layer?.borderWidth = 1
  77. self.emptyInfoView.layer?.masksToBounds = true
  78. for box in [self.clipBox, self.deleteBox, self.clearBox] {
  79. box?.borderWidth = 1
  80. box?.cornerRadius = 4
  81. }
  82. }
  83. func updateViewColor() {
  84. if KMAppearance.isDarkMode() {
  85. self.emptyInfoView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.05).cgColor
  86. self.emptyInfoView.layer?.borderColor = NSColor.white.withAlphaComponent(0.05).cgColor
  87. for box in [self.clipBox, self.deleteBox, self.clearBox] {
  88. box?.fillColor = NSColor(red: 110/255, green: 109/255, blue: 112/255, alpha: 1)
  89. box?.borderColor = NSColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.12)
  90. }
  91. self.emptyInfoLabel.textColor = KMAppearance.Layout.h1Color()
  92. self.titleLabel.textColor = NSColor(red: 200/255, green: 201/255, blue: 204/255, alpha: 1)
  93. self.emptyLabel.textColor = NSColor(red: 200/255, green: 201/255, blue: 204/255, alpha: 1)
  94. } else {
  95. self.emptyInfoView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.05).cgColor
  96. self.emptyInfoView.layer?.borderColor = NSColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1).cgColor
  97. for box in [self.clipBox, self.deleteBox, self.clearBox] {
  98. box?.fillColor = NSColor.white
  99. box?.borderColor = NSColor(red: 223/255, green: 225/255, blue: 229/255, alpha: 1)
  100. }
  101. self.emptyInfoLabel.textColor = NSColor.black.withAlphaComponent(0.85)
  102. self.titleLabel.textColor = NSColor.black.withAlphaComponent(0.85)
  103. self.emptyLabel.textColor = NSColor.black.withAlphaComponent(0.85)
  104. }
  105. }
  106. //MARK:
  107. func reloadData() {
  108. self.imagePropertyView.pdfView = self.pdfView
  109. self.textPropertyView.pdfView = self.pdfView
  110. self.emptyContendView.isHidden = false
  111. self.imagePropertyView.isHidden = true
  112. self.textPropertyView.isHidden = true
  113. self.updateButtonState()
  114. guard let annotation = self.currentAnnotation else {
  115. return
  116. }
  117. self.emptyContendView.isHidden = true
  118. if self.currentAnnotation is CPDFFreeTextAnnotation {
  119. self.textPropertyView.isHidden = false
  120. self.textPropertyView.currentAnnotation = annotation
  121. } else if self.currentAnnotation is CPDFStampAnnotation {
  122. self.imagePropertyView.isHidden = false
  123. self.imagePropertyView.currentAnnotation = annotation
  124. }
  125. }
  126. func updateButtonState() {
  127. var enableColor = NSColor.white
  128. var disableColor = NSColor(red: 237/255, green: 238/255, blue: 240/255, alpha: 1)
  129. var enableTextColor = NSColor(red: 37/255, green: 38/255, blue: 41/255, alpha: 1)
  130. var disableTextColor = NSColor(red: 177/255, green: 179/255, blue: 184/255, alpha: 1)
  131. if KMAppearance.isDarkMode() {
  132. enableColor = NSColor(red: 110/255, green: 109/255, blue: 112/255, alpha: 1)
  133. disableColor = NSColor(red: 110/255, green: 109/255, blue: 112/255, alpha: 1)
  134. enableTextColor = NSColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.85)
  135. disableTextColor = NSColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.25)
  136. }
  137. let page = self.pdfView?.document?.page(at: 0)
  138. if page?.annotations.isEmpty == false {
  139. self.clearButton.isEnabled = true
  140. self.clearButton.setTitleColor(enableTextColor)
  141. self.clearBox.fillColor = enableColor
  142. self.clipButton.isEnabled = true
  143. self.clipButton.setTitleColor(enableTextColor)
  144. self.clipBox.fillColor = enableColor
  145. } else {
  146. self.clearButton.isEnabled = false
  147. self.clearButton.setTitleColor(disableTextColor)
  148. self.clearBox.fillColor = disableColor
  149. self.clipButton.isEnabled = false
  150. self.clipButton.setTitleColor(disableTextColor)
  151. self.clipBox.fillColor = disableColor
  152. }
  153. self.deleteButton.isEnabled = false
  154. self.deleteButton.setTitleColor(disableTextColor)
  155. self.deleteBox.fillColor = disableColor
  156. guard self.currentAnnotation != nil else {
  157. return
  158. }
  159. self.deleteButton.isEnabled = true
  160. self.deleteButton.setTitleColor(enableTextColor)
  161. self.deleteBox.fillColor = enableColor
  162. }
  163. //MARK: IBAction
  164. @IBAction func clipAction(_ sender: Any) {
  165. if self.delegate != nil {
  166. self.delegate?.csRightToolbar(didPDFClip: self)
  167. }
  168. }
  169. @IBAction func deleteAction(_ sender: Any) {
  170. if self.delegate != nil {
  171. self.delegate?.csRightToolbar(self, didDeleteAnnotation: self.currentAnnotation)
  172. }
  173. }
  174. @IBAction func clearAction(_ sender: Any) {
  175. if self.delegate != nil {
  176. self.delegate?.csRightToolbar(self, didAnnotationClear: self.currentAnnotation)
  177. }
  178. }
  179. }