123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //
- // 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)
- }
- }
|