Browse Source

【注释】文本注释属性面板线段弹窗功能补充

wanjun 11 months ago
parent
commit
f799a371f5

+ 49 - 10
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/AnnotationProperty/KMGeneralAnnotationViewController.swift

@@ -1751,18 +1751,57 @@ let KMColorPickerViewHeight: CGFloat = 64
     }
     
     @IBAction func borderButtonAction(_ sender: NSButton) {
-        let fontWindowController = KMAnnotationLineWindowController.sharedAnnotationLine
-        guard let window = fontWindowController.window else { return }
-        fontWindowController.annotations = annotations
-        fontWindowController.annotationLineChangeBlock = { [weak self] lineNote in
-            if lineNote.borderStyle() == .solid {
-                self!.lineTypeButtonAction(self!.lineStyleButton)
-            } else if lineNote.borderStyle() == .dashed {
-                self!.lineTypeButtonAction(self!.dottedLineStyleButton)
-                for tAnnotation in self!.annotations {
-                    tAnnotation.setDashPattern(lineNote.dashPattern())
+        var lineWidth: CGFloat = 0.0
+        var lineStype: CPDFBorderStyle = .solid
+        var startingPoint: CPDFLineStyle = .none
+        var endPoint: CPDFLineStyle = .none
+        var dashPattern: [NSNumber] = []
+        var isLineAnnotation: Bool = false
+        if annotationModel?.annotations != nil {
+            for tAnnotation in self.annotations {
+                lineWidth = tAnnotation.lineWidth()
+                lineStype = tAnnotation.borderStyle()
+                dashPattern = tAnnotation.dashPattern() as! [NSNumber]
+                if tAnnotation is CPDFLineAnnotation {
+                    isLineAnnotation = true
+                    startingPoint = (tAnnotation as! CPDFLineAnnotation).startLineStyle
+                    endPoint = (tAnnotation as! CPDFLineAnnotation).endLineStyle
+                } else {
+                    isLineAnnotation = false
                 }
             }
+        } else {
+            lineWidth = String(format: "%0.1f pt", (annotationModel?.lineWidth())!).stringToCGFloat()
+            lineStype = (annotationModel?.style())! as CPDFBorderStyle
+            dashPattern = (annotationModel?.dashPattern())! as [NSNumber]
+            if annotationModel?.annotationType == .line {
+                isLineAnnotation = true
+                startingPoint = (annotationModel?.startLineStyle())! as CPDFLineStyle
+                endPoint = (annotationModel?.endLineStyle())! as CPDFLineStyle
+            } else {
+                isLineAnnotation = false
+            }
+        }
+        let lineModel = KMLineModel(lineWidth: lineWidth, lineStype: lineStype, startingPoint: startingPoint, endPoint: endPoint, dashPattern: dashPattern, isLineAnnotation: isLineAnnotation)
+
+        let fontWindowController = KMAnnotationLineWindowController.initWindowController(lineModel)
+        guard let window = fontWindowController.window else { return }
+//        fontWindowController.annotationLineChangeBlock = { [weak self] lineNote in
+//            if lineNote.borderStyle() == .solid {
+//                self!.lineTypeButtonAction(self!.lineStyleButton)
+//            } else if lineNote.borderStyle() == .dashed {
+//                self!.lineTypeButtonAction(self!.dottedLineStyleButton)
+//                for tAnnotation in self!.annotations {
+//                    tAnnotation.setDashPattern(lineNote.dashPattern())
+//                }
+//            }
+//        }
+        fontWindowController.callback = { model in
+            if self.annotationModel?.annotations != nil {
+                
+            } else {
+                
+            }
         }
         
         window.orderFront(sender)

+ 60 - 126
PDF Office/PDF Master/Class/PDFWindowController/Side/RightSide/AnnotationProperty/WindowController/KMAnnotationLineWindowController.swift

@@ -7,12 +7,32 @@
 
 import Cocoa
 
+@objcMembers
+class KMLineModel: NSObject {
+    var lineWidth: CGFloat = 0.0
+    var lineStype: CPDFBorderStyle = .solid
+    var startingPoint: CPDFLineStyle = .none
+    var endPoint: CPDFLineStyle = .none
+    var dashPattern: [NSNumber] = []
+    var isLineAnnotation: Bool = false
+
+    init(lineWidth: CGFloat, lineStype: CPDFBorderStyle, startingPoint: CPDFLineStyle, endPoint: CPDFLineStyle, dashPattern: [NSNumber], isLineAnnotation: Bool) {
+        self.lineWidth = lineWidth
+        self.lineStype = lineStype
+        self.startingPoint = startingPoint
+        self.endPoint = endPoint
+        self.dashPattern = dashPattern
+        self.isLineAnnotation = isLineAnnotation
+    }
+}
+
+@objcMembers
 class KMAnnotationLineWindowController: NSWindowController, NSTextFieldDelegate {
+    static var sharedAnnotationLine: KMAnnotationLineWindowController!
+
+    var lineModel: KMLineModel?
+    var callback: ((KMLineModel) -> Void)?
 
-    var _annotations: [CPDFAnnotation]?
-    var annotationLineChangeBlock: ((CPDFAnnotation) -> Void)?
-    static let sharedAnnotationLine: KMAnnotationLineWindowController = KMAnnotationLineWindowController()
-    
     @IBOutlet var lineWidthLabel: NSTextField!
     @IBOutlet var lineWidthTextField: NSTextField!
     @IBOutlet var lineWidthStepper: NSStepper!
@@ -34,36 +54,26 @@ class KMAnnotationLineWindowController: NSWindowController, NSTextFieldDelegate
     @IBOutlet var endPointPopUpButton: NSPopUpButton!
     @IBOutlet var endPointLayoutConstraint: NSLayoutConstraint!
 
-    var _annotation: CPDFAnnotation?
     var widthFloat: CGFloat = 0.0
 
     // MARK: Init Methods
     
     deinit {
         NotificationCenter.default.removeObserver(self)
-        
-        if let textAnnotation = self.annotation as? CPDFTextAnnotation {
-            let keys: [String] = ["string", "color"]
-            for key in keys {
-                textAnnotation.removeObserver(self, forKeyPath: key)
-            }
+    }
+    
+    static func initWindowController(_ lineModel: KMLineModel) -> KMAnnotationLineWindowController {
+        if sharedAnnotationLine != nil {
+            sharedAnnotationLine.lineModel = lineModel
+            return sharedAnnotationLine
         } else {
-            let keys: [String] = ["string", "color", "interiorColor", "border", "font", "fontColor", "alignment"]
-            for key in keys {
-                self.annotation.removeObserver(self, forKeyPath: key)
-            }
+            let configWC: KMAnnotationLineWindowController = KMAnnotationLineWindowController.init(windowNibName: "KMAnnotationLineWindowController")
+            sharedAnnotationLine = configWC;
+            sharedAnnotationLine.lineModel = lineModel
+            return sharedAnnotationLine
         }
     }
     
-//    convenience init() {
-//        let windowNibName = NSNib.Name("KMAnnotationFontWindowController")
-//        self.init(windowNibName: "KMAnnotationFontWindowController")
-//    }
-//
-//    required init?(coder: NSCoder) {
-//        super.init(coder: coder)
-//    }
-    
     override func windowDidLoad() {
         super.windowDidLoad()
 
@@ -97,64 +107,20 @@ class KMAnnotationLineWindowController: NSWindowController, NSTextFieldDelegate
 
         NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: dottedLineTextField)
     }
-    
-    // MARK: Get & Set
-    
-    var annotations: [CPDFAnnotation] {
-        set {
-            _annotations = newValue
-            
-            annotation = newValue.first!
-            self.reloadData()
-        }
-        get {
-            return _annotations!
-        }
-    }
-    
-    var annotation: CPDFAnnotation {
-        set {
-            let equal: Bool = ((_annotation?.isEqual(newValue)) != nil)
-            if equal {
-                if _annotation is CPDFTextAnnotation {
-                    let keys = ["string", "color"]
-//                    for key in keys {
-//                        _annotation?.removeObserver(self, forKeyPath: key)
-//                    }
-                    _annotation = newValue
-                    for key in keys {
-                        _annotation?.addObserver(self, forKeyPath: key, options: [.new, .old], context: nil)
-                    }
-                } else {
-                    let keys = ["string", "color", "interiorColor", "border", "font", "fontColor", "alignment"]
-//                    for key in keys {
-//                        _annotation?.removeObserver(self, forKeyPath: key)
-//                    }
-                    _annotation = newValue
-                    for key in keys {
-                        _annotation?.addObserver(self, forKeyPath: key, options: [.new, .old], context: nil)
-                    }
-                }
-            }
-        }
-        get {
-            return _annotation!
-        }
-    }
 
     
     // MARK: View Methods
     
     func reloadData() {
-        self.widthFloat = annotation.border.lineWidth
-        let lineDashPattern = annotation.border.dashPattern
+        self.widthFloat = self.lineModel!.lineWidth
+        let lineDashPattern = self.lineModel!.dashPattern
         
         let startMenu = startingPointPopUpButton.menu
         let endMenu = endPointPopUpButton.menu
         
-        if let lineAnnotation = annotation as? CPDFLineAnnotation {
-            let startLineStyle = lineAnnotation.startLineStyle
-            let endLineStyle = lineAnnotation.endLineStyle
+        if self.lineModel!.isLineAnnotation {
+            let startLineStyle = self.lineModel!.startingPoint
+            let endLineStyle = self.lineModel!.endPoint
             let startArr = self.startLineStyle()
             let endArr = self.endLineStyle()
             
@@ -189,10 +155,10 @@ class KMAnnotationLineWindowController: NSWindowController, NSTextFieldDelegate
         lineWidthStepper.cell?.floatValue = Float(widthFloat)
         lineWidthTextField.stringValue = String(format: "%.1f pt", widthFloat)
         
-        lineStypePopUpButton.selectItem(at: annotation.border.style.rawValue)
+        lineStypePopUpButton.selectItem(at: self.lineModel!.lineStype.rawValue)
         
-        if lineDashPattern!.count > 0 {
-            dottedLineTextField.stringValue = "\(lineDashPattern![0] as? String ?? "")"
+        if lineDashPattern.count > 0 {
+            dottedLineTextField.stringValue = "\(lineDashPattern[0])"
         } else {
             dottedLineTextField.stringValue = ""
         }
@@ -510,77 +476,46 @@ class KMAnnotationLineWindowController: NSWindowController, NSTextFieldDelegate
     @IBAction func lineTypeAction(_ sender: NSMenuItem) {
         let tag = sender.tag
         
-        for tAnnotation in self.annotations {
-            tAnnotation.setBorderStyle(CPDFBorderStyle(rawValue: tag) ?? .solid)
-            
-            if let annotationLineChangeBlock = self.annotationLineChangeBlock {
-                annotationLineChangeBlock(tAnnotation)
-            }
+        self.lineModel?.lineStype = CPDFBorderStyle(rawValue: tag) ?? .solid
+        if let callback = self.callback {
+            callback(lineModel!)
         }
     }
 
     @IBAction func lineWidthStepperAction(_ sender: NSStepper) {
         let fValue = sender.floatValue
         
+        self.lineModel?.lineWidth = CGFloat(fValue)
         lineWidthTextField.stringValue = String(format: "%.1f pt", fValue)
-        for tAnnotation in self.annotations {
-            tAnnotation.setLineWidth(CGFloat(fValue))
+        if let callback = self.callback {
+            callback(lineModel!)
         }
     }
     
     @IBAction func startPopUpButtonAction(_ sender: NSPopUpButton) {
         let startInt = sender.indexOfSelectedItem
-        for tAnnotation in annotations {
-            if self.annotation is CPDFLineAnnotation {
-                (tAnnotation as! CPDFLineAnnotation).startLineStyle = CPDFLineStyle(rawValue: startInt) ?? .none
-            }
-
-            if let annotationLineChangeBlock = annotationLineChangeBlock {
-                annotationLineChangeBlock(tAnnotation)
-            }
+        
+        self.lineModel?.startingPoint = CPDFLineStyle(rawValue: startInt) ?? .none
+        if let callback = self.callback {
+            callback(lineModel!)
         }
     }
 
     @IBAction func endPopUpButtonAction(_ sender: NSPopUpButton) {
         let endInt = sender.indexOfSelectedItem
-        for tAnnotation in annotations {
-            if self.annotation is CPDFLineAnnotation {
-                (tAnnotation as! CPDFLineAnnotation).endLineStyle = CPDFLineStyle(rawValue: endInt) ?? .none
-            }
-
-            if let annotationLineChangeBlock = annotationLineChangeBlock {
-                annotationLineChangeBlock(tAnnotation)
-            }
+        
+        self.lineModel?.endPoint = CPDFLineStyle(rawValue: endInt) ?? .none
+        if let callback = self.callback {
+            callback(lineModel!)
         }
     }
     
     // MARK: Notification
-    
-    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
-        guard let observedObject = object as AnyObject?, observedObject !== self.annotation else {
-            return
-        }
-
-        let newValue = change?[.newKey] ?? NSNull()
-        let oldValue = change?[.oldKey] ?? NSNull()
-
-        if (newValue as AnyObject).isEqual(oldValue) {
-            return
-        }
-
-        if keyPath == "string" {
-            if let newValue = newValue as? NSNull {
-                return
-            }
-        } else {
-            reloadData()
-        }
-    }
 
     @objc func textFieldDidEndEditingNotification(_ notification: Notification) {
         let dashFloat = Float(dottedLineTextField.stringValue)
         let number = NSNumber(value: dashFloat!)
-        var lineDashPattern: [NSNumber] = annotation.border?.dashPattern as! [NSNumber]
+        var lineDashPattern: [NSNumber] = self.lineModel!.dashPattern
         let array: [NSNumber]
         
         if lineDashPattern.isEmpty {
@@ -590,10 +525,9 @@ class KMAnnotationLineWindowController: NSWindowController, NSTextFieldDelegate
             array = lineDashPattern
         }
         
-        for tAnnotation in annotations {
-            tAnnotation.setDashPattern(array)
-            
-            annotationLineChangeBlock?(tAnnotation)
+        self.lineModel!.dashPattern = array
+        if let callback = self.callback {
+            callback(lineModel!)
         }
     }