KMCropController.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // KMCropController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/20.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMCropControllerDelegate: AnyObject {
  10. @objc optional func kmCropControllerDidCrop(_ controller: KMCropController, _ cropRect: CGRect, _ view: KMPageRangeSelectView)
  11. @objc optional func kmCropControllerDidCropSeparate(_ controller: KMCropController, _ view: KMPageRangeSelectView)
  12. @objc optional func kmCropControllerDidCropAuto(_ controller: KMCropController, _ view: KMPageRangeSelectView)
  13. @objc optional func kmCropControllerDidChangedSelectionOrMagnification(_ controller: KMCropController)
  14. }
  15. class KMCropController: NSViewController {
  16. @IBOutlet var contendView: NSView!
  17. @IBOutlet var preInfoView: NSView!
  18. @IBOutlet var propertyInfoView: NSView!
  19. private var documentPreview: KMEditDocumentController = KMEditDocumentController.init()
  20. private var coverController: KMCropCoverController?
  21. var pdfView: CPDFListView? {
  22. get {
  23. return documentPreview.pdfView
  24. }
  25. }
  26. var pdfDocument: CPDFDocument?
  27. var selectionRect: CGRect = .zero
  28. weak open var delegate: KMCropControllerDelegate?
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. // Do view setup here.
  32. setup()
  33. }
  34. func setup() {
  35. view.wantsLayer = true
  36. view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  37. documentPreview.view.frame = preInfoView.bounds
  38. documentPreview.view.autoresizingMask = [.width, .height]
  39. documentPreview.pdfView.pdfListViewDelegate = self
  40. preInfoView.addSubview(documentPreview.view)
  41. hiddenPropertyView(false)
  42. }
  43. func reloadData() {
  44. guard let pdfDocument = pdfDocument else {
  45. return
  46. }
  47. documentPreview.pdfDocument = pdfDocument
  48. documentPreview.reloadData()
  49. let page = pdfDocument.page(at: 0)
  50. let bounds = page?.bounds ?? CGRectZero
  51. documentPreview.pdfView.toolMode = .selectToolMode
  52. documentPreview.pdfView.selectionPageIndex = 0
  53. if CGRectEqualToRect(selectionRect, CGRectZero) {
  54. selectionRect = bounds
  55. } else {
  56. documentPreview.pdfView.selectionRect = selectionRect
  57. documentPreview.pdfView.setNeedsDisplayForVisiblePages()
  58. }
  59. }
  60. func hiddenPropertyView(_ hidden: Bool) {
  61. }
  62. }
  63. //MARK: - CPDFListViewDelegate
  64. extension KMCropController: CPDFListViewDelegate {
  65. func pdfListViewChangedSelectionOrMagnification(_ pdfListView: CPDFListView!) {
  66. delegate?.kmCropControllerDidChangedSelectionOrMagnification?(self)
  67. }
  68. func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) {
  69. }
  70. }
  71. //MARK: - KMCropPropertyControllerDelegate
  72. extension KMCropController: KMCropPropertyControllerDelegate {
  73. func kmCropControllerDidCrop(_ controller: KMCropPropertyController, _ cropRect: CGRect, _ view: KMPageRangeSelectView) {
  74. delegate?.kmCropControllerDidCrop?(self, cropRect, view)
  75. }
  76. func kmCropControllerDidCropAuto(_ controller: KMCropPropertyController, _ view: KMPageRangeSelectView) {
  77. delegate?.kmCropControllerDidCropAuto?(self, view)
  78. }
  79. func kmCropControllerDidCropSeparate(_ controller: KMCropPropertyController, _ view: KMPageRangeSelectView) {
  80. delegate?.kmCropControllerDidCropSeparate?(self, view)
  81. }
  82. }