KMRightSideViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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.kEventTag = self.kEventTag
  165. self.kEventTag = 0
  166. self.annotationProperties.annotationMode = annotationType
  167. } else if (annotationType == .line || annotationType == .polyLine || annotationType == .polyGon || (annotationType == .square && self.listView.toolMode != .noteToolMode)) && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  168. self.isHidden = false
  169. self.annotationProperties.annotations = []
  170. self.annotationProperties.measureMode = annotationType
  171. } else {
  172. self.isHidden = true
  173. if pdfView.activeAnnotation is CPDFLinkAnnotation {
  174. if activeAnnotations.count > 0 {
  175. self.isHidden = false
  176. self.annotationProperties.annotations = activeAnnotations
  177. } else {
  178. self.annotationProperties.annotations = []
  179. self.annotationProperties.annotationMode = annotationType
  180. }
  181. }
  182. }
  183. if (self.isHidden) {
  184. self.annotationProperties.isEmptyAnnotation = self.isHidden
  185. } else {
  186. var isShowEmpty = self.isHidden;
  187. if annotationType == .link && activeAnnotations.count == 0 {
  188. self.isHidden = true
  189. } else if (annotationType == .stamp || annotationType == .signSignature) && isShowEmpty {
  190. let continuousAddStamp = UserDefaults.standard.bool(forKey: "KMContinuousAdditionStamp")
  191. if continuousAddStamp {
  192. isShowEmpty = false
  193. self.annotationProperties.isContinuousAddStamp = true
  194. UserDefaults.standard.setValue(false, forKey: "KMContinuousAdditionStamp")
  195. UserDefaults.standard.synchronize()
  196. }
  197. } else {
  198. if isShowEmpty {
  199. } else {
  200. if _subViewType == RightSubViewType.AnnotationProperts {
  201. self.contextBox.contentView = self.annotationProperties.view
  202. }
  203. }
  204. }
  205. self.annotationProperties.isEmptyAnnotation = isShowEmpty
  206. }
  207. }
  208. private func initEditPDFTextPropertyViewController() {
  209. _ = KMEditPDFTextManager.manager.updateTextFontNames(listView: self.listView)
  210. self.eidtPDFTextProperty = KMEditPDFTextPropertyViewController()
  211. self.eidtPDFTextProperty.listView = self.listView
  212. self.eidtPDFTextProperty.view.frame = self.view.bounds
  213. self.eidtPDFTextProperty.view.autoresizingMask = [.height]
  214. self.contextBox.contentView = self.eidtPDFTextProperty.view
  215. }
  216. private func initEditPDFImagePropertyViewController() {
  217. self.eidtPDFImageProperty = KMEditImagePropertyViewController()
  218. self.eidtPDFImageProperty.listView = self.listView
  219. self.eidtPDFImageProperty.delegate = self
  220. self.eidtPDFImageProperty.view.frame = self.view.bounds
  221. self.eidtPDFImageProperty.view.autoresizingMask = [.height]
  222. self.contextBox.contentView = self.eidtPDFImageProperty.view
  223. }
  224. private func initCipherTextView() {
  225. self.cipherTextView = CipherTextView.createFromNib()
  226. self.cipherTextView.frame = self.view.bounds
  227. self.cipherTextView.autoresizingMask = [.height]
  228. self.cipherTextView.setUp()
  229. self.contextBox.contentView = self.cipherTextView
  230. }
  231. private func initAnnotationProperts() {
  232. self.annotationProperties = KMAnnotationPropertiesViewController()
  233. self.annotationProperties.view.frame = self.view.bounds
  234. self.annotationProperties.pdfView = self.listView
  235. self.annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  236. self.contextBox.contentView = self.annotationProperties.view
  237. self.annotationProperties.annoTypeDidChange = { [weak self] anno in
  238. guard let callback = self?.propertyDidChange else {
  239. return
  240. }
  241. callback(anno)
  242. }
  243. if self.listView.toolMode == .noteToolMode || self.listView.toolMode == .formToolMode
  244. || self.listView.toolMode == .selfSignMode {
  245. self.reloadDataWithPDFView(pdfView: self.listView, isShow: true)
  246. }
  247. }
  248. private func initBatesViewController() {
  249. let Controller: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: [])
  250. Controller.onlyManagerTemplate = true
  251. Controller.isBatchOperation = false
  252. Controller.pdfView = self.listView
  253. Controller.isBatchOperation = false
  254. Controller.isBates = true
  255. self.contextBox.contentView? = Controller.view
  256. curcontroller = Controller
  257. Controller.view.mas_makeConstraints { make in
  258. make?.edges.equalTo()(self.view)
  259. }
  260. }
  261. private func initHeaderFooterViewController() {
  262. let headerFooterViewController: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: [])
  263. headerFooterViewController.onlyManagerTemplate = true
  264. headerFooterViewController.isBatchOperation = false
  265. headerFooterViewController.pdfView = self.listView
  266. self.contextBox.contentView? = headerFooterViewController.view
  267. curcontroller = headerFooterViewController
  268. headerFooterViewController.view.mas_makeConstraints { make in
  269. make?.edges.equalTo()(self.view)
  270. }
  271. }
  272. private func initBackgroundViewController() {
  273. let Controller: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: [])
  274. Controller.onlyManagerTemplate = true
  275. Controller.isBatchOperation = false
  276. Controller.pdfView = self.listView
  277. Controller.isBackground = true
  278. self.contextBox.contentView? = Controller.view
  279. curcontroller = Controller
  280. Controller.view.mas_makeConstraints { make in
  281. make?.edges.equalTo()(self.view)
  282. }
  283. }
  284. private func initWatermarkViewController() {
  285. let addWatermarkViewController: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: [])
  286. addWatermarkViewController.onlyManagerTemplate = true
  287. addWatermarkViewController.isBatchOperation = false
  288. addWatermarkViewController.pdfView = self.listView
  289. self.contextBox.contentView? = addWatermarkViewController.view
  290. curcontroller = addWatermarkViewController
  291. addWatermarkViewController.view.mas_makeConstraints { make in
  292. make?.edges.equalTo()(self.view)
  293. }
  294. }
  295. private func initViewSettingsViewController() {
  296. annotationProperties = KMAnnotationPropertiesViewController()
  297. annotationProperties.view.frame = self.view.bounds
  298. annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  299. annotationProperties.mainController = mainController
  300. annotationProperties.pdfView = listView
  301. annotationProperties.openPropertiesType = .pageDisplay
  302. annotationProperties.pageDisplayReaderMode = { [weak self] isReaderMode in
  303. guard let self = self else { return }
  304. self.mainController!.readMode(self)
  305. }
  306. annotationProperties.isEmptyAnnotation = false
  307. contextBox.contentView = self.annotationProperties.view
  308. }
  309. //MARK: CipherTextViewDelegate
  310. func cipherTextViewButtonClicked(textView: CipherTextView, buttonIndex: Int) {
  311. if buttonIndex == 0 {
  312. }
  313. }
  314. // MARK: NSNotification Methods
  315. @objc func PDFViewActiveAnnotationDidChangeNotification(_ notification: Notification) {
  316. if let pdfView = notification.object as? CPDFListView {
  317. reloadDataWithPDFView(pdfView: pdfView, isShow: true)
  318. }
  319. }
  320. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  321. super.interfaceThemeDidChanged(appearance)
  322. self.view.wantsLayer = true
  323. self.view.superview?.wantsLayer = true
  324. if KMAppearance.isDarkMode() {
  325. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  326. } else {
  327. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  328. }
  329. }
  330. @objc func signatureSelectedNotification(_ notification: Notification) {
  331. guard let theWindow = self.view.window else {
  332. return
  333. }
  334. guard let pdfView = notification.userInfo?["pdfView"] as? CPDFView else {
  335. return
  336. }
  337. if pdfView.window != theWindow {
  338. return
  339. }
  340. if self.contextBox.contentView != self.annotationProperties.view {
  341. self.contextBox.contentView = self.annotationProperties.view
  342. }
  343. let editAnnotation: CPDFAnnotation = notification.object as! CPDFAnnotation
  344. if self.mainController?.model.rightPanelIsOpen == false {
  345. self.mainController?.openRightPane()
  346. }
  347. self.annotationProperties.mainController = self.mainController
  348. self.annotationProperties.isClickFormSignature = true;
  349. self.annotationProperties.annotations = [editAnnotation]
  350. self.annotationProperties.openPropertiesType = .formSignatureClick
  351. self.annotationProperties.isEmptyAnnotation = false
  352. }
  353. }
  354. extension KMRightSideViewController: KMEditImagePropertyViewControllerDelegate {
  355. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  356. self.delegate?.editImagePropertyViewControllerDidChanged(controller: controller, type: type)
  357. }
  358. }