Przeglądaj źródła

【2025】【Edit】裁剪逻辑完善

niehaoyu 1 miesiąc temu
rodzic
commit
4a4b506adb

+ 35 - 1
PDF Office/PDF Master/KMClass/KMPDFViewController/EditTool/Crop/KMCropManager.swift

@@ -8,5 +8,39 @@
 import Cocoa
 
 class KMCropManager: NSObject {
-
+    
+    static let defaultManager = KMCropManager()
+    
+    var cropRect: CGRect? = nil
+    
+    var cropSeparateOn: Bool = false {
+        didSet {
+            UserDefaults.setDefaultBoolValue(cropSeparateOn, toKey: "CropSeparateOnKey") 
+         }
+    }
+    
+    var cropAutoOn: Bool = false  {
+        didSet {
+            UserDefaults.standard.setValue(cropAutoOn, forKey: "CropAutoOnKey")
+        }
+    }
+     
+    override init() {
+        super.init()
+         
+        self.initData()
+        
+    }
+     
+    func initData() {
+        self.cropSeparateOn = UserDefaults.getDefaultBoolValue(forKey: "CropSeparateOnKey")
+        
+        self.cropAutoOn = UserDefaults.getDefaultBoolValue(forKey: "CropAutoOnKey")
+    }
+    
+    func clear() {
+        cropRect = nil
+        
+    }
+    
 }

+ 14 - 36
PDF Office/PDF Master/KMClass/KMPDFViewController/EditTool/Crop/Views/KMCropPropertyController.swift

