KMRightSideViewController.swift 16 KB

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