Browse Source

Merge branch 'develop_PDFReaderProNew' of git.kdan.cc:Mac_PDF/PDF_Office into develop_PDFReaderProNew

tangchao 1 year ago
parent
commit
dc3016beaf

+ 32 - 24
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/StampList/CreateStamp/Controller/KMStampCreaterWindowController.swift

@@ -43,6 +43,8 @@ class KMStampCreaterWindowController: NSWindowController, NSTextViewDelegate{
     @IBOutlet private var dateLabel: NSTextField!
     @IBOutlet private var timeLabel: NSTextField!
     
+    private var handler: ((Int) -> Void)!
+
     convenience init(){
         self.init(windowNibName: "KMStampCreaterWindowController")
     }
@@ -114,18 +116,6 @@ class KMStampCreaterWindowController: NSWindowController, NSTextViewDelegate{
         boardView.frame = NSRect(x: bounds.origin.x - 4, y: bounds.origin.y - 4, width: bounds.size.width + 8, height: bounds.size.height + 8)
     }
     
-    func beginSheetModal(for window: NSWindow, completionHandler handler: ((Int) -> Void)?) {
-        struct Context {
-            var handler: ((Int) -> Void)?
-        }
-        
-        var context = Context(handler: handler)
-        
-        // Pass the address of the struct as contextInfo
-        let contextPointer = UnsafeMutableRawPointer(&context)
-        NSApp.beginSheet(self.window!, modalFor: window, modalDelegate: self, didEnd: #selector(dismissSheet(_:)), contextInfo: contextPointer)
-    }
-    
     @IBAction private func colorButton_Click(_ sender: NSButton) {
         changeBoardView(bounds: sender.frame)
         self.stamp?.bounds = self.preView.bounds
@@ -223,24 +213,42 @@ class KMStampCreaterWindowController: NSWindowController, NSTextViewDelegate{
             self.preView.image = self.stamp?.stampPreviewImage()
         }
     }
+    
+    // MARK: Show Methods
  
     @IBAction private func dismissSheet(_ sender: NSButton) {
-        if sender == self.addButton {
-            if self.stamp?.contentStr.count ?? 0 < 1 && self.stamp?.dateString.count ?? 0 < 1 {
-                return
-            }
-            if KMStampManagerNew.shareManager.addStamp(withString: self.stamp?.contentStr, dateString: self.stamp?.dateString, color: self.stamp?.stampColor ?? .RedColor){
-                
-            }
-            if (self.closeCallBack != nil) {
-                closeCallBack!(1)
+        if #available(macOS 10.13, *) {
+            if sender == self.addButton {
+                if ((stamp?.contentStr.count)! < 1) && ((stamp?.dateString.count)! < 1) {
+                    return
+                }
+                let stampStyle: CPDFStampStyle = CPDFStampStyle(rawValue: (stamp?.stampStyle(stampColor: KMAnnotationStampColorType(rawValue: (stamp?.stampColor)!.rawValue) ?? .RedColor))!.rawValue) ?? .white
+                let stampShape: CPDFStampShape = CPDFStampShape(rawValue: (stamp?.stampShape(stampColor: KMAnnotationStampColorType(rawValue: (stamp?.stampColor)!.rawValue) ?? .RedColor))!.rawValue) ?? .rectangle
+                KMStampManager.defaultManager.addStamp(withString: self.stamp?.contentStr, categoryString: .text, nameString: nil, dateString: self.stamp?.dateString, needExchangeFontSize: false, color: stampStyle, shape: stampShape, date: nil, time: nil)
+                NSApp.endSheet(window!, returnCode: 1)
+            } else {
+                NSApp.endSheet(window!, returnCode: 0)
             }
         } else {
-            if (self.closeCallBack != nil) {
-                closeCallBack!(0)
+            NSApp.endSheet(window!)
+        }
+        window!.orderOut(self)
+    }
+    
+    @objc private func didEndSheet(_ sheet: NSWindow?, returnCode: Int, contextInfo: UnsafeMutableRawPointer?) {
+        if contextInfo != nil && self.handler != nil {
+            self.handler!(returnCode)
+        }
+    }
+    
+    func beginSheetModal(for window: NSWindow?, completionHandler handler: ((Int) -> Void)?) {
+        if window != nil {
+            window!.beginSheet(self.window!) { ModalResponse in
+                self.handler?(ModalResponse.rawValue)
             }
         }
-        self.km_quick_endSheet()
+        self.handler = handler
     }
+
 }
 

+ 50 - 1
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/StampList/CreateStamp/View/KMAnnotationStamp.swift

@@ -308,6 +308,36 @@ let StampStringEdgeSizeScale = 0.1
         self.imageAlpha = 1.0
     }
     
+    func stampStyle(stampColor: KMAnnotationStampColorType) -> CPDFStampStyle {
+        switch stampColor {
+        case .RedColor, .LeftStampRedColor, .RightStampRedColor:
+            return .red
+        case .GreenColor, .LeftStampGreenColor, .RightStampGreenColor:
+            return .green
+        case .BlueColor, .LeftStampBlueColor, .RightStampBlueColor:
+            return .blue
+        case .BlackColor, .Normal:
+            return .white
+        default:
+            return .white
+        }
+    }
+    
+    func stampShape(stampColor: KMAnnotationStampColorType) -> CPDFStampShape {
+        switch stampColor {
+        case .RedColor, .GreenColor, .BlueColor, .BlackColor:
+            return .rectangle
+        case .LeftStampRedColor, .LeftStampGreenColor, .LeftStampBlueColor:
+            return .arrowLeft
+        case .RightStampRedColor, .RightStampGreenColor, .RightStampBlueColor:
+            return .arrowRight
+        case .Normal:
+            return .none
+        default:
+            return .rectangle
+        }
+    }
+    
     func sizeOfString(string: String, witFontSize fontSize: CGFloat) -> NSSize {
         let font = NSFont(name: "Helvetica", size: fontSize)
         
@@ -329,6 +359,7 @@ let StampStringEdgeSizeScale = 0.1
         
         return size
     }
+    
     func stampPreviewImage() -> NSImage? {
         if !self.isStandardStamp {
             self.setCustomStampString(self.contentStr, dateString: self.dateString)
@@ -353,6 +384,7 @@ let StampStringEdgeSizeScale = 0.1
         
         return image
     }
+    
     func drawPreviewTextWithContextRef(_ context: CGContext?) {
         NSGraphicsContext.saveGraphicsState()
         if context != nil {
@@ -483,6 +515,7 @@ let StampStringEdgeSizeScale = 0.1
         }
         NSGraphicsContext.restoreGraphicsState()
     }
+    
     func stampStringDefaultFontSize() -> CGFloat {
         var tSize = StampStringDefaultFontSize
         if self.needExchangeFontSize {
@@ -490,6 +523,7 @@ let StampStringEdgeSizeScale = 0.1
         }
         return tSize
     }
+    
     func stampDateStringDefaultFontSize() -> CGFloat {
         var tSize = StampDateStringDefaultFontSize
         if self.needExchangeFontSize {
@@ -497,10 +531,10 @@ let StampStringEdgeSizeScale = 0.1
         }
         return tSize
     }
+    
     func setCustomStampString(_ string: String, dateString: String) {
         isStandardStamp = false
         isCustomImageStamp = false
-//        self.setName("")
         self.contentStr = string
         self.dateString = dateString
         
@@ -559,6 +593,7 @@ let StampStringEdgeSizeScale = 0.1
         self.bounds = bounds
         self.borderBounds = self.bounds
     }
+    
     func getBtnStampImage() -> NSImage? {
         if self.isCustomImageStamp {
             return self.image
@@ -587,6 +622,7 @@ let StampStringEdgeSizeScale = 0.1
             return image
         }
     }
+    
     func drawBorderWithContext(context: CGContext) {
         if .Witness == self.customStampType {
             c01 = 1-0.0274506
@@ -702,6 +738,7 @@ let StampStringEdgeSizeScale = 0.1
             }
         }
     }
+    
     func drawArrowsBorder(context: CGContext, withBounds tmpBounds: NSRect) { context.beginPath()
         let tmpHeight = 11.0 * tmpBounds.size.height/50
         let tmpWidth = 3.0 * tmpBounds.size.height/50
@@ -757,6 +794,7 @@ let StampStringEdgeSizeScale = 0.1
         
         context.strokePath()
     }
+    
     func drawTextWithContextRef(context: CGContext?) {
         NSGraphicsContext.saveGraphicsState()
         if context != nil {
@@ -934,6 +972,7 @@ let StampStringEdgeSizeScale = 0.1
             }
         }
     }
+    
     func drawTickBorder(context: CGContext, withBounds tmpBounds: CGRect) {
         let w = tmpBounds.size.height / 23
         let h1 = 20.05784 * w
@@ -985,6 +1024,7 @@ let StampStringEdgeSizeScale = 0.1
         context.addCurve(to: CGPoint(x: tmpBounds.origin.x + w6, y: tmpBounds.origin.y + h6), control1: CGPoint(x: tmpBounds.origin.x + w6 - 2.57129 * w, y: tmpBounds.origin.y + h6 + 1.68213 * w), control2: CGPoint(x: tmpBounds.origin.x + w1 - 0.05078 * w, y: tmpBounds.origin.y + h1 - 0.02491 * w))
         context.strokePath()
     }
+    
     func drawCrossBorder(context: CGContext, withBounds tmpBoundsOring: NSRect) {
         let w = tmpBoundsOring.size.height / 23
         let tmpBounds = tmpBoundsOring.insetBy(dx: 2 * w, dy: 2 * w)
@@ -1048,6 +1088,7 @@ let StampStringEdgeSizeScale = 0.1
         
         context.strokePath()
     }
+    
     func drawBlackCross(context: CGContext, withBounds tmpBounds: CGRect) {
         let w = tmpBounds.size.height / 18
         let insetBounds = tmpBounds.insetBy(dx: 2 * w, dy: 2 * w)
@@ -1064,6 +1105,7 @@ let StampStringEdgeSizeScale = 0.1
         
         context.strokePath()
     }
+    
     func drawBlackChick(context: CGContext, withBounds tmpBounds: CGRect) {
         let w = tmpBounds.size.height / 18
         let insetBounds = tmpBounds.insetBy(dx: 2 * w, dy: 2 * w)
@@ -1078,6 +1120,7 @@ let StampStringEdgeSizeScale = 0.1
         
         context.strokePath()
     }
+    
     func drawBlackCircle(context: CGContext, withBounds tmpBounds: CGRect) {
         let w = tmpBounds.size.height / 18
         let magicNumber: CGFloat = 0.551784
@@ -1097,6 +1140,7 @@ let StampStringEdgeSizeScale = 0.1
         context.drawPath(using: .fillStroke)
         context.strokePath()
     }
+    
     func drawLeftCommonBorder(context: CGContext, withBounds tmpBounds: CGRect) {
         drawLeftBounds(context: context, withBounds: tmpBounds)
         context.clip()
@@ -1122,6 +1166,7 @@ let StampStringEdgeSizeScale = 0.1
         
         context.strokePath()
     }
+    
     func drawLeftBounds(context: CGContext, withBounds tmpBounds: CGRect) {
         let tmpHeight = 11.0 * tmpBounds.size.height / 50
         let tmpWidth = 3.0 * tmpBounds.size.height / 50
@@ -1150,6 +1195,7 @@ let StampStringEdgeSizeScale = 0.1
         
         context.closePath()
     }
+    
     func drawRightCommonBorder(context: CGContext, withBounds tmpBounds: CGRect) { drawRightBounds(context: context, withBounds: tmpBounds)
         context.clip()
         
@@ -1174,6 +1220,7 @@ let StampStringEdgeSizeScale = 0.1
         drawRightBounds(context: context, withBounds: tmpBounds)
         context.strokePath()
     }
+    
     func drawRightBounds(context: CGContext, withBounds tmpBounds: NSRect) {
         let tmpHeight = 11.0 * tmpBounds.size.height/50
         let tmpWidth = 3.0 * tmpBounds.size.height/50
@@ -1202,6 +1249,7 @@ let StampStringEdgeSizeScale = 0.1
         
         context.closePath()
     }
+    
     func drawNormalBorder(context: CGContext, withBounds tmpBounds: NSRect) {
         context.beginPath()
         let tmpHeight = 11.0 * tmpBounds.size.height/50
@@ -1254,6 +1302,7 @@ let StampStringEdgeSizeScale = 0.1
         
         context.strokePath()
     }
+    
     func drawCommonBorder(context: CGContext, withBounds tmpBounds: CGRect) {
         context.beginPath()
         let tmpHeight = 11.0 * tmpBounds.size.height/50

+ 20 - 11
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/StampList/KMAnnotationStampViewController.swift

@@ -615,7 +615,6 @@ let KMPDFDynamicStampNeedAuthorKey1 = "km_pdfview_dynamic_stamp_need_author"
                 self.customStamps?.append(obj)
             }
         }
