KMLineInspector.swift 25 KB

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