// // KMHeaderFooterController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/8. // import Cocoa import KMComponentLibrary @objc protocol KMHeaderFooterControllerDelegate: AnyObject { @objc optional func kmHeaderFooterControllerDidUpdateMode(_ view: KMHeaderFooterController) //数据发生变化时处理 @objc optional func kmHeaderFooterControllerDidModelDataUpdated(_ view: KMHeaderFooterController) } class KMHeaderFooterController: NSViewController { @IBOutlet var contendView: NSView! @IBOutlet var preInfoView: NSView! @IBOutlet var propertyInfoView: NSView! var documentPreview: KMEditDocumentController = KMEditDocumentController.init() var propertyController: KMHeaderPropertyController = KMHeaderPropertyController.init() var templatesController: KMHFTemplateController = KMHFTemplateController() var saveTemplateMessage: ComponentMessage = ComponentMessage() var totalPDFCount: Int = 1 var pdfDocument: CPDFDocument? var headerFooterModel: KMHeaderFooterObject? { get { if editSubType == .template { return templatesController.selectedModel } else if editSubType == .add || editSubType == .edit { return propertyController.headerFooterModel } return nil } } weak open var delegate: KMHeaderFooterControllerDelegate? var editSubType: KMPDFEditSubModeType = .none 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() { documentPreview.pdfDocument = self.pdfDocument documentPreview.reloadData() propertyController.totalPDFCount = totalPDFCount if editSubType == .template { templatesController.reloadData() } else if editSubType == .add { propertyController.editSubType = editSubType propertyController.headerFooterModel = KMHeaderFooterManager.defaultManager.defaultHeaderFooter propertyController.reloadData() } else if editSubType == .edit { propertyController.reloadData() } updatePDFDocumentHeaderFooter() } func updatePDFDocumentHeaderFooter() { if let data = headerFooterModel { let cPDFHeaderFooter = documentPreview.pdfDocument?.headerFooter() cPDFHeaderFooter?.pageString = "0" let fontSize = data.fontsize let fontName: String = data.fontName let font = NSFont.boldSystemFont(ofSize:fontSize) let style = NSMutableParagraphStyle() style.alignment = .center style.lineBreakMode = .byCharWrapping let size: NSSize = "text".boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: [NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style]).size cPDFHeaderFooter?.margin = NSEdgeInsetsMake(max(CGFloat(data.topMargin)-size.height, 0), CGFloat(data.leftMargin), max(CGFloat(data.bottomMargin)-size.height, 0), CGFloat(data.rightMargin)) let strings = KMHeaderFooterObject.parseModel(model: data, pageCount: totalPDFCount) var count: Int = 0 let color = data.color for text in strings { cPDFHeaderFooter?.setText(text, at: UInt(count)) cPDFHeaderFooter?.setTextColor(color, at: UInt(count)) cPDFHeaderFooter?.setFontSize(fontSize, at: UInt(count)) cPDFHeaderFooter?.setFontName(fontName, at: UInt(count)) count += 1 } cPDFHeaderFooter?.update() documentPreview.refreshUI() } else { pdfDocument?.headerFooter().clear() documentPreview.refreshUI() } } } //MARK: - KMHeaderPropertyControllerDelegate extension KMHeaderFooterController: KMHeaderPropertyControllerDelegate { func headerFooterPropertyControllerDidUpdate(_ controller: KMHeaderPropertyController) { updatePDFDocumentHeaderFooter() delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self) } func headerFooterPropertyControllerDidChangetoTemplate(_ controller: KMHeaderPropertyController) { editSubType = .template resetUI() delegate?.kmHeaderFooterControllerDidUpdateMode?(self) updatePDFDocumentHeaderFooter() delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self) } func headerFooterPropertyControllerSaveTemplateSuccess(_ controller: KMHeaderPropertyController, _ data: KMHeaderFooterObject) { editSubType = .template resetUI() delegate?.kmHeaderFooterControllerDidUpdateMode?(self) templatesController.selectedModel = data templatesController.reloadData() updatePDFDocumentHeaderFooter() delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self) } func headerFooterPropertyControllerCancelTemplateEdit(_ controller: KMHeaderPropertyController) { editSubType = .template resetUI() delegate?.kmHeaderFooterControllerDidUpdateMode?(self) templatesController.selectedModel = controller.headerFooterModel templatesController.reloadData() updatePDFDocumentHeaderFooter() delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self) } func headerFooterPropertyControllerFinishTemplateEdit(_ controller: KMHeaderPropertyController) { editSubType = .template resetUI() delegate?.kmHeaderFooterControllerDidUpdateMode?(self) templatesController.selectedModel = controller.headerFooterModel templatesController.reloadData() updatePDFDocumentHeaderFooter() delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self) } } //MARK: - KMHFTemplateControllerDelegate extension KMHeaderFooterController: KMHFTemplateControllerDelegate { func templateControllerDidSelectedChanged(_ controller: KMHFTemplateController) { updatePDFDocumentHeaderFooter() delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self) } func templateControllerDidAddData(_ controller: KMHFTemplateController) { editSubType = .add resetUI() delegate?.kmHeaderFooterControllerDidUpdateMode?(self) propertyController.clearData() propertyController.headerFooterModel = KMHeaderFooterManager.defaultManager.defaultHeaderFooter propertyController.reloadData() updatePDFDocumentHeaderFooter() delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self) } func templateControllerDidEditData(_ controller: KMHFTemplateController, _ data: KMHeaderFooterObject) { editSubType = .edit resetUI() delegate?.kmHeaderFooterControllerDidUpdateMode?(self) propertyController.originalDataDict = KMHeaderFooterManager.defaultManager.parseModel(model: data) propertyController.headerFooterModel = data propertyController.reloadData() updatePDFDocumentHeaderFooter() delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self) } }