KMLineInspector.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. //
  2. // KMLineInspector.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/10.
  6. //
  7. import Cocoa
  8. enum KMLineChangeAction: Int {
  9. case no = 0
  10. case lineWidth
  11. case style
  12. case dashPattern
  13. case startLineStyle
  14. case endLineStyle
  15. }
  16. @objcMembers class KMLineInspector: NSWindowController {
  17. private let LINEWIDTH_KEY = "lineWidth"
  18. private let STYLE_KEY = "style"
  19. private let DASHPATTERN_KEY = "dashPattern"
  20. private let STARTLINESTYLE_KEY = "startLineStyle"
  21. private let ENDLINESTYLE_KEY = "endLineStyle"
  22. private let ACTION_KEY = "action"
  23. private let SKLineInspectorFrameAutosaveName = "SKLineInspector"
  24. public static let lineAttributeDidChangeNotification = NSNotification.Name("SKLineInspectorLineAttributeDidChangeNotification")
  25. /*
  26. + (BOOL)sharedLineInspectorExists;
  27. */
  28. private var _currentLineChangeAction: KMLineChangeAction = .lineWidth
  29. var currentLineChangeAction: KMLineChangeAction {
  30. get {
  31. return self._currentLineChangeAction
  32. }
  33. }
  34. @IBOutlet var lineWidthSlider: NSSlider!
  35. @IBOutlet var lineWidthField: NSTextField!
  36. @IBOutlet var dashPatternField: NSTextField!
  37. @IBOutlet var styleButton: NSSegmentedControl!
  38. @IBOutlet var startLineStyleButton: NSSegmentedControl!
  39. @IBOutlet var endLineStyleButton: NSSegmentedControl!
  40. @IBOutlet var lineWidthLabelField: NSTextField!
  41. @IBOutlet var styleLabelField: NSTextField!
  42. @IBOutlet var dashPatternLabelField: NSTextField!
  43. @IBOutlet var startLineStyleLabelField: NSTextField!
  44. @IBOutlet var endLineStyleLabelField: NSTextField!
  45. @IBOutlet var lineWell: KMLineWell!
  46. @IBOutlet weak var labelField1: NSTextField!
  47. @IBOutlet weak var labelField2: NSTextField!
  48. @IBOutlet weak var labelField3: NSTextField!
  49. @IBOutlet weak var labelField4: NSTextField!
  50. @IBOutlet weak var labelField5: NSTextField!
  51. @IBOutlet weak var LineborderStyleLable: NSTextField!
  52. @IBOutlet weak var lineEndingStyleLabel: NSTextField!
  53. var labelFields: [NSTextField] = []
  54. private var _lineWidth: CGFloat = 0
  55. var lineWidth: CGFloat {
  56. get {
  57. return self._lineWidth
  58. }
  59. set {
  60. if (abs(self.lineWidth - newValue) > 0.00001) {
  61. self._lineWidth = newValue
  62. self.lineWell?.lineWidth = newValue
  63. self.lineWidthField.stringValue = String(format: "%.0f", self.lineWidth)
  64. self.lineWidthSlider.doubleValue = self.lineWidth
  65. self._notifyChangeAction(.lineWidth)
  66. }
  67. }
  68. }
  69. private var _style: Int = CPDFBorderStyle.solid.rawValue
  70. var style: Int {
  71. get {
  72. return self._style
  73. }
  74. set {
  75. if (newValue != self._style) {
  76. self._style = newValue
  77. self.lineWell?.style = CPDFBorderStyle(rawValue: newValue) ?? .solid
  78. self.styleButton.selectedSegment = self.style
  79. self._notifyChangeAction(.style)
  80. }
  81. }
  82. }
  83. private var _startLineStyle: Int = CPDFLineStyle.none.rawValue
  84. var startLineStyle: Int {
  85. get {
  86. return self._startLineStyle
  87. }
  88. set {
  89. if (newValue != self.startLineStyle) {
  90. self._startLineStyle = newValue
  91. self.lineWell?.startLineStyle = CPDFLineStyle(rawValue: newValue) ?? .none
  92. self._notifyChangeAction(.startLineStyle)
  93. }
  94. }
  95. }
  96. var _endLineStyle: Int = CPDFLineStyle.none.rawValue
  97. var endLineStyle: Int {
  98. get {
  99. return self._endLineStyle
  100. }
  101. set {
  102. if (newValue != self.endLineStyle) {
  103. self._endLineStyle = newValue
  104. self.lineWell?.endLineStyle = CPDFLineStyle(rawValue: newValue) ?? .none
  105. self._notifyChangeAction(.endLineStyle)
  106. }
  107. }
  108. }
  109. private var _dashPattern: [CGFloat] = []
  110. var dashPattern: [CGFloat] {
  111. get {
  112. return self._dashPattern
  113. }
  114. set {
  115. if newValue != self._dashPattern && (newValue.isEmpty == false || self._dashPattern.isEmpty == false) {
  116. self._dashPattern = newValue
  117. self.lineWell?.dashPattern = newValue as NSArray
  118. self.dashPatternField.stringValue = "\(self.dashPattern.count)"
  119. self._notifyChangeAction(.dashPattern)
  120. } else {
  121. self._dashPattern = []
  122. self.lineWell?.dashPattern = newValue as NSArray
  123. self.dashPatternField.stringValue = "\(self.dashPattern.count)"
  124. self._notifyChangeAction(.dashPattern)
  125. }
  126. }
  127. }
  128. static let shared = KMLineInspector(windowNibName: "LineInspector")
  129. override func windowDidLoad() {
  130. super.windowDidLoad()
  131. self._initDefalutValue()
  132. self._initLayoutUI()
  133. self._initValues()
  134. self._initActions()
  135. self._initStyleImages()
  136. self._initStartImages()
  137. self._initEndImages()
  138. }
  139. private func _initLayoutUI() {
  140. let dw = KMAutoSizeLabelFields(labelFields, [lineWidthSlider, lineWidthField, styleButton, dashPatternField, startLineStyleButton, endLineStyleButton], false)
  141. if (abs(dw) > 0.0) {
  142. KMResizeWindow(self.window!, dw)
  143. }
  144. }
  145. private func _initDefalutValue() {
  146. self.window?.title = NSLocalizedString("Lines", comment: "")
  147. self.styleButton.setHelp(KMLocalizedString("Solid line style"), for: CPDFBorderStyle.solid.rawValue)
  148. self.styleButton.setHelp(KMLocalizedString("Dashed line style"), for: CPDFBorderStyle.dashed.rawValue)
  149. self.styleButton.setHelp(KMLocalizedString("Beveled line style"), for: CPDFBorderStyle.beveled.rawValue)
  150. self.styleButton.setHelp(KMLocalizedString("Inset line style"), for: CPDFBorderStyle.inset.rawValue)
  151. self.styleButton.setHelp(KMLocalizedString("Underline line style"), for: CPDFBorderStyle.underline.rawValue)
  152. self.startLineStyleButton.setHelp(KMLocalizedString("No start line style"), for: CPDFLineStyle.none.rawValue)
  153. self.startLineStyleButton.setHelp(KMLocalizedString("Square start line style"), for: CPDFLineStyle.square.rawValue)
  154. self.startLineStyleButton.setHelp(KMLocalizedString("Circle start line style"), for: CPDFLineStyle.circle.rawValue)
  155. self.startLineStyleButton.setHelp(KMLocalizedString("Diamond start line style"), for: CPDFLineStyle.diamond.rawValue)
  156. self.startLineStyleButton.setHelp(KMLocalizedString("Open arrow start line style"), for: CPDFLineStyle.openArrow.rawValue)
  157. self.startLineStyleButton.setHelp(KMLocalizedString("Closed arrow start line style"), for: CPDFLineStyle.closedArrow.rawValue)
  158. self.endLineStyleButton.setHelp(KMLocalizedString("No end line style"), for: CPDFLineStyle.none.rawValue)
  159. self.endLineStyleButton.setHelp(KMLocalizedString("Square end line style"), for: CPDFLineStyle.square.rawValue)
  160. self.endLineStyleButton.setHelp(KMLocalizedString("Circle end line style"), for: CPDFLineStyle.circle.rawValue)
  161. self.endLineStyleButton.setHelp(KMLocalizedString("Diamond end line style"), for: CPDFLineStyle.diamond.rawValue)
  162. self.endLineStyleButton.setHelp(KMLocalizedString("Open arrow end line style"), for: CPDFLineStyle.openArrow.rawValue)
  163. self.endLineStyleButton.setHelp(KMLocalizedString("Closed arrow end line style"), for: CPDFLineStyle.closedArrow.rawValue)
  164. self.LineborderStyleLable.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  165. self.lineEndingStyleLabel.stringValue = NSLocalizedString("Line Ending Style", comment: "")
  166. self.lineWidthLabelField.stringValue = NSLocalizedString("Line Width:", comment: "")
  167. self.styleLabelField.stringValue = NSLocalizedString("Line Style:", comment: "")
  168. self.dashPatternLabelField.stringValue = NSLocalizedString("Dash Pattern:", comment: "")
  169. self.startLineStyleLabelField.stringValue = NSLocalizedString("Start:", comment: "")
  170. self.endLineStyleLabelField.stringValue = NSLocalizedString("End:", comment: "")
  171. self.windowFrameAutosaveName = SKLineInspectorFrameAutosaveName
  172. self._currentLineChangeAction = .no
  173. self.lineWidthField.formatter = NumberFormatter()
  174. self.dashPatternField.formatter = NumberFormatter()
  175. }
  176. private func _initValues() {
  177. self.labelFields = [self.labelField1, self.labelField2, self.labelField3, self.labelField4, self.labelField5]
  178. self.lineWell.canActivate = false
  179. self.lineWell.lineWidth = self.lineWidth
  180. self.lineWell.style = CPDFBorderStyle(rawValue: self.style) ?? .solid
  181. self.lineWell.dashPattern = self.dashPattern as NSArray
  182. self.lineWell.startLineStyle = CPDFLineStyle(rawValue: self.startLineStyle) ?? .none
  183. self.lineWell.endLineStyle = CPDFLineStyle(rawValue: self.endLineStyle) ?? .none
  184. self.lineWidthSlider.floatValue = Float(self.lineWidth)
  185. self.lineWidthField.stringValue = "\(self.lineWidth)"
  186. self.styleButton.selectedSegment = self.style
  187. self.dashPatternField.stringValue = "\(self.dashPattern.count)"
  188. self.startLineStyleButton.selectedSegment = self.startLineStyle
  189. self.endLineStyleButton.selectedSegment = self.endLineStyle
  190. }
  191. private func _initActions() {
  192. self.lineWidthSlider.target = self
  193. self.lineWidthSlider.action = #selector(lineWidthSliderAction)
  194. self.styleButton.target = self
  195. self.styleButton.action = #selector(styleAction)
  196. self.startLineStyleButton.target = self
  197. self.startLineStyleButton.action = #selector(startLineStyleAction)
  198. self.endLineStyleButton.target = self
  199. self.endLineStyleButton.action = #selector(endLineStyleAction)
  200. self.lineWidthField.delegate = self
  201. self.dashPatternField.delegate = self
  202. }
  203. private func _initStyleImages() {
  204. var image: NSImage?
  205. var size = NSMakeSize(29.0, 12.0)
  206. image = NSImage.image(with: size, drawingHandler: { rect in
  207. let path = NSBezierPath(rect: NSMakeRect(6.0, 3.0, 17.0, 6.0))
  208. path.lineWidth = 2.0
  209. NSColor.black.setStroke()
  210. path.stroke()
  211. return true
  212. })
  213. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.solid.rawValue)
  214. image = NSImage.image(with: size, drawingHandler: { dstRect in
  215. let path = NSBezierPath()
  216. path.move(to: NSMakePoint(6.0, 5.0))
  217. path.line(to: NSMakePoint(6.0, 3.0))
  218. path.line(to: NSMakePoint(9.0, 3.0))
  219. path.move(to: NSMakePoint(12.0, 3.0))
  220. path.line(to: NSMakePoint(17.0, 3.0))
  221. path.move(to: NSMakePoint(20.0, 3.0))
  222. path.line(to: NSMakePoint(23.0, 3.0))
  223. path.line(to: NSMakePoint(23.0, 5.0))
  224. path.move(to: NSMakePoint(23.0, 7.0))
  225. path.line(to: NSMakePoint(23.0, 9.0))
  226. path.line(to: NSMakePoint(20.0, 9.0))
  227. path.move(to: NSMakePoint(17.0, 9.0))
  228. path.line(to: NSMakePoint(12.0, 9.0))
  229. path.move(to: NSMakePoint(9.0, 9.0))
  230. path.line(to: NSMakePoint(6.0, 9.0))
  231. path.line(to: NSMakePoint(6.0, 7.0))
  232. path.lineWidth = 2.0
  233. NSColor.black.setStroke()
  234. path.stroke()
  235. return true
  236. })
  237. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.dashed.rawValue)
  238. image = NSImage.image(with: size, drawingHandler: { dstRect in
  239. var path = NSBezierPath(rect: NSMakeRect(6.0, 3.0, 17.0, 6.0))
  240. path.lineWidth = 2.0
  241. NSColor(calibratedWhite: 0, alpha: 0.25).setStroke()
  242. path.stroke()
  243. path = NSBezierPath()
  244. path.move(to: NSMakePoint(7.0, 3.0))
  245. path.line(to: NSMakePoint(23.0, 3.0))
  246. path.line(to: NSMakePoint(23.0, 8.0))
  247. path.lineWidth = 2.0
  248. NSColor(calibratedWhite: 0, alpha: 0.35).set()
  249. path.stroke()
  250. path = NSBezierPath()
  251. path.move(to: NSMakePoint(5.0, 2.0))
  252. path.line(to: NSMakePoint(7.0, 4.0))
  253. path.line(to: NSMakePoint(7.0, 2.0))
  254. path.close()
  255. path.move(to: NSMakePoint(24.0, 10.0))
  256. path.line(to: NSMakePoint(22.0, 8.0))
  257. path.line(to: NSMakePoint(24.0, 8.0))
  258. path.close()
  259. path.fill()
  260. return true
  261. })
  262. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.beveled.rawValue)
  263. image = NSImage.image(with: size, drawingHandler: { dstRect in
  264. var path = NSBezierPath(rect: NSMakeRect(6.0, 3.0, 17.0, 6.0))
  265. path.lineWidth = 2.0
  266. NSColor(calibratedWhite: 0, alpha: 0.25).setStroke()
  267. path.stroke()
  268. path = NSBezierPath()
  269. path.move(to: NSMakePoint(6.0, 4.0))
  270. path.line(to: NSMakePoint(6.0, 9.0))
  271. path.line(to: NSMakePoint(22.0, 9.0))
  272. path.lineWidth = 2.0
  273. NSColor(calibratedWhite: 0, alpha: 0.35).set()
  274. path.stroke()
  275. path = NSBezierPath()
  276. path.move(to: NSMakePoint(5.0, 2.0))
  277. path.line(to: NSMakePoint(7.0, 4.0))
  278. path.line(to: NSMakePoint(5.0, 4.0))
  279. path.close()
  280. path.move(to: NSMakePoint(24.0, 10.0))
  281. path.line(to: NSMakePoint(22.0, 8.0))
  282. path.line(to: NSMakePoint(22.0, 10.0))
  283. path.close()
  284. path.fill()
  285. return true
  286. })
  287. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.inset.rawValue)
  288. image = NSImage.image(with: size, drawingHandler: { dstRect in
  289. let path = NSBezierPath()
  290. path.move(to: NSMakePoint(6.0, 3.0))
  291. path.line(to: NSMakePoint(23.0, 3.0))
  292. path.lineWidth = 2.0
  293. NSColor(calibratedWhite: 0, alpha: 0.5).setStroke()
  294. path.stroke()
  295. return true
  296. })
  297. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.underline.rawValue)
  298. }
  299. private func _initStartImages() {
  300. var image: NSImage?
  301. var size = NSMakeSize(24.0, 12.0)
  302. image = NSImage.image(with: size, drawingHandler: { dstRect in
  303. let path = NSBezierPath()
  304. path.move(to: NSMakePoint(20.0, 6.0))
  305. path.line(to: NSMakePoint(8.0, 6.0))
  306. path.lineWidth = 2.0
  307. NSColor.black.setStroke()
  308. path.stroke()
  309. return true
  310. })
  311. self.startLineStyleButton.setImage(image, forSegment: CPDFLineStyle.none.rawValue)
  312. image = NSImage.image(with: size, drawingHandler: { rect in
  313. let path = NSBezierPath()
  314. path.move(to: NSMakePoint(20.0, 6.0))
  315. path.line(to: NSMakePoint(8.0, 6.0))
  316. path.appendRect(NSMakeRect(5.0, 3.0, 6.0, 6.0))
  317. path.lineWidth = 2.0
  318. NSColor.black.setStroke()
  319. path.stroke()
  320. return true
  321. })
  322. // CPDFLineStyle.square.rawValue
  323. self.startLineStyleButton.setImage(image, forSegment: 1)
  324. image = NSImage.image(with: size, drawingHandler: { dstRect in
  325. let path = NSBezierPath()
  326. path.move(to: NSMakePoint(20.0, 6.0))
  327. path.line(to: NSMakePoint(8.0, 6.0))
  328. path.appendOval(in: NSMakeRect(5.0, 3.0, 6.0, 6.0))
  329. path.lineWidth = 2.0
  330. NSColor.black.setStroke()
  331. path.stroke()
  332. return true
  333. })
  334. // CPDFLineStyle.circle.rawValue
  335. self.startLineStyleButton.setImage(image, forSegment: 2)
  336. image = NSImage.image(with: size, drawingHandler: { dstRect in
  337. let path = NSBezierPath()
  338. path.move(to: NSMakePoint(20.0, 6.0))
  339. path.line(to: NSMakePoint(8.0, 6.0))
  340. path.move(to: NSMakePoint(12.0, 6.0))
  341. path.line(to: NSMakePoint(8.0, 10.0))
  342. path.line(to: NSMakePoint(4.0, 6.0))
  343. path.line(to: NSMakePoint(8.0, 2.0))
  344. path.close()
  345. path.lineWidth = 2.0
  346. path.stroke()
  347. return true
  348. })
  349. // CPDFLineStyle.diamond.rawValue
  350. self.startLineStyleButton.setImage(image, forSegment: 3)
  351. image = NSImage.image(with: size, drawingHandler: { dstRect in
  352. let path = NSBezierPath()
  353. path.move(to: NSMakePoint(20.0, 6.0))
  354. path.line(to: NSMakePoint(8.0, 6.0))
  355. path.move(to: NSMakePoint(14.0, 3.0))
  356. path.line(to: NSMakePoint(8.0, 6.0))
  357. path.line(to: NSMakePoint(14.0, 9.0))
  358. path.lineWidth = 2.0
  359. NSColor.black.setStroke()
  360. path.stroke()
  361. return true
  362. })
  363. // CPDFLineStyle.openArrow.rawValue
  364. self.startLineStyleButton.setImage(image, forSegment: 4)
  365. image = NSImage.image(with: size, drawingHandler: { dstRect in
  366. let path = NSBezierPath()
  367. path.move(to: NSMakePoint(20.0, 6.0))
  368. path.line(to: NSMakePoint(8.0, 6.0))
  369. path.move(to: NSMakePoint(14.0, 3.0))
  370. path.line(to: NSMakePoint(8.0, 6.0))
  371. path.line(to: NSMakePoint(14.0, 9.0))
  372. path.close()
  373. path.lineWidth = 2
  374. NSColor.black.setStroke()
  375. path.stroke()
  376. return true
  377. })
  378. // CPDFLineStyle.closedArrow.rawValue
  379. self.startLineStyleButton.setImage(image, forSegment: 5)
  380. }
  381. private func _initEndImages() {
  382. var image: NSImage?
  383. var size = NSMakeSize(24.0, 12.0)
  384. image = NSImage.image(with: size, drawingHandler: { dstRect in
  385. let path = NSBezierPath()
  386. path.move(to: NSMakePoint(4.0, 6.0))
  387. path.line(to: NSMakePoint(16.0, 6.0))
  388. path.lineWidth = 2.0
  389. NSColor.black.setStroke()
  390. path.stroke()
  391. return true
  392. })
  393. self.endLineStyleButton.setImage(image, forSegment: CPDFLineStyle.none.rawValue)
  394. image = NSImage.image(with: size, drawingHandler: { dstRect in
  395. let path = NSBezierPath()
  396. path.move(to: NSMakePoint(4.0, 6.0))
  397. path.line(to: NSMakePoint(16.0, 6.0))
  398. path.appendRect(NSMakeRect(13.0, 3.0, 6.0, 6.0))
  399. path.lineWidth = 2.0
  400. NSColor.black.setStroke()
  401. path.stroke()
  402. return true
  403. })
  404. // CPDFLineStyle.square.rawValue
  405. self.endLineStyleButton.setImage(image, forSegment: 1)
  406. image = NSImage.image(with: size, drawingHandler: { dstRect in
  407. let path = NSBezierPath()
  408. path.move(to: NSMakePoint(4.0, 6.0))
  409. path.line(to: NSMakePoint(16.0, 6.0))
  410. path.appendOval(in: NSMakeRect(13.0, 3.0, 6.0, 6.0))
  411. path.lineWidth = 2.0
  412. NSColor.black.setStroke()
  413. path.stroke()
  414. return true
  415. })
  416. // CPDFLineStyle.circle.rawValue
  417. self.endLineStyleButton.setImage(image, forSegment: 2)
  418. image = NSImage.image(with: size, drawingHandler: { dstRect in
  419. let path = NSBezierPath()
  420. path.move(to: NSMakePoint(4.0, 6.0))
  421. path.line(to: NSMakePoint(16.0, 6.0))
  422. path.move(to: NSMakePoint(12.0, 6.0))
  423. path.line(to: NSMakePoint(16.0, 10.0))
  424. path.line(to: NSMakePoint(20.0, 6.0))
  425. path.line(to: NSMakePoint(16.0, 2.0))
  426. path.close()
  427. path.lineWidth = 2.0
  428. path.stroke()
  429. return true
  430. })
  431. // CPDFLineStyle.diamond.rawValue
  432. self.endLineStyleButton.setImage(image, forSegment: 3)
  433. image = NSImage.image(with: size, drawingHandler: { dstRect in
  434. let path = NSBezierPath()
  435. path.move(to: NSMakePoint(4.0, 6.0))
  436. path.line(to: NSMakePoint(16.0, 6.0))
  437. path.move(to: NSMakePoint(10.0, 3.0))
  438. path.line(to: NSMakePoint(16.0, 6.0))
  439. path.line(to: NSMakePoint(10.0, 9.0))
  440. path.lineWidth = 2
  441. NSColor.black.setStroke()
  442. path.stroke()
  443. return true
  444. })
  445. // CPDFLineStyle.openArrow.rawValue
  446. self.endLineStyleButton.setImage(image, forSegment: 4)
  447. image = NSImage.image(with: size, drawingHandler: { dstRect in
  448. let path = NSBezierPath()
  449. path.move(to: NSMakePoint(4.0, 6.0))
  450. path.line(to: NSMakePoint(16.0, 6.0))
  451. path.move(to: NSMakePoint(10.0, 3.0))
  452. path.line(to: NSMakePoint(16.0, 6.0))
  453. path.line(to: NSMakePoint(10.0, 9.0))
  454. path.close()
  455. path.lineWidth = 2
  456. NSColor.black.setStroke()
  457. path.stroke()
  458. return true
  459. })
  460. // CPDFLineStyle.closedArrow.rawValue
  461. self.endLineStyleButton.setImage(image, forSegment: 5)
  462. }
  463. override func setNilValueForKey(_ key: String) {
  464. if key == LINEWIDTH_KEY {
  465. self.setValue(0.0, forKey: key)
  466. } else if key == STYLE_KEY || key == STARTLINESTYLE_KEY || key == ENDLINESTYLE_KEY {
  467. self.setValue(0, forKey: key)
  468. } else {
  469. super.setNilValueForKey(key)
  470. }
  471. }
  472. override func value(forUndefinedKey key: String) -> Any? {
  473. KMPrint("forUndefinedKey: \(key)")
  474. }
  475. func setAnnotationStyle(_ annotation: CPDFAnnotation) {
  476. let type = annotation.type
  477. if type == SKNFreeTextString || type == CPDFAnnotation.kCircleType || type == CPDFAnnotation.kSquareType || type == CPDFAnnotation.kArrowType || type == CPDFAnnotation.kLineType || type == SKNInkString {
  478. if let model = CPDFAnnotationModel(pdfAnnotations: [annotation]) {
  479. self.lineWidth = model.lineWidth()
  480. self.style = model.style().rawValue
  481. self.dashPattern = model.dashPattern() as? [CGFloat] ?? []
  482. }
  483. }
  484. if type == CPDFAnnotation.kArrowType || type == CPDFAnnotation.kLineType {
  485. if let anno = annotation as? CPDFLineAnnotation {
  486. self.startLineStyle = anno.startLineStyle.rawValue
  487. self.endLineStyle = anno.endLineStyle.rawValue
  488. }
  489. }
  490. }
  491. // MARK: - Actions
  492. @objc func lineWidthSliderAction(_ sender: NSSlider) {
  493. self.lineWidth = sender.floatValue.cgFloat
  494. }
  495. @objc func styleAction(_ sender: NSSegmentedControl) {
  496. self.style = sender.selectedSegment
  497. }
  498. @objc func startLineStyleAction(_ sender: NSSegmentedControl) {
  499. let index = sender.selectedSegment
  500. if index == 0 {
  501. self.startLineStyle = CPDFLineStyle.none.rawValue
  502. } else if index == 1 {
  503. self.startLineStyle = CPDFLineStyle.square.rawValue
  504. } else if index == 2 {
  505. self.startLineStyle = CPDFLineStyle.circle.rawValue
  506. } else if index == 3 {
  507. self.startLineStyle = CPDFLineStyle.diamond.rawValue
  508. } else if index == 4 {
  509. self.startLineStyle = CPDFLineStyle.openArrow.rawValue
  510. } else if index == 5 {
  511. self.startLineStyle = CPDFLineStyle.closedArrow.rawValue
  512. }
  513. }
  514. @objc func endLineStyleAction(_ sender: NSSegmentedControl) {
  515. let index = sender.selectedSegment
  516. if index == 0 {
  517. self.endLineStyle = CPDFLineStyle.none.rawValue
  518. } else if index == 1 {
  519. self.endLineStyle = CPDFLineStyle.square.rawValue
  520. } else if index == 2 {
  521. self.endLineStyle = CPDFLineStyle.circle.rawValue
  522. } else if index == 3 {
  523. self.endLineStyle = CPDFLineStyle.diamond.rawValue
  524. } else if index == 4 {
  525. self.endLineStyle = CPDFLineStyle.openArrow.rawValue
  526. } else if index == 5 {
  527. self.endLineStyle = CPDFLineStyle.closedArrow.rawValue
  528. }
  529. }
  530. }
  531. // MARK: - Private Methods
  532. extension KMLineInspector {
  533. private func _notifyChangeAction(_ action: KMLineChangeAction) {
  534. self._currentLineChangeAction = action
  535. let selector = NSSelectorFromString("changeLineAttribute:")
  536. let mainWindow = NSApp.mainWindow
  537. var responder = mainWindow?.firstResponder
  538. while (responder != nil && responder!.responds(to: selector) == false) {
  539. responder = responder!.nextResponder
  540. }
  541. responder?.perform(selector, with: self)
  542. let userInfo = [ACTION_KEY : NSNumber(value: action.rawValue)]
  543. NotificationCenter.default.post(name: Self.lineAttributeDidChangeNotification, object: self, userInfo: userInfo)
  544. self._currentLineChangeAction = .no
  545. }
  546. }
  547. extension KMLineInspector: NSTextFieldDelegate {
  548. func controlTextDidChange(_ obj: Notification) {
  549. if self.lineWidthField.isEqual(to: obj.object) {
  550. let value = self.lineWidthField.doubleValue
  551. if value < self.lineWidthSlider.minValue {
  552. self.lineWidth = self.lineWidthSlider.minValue
  553. } else if value > self.lineWidthSlider.maxValue {
  554. self.lineWidth = self.lineWidthSlider.maxValue
  555. } else {
  556. self.lineWidth = value
  557. }
  558. } else if self.dashPatternField.isEqual(to: obj.object) {
  559. let cnt = self.dashPatternField.integerValue
  560. let data = [CGFloat](repeating: 3.0, count: cnt)
  561. self.dashPattern = data
  562. }
  563. }
  564. }