|
@@ -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!)
|
|
|
}
|
|
|
}
|
|
|
|