KMRightSideViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // KMRightSideViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2022/10/26.
  6. //
  7. import Cocoa
  8. enum RightSubViewType : Int {
  9. case None
  10. case CipherTextType
  11. case EditPDFAddText
  12. case EditPDFAddImage
  13. case AnnotationProperts
  14. case Bates
  15. case Headerfooter
  16. case Background
  17. case Watermark
  18. case ViewSettings
  19. }
  20. typealias KMRightSidePropertyDidChange = (_ model: AnyObject?) -> ()
  21. class KMRightSideViewController: NSViewController,CipherTextViewDelegate {
  22. @IBOutlet weak var contextBox: NSBox!
  23. var cipherTextView : CipherTextView!
  24. var eidtPDFTextProperty : KMEditPDFTextPropertyViewController!
  25. var eidtPDFImageProperty : KMEditImagePropertyViewController!
  26. var annotationProperties : KMAnnotationPropertiesViewController!
  27. var listView : CPDFListView!
  28. var _subViewType : RightSubViewType?
  29. var emptyVC : KMRightSideEmptyVC!
  30. var emptyVC_link: KMLinkAnnotationPropertyEmptyController?
  31. var _isHidden: Bool = false
  32. var model: AnyObject?
  33. var propertyDidChange: KMRightSidePropertyDidChange!
  34. weak var delegate: KMEditImagePropertyViewControllerDelegate?
  35. weak var mainController: KMMainViewController?
  36. var curcontroller: NSViewController?
  37. deinit {
  38. NotificationCenter.default.removeObserver(self)
  39. KMPrint("\(self.className) deinit.")
  40. }
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. // Do view setup here.
  44. emptyVC = KMRightSideEmptyVC.init(nibName: "KMRightSideEmptyVC", bundle: nil)
  45. contextBox.contentView = emptyVC.view
  46. self.emptyVC_link = KMLinkAnnotationPropertyEmptyController()
  47. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewActiveAnnotationDidChangeNotification(_:)), name: NSNotification.Name("KMPDFViewActiveAnnotationDidChangeNotification"), object: nil)
  48. NotificationCenter.default.addObserver(self, selector: #selector(signatureSelectedNotification(_:)), name: NSNotification.Name("KMSignatureListSelectNotification"), object: nil)
  49. }
  50. //MARK: Setter && Get
  51. var subViewType : RightSubViewType? {
  52. set {
  53. _subViewType = newValue
  54. if _subViewType == RightSubViewType.CipherTextType {
  55. self.initCipherTextView()
  56. } else if _subViewType == RightSubViewType.EditPDFAddText{
  57. self.initEditPDFTextPropertyViewController()
  58. } else if _subViewType == RightSubViewType.EditPDFAddImage{
  59. self.initEditPDFImagePropertyViewController()
  60. } else if _subViewType == RightSubViewType.AnnotationProperts {
  61. self.initAnnotationProperts()
  62. } else if _subViewType == .Bates {
  63. self.initBatesViewController()
  64. } else if subViewType == .Headerfooter {
  65. self.initHeaderFooterViewController()
  66. } else if subViewType == .Background {
  67. self.initBackgroundViewController()
  68. } else if subViewType == .Watermark {
  69. self.initWatermarkViewController()
  70. } else if subViewType == .ViewSettings {
  71. self.initViewSettingsViewController()
  72. }
  73. }
  74. get {
  75. return _subViewType
  76. }
  77. }
  78. var isHidden: Bool {
  79. get {
  80. return _isHidden
  81. }
  82. set {
  83. _isHidden = newValue
  84. if _isHidden {
  85. if (self.listView.annotationType == .link) {
  86. self.contextBox.contentView = self.emptyVC_link?.view
  87. } else {
  88. self.contextBox.contentView = emptyVC.view
  89. }
  90. }
  91. }
  92. }
  93. //MARK: InitSubViews
  94. func removeSubViews() {
  95. }
  96. public func reloadDataWithPDFView(pdfView:CPDFListView,isShow:Bool) {
  97. if self.annotationProperties == nil {
  98. self.subViewType = .AnnotationProperts
  99. }
  100. if(!isShow) {
  101. self.isHidden = true
  102. return
  103. }
  104. self.annotationProperties.pdfView = pdfView
  105. var selectedAnnotation : CPDFAnnotation? = nil
  106. var activeAnnotations : [CPDFAnnotation] = []
  107. if pdfView.activeAnnotations != nil && pdfView.activeAnnotations.count > 0 {
  108. selectedAnnotation = pdfView.activeAnnotations.firstObject as? CPDFAnnotation
  109. for annotation in pdfView.activeAnnotations {
  110. activeAnnotations.append(annotation as! CPDFAnnotation)
  111. }
  112. }
  113. var isSameAnnotation: Bool = true
  114. for tAnnotation in activeAnnotations {
  115. if tAnnotation.className != selectedAnnotation?.className {
  116. isSameAnnotation = false
  117. break
  118. }
  119. }
  120. if !isSameAnnotation {
  121. self.annotationProperties.annotations = []
  122. self.annotationProperties.isEmptyAnnotation = true
  123. return
  124. } else if selectedAnnotation is CPDFTextWidgetAnnotation || selectedAnnotation is CPDFChoiceWidgetAnnotation || selectedAnnotation is CPDFButtonWidgetAnnotation {
  125. if pdfView.toolMode != .formToolMode {
  126. self.annotationProperties.annotations = []
  127. self.annotationProperties.isEmptyAnnotation = true
  128. return
  129. }
  130. } else if (selectedAnnotation is KMAnnotationFromSignature) && (activeAnnotations.count > 1) {
  131. self.annotationProperties.annotations = []
  132. self.annotationProperties.isEmptyAnnotation = true
  133. return
  134. }
  135. let annotationType = pdfView.annotationType
  136. if KMAnnotationPropertiesViewController.height(with: selectedAnnotation) > 0 {
  137. if activeAnnotations.count > 0{
  138. if self.subViewType != .AnnotationProperts {
  139. self.subViewType = .AnnotationProperts
  140. }
  141. }
  142. self.annotationProperties.annotations = activeAnnotations
  143. self.isHidden = !(activeAnnotations.count > 0)
  144. } else if self.listView.toolMode == .noteToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  145. self.isHidden = false
  146. if pdfView.activeAnnotation is CPDFLinkAnnotation {
  147. if activeAnnotations.count > 0 {
  148. self.annotationProperties.annotations = activeAnnotations
  149. } else {
  150. self.annotationProperties.annotations = []
  151. self.annotationProperties.annotationMode = annotationType
  152. }
  153. } else {
  154. self.annotationProperties.annotations = []
  155. self.annotationProperties.annotationMode = annotationType
  156. }
  157. } else if listView.toolMode == .formToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  158. self.isHidden = false
  159. self.annotationProperties.annotations = []
  160. self.annotationProperties.annotationMode = annotationType
  161. } else if listView.toolMode == .selfSignMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  162. self.isHidden = false
  163. self.annotationProperties.annotations = []
  164. self.annotationProperties.annotationMode = annotationType
  165. } else {
  166. self.isHidden = true
  167. if pdfView.activeAnnotation is CPDFLinkAnnotation {
  168. if activeAnnotations.count > 0 {
  169. self.isHidden = false
  170. self.annotationProperties.annotations = activeAnnotations
  171. } else {
  172. self.annotationProperties.annotations = []
  173. self.annotationProperties.annotationMode = annotationType
  174. }
  175. }
  176. }
  177. if (self.isHidden) {
  178. self.annotationProperties.isEmptyAnnotation = self.isHidden
  179. } else {
  180. var isShowEmpty = self.isHidden;
  181. if annotationType == .link && activeAnnotations.count == 0 {
  182. self.isHidden = true
  183. } else if (annotationType == .stamp || annotationType == .signSignature) && isShowEmpty {
  184. let continuousAddStamp = UserDefaults.standard.bool(forKey: "KMContinuousAdditionStamp")
  185. if continuousAddStamp {
  186. isShowEmpty = false
  187. self.annotationProperties.isContinuousAddStamp = true
  188. UserDefaults.standard.setValue(false, forKey: "KMContinuousAdditionStamp")
  189. UserDefaults.standard.synchronize()
  190. }
  191. } else {
  192. if isShowEmpty {
  193. } else {
  194. if _subViewType == RightSubViewType.AnnotationProperts {
  195. self.contextBox.contentView = self.annotationProperties.view
  196. }
  197. }
  198. }
  199. self.annotationProperties.isEmptyAnnotation = isShowEmpty
  200. }
  201. }
  202. private func initEditPDFTextPropertyViewController() {
  203. _ = KMEditPDFTextManager.manager.updateTextFontNames(listView: self.listView)
  204. self.eidtPDFTextProperty = KMEditPDFTextPropertyViewController()
  205. self.eidtPDFTextProperty.listView = self.listView
  206. self.eidtPDFTextProperty.view.frame = self.view.bounds
  207. self.eidtPDFTextProperty.view.autoresizingMask = [.height]
  208. self.contextBox.contentView = self.eidtPDFTextProperty.view
  209. }
  210. private func initEditPDFImagePropertyViewController() {
  211. self.eidtPDFImageProperty = KMEditImagePropertyViewController()
  212. self.eidtPDFImageProperty.listView = self.listView
  213. self.eidtPDFImageProperty.delegate = self
  214. self.eidtPDFImageProperty.view.frame = self.view.bounds
  215. self.eidtPDFImageProperty.view.autoresizingMask = [.height]
  216. self.contextBox.contentView = self.eidtPDFImageProperty.view
  217. }
  218. private func initCipherTextView() {
  219. self.cipherTextView = CipherTextView.createFromNib()
  220. self.cipherTextView.frame = self.view.bounds
  221. self.cipherTextView.autoresizingMask = [.height]
  222. self.cipherTextView.setUp()
  223. self.contextBox.contentView = self.cipherTextView
  224. }
  225. private func initAnnotationProperts() {
  226. self.annotationProperties = KMAnnotationPropertiesViewController()
  227. self.annotationProperties.view.frame = self.view.bounds
  228. self.annotationProperties.pdfView = self.listView
  229. self.annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  230. self.contextBox.contentView = self.annotationProperties.view
  231. self.annotationProperties.annoTypeDidChange = { [weak self] anno in
  232. guard let callback = self?.propertyDidChange else {
  233. return
  234. }
  235. callback(anno)
  236. }
  237. if self.listView.toolMode == .noteToolMode || self.listView.toolMode == .formToolMode
  238. || self.listView.toolMode == .selfSignMode {
  239. self.reloadDataWithPDFView(pdfView: self.listView, isShow: true)
  240. }
  241. }
  242. private func initBatesViewController() {
  243. let Controller: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: [])
  244. Controller.onlyManagerTemplate = true
  245. Controller.isBatchOperation = false
  246. Controller.pdfView = self.listView
  247. Controller.isBatchOperation = false
  248. Controller.isBates = true
  249. self.contextBox.contentView? = Controller.view
  250. curcontroller = Controller
  251. Controller.view.mas_makeConstraints { make in
  252. make?.edges.equalTo()(self.view)
  253. }
  254. }
  255. private func initHeaderFooterViewController() {
  256. let headerFooterViewController: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: [])
  257. headerFooterViewController.onlyManagerTemplate = true
  258. headerFooterViewController.isBatchOperation = false
  259. headerFooterViewController.pdfView = self.listView
  260. self.contextBox.contentView? = headerFooterViewController.view
  261. curcontroller = headerFooterViewController
  262. headerFooterViewController.view.mas_makeConstraints { make in
  263. make?.edges.equalTo()(self.view)
  264. }
  265. }
  266. private func initBackgroundViewController() {
  267. let Controller: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: [])
  268. Controller.onlyManagerTemplate = true
  269. Controller.isBatchOperation = false
  270. Controller.pdfView = self.listView
  271. Controller.isBackground = true
  272. self.contextBox.contentView? = Controller.view
  273. curcontroller = Controller
  274. Controller.view.mas_makeConstraints { make in
  275. make?.edges.equalTo()(self.view)
  276. }
  277. }
  278. private func initWatermarkViewController() {
  279. let addWatermarkViewController: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: [])
  280. addWatermarkViewController.onlyManagerTemplate = true
  281. addWatermarkViewController.isBatchOperation = false
  282. addWatermarkViewController.pdfView = self.listView
  283. self.contextBox.contentView? = addWatermarkViewController.view
  284. curcontroller = addWatermarkViewController
  285. addWatermarkViewController.view.mas_makeConstraints { make in
  286. make?.edges.equalTo()(self.view)
  287. }
  288. }
  289. private func initViewSettingsViewController() {
  290. annotationProperties = KMAnnotationPropertiesViewController()
  291. annotationProperties.view.frame = self.view.bounds
  292. annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  293. annotationProperties.mainController = mainController
  294. annotationProperties.pdfView = listView
  295. annotationProperties.openPropertiesType = .pageDisplay
  296. annotationProperties.pageDisplayReaderMode = { [weak self] isReaderMode in
  297. guard let self = self else { return }
  298. self.mainController!.readMode(self)
  299. }
  300. annotationProperties.isEmptyAnnotation = false
  301. contextBox.contentView = self.annotationProperties.view
  302. }
  303. //MARK: CipherTextViewDelegate
  304. func cipherTextViewButtonClicked(textView: CipherTextView, buttonIndex: Int) {
  305. if buttonIndex == 0 {
  306. }
  307. }
  308. // MARK: NSNotification Methods
  309. @objc func PDFViewActiveAnnotationDidChangeNotification(_ notification: Notification) {
  310. if let pdfView = notification.object as? CPDFListView {
  311. reloadDataWithPDFView(pdfView: pdfView, isShow: true)
  312. }
  313. }
  314. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  315. super.interfaceThemeDidChanged(appearance)
  316. self.view.wantsLayer = true
  317. self.view.superview?.wantsLayer = true
  318. if KMAppearance.isDarkMode() {
  319. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  320. } else {
  321. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  322. }
  323. }
  324. @objc func signatureSelectedNotification(_ notification: Notification) {
  325. if self.contextBox.contentView != self.annotationProperties.view {
  326. self.contextBox.contentView = self.annotationProperties.view
  327. }
  328. let editAnnotation: CPDFAnnotation = notification.object as! CPDFAnnotation
  329. if self.mainController?.model.rightPanelIsOpen == false {
  330. self.mainController?.openRightPane()
  331. }
  332. self.annotationProperties.mainController = self.mainController
  333. self.annotationProperties.isClickFormSignature = true;
  334. self.annotationProperties.annotations = [editAnnotation]
  335. self.annotationProperties.openPropertiesType = .formSignatureClick
  336. self.annotationProperties.isEmptyAnnotation = false
  337. }
  338. }
  339. extension KMRightSideViewController: KMEditImagePropertyViewControllerDelegate {
  340. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  341. self.delegate?.editImagePropertyViewControllerDidChanged(controller: controller, type: type)
  342. }
  343. }