// // KMNPopAnnotationWindowController.swift // PDF Reader Pro // // Created by 丁林圭 on 2024/12/2. // import Cocoa import KMComponentLibrary let popOffSet:CGFloat = 8.0 typealias PaneCallback = (_ isOpenPane: Bool) -> () typealias UpdatePDFViewCallback = () -> () typealias FrameCallback = () -> () typealias CloseWindowCallback = () -> () class KMNPopAnnotationViewController: KMNBaseViewController { public var popType:ListViewPopType = .popTypeNone { didSet { rightOffsetConstraint.constant = 49.0 operationHeightConstraint.constant = 32.0 paneBox.isHidden = false switch popType { case .generaAnnotation : generaPopView.layoutSubtreeIfNeeded() operationWidthConstraint.constant = generaPopView.bounds.width self.view.display() //需刷新约束才会有值,不然会变化 operationBox.contentView = generaPopView congfigGeneraPopUI() break case .freeTextAnnotation: fontPopView.layoutSubtreeIfNeeded() operationWidthConstraint.constant = fontPopView.bounds.width self.view.display() //需刷新约束才会有值,不然会变化 operationBox.contentView = fontPopView break case .shapeAnnotation: var isContainSelfDot = false for i in 0.. 2) { vc.detailPopType = .freeTextMultpleAlight } vc.objectChangeAlighCallback = { [weak self] alight in self?.objectAlignType = alight self?.updatePDFViewCallback?() } ObjectPopover = createFilePopover vc.objectAlignType = objectAlignType } } @objc func linkGoButtonClicked(_ sender: NSView) { if linkType == .url { guard let webString = linkAnnotation.url() else { return } if let data = URL(string: webString) { NSWorkspace.shared.open(data) } } else if linkType == .email { guard let emailString = linkAnnotation.url() else { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "") alert.runModal() return } if KMNTools.isValidateEmail(emailString) { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Invalid Email. Please try again.", comment: "") alert.runModal() } else { NSWorkspace.shared.open(URL(string: emailString)!) } } else if linkType == .page { if let destination = linkAnnotation.destination() { listView?.go(to: destination) closeWindowCallback?() } } } @objc func linkBackButtonClicked(_ sender: NSView) { linkType = .linkSetting } @objc func fontColorChange(_ sender: Any) { if let color = (sender as? NSColorPanel)?.color { if let freeText = annotationPopMode.annotation as? CPDFFreeTextAnnotation { if listView?.isEdit(withCurrentFreeText: freeText) == true { listView?.commitEditAnnotationFreeText(freeText) } } annotationPopMode.setAnnotationFontColor(annotationFontColor: color) fontColor = color updatePDFViewCallback?() } } } //MARK: - ComponentCColorDelegate extension KMNPopAnnotationViewController: ComponentCColorDelegate { func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) { if(view == generaColorGroup) { self.annotationPopMode.setAnnotationColor(annotationColor: color) updatePDFViewCallback?() } else if (view == shapeColorGroup) { self.annotationPopMode.setAnnotationColor(annotationColor: color) updatePDFViewCallback?() } else if (view == fontColorItem) { fontColorItem.properties?.state = .normal fontColorItem.reloadData() let colorPanel = NSColorPanel.shared colorPanel.setTarget(self) colorPanel.showsAlpha = false colorPanel.setAction(#selector(self.fontColorChange(_:))) colorPanel.orderFront(nil) } } } //MARK: - ComponentInputDelegate extension KMNPopAnnotationViewController: ComponentInputDelegate { func componentInputDidChanged(inputView: ComponentInput) { if (inputView == fildNameInput) { for i in 0 ..< annotationPopMode.annotations.count { let annotation = annotationPopMode.annotations[i] if let widgt = annotation as? CPDFWidgetAnnotation { widgt.setFieldName(inputView.properties.text) } } updatePDFViewCallback?() } else if (inputView == gropNameInput) { for i in 0 ..< annotationPopMode.annotations.count { let annotation = annotationPopMode.annotations[i] if let button = annotation as? CPDFButtonWidgetAnnotation { button.setButtonWidgetStateString(inputView.properties.text) } } updatePDFViewCallback?() } else if (inputView == urlInput) { if linkType == .email { let emailString = urlInput.properties.text if KMNTools.isValidateEmail(emailString) { linkAnnotation.setDestination(nil) let linkUrlPath = KMNTools.judgeEmailURL(emailString) linkAnnotation.setURL(linkUrlPath) updatePDFViewCallback?() } } else if linkType == .url { let webString = urlInput.properties.text linkAnnotation.setDestination(nil) let linkUrlPath = KMNTools.judgeWebURL(webString) linkAnnotation.setURL(linkUrlPath) updatePDFViewCallback?() } } } func componentInputDidEndEditing(inputView: ComponentInput) { if (inputView == fildNameInput) { } else if (inputView == gropNameInput) { } else if (inputView == urlInput) { } } } //MARK: - ComponentSelectDelegate extension KMNPopAnnotationViewController: ComponentSelectDelegate { func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) { if(view == fontNameSelect) { if menuItemProperty?.identifier == "1" { let familyName = fontNameSelect.properties.text ?? "Helvetica" let styles = CPDFFont.fontNames(forFamilyName: familyName) compdfFont = CPDFFont(familyName: familyName, fontStyle: styles.first ?? "") annotationPopMode.setAnnotationFont(font: compdfFont ?? CPDFFont.init(familyName: "Helvetica", fontStyle: "")) updatePDFViewCallback?() } else if menuItemProperty?.identifier == "2"{ //字体样式 compdfFont = CPDFFont(familyName: compdfFont?.familyName ?? "Helvetica", fontStyle: menuItemProperty?.text ?? "") annotationPopMode.setAnnotationFont(font: compdfFont ?? CPDFFont.init(familyName: "Helvetica", fontStyle: "")) updatePDFViewCallback?() } } } } //MARK: - ComponentSelectDelegate extension KMNPopAnnotationViewController: NSPopoverDelegate { func popoverWillClose(_ notification: Notification) { if fontPopover == (notification.object as? NSPopover) { if(fontAlightButton.properties.state == .pressed) { fontAlightButton.properties.state = .normal fontAlightButton.reloadData() } } else if ObjectPopover == (notification.object as? NSPopover) { if(alightButton.properties.state == .pressed) { alightButton.properties.state = .normal alightButton.reloadData() } } } } //MARK: - ComponentPaginationDelegate extension KMNPopAnnotationViewController: ComponentPaginationDelegate { public func componentPaginationDidValueChanged(pagination: ComponentPagination) { let pageIndex = pagination.properties.currentIndex if let page = listView?.document.page(at: UInt(pageIndex)) { linkAnnotation.setURL(nil) let destination = CPDFDestination(document: listView?.document ?? CPDFDocument(), pageIndex: Int(paginationView.properties.currentIndex - 1)) linkAnnotation.setDestination(destination) refreshPageThum() reloadPageUrlData() updatePDFViewCallback?() } } }