KMRightSideViewController.swift 15 KB

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