KMRightSideViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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.annotationMode = annotationType
  116. } else {
  117. self.isHidden = true
  118. }
  119. if (self.isHidden) {
  120. self.annotationProperties.isEmptyAnnotation = self.isHidden
  121. } else {
  122. var isShowEmpty = self.isHidden;
  123. if annotationType == .link && activeAnnotations.count == 0 {
  124. self.isHidden = true
  125. } else if (annotationType == .stamp || annotationType == .signSignature) && isShowEmpty {
  126. let continuousAddStamp = UserDefaults.standard.bool(forKey: "KMContinuousAdditionStamp")
  127. if continuousAddStamp {
  128. isShowEmpty = false
  129. self.annotationProperties.isContinuousAddStamp = true
  130. UserDefaults.standard.setValue(false, forKey: "KMContinuousAdditionStamp")
  131. UserDefaults.standard.synchronize()
  132. }
  133. } else {
  134. if isShowEmpty {
  135. } else {
  136. if _subViewType == RightSubViewType.AnnotationProperts {
  137. self.contextBox.contentView = self.annotationProperties.view
  138. }
  139. }
  140. }
  141. self.annotationProperties.isEmptyAnnotation = isShowEmpty
  142. }
  143. }
  144. //MARK: Setter && Get
  145. var subViewType : RightSubViewType? {
  146. set {
  147. _subViewType = newValue
  148. if _subViewType == RightSubViewType.CipherTextType {
  149. self.initCipherTextView()
  150. } else if _subViewType == RightSubViewType.EditPDFAddText{
  151. self.initEditPDFTextPropertyViewController()
  152. } else if _subViewType == RightSubViewType.EditPDFAddImage{
  153. self.initEditPDFImagePropertyViewController()
  154. } else if _subViewType == RightSubViewType.AnnotationProperts {
  155. self.initAnnotationProperts()
  156. } else if _subViewType == .Bates {
  157. self.initBatesViewController()
  158. } else if subViewType == .Headerfooter {
  159. self.initHeaderFooterViewController()
  160. } else if subViewType == .Background {
  161. self.initBackgroundViewController()
  162. } else if subViewType == .Watermark {
  163. self.initWatermarkViewController()
  164. } else if subViewType == .ViewSettings {
  165. self.initViewSettingsViewController()
  166. }
  167. }
  168. get {
  169. return _subViewType
  170. }
  171. }
  172. var isHidden: Bool {
  173. get {
  174. return _isHidden
  175. }
  176. set {
  177. _isHidden = newValue
  178. if _isHidden {
  179. if (self.listView.annotationType == .link) {
  180. self.contextBox.contentView = self.emptyVC_link?.view
  181. } else {
  182. self.contextBox.contentView = emptyVC.view
  183. }
  184. }
  185. }
  186. }
  187. //MARK: InitSubViews
  188. func removeSubViews() {
  189. }
  190. private func initEditPDFTextPropertyViewController() {
  191. _ = KMEditPDFTextManager.manager.updateTextFontNames(listView: self.listView)
  192. self.eidtPDFTextProperty = KMEditPDFTextPropertyViewController()
  193. self.eidtPDFTextProperty.listView = self.listView
  194. self.eidtPDFTextProperty.view.frame = self.view.bounds
  195. self.eidtPDFTextProperty.view.autoresizingMask = [.height]
  196. self.contextBox.contentView = self.eidtPDFTextProperty.view
  197. }
  198. private func initEditPDFImagePropertyViewController() {
  199. self.eidtPDFImageProperty = KMEditImagePropertyViewController()
  200. self.eidtPDFImageProperty.listView = self.listView
  201. self.eidtPDFImageProperty.delegate = self
  202. self.eidtPDFImageProperty.view.frame = self.view.bounds
  203. self.eidtPDFImageProperty.view.autoresizingMask = [.height]
  204. self.contextBox.contentView = self.eidtPDFImageProperty.view
  205. }
  206. private func initCipherTextView() {
  207. self.cipherTextView = CipherTextView.createFromNib()
  208. self.cipherTextView.frame = self.view.bounds
  209. self.cipherTextView.autoresizingMask = [.height]
  210. self.cipherTextView.setUp()
  211. self.contextBox.contentView = self.cipherTextView
  212. }
  213. private func initAnnotationProperts() {
  214. self.annotationProperties = KMAnnotationPropertiesViewController()
  215. self.annotationProperties.view.frame = self.view.bounds
  216. self.annotationProperties.pdfView = self.listView
  217. self.annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  218. self.contextBox.contentView = self.annotationProperties.view
  219. self.annotationProperties.annoTypeDidChange = { [weak self] anno in
  220. guard let callback = self?.propertyDidChange else {
  221. return
  222. }
  223. callback(anno)
  224. }
  225. if self.listView.toolMode == .noteToolMode || self.listView.toolMode == .formToolMode
  226. || self.listView.toolMode == .selfSignMode {
  227. self.reloadDataWithPDFView(pdfView: self.listView, isShow: true)
  228. }
  229. }
  230. var curcontroller: NSViewController?
  231. private func initBatesViewController() {
  232. let Controller: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: [])
  233. Controller.onlyManagerTemplate = true
  234. Controller.isBatchOperation = false
  235. Controller.pdfView = self.listView
  236. Controller.isBatchOperation = false
  237. Controller.isBates = true
  238. self.contextBox.contentView? = Controller.view
  239. curcontroller = Controller
  240. Controller.view.mas_makeConstraints { make in
  241. make?.edges.equalTo()(self.view)
  242. }
  243. }
  244. private func initHeaderFooterViewController() {
  245. let headerFooterViewController: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: [])
  246. headerFooterViewController.onlyManagerTemplate = true
  247. headerFooterViewController.isBatchOperation = false
  248. headerFooterViewController.pdfView = self.listView
  249. self.contextBox.contentView? = headerFooterViewController.view
  250. curcontroller = headerFooterViewController
  251. headerFooterViewController.view.mas_makeConstraints { make in
  252. make?.edges.equalTo()(self.view)
  253. }
  254. }
  255. private func initBackgroundViewController() {
  256. let Controller: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: [])
  257. Controller.onlyManagerTemplate = true
  258. Controller.isBatchOperation = false
  259. Controller.pdfView = self.listView
  260. Controller.isBackground = true
  261. self.contextBox.contentView? = Controller.view
  262. curcontroller = Controller
  263. Controller.view.mas_makeConstraints { make in
  264. make?.edges.equalTo()(self.view)
  265. }
  266. }
  267. private func initWatermarkViewController() {
  268. let addWatermarkViewController: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: [])
  269. addWatermarkViewController.onlyManagerTemplate = true
  270. addWatermarkViewController.isBatchOperation = false
  271. addWatermarkViewController.pdfView = self.listView
  272. self.contextBox.contentView? = addWatermarkViewController.view
  273. curcontroller = addWatermarkViewController
  274. addWatermarkViewController.view.mas_makeConstraints { make in
  275. make?.edges.equalTo()(self.view)
  276. }
  277. }
  278. private func initViewSettingsViewController() {
  279. annotationProperties = KMAnnotationPropertiesViewController()
  280. annotationProperties.view.frame = self.view.bounds
  281. annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  282. annotationProperties.mainController = mainController
  283. annotationProperties.pdfView = listView
  284. annotationProperties.openPropertiesType = .pageDisplay
  285. annotationProperties.pageDisplayReaderMode = { [weak self] isReaderMode in
  286. guard let self = self else { return }
  287. self.mainController!.readMode(self)
  288. }
  289. annotationProperties.isEmptyAnnotation = false
  290. contextBox.contentView = self.annotationProperties.view
  291. }
  292. //MARK: CipherTextViewDelegate
  293. func cipherTextViewButtonClicked(textView: CipherTextView, buttonIndex: Int) {
  294. if buttonIndex == 0 {
  295. }
  296. }
  297. // MARK: NSNotification Methods
  298. @objc func PDFViewActiveAnnotationDidChangeNotification(_ notification: Notification) {
  299. if let pdfView = notification.object as? CPDFListView {
  300. reloadDataWithPDFView(pdfView: pdfView, isShow: true)
  301. }
  302. }
  303. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  304. super.interfaceThemeDidChanged(appearance)
  305. self.view.wantsLayer = true
  306. self.view.superview?.wantsLayer = true
  307. if KMAppearance.isDarkMode() {
  308. self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  309. // self.view.superview?.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor
  310. } else {
  311. self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  312. // self.view.superview?.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor
  313. }
  314. }
  315. @objc func signatureSelectedNotification(_ notification: Notification) {
  316. let editAnnotation: CPDFAnnotation = notification.object as! CPDFAnnotation
  317. if self.mainController?.model.rightPanelIsOpen == false {
  318. self.mainController?.toggleRightPane()
  319. }
  320. self.annotationProperties.mainController = self.mainController
  321. self.annotationProperties.isClickFormSignature = true;
  322. self.annotationProperties.annotations = [editAnnotation]
  323. self.annotationProperties.openPropertiesType = .formSignatureClick
  324. self.annotationProperties.isEmptyAnnotation = false
  325. }
  326. }
  327. extension KMRightSideViewController: KMEditImagePropertyViewControllerDelegate {
  328. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  329. self.delegate?.editImagePropertyViewControllerDidChanged(controller: controller, type: type)
  330. }
  331. }