KMRightSideController.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // KMRightSideController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/12.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMRightSideController: NSViewController {
  10. @IBOutlet var contendView: NSView!
  11. @IBOutlet var contendLeftDivider: ComponentDivider!
  12. @IBOutlet var titleLabel: NSTextField!
  13. @IBOutlet var infoContendView: NSView!
  14. var annotations: [CPDFAnnotation] = [] {
  15. didSet {
  16. reloadData()
  17. }
  18. }
  19. var pdfView: CPDFListView?
  20. var subToolMode: KMPDFSubToolMode = .None {
  21. //二级工具栏
  22. didSet {
  23. updateUI()
  24. }
  25. }
  26. var contentViewController: NSViewController?
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. // Do view setup here.
  30. setupUI()
  31. updateUI()
  32. }
  33. func setupUI() {
  34. contendView.wantsLayer = true
  35. contendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  36. contendLeftDivider.properties = ComponentDividerProperty(type: .vertical)
  37. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  38. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  39. }
  40. func updateTitleLabel() {
  41. if subToolMode == .Edit_text {
  42. titleLabel.stringValue = KMLocalizedString("Text")
  43. } else if subToolMode == .Edit_Image {
  44. titleLabel.stringValue = KMLocalizedString("Image")
  45. } else if subToolMode == .Edit_Link {
  46. titleLabel.stringValue = KMLocalizedString("Link")
  47. } else if subToolMode == .Edit_Crop {
  48. titleLabel.stringValue = KMLocalizedString("Crop")
  49. }
  50. }
  51. func updateUI() {
  52. updateTitleLabel()
  53. contentViewController?.view.removeFromSuperview()
  54. contentViewController = nil
  55. if subToolMode == .Edit_Link {
  56. let controller = KMLinkViewController.init()
  57. controller.view.frame = infoContendView.bounds
  58. controller.view.autoresizingMask = [.width, .height]
  59. infoContendView.addSubview(controller.view)
  60. contentViewController = controller
  61. controller.pdfView = self.pdfView
  62. }
  63. }
  64. func reloadData() {
  65. if subToolMode == .Edit_Link && contentViewController is KMLinkViewController {
  66. if (contentViewController as! KMLinkViewController).pdfView != self.pdfView {
  67. (contentViewController as! KMLinkViewController).pdfView = self.pdfView
  68. }
  69. var linkAnnotations: [CPDFLinkAnnotation] = []
  70. for annotation in self.annotations {
  71. if annotation is CPDFLinkAnnotation {
  72. linkAnnotations.append(annotation as! CPDFLinkAnnotation)
  73. }
  74. }
  75. (contentViewController as! KMLinkViewController).annotations = linkAnnotations
  76. (contentViewController as! KMLinkViewController).reloadData()
  77. }
  78. }
  79. public func reloadDataWithPDFView(pdfView: CPDFListView) {
  80. self.pdfView = pdfView
  81. var selectedAnnotation : CPDFAnnotation? = nil
  82. var activeAnnotations : [CPDFAnnotation] = []
  83. if pdfView.activeAnnotations != nil && pdfView.activeAnnotations.count > 0 {
  84. selectedAnnotation = pdfView.activeAnnotations.firstObject as? CPDFAnnotation
  85. for annotation in pdfView.activeAnnotations {
  86. activeAnnotations.append(annotation as! CPDFAnnotation)
  87. }
  88. }
  89. var isSameAnnotation: Bool = true
  90. for tAnnotation in activeAnnotations {
  91. if tAnnotation.className != selectedAnnotation?.className {
  92. isSameAnnotation = false
  93. break
  94. }
  95. }
  96. self.annotations = activeAnnotations
  97. // if !isSameAnnotation {
  98. // self.annotationProperties.annotations = []
  99. // self.annotationProperties.isEmptyAnnotation = true
  100. // return
  101. // } else if selectedAnnotation is CPDFTextWidgetAnnotation || selectedAnnotation is CPDFChoiceWidgetAnnotation || selectedAnnotation is CPDFButtonWidgetAnnotation {
  102. // if pdfView.toolMode != .formToolMode {
  103. // self.annotationProperties.annotations = []
  104. // self.annotationProperties.isEmptyAnnotation = true
  105. // return
  106. // }
  107. // } else if (selectedAnnotation is KMAnnotationFromSignature) && (activeAnnotations.count > 1) {
  108. // self.annotationProperties.annotations = []
  109. // self.annotationProperties.isEmptyAnnotation = true
  110. // return
  111. // }
  112. //
  113. // let annotationType = pdfView.annotationType
  114. // if KMAnnotationPropertiesViewController.height(with: selectedAnnotation) > 0 {
  115. // if activeAnnotations.count > 0{
  116. // if self.subViewType != .AnnotationProperts {
  117. // self.subViewType = .AnnotationProperts
  118. // }
  119. // }
  120. // self.annotationProperties.annotations = activeAnnotations
  121. // self.isHidden = !(activeAnnotations.count > 0)
  122. // } else if self.listView.toolMode == .noteToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  123. // self.isHidden = false
  124. // if pdfView.activeAnnotation is CPDFLinkAnnotation {
  125. // if activeAnnotations.count > 0 {
  126. // self.annotationProperties.annotations = activeAnnotations
  127. // } else {
  128. // self.annotationProperties.annotations = []
  129. // self.annotationProperties.annotationMode = annotationType
  130. // }
  131. // } else {
  132. // self.annotationProperties.annotations = []
  133. // self.annotationProperties.annotationMode = annotationType
  134. // }
  135. // } else if listView.toolMode == .formToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  136. // self.isHidden = false
  137. // self.annotationProperties.annotations = []
  138. // self.annotationProperties.annotationMode = annotationType
  139. // } else if listView.toolMode == .selfSignMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  140. // self.isHidden = false
  141. // self.annotationProperties.annotations = []
  142. // self.annotationProperties.kEventTag = self.kEventTag
  143. // self.kEventTag = 0
  144. // self.annotationProperties.annotationMode = annotationType
  145. // } else if (annotationType == .line || annotationType == .polyLine || annotationType == .polyGon || (annotationType == .square && self.listView.toolMode != .noteToolMode)) && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  146. // self.isHidden = false
  147. // self.annotationProperties.annotations = []
  148. // self.annotationProperties.measureMode = annotationType
  149. // } else {
  150. // self.isHidden = true
  151. // }
  152. //
  153. // if (self.isHidden) {
  154. // self.annotationProperties.isEmptyAnnotation = self.isHidden
  155. // } else {
  156. // var isShowEmpty = self.isHidden;
  157. // if annotationType == .link && activeAnnotations.count == 0 {
  158. // self.isHidden = true
  159. // } else if (annotationType == .stamp || annotationType == .signSignature) && isShowEmpty {
  160. // let continuousAddStamp = UserDefaults.standard.bool(forKey: "KMContinuousAdditionStamp")
  161. // if continuousAddStamp {
  162. // isShowEmpty = false
  163. // self.annotationProperties.isContinuousAddStamp = true
  164. //
  165. // UserDefaults.standard.setValue(false, forKey: "KMContinuousAdditionStamp")
  166. // UserDefaults.standard.synchronize()
  167. // }
  168. // } else {
  169. // if isShowEmpty {
  170. //
  171. // } else {
  172. // if _subViewType == RightSubViewType.AnnotationProperts {
  173. // self.contextBox.contentView = self.annotationProperties.view
  174. // }
  175. // }
  176. // }
  177. // self.annotationProperties.isEmptyAnnotation = isShowEmpty
  178. // }
  179. }
  180. }