// // KMRightSideViewController.swift // PDF Reader Pro // // 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 case ViewSettings } 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? weak var delegate: KMEditImagePropertyViewControllerDelegate? weak var mainController: KMMainViewController? 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() NotificationCenter.default.addObserver(self, selector: #selector(PDFViewActiveAnnotationDidChangeNotification(_:)), name: NSNotification.Name("KMPDFViewActiveAnnotationDidChangeNotification"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(signatureSelectedNotification(_:)), name: NSNotification.Name("KMSignatureListSelectNotification"), object: nil) } 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) } } var isSameAnnotation: Bool = true for tAnnotation in activeAnnotations { if tAnnotation.className != selectedAnnotation?.className { isSameAnnotation = false break } } if !isSameAnnotation { self.annotationProperties.annotations = [] self.annotationProperties.isEmptyAnnotation = true return } else if selectedAnnotation is CPDFTextWidgetAnnotation || selectedAnnotation is CPDFChoiceWidgetAnnotation || selectedAnnotation is CPDFButtonWidgetAnnotation { if pdfView.toolMode != .formToolMode { self.annotationProperties.annotations = [] self.annotationProperties.isEmptyAnnotation = true return } } else if (selectedAnnotation is KMAnnotationFromSignature) && (activeAnnotations.count > 1) { self.annotationProperties.annotations = [] self.annotationProperties.isEmptyAnnotation = true return } let annotationType = pdfView.annotationType if KMAnnotationPropertiesViewController.height(with: selectedAnnotation) > 0 { if activeAnnotations.count > 0{ if self.subViewType != .AnnotationProperts { self.subViewType = .AnnotationProperts } } 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; 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() } else if subViewType == .ViewSettings { self.initViewSettingsViewController() } } 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() { _ = KMEditPDFTextManager.manager.updateTextFontNames(listView: self.listView) 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.delegate = self 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 self.annotationProperties.annoTypeDidChange = { [weak self] anno in guard let callback = self?.propertyDidChange else { return } callback(anno) } 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: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: []) Controller.onlyManagerTemplate = true Controller.isBatchOperation = false Controller.pdfView = self.listView Controller.isBatchOperation = false Controller.isBates = true self.contextBox.contentView? = Controller.view curcontroller = Controller Controller.view.mas_makeConstraints { make in make?.edges.equalTo()(self.view) } } private func initHeaderFooterViewController() { let headerFooterViewController: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: []) headerFooterViewController.onlyManagerTemplate = true headerFooterViewController.isBatchOperation = false headerFooterViewController.pdfView = self.listView self.contextBox.contentView? = headerFooterViewController.view curcontroller = headerFooterViewController headerFooterViewController.view.mas_makeConstraints { make in make?.edges.equalTo()(self.view) } } private func initBackgroundViewController() { let Controller: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: []) Controller.onlyManagerTemplate = true Controller.isBatchOperation = false Controller.pdfView = self.listView Controller.isBackground = true self.contextBox.contentView? = Controller.view curcontroller = Controller Controller.view.mas_makeConstraints { make in make?.edges.equalTo()(self.view) } } private func initWatermarkViewController() { let addWatermarkViewController: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: []) addWatermarkViewController.onlyManagerTemplate = true addWatermarkViewController.isBatchOperation = false addWatermarkViewController.pdfView = self.listView self.contextBox.contentView? = addWatermarkViewController.view curcontroller = addWatermarkViewController addWatermarkViewController.view.mas_makeConstraints { make in make?.edges.equalTo()(self.view) } } private func initViewSettingsViewController() { annotationProperties = KMAnnotationPropertiesViewController() annotationProperties.view.frame = self.view.bounds annotationProperties.view.autoresizingMask = [.height,.maxXMargin] annotationProperties.mainController = mainController annotationProperties.pdfView = listView annotationProperties.openPropertiesType = .pageDisplay annotationProperties.pageDisplayReaderMode = { [weak self] isReaderMode in guard let self = self else { return } self.mainController!.readMode(self) } annotationProperties.isEmptyAnnotation = false contextBox.contentView = self.annotationProperties.view } //MARK: CipherTextViewDelegate func cipherTextViewButtonClicked(textView: CipherTextView, buttonIndex: Int) { if buttonIndex == 0 { } } // MARK: NSNotification Methods @objc func PDFViewActiveAnnotationDidChangeNotification(_ notification: Notification) { if let pdfView = notification.object as? CPDFListView { reloadDataWithPDFView(pdfView: pdfView, isShow: true) } } override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) { super.interfaceThemeDidChanged(appearance) self.view.wantsLayer = true self.view.superview?.wantsLayer = true if KMAppearance.isDarkMode() { self.view.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor // self.view.superview?.layer?.backgroundColor = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1).cgColor } else { self.view.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor // self.view.superview?.layer?.backgroundColor = NSColor(red: 0.988, green: 0.992, blue: 1, alpha: 1).cgColor } } @objc func signatureSelectedNotification(_ notification: Notification) { let editAnnotation: CPDFAnnotation = notification.object as! CPDFAnnotation if self.mainController?.model.rightPanelIsOpen == false { self.mainController?.toggleRightPane() } self.annotationProperties.mainController = self.mainController self.annotationProperties.isClickFormSignature = true; self.annotationProperties.annotations = [editAnnotation] self.annotationProperties.openPropertiesType = .formSignatureClick self.annotationProperties.isEmptyAnnotation = false } } extension KMRightSideViewController: KMEditImagePropertyViewControllerDelegate { func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) { self.delegate?.editImagePropertyViewControllerDidChanged(controller: controller, type: type) } }