@@ -53,9 +53,7 @@ class KMCropPropertyController: NSViewController {
     @IBOutlet var pangeRangeSelectView: KMPageRangeSelectView!
     
     @IBOutlet var cropButton: ComponentButton!
-    
-    private var _cropSeparateOn: Bool = false
-    private var _cropAutoOn: Bool = false
+     
     private var _syncChangeBounds: Bool = false //同步修改宽高
     
     weak open var delegate: KMCropPropertyControllerDelegate?
@@ -68,8 +66,6 @@ class KMCropPropertyController: NSViewController {
         super.viewDidLoad()
         // Do view setup here.
         
-        cropSeparateOn = UserDefaults.standard.bool(forKey: "CropSeparateOnKey")
-        cropAutoOn = UserDefaults.standard.bool(forKey: "CropAutoOnKey")
         if UserDefaults.standard.object(forKey: "CropSyncChangeBoundsKey") == nil {
             UserDefaults.standard.setValue(true, forKey: "CropSyncChangeBoundsKey")
         }
@@ -81,27 +77,6 @@ class KMCropPropertyController: NSViewController {
     }
     
     //MARK: - Setter and Getter
-    public var cropSeparateOn : Bool {
-        get {
-            return _cropSeparateOn
-        }
-        set {
-            _cropSeparateOn = newValue
-            
-            UserDefaults.standard.setValue(_cropSeparateOn, forKey: "CropSeparateOnKey")
-        }
-    }
-    
-    public var cropAutoOn : Bool {
-        get {
-            return _cropAutoOn
-        }
-        set {
-            _cropAutoOn = newValue
-            
-            UserDefaults.standard.setValue(_cropAutoOn, forKey: "CropAutoOnKey")
-        }
-    }
     
     public var syncChangeBounds : Bool {
         get {
@@ -213,10 +188,10 @@ class KMCropPropertyController: NSViewController {
     
     func reloadData() {
         //White Margin
-        separateSwitch.properties.open = cropSeparateOn
+        separateSwitch.properties.open = KMCropManager.defaultManager.cropSeparateOn
         separateSwitch.reloadData()
         
-        autoSwitch.properties.open = cropAutoOn
+        autoSwitch.properties.open = KMCropManager.defaultManager.cropAutoOn
         autoSwitch.reloadData()
         
         guard let pdfView = self.pdfView else {
@@ -279,9 +254,9 @@ class KMCropPropertyController: NSViewController {
     }
     
     @objc func cropButtonClicked(_ sender: ComponentButton) {
-        if cropSeparateOn {
+        if KMCropManager.defaultManager.cropSeparateOn {
             delegate?.kmCropControllerDidCropSeparate?(self, pangeRangeSelectView)
-        } else if cropAutoOn {
+        } else if KMCropManager.defaultManager.cropAutoOn {
             delegate?.kmCropControllerDidCropAuto?(self, pangeRangeSelectView)
         } else {
             delegate?.kmCropControllerDidCrop?(self, pdfView?.selectionRect ?? CGRectZero, pangeRangeSelectView)
@@ -298,16 +273,18 @@ class KMCropPropertyController: NSViewController {
         }
         
         if sender == separateSwitch {
-            cropSeparateOn = sender.properties.open
-            cropAutoOn = false
+            KMCropManager.defaultManager.cropSeparateOn = sender.properties.open
+            KMCropManager.defaultManager.cropAutoOn = false
+            KMCropManager.defaultManager.cropRect = nil
             
             let rect = KMCropTools.getPageForegroundBox(page)
             pdfView.selectionRect = rect
             pdfView.setNeedsDisplayForVisiblePages()
             
         } else if sender == autoSwitch {
-            cropAutoOn = sender.properties.open
-            cropSeparateOn = false
+            KMCropManager.defaultManager.cropAutoOn = sender.properties.open
+            KMCropManager.defaultManager.cropSeparateOn = false
+            KMCropManager.defaultManager.cropRect = nil
             
             let rect = KMCropTools.getPageForegroundBox(page)
             pdfView.selectionRect = rect
@@ -368,8 +345,9 @@ extension KMCropPropertyController: ComponentInputNumberDelegate {
         pdfView.selectionRect = rect
         pdfView.setNeedsDisplayForVisiblePages()
         
-        cropAutoOn = false
-        cropSeparateOn = false
+        KMCropManager.defaultManager.cropRect = rect
+        KMCropManager.defaultManager.cropAutoOn = false
+        KMCropManager.defaultManager.cropSeparateOn = false
         
         reloadData()
     }

+ 18 - 3
PDF Office/PDF Master/KMClass/KMPDFViewController/KMMainViewController.swift

@@ -1623,7 +1623,20 @@ struct KMNMWCFlags {
         if let editPage = editDocument?.page(at: 0) {
             let mediaRect = editPage.bounds(for: .mediaBox)
             editPage.setBounds(mediaRect, for: .cropBox)
-            controller.selectionRect = page?.bounds(for: .cropBox) ?? .zero
+            
+            if let rect = KMCropManager.defaultManager.cropRect {
+                controller.selectionRect = rect
+            } else {
+                if KMCropManager.defaultManager.cropSeparateOn {
+                    let rect = KMCropTools.getPageForegroundBox(editPage)
+                    controller.selectionRect = rect
+                } else if KMCropManager.defaultManager.cropAutoOn {
+                    let rect = KMCropTools.getPageForegroundBox(editPage)
+                    controller.selectionRect = rect
+                } else {
+                    controller.selectionRect = page?.bounds(for: .cropBox) ?? .zero
+                }
+            }
             
             if let cropVC = rightSideController?.edit_cropController {
                 cropVC.pageCropBounds = page?.bounds(for: .cropBox) ?? .zero
@@ -1639,6 +1652,8 @@ struct KMNMWCFlags {
             cropController?.view.removeFromSuperview()
             cropController = nil
             
+            KMCropManager.defaultManager.clear()
+            
             toolbarManager.edit_crop_Property.state = .normal
         }
     }
@@ -4841,8 +4856,8 @@ extension KMMainViewController: KMCropControllerDelegate {
             if cropVC.pdfView != controller.pdfView {
                 cropVC.pdfView = controller.pdfView
             }
-            cropVC.cropSeparateOn = false
-            cropVC.cropAutoOn = false
+            KMCropManager.defaultManager.cropSeparateOn = false
+            KMCropManager.defaultManager.cropAutoOn = false
             cropVC.reloadData()
         }
     }