KMLineInspector.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. 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.dashPatternField.stringValue = "\(self.dashPattern.count)"
  169. self.startLineStyleButton.selectedSegment = self.startLineStyle
  170. self.endLineStyleButton.selectedSegment = self.endLineStyle
  171. }
  172. private func _initActions() {
  173. self.lineWidthSlider.target = self
  174. self.lineWidthSlider.action = #selector(lineWidthSliderAction)
  175. self.styleButton.target = self
  176. self.styleButton.action = #selector(styleAction)
  177. self.startLineStyleButton.target = self
  178. self.startLineStyleButton.action = #selector(startLineStyleAction)
  179. self.endLineStyleButton.target = self
  180. self.endLineStyleButton.action = #selector(endLineStyleAction)
  181. self.lineWidthField.delegate = self
  182. self.dashPatternField.delegate = self
  183. }
  184. private func _initStyleImages() {
  185. var image: NSImage?
  186. var size = NSMakeSize(29.0, 12.0)
  187. image = NSImage.image(with: size, drawingHandler: { rect in
  188. let path = NSBezierPath(rect: NSMakeRect(6.0, 3.0, 17.0, 6.0))
  189. path.lineWidth = 2.0
  190. NSColor.black.setStroke()
  191. path.stroke()
  192. return true
  193. })
  194. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.solid.rawValue)
  195. image = NSImage.image(with: size, drawingHandler: { dstRect in
  196. let path = NSBezierPath()
  197. path.move(to: NSMakePoint(6.0, 5.0))
  198. path.line(to: NSMakePoint(6.0, 3.0))
  199. path.line(to: NSMakePoint(9.0, 3.0))
  200. path.move(to: NSMakePoint(12.0, 3.0))
  201. path.line(to: NSMakePoint(17.0, 3.0))
  202. path.move(to: NSMakePoint(20.0, 3.0))
  203. path.line(to: NSMakePoint(23.0, 3.0))
  204. path.line(to: NSMakePoint(23.0, 5.0))
  205. path.move(to: NSMakePoint(23.0, 7.0))
  206. path.line(to: NSMakePoint(23.0, 9.0))
  207. path.line(to: NSMakePoint(20.0, 9.0))
  208. path.move(to: NSMakePoint(17.0, 9.0))
  209. path.line(to: NSMakePoint(12.0, 9.0))
  210. path.move(to: NSMakePoint(9.0, 9.0))
  211. path.line(to: NSMakePoint(6.0, 9.0))
  212. path.line(to: NSMakePoint(6.0, 7.0))
  213. path.lineWidth = 2.0
  214. NSColor.black.setStroke()
  215. path.stroke()
  216. return true
  217. })
  218. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.dashed.rawValue)
  219. image = NSImage.image(with: size, drawingHandler: { dstRect in
  220. var path = NSBezierPath(rect: NSMakeRect(6.0, 3.0, 17.0, 6.0))
  221. path.lineWidth = 2.0
  222. NSColor(calibratedWhite: 0, alpha: 0.25).setStroke()
  223. path.stroke()
  224. path = NSBezierPath()
  225. path.move(to: NSMakePoint(7.0, 3.0))
  226. path.line(to: NSMakePoint(23.0, 3.0))
  227. path.line(to: NSMakePoint(23.0, 8.0))
  228. path.lineWidth = 2.0
  229. NSColor(calibratedWhite: 0, alpha: 0.35).set()
  230. path.stroke()
  231. path = NSBezierPath()
  232. path.move(to: NSMakePoint(5.0, 2.0))
  233. path.line(to: NSMakePoint(7.0, 4.0))
  234. path.line(to: NSMakePoint(7.0, 2.0))
  235. path.close()
  236. path.move(to: NSMakePoint(24.0, 10.0))
  237. path.line(to: NSMakePoint(22.0, 8.0))
  238. path.line(to: NSMakePoint(24.0, 8.0))
  239. path.close()
  240. path.fill()
  241. return true
  242. })
  243. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.beveled.rawValue)
  244. image = NSImage.image(with: size, drawingHandler: { dstRect in
  245. var path = NSBezierPath(rect: NSMakeRect(6.0, 3.0, 17.0, 6.0))
  246. path.lineWidth = 2.0
  247. NSColor(calibratedWhite: 0, alpha: 0.25).setStroke()
  248. path.stroke()
  249. path = NSBezierPath()
  250. path.move(to: NSMakePoint(6.0, 4.0))
  251. path.line(to: NSMakePoint(6.0, 9.0))
  252. path.line(to: NSMakePoint(22.0, 9.0))
  253. path.lineWidth = 2.0
  254. NSColor(calibratedWhite: 0, alpha: 0.35).set()
  255. path.stroke()
  256. path = NSBezierPath()
  257. path.move(to: NSMakePoint(5.0, 2.0))
  258. path.line(to: NSMakePoint(7.0, 4.0))
  259. path.line(to: NSMakePoint(5.0, 4.0))
  260. path.close()
  261. path.move(to: NSMakePoint(24.0, 10.0))
  262. path.line(to: NSMakePoint(22.0, 8.0))
  263. path.line(to: NSMakePoint(22.0, 10.0))
  264. path.close()
  265. path.fill()
  266. return true
  267. })
  268. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.inset.rawValue)
  269. image = NSImage.image(with: size, drawingHandler: { dstRect in
  270. let path = NSBezierPath()
  271. path.move(to: NSMakePoint(6.0, 3.0))
  272. path.line(to: NSMakePoint(23.0, 3.0))
  273. path.lineWidth = 2.0
  274. NSColor(calibratedWhite: 0, alpha: 0.5).setStroke()
  275. path.stroke()
  276. return true
  277. })
  278. self.styleButton.setImage(image, forSegment: CPDFBorderStyle.underline.rawValue)
  279. }
  280. private func _initStartImages() {
  281. var image: NSImage?
  282. var size = NSMakeSize(24.0, 12.0)
  283. image = NSImage.image(with: size, drawingHandler: { dstRect in
  284. let path = NSBezierPath()
  285. path.move(to: NSMakePoint(20.0, 6.0))
  286. path.line(to: NSMakePoint(8.0, 6.0))
  287. path.lineWidth = 2.0
  288. NSColor.black.setStroke()
  289. path.stroke()
  290. return true
  291. })
  292. self.startLineStyleButton.setImage(image, forSegment: CPDFLineStyle.none.rawValue)
  293. image = NSImage.image(with: size, drawingHandler: { rect in
  294. let path = NSBezierPath()
  295. path.move(to: NSMakePoint(20.0, 6.0))
  296. path.line(to: NSMakePoint(8.0, 6.0))
  297. path.appendRect(NSMakeRect(5.0, 3.0, 6.0, 6.0))
  298. path.lineWidth = 2.0
  299. NSColor.black.setStroke()
  300. path.stroke()
  301. return true
  302. })
  303. // CPDFLineStyle.square.rawValue
  304. self.startLineStyleButton.setImage(image, forSegment: 1)
  305. image = NSImage.image(with: size, drawingHandler: { dstRect 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.appendOval(in: 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.circle.rawValue
  316. self.startLineStyleButton.setImage(image, forSegment: 2)
  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.move(to: NSMakePoint(12.0, 6.0))
  322. path.line(to: NSMakePoint(8.0, 10.0))
  323. path.line(to: NSMakePoint(4.0, 6.0))
  324. path.line(to: NSMakePoint(8.0, 2.0))
  325. path.close()
  326. path.lineWidth = 2.0
  327. path.stroke()
  328. return true
  329. })
  330. // CPDFLineStyle.diamond.rawValue
  331. self.startLineStyleButton.setImage(image, forSegment: 3)
  332. image = NSImage.image(with: size, drawingHandler: { dstRect in
  333. let path = NSBezierPath()
  334. path.move(to: NSMakePoint(20.0, 6.0))
  335. path.line(to: NSMakePoint(8.0, 6.0))
  336. path.move(to: NSMakePoint(14.0, 3.0))
  337. path.line(to: NSMakePoint(8.0, 6.0))
  338. path.line(to: NSMakePoint(14.0, 9.0))
  339. path.lineWidth = 2.0
  340. NSColor.black.setStroke()
  341. path.stroke()
  342. return true
  343. })
  344. // CPDFLineStyle.openArrow.rawValue
  345. self.startLineStyleButton.setImage(image, forSegment: 4)
  346. image = NSImage.image(with: size, drawingHandler: { dstRect in
  347. let path = NSBezierPath()
  348. path.move(to: NSMakePoint(20.0, 6.0))
  349. path.line(to: NSMakePoint(8.0, 6.0))
  350. path.move(to: NSMakePoint(14.0, 3.0))
  351. path.line(to: NSMakePoint(8.0, 6.0))
  352. path.line(to: NSMakePoint(14.0, 9.0))
  353. path.close()
  354. path.lineWidth = 2
  355. NSColor.black.setStroke()
  356. path.stroke()
  357. return true
  358. })
  359. // CPDFLineStyle.closedArrow.rawValue
  360. self.startLineStyleButton.setImage(image, forSegment: 5)
  361. }
  362. private func _initEndImages() {
  363. var image: NSImage?
  364. var size = NSMakeSize(24.0, 12.0)
  365. image = NSImage.image(with: size, drawingHandler: { dstRect in
  366. let path = NSBezierPath()
  367. path.move(to: NSMakePoint(4.0, 6.0))
  368. path.line(to: NSMakePoint(16.0, 6.0))
  369. path.lineWidth = 2.0
  370. NSColor.black.setStroke()
  371. path.stroke()
  372. return true
  373. })
  374. self.endLineStyleButton.setImage(image, forSegment: CPDFLineStyle.none.rawValue)
  375. image = NSImage.image(with: size, drawingHandler: { dstRect in
  376. let path = NSBezierPath()
  377. path.move(to: NSMakePoint(4.0, 6.0))
  378. path.line(to: NSMakePoint(16.0, 6.0))
  379. path.appendRect(NSMakeRect(13.0, 3.0, 6.0, 6.0))
  380. path.lineWidth = 2.0
  381. NSColor.black.setStroke()
  382. path.stroke()
  383. return true
  384. })
  385. // CPDFLineStyle.square.rawValue
  386. self.endLineStyleButton.setImage(image, forSegment: 1)
  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.appendOval(in: 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.circle.rawValue
  398. self.endLineStyleButton.setImage(image, forSegment: 2)
  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.move(to: NSMakePoint(12.0, 6.0))
  404. path.line(to: NSMakePoint(16.0, 10.0))
  405. path.line(to: NSMakePoint(20.0, 6.0))
  406. path.line(to: NSMakePoint(16.0, 2.0))
  407. path.close()
  408. path.lineWidth = 2.0
  409. path.stroke()
  410. return true
  411. })
  412. // CPDFLineStyle.diamond.rawValue
  413. self.endLineStyleButton.setImage(image, forSegment: 3)
  414. image = NSImage.image(with: size, drawingHandler: { dstRect in
  415. let path = NSBezierPath()
  416. path.move(to: NSMakePoint(4.0, 6.0))
  417. path.line(to: NSMakePoint(16.0, 6.0))
  418. path.move(to: NSMakePoint(10.0, 3.0))
  419. path.line(to: NSMakePoint(16.0, 6.0))
  420. path.line(to: NSMakePoint(10.0, 9.0))
  421. path.lineWidth = 2
  422. NSColor.black.setStroke()
  423. path.stroke()
  424. return true
  425. })
  426. // CPDFLineStyle.openArrow.rawValue
  427. self.endLineStyleButton.setImage(image, forSegment: 4)
  428. image = NSImage.image(with: size, drawingHandler: { dstRect in
  429. let path = NSBezierPath()
  430. path.move(to: NSMakePoint(4.0, 6.0))
  431. path.line(to: NSMakePoint(16.0, 6.0))
  432. path.move(to: NSMakePoint(10.0, 3.0))
  433. path.line(to: NSMakePoint(16.0, 6.0))
  434. path.line(to: NSMakePoint(10.0, 9.0))
  435. path.close()
  436. path.lineWidth = 2
  437. NSColor.black.setStroke()
  438. path.stroke()
  439. return true
  440. })
  441. // CPDFLineStyle.closedArrow.rawValue
  442. self.endLineStyleButton.setImage(image, forSegment: 5)
  443. }
  444. override func setNilValueForKey(_ key: String) {
  445. if key == LINEWIDTH_KEY {
  446. self.setValue(0.0, forKey: key)
  447. } else if key == STYLE_KEY || key == STARTLINESTYLE_KEY || key == ENDLINESTYLE_KEY {
  448. self.setValue(0, forKey: key)
  449. } else {
  450. super.setNilValueForKey(key)
  451. }
  452. }
  453. override func value(forUndefinedKey key: String) -> Any? {
  454. KMPrint("forUndefinedKey: \(key)")
  455. }
  456. func setAnnotationStyle(_ annotation: CPDFAnnotation) {
  457. let type = annotation.type
  458. // SKNLineString
  459. if type == SKNFreeTextString || type == SKNCircleString || type == SKNSquareString || type == SKNLine_NoneString || type == SKNInkString {
  460. if let border = annotation.border {
  461. self.lineWidth = border.lineWidth
  462. self.style = border.style.rawValue
  463. } else {
  464. self.lineWidth = 0.0
  465. self.style = 0
  466. }
  467. self.dashPattern = (annotation.border.dashPattern as? [CGFloat]) ?? []
  468. }
  469. // SKNLineString
  470. if type == SKNLine_NoneString {
  471. if let anno = annotation as? CPDFLineAnnotation {
  472. self.startLineStyle = anno.startLineStyle.rawValue
  473. self.endLineStyle = anno.endLineStyle.rawValue
  474. }
  475. }
  476. }
  477. // MARK: - Actions
  478. @objc func lineWidthSliderAction(_ sender: NSSlider) {
  479. self.lineWidth = sender.floatValue.cgFloat
  480. self.lineWidthField.stringValue = String(format: "%.0f", sender.floatValue)
  481. }
  482. @objc func styleAction(_ sender: NSSegmentedControl) {
  483. self.style = sender.selectedSegment
  484. }
  485. @objc func startLineStyleAction(_ sender: NSSegmentedControl) {
  486. let index = sender.selectedSegment
  487. if index == 0 {
  488. self.startLineStyle = CPDFLineStyle.none.rawValue
  489. } else if index == 1 {
  490. self.startLineStyle = CPDFLineStyle.square.rawValue
  491. } else if index == 2 {
  492. self.startLineStyle = CPDFLineStyle.circle.rawValue
  493. } else if index == 3 {
  494. self.startLineStyle = CPDFLineStyle.diamond.rawValue
  495. } else if index == 4 {
  496. self.startLineStyle = CPDFLineStyle.openArrow.rawValue
  497. } else if index == 5 {
  498. self.startLineStyle = CPDFLineStyle.closedArrow.rawValue
  499. }
  500. }
  501. @objc func endLineStyleAction(_ sender: NSSegmentedControl) {
  502. let index = sender.selectedSegment
  503. if index == 0 {
  504. self.endLineStyle = CPDFLineStyle.none.rawValue
  505. } else if index == 1 {
  506. self.endLineStyle = CPDFLineStyle.square.rawValue
  507. } else if index == 2 {
  508. self.endLineStyle = CPDFLineStyle.circle.rawValue
  509. } else if index == 3 {
  510. self.endLineStyle = CPDFLineStyle.diamond.rawValue
  511. } else if index == 4 {
  512. self.endLineStyle = CPDFLineStyle.openArrow.rawValue
  513. } else if index == 5 {
  514. self.endLineStyle = CPDFLineStyle.closedArrow.rawValue
  515. }
  516. }
  517. }
  518. // MARK: - Private Methods
  519. extension KMLineInspector {
  520. private func _notifyChangeAction(_ action: KMLineChangeAction) {
  521. self._currentLineChangeAction = action
  522. let selector = NSSelectorFromString("changeLineAttribute:")
  523. let mainWindow = NSApp.mainWindow
  524. var responder = mainWindow?.firstResponder
  525. while (responder != nil && responder!.responds(to: selector) == false) {
  526. responder = responder!.nextResponder
  527. }
  528. responder?.perform(selector, with: self)
  529. let userInfo = [ACTION_KEY : NSNumber(value: action.rawValue)]
  530. NotificationCenter.default.post(name: Self.lineAttributeDidChangeNotification, object: self, userInfo: userInfo)
  531. self._currentLineChangeAction = .no
  532. }
  533. }
  534. extension KMLineInspector: NSTextFieldDelegate {
  535. func controlTextDidChange(_ obj: Notification) {
  536. if self.lineWidthField.isEqual(to: obj.object) {
  537. let value = self.lineWidthField.doubleValue
  538. if value < self.lineWidthSlider.minValue {
  539. self.lineWidth = self.lineWidthSlider.minValue
  540. self.lineWidthField.stringValue = String(format: "%.0f", self.lineWidth)
  541. } else if value > self.lineWidthSlider.maxValue {
  542. self.lineWidth = self.lineWidthSlider.maxValue
  543. self.lineWidthField.stringValue = String(format: "%.0f", self.lineWidth)
  544. } else {
  545. self.lineWidth = value
  546. }
  547. self.lineWidthSlider.doubleValue = self.lineWidth
  548. } else if self.dashPatternField.isEqual(to: obj.object) {
  549. let cnt = self.dashPatternField.integerValue
  550. let data = [CGFloat](repeating: 3.0, count: cnt)
  551. self.dashPattern = data
  552. }
  553. }
  554. }