// // KMCropController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/20. // import Cocoa import KMComponentLibrary @objc protocol KMCropControllerDelegate: AnyObject { @objc optional func kmCropControllerDidCrop(_ controller: KMCropController, _ cropRect: CGRect, _ view: KMPageRangeSelectView) @objc optional func kmCropControllerDidCropSeparate(_ controller: KMCropController, _ view: KMPageRangeSelectView) @objc optional func kmCropControllerDidCropAuto(_ controller: KMCropController, _ view: KMPageRangeSelectView) @objc optional func kmCropControllerDidChangedSelectionOrMagnification(_ controller: KMCropController) } class KMCropController: NSViewController { @IBOutlet var contendView: NSView! @IBOutlet var preInfoView: NSView! @IBOutlet var propertyInfoView: NSView! private var documentPreview: KMEditDocumentController = KMEditDocumentController.init() private var coverController: KMCropCoverController? var pdfView: CPDFListView? { get { return documentPreview.pdfView } } var pdfDocument: CPDFDocument? var selectionRect: CGRect = .zero weak open var delegate: KMCropControllerDelegate? override func viewDidLoad() { super.viewDidLoad() // Do view setup here. setup() } func setup() { view.wantsLayer = true view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor documentPreview.view.frame = preInfoView.bounds documentPreview.view.autoresizingMask = [.width, .height] documentPreview.pdfView.pdfListViewDelegate = self preInfoView.addSubview(documentPreview.view) hiddenPropertyView(false) } func reloadData() { guard let pdfDocument = pdfDocument else { return } documentPreview.pdfDocument = pdfDocument documentPreview.reloadData() let page = pdfDocument.page(at: 0) let bounds = page?.bounds ?? CGRectZero documentPreview.pdfView.toolMode = .selectToolMode documentPreview.pdfView.selectionPageIndex = 0 if CGRectEqualToRect(selectionRect, CGRectZero) { selectionRect = bounds } else { documentPreview.pdfView.selectionRect = selectionRect documentPreview.pdfView.setNeedsDisplayForVisiblePages() } } func hiddenPropertyView(_ hidden: Bool) { } } //MARK: - CPDFListViewDelegate extension KMCropController: CPDFListViewDelegate { func pdfListViewChangedSelectionOrMagnification(_ pdfListView: CPDFListView!) { delegate?.kmCropControllerDidChangedSelectionOrMagnification?(self) } func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) { } } //MARK: - KMCropPropertyControllerDelegate extension KMCropController: KMCropPropertyControllerDelegate { func kmCropControllerDidCrop(_ controller: KMCropPropertyController, _ cropRect: CGRect, _ view: KMPageRangeSelectView) { delegate?.kmCropControllerDidCrop?(self, cropRect, view) } func kmCropControllerDidCropAuto(_ controller: KMCropPropertyController, _ view: KMPageRangeSelectView) { delegate?.kmCropControllerDidCropAuto?(self, view) } func kmCropControllerDidCropSeparate(_ controller: KMCropPropertyController, _ view: KMPageRangeSelectView) { delegate?.kmCropControllerDidCropSeparate?(self, view) } }