KMLineInspector.swift 25 KB

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