-
         if let customStamps = self.customStamps, customStamps.count > 0 {
             reloadCustomEmptyState(false)
         } else {
@@ -892,7 +891,9 @@ let KMPDFDynamicStampNeedAuthorKey1 = "km_pdfview_dynamic_stamp_need_author"
         let index = button.tag
         
         if annotationStampType == .type_default {
-            if KMStampManagerNew.shareManager.removeStamp(at: index) {
+            if stampType == .custom {
+                let stampObject = customStamps[index]
+                let isRemove = KMStampManager.defaultManager.removeStamp(stampObject)
                 loadCustomStamps()
                 createStampTableView.reloadData()
             }
@@ -1040,17 +1041,25 @@ let KMPDFDynamicStampNeedAuthorKey1 = "km_pdfview_dynamic_stamp_need_author"
         }
         
         let stampCreaterWindowController = KMStampCreaterWindowController()
-        stampCreaterWindowController.closeCallBack = { [weak self] result in
-            if result == 0 {
-                return
-            }
-            
-            self?.loadCustomStamps()
-            Task { @MainActor in
-                self?.createStampTableView.reloadData()
+//        stampCreaterWindowController.closeCallBack = { [weak self] result in
+//            if result == 0 {
+//                return
+//            }
+//            
+//            self?.loadCustomStamps()
+//            Task { @MainActor in
+//                self?.createStampTableView.reloadData()
+//            }
+//        }
+//        self.km_beginSheet(windowC: stampCreaterWindowController)
+        stampCreaterWindowController.beginSheetModal(for: self.view.window) { [weak self] result in
+            if result == 1 {
+                self?.loadCustomStamps()
+                Task { @MainActor in
+                    self?.createStampTableView.reloadData()
+                }
             }
         }
-        self.km_beginSheet(windowC: stampCreaterWindowController)
     }
     
     @objc func delete() {

+ 1 - 2
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+Action.swift

@@ -2950,9 +2950,8 @@ extension KMMainViewController {
         let stampCreaterWindowController = KMStampCreaterWindowController()
         stampCreaterWindowController.window?.backgroundColor = .clear
         self.km_beginSheet(windowC: stampCreaterWindowController)
-        stampCreaterWindowController.closeCallBack = { result in
+        stampCreaterWindowController.beginSheetModal(for: self.view.window) { [weak self] result in
             if result == 1 {
-                
             }
         }
     }