KMRightSideViewController.swift 17 KB

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