// // KMRightSideViewController.swift // PDF Master // // Created by Niehaoyu on 2022/10/26. // import Cocoa enum RightSubViewType : Int { case None case CipherTextType case EditPDFAddText case EditPDFAddImage case AnnotationProperts case Bates case Headerfooter case Background case Watermark } typealias KMRightSidePropertyDidChange = (_ model: AnyObject?) -> () class KMRightSideViewController: NSViewController,CipherTextViewDelegate { var cipherTextView : CipherTextView! var eidtPDFTextProperty : KMEditPDFTextPropertyViewController! var eidtPDFImageProperty : KMEditImagePropertyViewController! var annotationProperties : KMAnnotationPropertiesViewController! var listView : CPDFListView! var _subViewType : RightSubViewType? @IBOutlet weak var contextBox: NSBox! var emptyVC : KMRightSideEmptyVC! var emptyVC_link: KMLinkAnnotationPropertyEmptyController? var _isHidden: Bool = false var propertyDidChange: KMRightSidePropertyDidChange! var model: AnyObject? override func viewDidLoad() { super.viewDidLoad() // Do view setup here. // self.view.wantsLayer = true // self.view.layer?.backgroundColor = NSColor.red.cgColor emptyVC = KMRightSideEmptyVC.init(nibName: "KMRightSideEmptyVC", bundle: nil) contextBox.contentView = emptyVC.view self.emptyVC_link = KMLinkAnnotationPropertyEmptyController() } public func reloadDataWithPDFView(pdfView:CPDFListView,isShow:Bool) { if self.annotationProperties == nil { self.subViewType = .AnnotationProperts } if(!isShow) { self.isHidden = true return } self.annotationProperties.pdfView = pdfView var selectedAnnotation : CPDFAnnotation? = nil var activeAnnotations : [CPDFAnnotation] = [] if pdfView.activeAnnotations != nil && pdfView.activeAnnotations.count > 0 { selectedAnnotation = pdfView.activeAnnotations.firstObject as? CPDFAnnotation for annotation in pdfView.activeAnnotations { activeAnnotations.append(annotation as! CPDFAnnotation) } } let annotationType = pdfView.annotationType if KMAnnotationPropertiesViewController.height(with: selectedAnnotation) > 0 { self.annotationProperties.annotations = activeAnnotations self.isHidden = !(activeAnnotations.count > 0) } else if self.listView.toolMode == .noteToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 { self.isHidden = false if pdfView.activeAnnotation is CPDFLinkAnnotation { if activeAnnotations.count > 0 { self.annotationProperties.annotations = activeAnnotations } else { self.annotationProperties.annotations = [] self.annotationProperties.annotationMode = annotationType } } else { self.annotationProperties.annotations = [] self.annotationProperties.annotationMode = annotationType } } else if listView.toolMode == .formToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 { self.isHidden = false self.annotationProperties.annotations = [] self.annotationProperties.annotationMode = annotationType } else if listView.toolMode == .selfSignMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 { self.isHidden = false self.annotationProperties.annotations = [] self.annotationProperties.annotationMode = annotationType } else { self.isHidden = true } if (self.isHidden) { self.annotationProperties.isEmptyAnnotation = self.isHidden } else { var isShowEmpty = self.isHidden; for annotation in activeAnnotations { if (annotation is CPDFSignatureAnnotation) || (annotation is CPDFStampAnnotation) { if (annotation is CPDFStampAnnotation) { if (annotation is CSelfSignAnnotation) { break } } isShowEmpty = true; break } } if annotationType == .link && activeAnnotations.count == 0 { self.isHidden = true } else if (annotationType == .stamp || annotationType == .signSignature) && isShowEmpty { let continuousAddStamp = UserDefaults.standard.bool(forKey: "KMContinuousAdditionStamp") if continuousAddStamp { isShowEmpty = false self.annotationProperties.isContinuousAddStamp = true UserDefaults.standard.setValue(false, forKey: "KMContinuousAdditionStamp") UserDefaults.standard.synchronize() } } else { if isShowEmpty { } else { if _subViewType == RightSubViewType.AnnotationProperts { self.contextBox.contentView = self.annotationProperties.view } } } self.annotationProperties.isEmptyAnnotation = isShowEmpty } } //MARK: Setter && Get var subViewType : RightSubViewType? { set { _subViewType = newValue if _subViewType == RightSubViewType.CipherTextType { self.initCipherTextView() } else if _subViewType == RightSubViewType.EditPDFAddText{ self.initEditPDFTextPropertyViewController() } else if _subViewType == RightSubViewType.EditPDFAddImage{ self.initEditPDFImagePropertyViewController() } else if _subViewType == RightSubViewType.AnnotationProperts { self.initAnnotationProperts() } else if _subViewType == .Bates { self.initBatesViewController() } else if subViewType == .Headerfooter { self.initHeaderFooterViewController() } else if subViewType == .Background { self.initBackgroundViewController() } else if subViewType == .Watermark { self.initWatermarkViewController() } } get { return _subViewType } } var isHidden: Bool { get { return _isHidden } set { _isHidden = newValue if _isHidden { if (self.listView.annotationType == .link) { self.contextBox.contentView = self.emptyVC_link?.view } else { self.contextBox.contentView = emptyVC.view } } } } //MARK: InitSubViews func removeSubViews() { } private func initEditPDFTextPropertyViewController() { self.eidtPDFTextProperty = KMEditPDFTextPropertyViewController() self.eidtPDFTextProperty.listView = self.listView self.eidtPDFTextProperty.view.frame = self.view.bounds self.eidtPDFTextProperty.view.autoresizingMask = [.height] self.contextBox.contentView = self.eidtPDFTextProperty.view } private func initEditPDFImagePropertyViewController() { self.eidtPDFImageProperty = KMEditImagePropertyViewController() self.eidtPDFImageProperty.listView = self.listView self.eidtPDFImageProperty.view.frame = self.view.bounds self.eidtPDFImageProperty.view.autoresizingMask = [.height] self.contextBox.contentView = self.eidtPDFImageProperty.view } private func initCipherTextView() { self.cipherTextView = CipherTextView.createFromNib() self.cipherTextView.frame = self.view.bounds self.cipherTextView.autoresizingMask = [.height] self.cipherTextView.setUp() self.contextBox.contentView = self.cipherTextView } private func initAnnotationProperts() { self.annotationProperties = KMAnnotationPropertiesViewController() self.annotationProperties.view.frame = self.view.bounds self.annotationProperties.pdfView = self.listView self.annotationProperties.view.autoresizingMask = [.height,.maxXMargin] self.contextBox.contentView = self.annotationProperties.view if self.listView.toolMode == .noteToolMode || self.listView.toolMode == .formToolMode || self.listView.toolMode == .selfSignMode { self.reloadDataWithPDFView(pdfView: self.listView, isShow: true) } } var curcontroller: NSViewController? private func initBatesViewController() { let controller = KMBatesPropertyHomeController() controller.pageCount = Int(self.listView.document.pageCount) controller.view.frame = self.view.bounds controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.contextBox.contentView = controller.view curcontroller = controller controller.modelDidChange = { [weak self] model in guard let callback = self?.propertyDidChange else { return } self!.model = model callback(model) } } private func initHeaderFooterViewController() { let controller = KMHeaderFooterPropertyMainController() controller.pageCount = Int(self.listView.document.pageCount) controller.view.frame = self.view.bounds controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.contextBox.contentView = controller.view curcontroller = controller controller.modelDidChange = { [weak self] model in guard let callback = self?.propertyDidChange else { return } self!.model = model callback(model) } } private func initBackgroundViewController() { // let controller = KMBackgroundPropertyHomeController() // controller.preView = self.listView // controller.view.frame = self.view.bounds // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) // self.contextBox.contentView = controller.view // // controller.modelDidChange = { // [weak self] (model: KMBackgroundModel?) -> () in // guard let callback = self?.propertyDidChange else { // return // } // // self!.model = model // callback(model) // } } private func initWatermarkViewController() { // let controller = KMWatermarkPropertyHomeController() // controller.preView = self.listView // controller.view.frame = self.view.bounds // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) // self.contextBox.contentView = controller.view // // controller.modelDidChange = { // [weak self] (model: KMWatermarkModel?) -> () in // guard let callback = self?.propertyDidChange else { // return // } // // self!.model = model // callback(model) // } } //MARK: CipherTextViewDelegate func cipherTextViewButtonClicked(textView: CipherTextView, buttonIndex: Int) { if buttonIndex == 0 { } } }