KMRightSideViewController.swift 17 KB

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