KMRightSideViewController.swift 16 KB

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