KMRightSideViewController.swift 16 KB

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