KMLineInspector.swift 25 KB

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