// // KMBackgroundController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/7. // import Cocoa import KMComponentLibrary @objc protocol KMBackgroundControllerDelegate: AnyObject { @objc optional func kmBackgroundControllerDidUpdateMode(_ view: KMBackgroundController) //数据发生变化时处理 @objc optional func kmBackgroundControllerDidWatermarkUpdated(_ view: KMBackgroundController) } class KMBackgroundController: NSViewController { @IBOutlet var contendView: NSView! @IBOutlet var preInfoView: NSView! @IBOutlet var propertyInfoView: NSView! var documentPreview: KMEditDocumentController = KMEditDocumentController.init() var propertyController: KMBackgroundPropertyController = KMBackgroundPropertyController.init() var templatesController: KMBGTemplateController = KMBGTemplateController() var saveTemplateMessage: ComponentMessage = ComponentMessage() var pdfDocument: CPDFDocument? var backgroundModel: KMBackgroundModel? var editSubType: KMPDFEditSubModeType = .none weak open var delegate: KMBackgroundControllerDelegate? override func viewDidLoad() { super.viewDidLoad() // Do view setup here. documentPreview.view.frame = preInfoView.bounds documentPreview.view.autoresizingMask = [.width, .height] preInfoView.addSubview(documentPreview.view) propertyController.view.frame = propertyInfoView.bounds propertyController.view.autoresizingMask = [.width, .height] propertyController.delegate = self propertyController.view.isHidden = true propertyInfoView.addSubview(propertyController.view) templatesController.view.frame = propertyInfoView.bounds templatesController.view.autoresizingMask = [.width, .height] templatesController.delegate = self templatesController.view.isHidden = true propertyInfoView.addSubview(templatesController.view) setupProperty() } func setupProperty() { saveTemplateMessage.properties = ComponentMessageProperty(messageType: .success, title: KMLocalizedString("Saved Template")) } func resetUI() { propertyController.view.isHidden = true templatesController.view.isHidden = true if editSubType == .template { templatesController.view.isHidden = false } else if editSubType == .add { propertyController.view.isHidden = false propertyController.editSubType = editSubType } else if editSubType == .edit { propertyController.view.isHidden = false propertyController.editSubType = editSubType } } func reloadData() { if editSubType == .template { templatesController.reloadData() backgroundModel = templatesController.selectedBackground } else if editSubType == .add { propertyController.editSubType = editSubType propertyController.backgroundData = KMBackgroundManager.defaultManager.defaultModel propertyController.reloadData() backgroundModel = propertyController.backgroundData } else if editSubType == .edit { propertyController.editSubType = editSubType propertyController.reloadData() backgroundModel = propertyController.backgroundData } documentPreview.pdfDocument = pdfDocument documentPreview.reloadData() updatePDFDocumentBackground() delegate?.kmBackgroundControllerDidWatermarkUpdated?(self) } func updatePDFDocumentBackground() { if let data = backgroundModel { let cPDFBackground = documentPreview.pdfDocument?.background() cPDFBackground?.type = data.type if cPDFBackground?.type == .color { cPDFBackground?.color = data.color } else if cPDFBackground?.type == .image { if data.imagePath != nil { cPDFBackground?.setImage(data.image()) } else { cPDFBackground?.setImage(nil) } } cPDFBackground?.scale = data.scale cPDFBackground?.rotation = data.rotation cPDFBackground?.opacity = data.opacity cPDFBackground?.verticalAlignment = UInt(data.verticalMode) cPDFBackground?.horizontalAlignment = UInt(data.horizontalMode) cPDFBackground?.xOffset = data.horizontalSpace cPDFBackground?.yOffset = data.verticalSpace cPDFBackground?.isAllowsView = true cPDFBackground?.pageString = "0" cPDFBackground?.update() self.documentPreview.refreshUI() } else { pdfDocument?.background().clear() documentPreview.refreshUI() } } override func mouseUp(with event: NSEvent) { super.mouseUp(with: event) } } //MARK: - KMBackgroundPropertyControllerDelegate extension KMBackgroundController: KMBackgroundPropertyControllerDelegate { func backgroundPropertyControllerDidUpdate(_ controller: KMBackgroundPropertyController) { backgroundModel = controller.backgroundData updatePDFDocumentBackground() delegate?.kmBackgroundControllerDidWatermarkUpdated?(self) } func backgroundPropertyControllerDidChangetoTemplate(_ controller: KMBackgroundPropertyController) { editSubType = .template resetUI() delegate?.kmBackgroundControllerDidUpdateMode?(self) backgroundModel = templatesController.selectedBackground reloadData() } func backgroundPropertyControllerSaveTemplateSuccess(_ controller: KMBackgroundPropertyController, _ data: KMBackgroundModel) { saveTemplateMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewWidth)/2, CGRectGetHeight(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewHeight - 8, saveTemplateMessage.properties.propertyInfo.viewWidth, saveTemplateMessage.properties.propertyInfo.viewHeight) saveTemplateMessage.reloadData() saveTemplateMessage.show(inView: self.view, autoHideSeconde: 2) editSubType = .template resetUI() delegate?.kmBackgroundControllerDidUpdateMode?(self) templatesController.selectedBackground = data reloadData() } func backgroundPropertyControllerCancelTemplateEdit(_ controller: KMBackgroundPropertyController) { editSubType = .template resetUI() delegate?.kmBackgroundControllerDidUpdateMode?(self) templatesController.selectedBackground = controller.backgroundData reloadData() } func backgroundPropertyControllerFinishTemplateEdit(_ controller: KMBackgroundPropertyController) { editSubType = .template resetUI() delegate?.kmBackgroundControllerDidUpdateMode?(self) templatesController.selectedBackground = controller.backgroundData reloadData() } } //MARK: - KMBGTemplateControllerDelegate extension KMBackgroundController: KMBGTemplateControllerDelegate { func templateControllerDidSelectedChanged(_ controller: KMBGTemplateController) { backgroundModel = controller.selectedBackground updatePDFDocumentBackground() delegate?.kmBackgroundControllerDidWatermarkUpdated?(self) } func templateControllerDidAddData(_ controller: KMBGTemplateController) { editSubType = .add resetUI() delegate?.kmBackgroundControllerDidUpdateMode?(self) propertyController.backgroundData = KMBackgroundManager.defaultManager.defaultModel backgroundModel = propertyController.backgroundData reloadData() documentPreview.layoutDocumentView() } func templateControllerDidEditData(_ controller: KMBGTemplateController, _ data: KMBackgroundModel) { editSubType = .edit resetUI() delegate?.kmBackgroundControllerDidUpdateMode?(self) propertyController.originalDataDict = KMBackgroundManager.defaultManager.parseModelToDict(model: data) propertyController.backgroundData = data propertyController.reloadData() backgroundModel = propertyController.backgroundData documentPreview.layoutDocumentView() } }