KMLineController.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. //
  2. // KMLineController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMLineController: KMNBaseViewController {
  10. //Color
  11. @IBOutlet var colorBGView: NSView!
  12. @IBOutlet var colorLabel: NSTextField!
  13. @IBOutlet var colorGroup: ComponentCColorGroup!
  14. @IBOutlet var colorSlider: ComponentSlider!
  15. @IBOutlet var colorOpacitySelect: ComponentSelect!
  16. //Line
  17. @IBOutlet var lineBGView: NSView!
  18. @IBOutlet var lineLabel: NSTextField!
  19. @IBOutlet var lineTypeSelector: ComponentCSelectorGroup!
  20. @IBOutlet var lineWidthSlider: ComponentSlider!
  21. @IBOutlet var lineWidthSelect: ComponentSelect!
  22. @IBOutlet var linedashInfoView: NSView!
  23. @IBOutlet var lineDashSlider: ComponentSlider!
  24. @IBOutlet var lineDashSelect: ComponentSelect!
  25. @IBOutlet var arrowTypeView: NSView!
  26. @IBOutlet var arrowTypeTopConst: NSLayoutConstraint!
  27. @IBOutlet var arrow_leftTypeSelect: ComponentSelect!
  28. @IBOutlet var arrow_RightTypeSelect: ComponentSelect!
  29. private let solidProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_solid"))
  30. private let dashProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_dash"))
  31. private var annotations: [CPDFLineAnnotation] = []
  32. private var lineAnnotations: [CPDFLineAnnotation] {
  33. get {
  34. var validAnnotations: [CPDFLineAnnotation] = []
  35. for annotation in annotations {
  36. if annotation.isArrowLine() == false {
  37. validAnnotations.append(annotation)
  38. }
  39. }
  40. return validAnnotations
  41. }
  42. }
  43. private var arrowAnnotations: [CPDFLineAnnotation] {
  44. get {
  45. var validAnnotations: [CPDFLineAnnotation] = []
  46. for annotation in annotations {
  47. if annotation.isArrowLine() == true {
  48. validAnnotations.append(annotation)
  49. }
  50. }
  51. return validAnnotations
  52. }
  53. }
  54. var pdfView: CPDFListView?
  55. var viewManager: KMPDFViewManager?
  56. //MARK: - func
  57. override func viewDidAppear() {
  58. super.viewDidAppear()
  59. colorSlider.reloadData()
  60. lineWidthSlider.reloadData()
  61. lineDashSlider.reloadData()
  62. }
  63. override func viewDidLoad() {
  64. super.viewDidLoad()
  65. // Do view setup here.
  66. setupProperty()
  67. }
  68. override func updateUILanguage() {
  69. super.updateUILanguage()
  70. reloadUI()
  71. }
  72. override func updateUIThemeColor() {
  73. super.updateUIThemeColor()
  74. reloadUI()
  75. }
  76. func getValidAnnotations() -> [CPDFLineAnnotation] {
  77. return annotations
  78. }
  79. func reloadUI() {
  80. //Color
  81. colorLabel.stringValue = KMLocalizedString("Color")
  82. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  83. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  84. //Line
  85. lineLabel.stringValue = KMLocalizedString("Line")
  86. lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  87. lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  88. }
  89. func setupProperty() {
  90. colorGroup.delegate = self
  91. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  92. colorSlider.delegate = self
  93. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  94. state: .normal,
  95. creatable: true,
  96. text: "100%",
  97. textUnit: "%",
  98. regexString: "0123456789%")
  99. if true {
  100. var opacityItems: [ComponentMenuitemProperty] = []
  101. for string in ["25%", "50%", "75%", "100%"] {
  102. let item = ComponentMenuitemProperty(type: .normal, text: string)
  103. opacityItems.append(item)
  104. }
  105. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  106. }
  107. colorOpacitySelect.delegate = self
  108. lineTypeSelector.updateItemProperty([solidProperty, dashProperty])
  109. lineTypeSelector.delegate = self
  110. lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  111. lineWidthSlider.delegate = self
  112. lineWidthSelect.properties = ComponentSelectProperties(size: .s,
  113. state: .normal,
  114. creatable: true,
  115. text: "2",
  116. textUnit: " pt",
  117. regexString: "0123456789 pt")
  118. if true {
  119. var opacityItems: [ComponentMenuitemProperty] = []
  120. for string in ["1 pt", "2 pt", "4 pt", "6 pt", "8 pt", "10 pt"] {
  121. let item = ComponentMenuitemProperty(type: .normal, text: string)
  122. opacityItems.append(item)
  123. }
  124. lineWidthSelect.updateMenuItemsArr(opacityItems)
  125. }
  126. lineWidthSelect.delegate = self
  127. lineDashSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  128. lineDashSlider.delegate = self
  129. lineDashSelect.properties = ComponentSelectProperties(size: .s,
  130. state: .normal,
  131. creatable: true,
  132. text: "2",
  133. textUnit: " pt",
  134. regexString: "0123456789 pt")
  135. if true {
  136. var opacityItems: [ComponentMenuitemProperty] = []
  137. for string in ["1 pt", "2 pt", "4 pt", "6 pt", "8 pt", "10 pt"] {
  138. let item = ComponentMenuitemProperty(type: .normal, text: string)
  139. opacityItems.append(item)
  140. }
  141. lineDashSelect.updateMenuItemsArr(opacityItems)
  142. }
  143. lineDashSelect.delegate = self
  144. //ArrowType
  145. arrow_leftTypeSelect.properties = ComponentSelectProperties(size: .s,
  146. state: .normal,
  147. creatable: false)
  148. if true {
  149. let stringItems = ["None", "Arrow Open", "Arrow Close", "Square", "Oval", "Diamond", "Butt", "R Open Arrow", "R Close Arrow", "Slash"]
  150. let imageNames = ["arrowType_line", "arrowType_left_Arrow_open", "arrowType_left_Arrow_close",
  151. "arrowType_left_Square", "arrowType_left_Oval", "arrowType_left_Diamond",
  152. "arrowType_left_Butt", "arrowType_left_Open_arrow",
  153. "arrowType_left_Close_arrow", "arrowType_left_Slash"]
  154. var opacityItems: [ComponentMenuitemProperty] = []
  155. for i in 0...stringItems.count-1 {
  156. let string = stringItems[i]
  157. let imageName = imageNames[i]
  158. let item = ComponentMenuitemProperty(type: .normal, lefticon: NSImage(named: imageName), text: string, identifier: string)
  159. opacityItems.append(item)
  160. }
  161. arrow_leftTypeSelect.updateMenuItemsArr(opacityItems)
  162. }
  163. arrow_leftTypeSelect.delegate = self
  164. arrow_RightTypeSelect.properties = ComponentSelectProperties(size: .s,
  165. state: .normal,
  166. creatable: false)
  167. if true {
  168. let stringItems = ["None", "Arrow Open", "Arrow Close", "Square", "Oval", "Diamond", "Butt", "R Open Arrow", "R Close Arrow", "Slash"]
  169. let imageNames = ["arrowType_line", "arrowType_right_Arrow_open", "arrowType_right_Arrow_close",
  170. "arrowType_right_square", "arrowType_right_Oval", "arrowType_right_Diamond",
  171. "arrowType_right_Butt", "arrowType_right_Open_arrow",
  172. "arrowType_right_Close_arrow", "arrowType_right_Slash"]
  173. var opacityItems: [ComponentMenuitemProperty] = []
  174. for i in 0...stringItems.count-1 {
  175. let string = stringItems[i]
  176. let imageName = imageNames[i]
  177. let item = ComponentMenuitemProperty(type: .normal, lefticon: NSImage(named: imageName), text: string, identifier: string)
  178. opacityItems.append(item)
  179. }
  180. arrow_RightTypeSelect.updateMenuItemsArr(opacityItems)
  181. }
  182. arrow_RightTypeSelect.delegate = self
  183. }
  184. func reloadData() {
  185. guard let pdfView = self.pdfView else {
  186. return
  187. }
  188. var firstAnnotation: CPDFLineAnnotation?
  189. self.annotations.removeAll()
  190. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  191. for annotation in allAnnotations {
  192. if annotation is CPDFLineAnnotation {
  193. annotations.append((annotation as! CPDFLineAnnotation))
  194. }
  195. }
  196. if annotations.count > 0 {
  197. firstAnnotation = annotations.first
  198. }
  199. //Color
  200. var colors: [NSColor] = []
  201. if let annotation = firstAnnotation {
  202. if annotation.isArrowLine() == true {
  203. colors = KMAnnotationPropertiesColorManager.manager.arrowColors
  204. } else {
  205. colors = KMAnnotationPropertiesColorManager.manager.lineColors
  206. }
  207. } else if viewManager?.subToolMode == .Line {
  208. colors = KMAnnotationPropertiesColorManager.manager.lineColors
  209. } else if viewManager?.subToolMode == .Arrow {
  210. colors = KMAnnotationPropertiesColorManager.manager.arrowColors
  211. }
  212. if colors.count > 4 {
  213. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  214. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  215. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  216. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  217. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  218. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  219. }
  220. var startLineStyle = CPDFLineAnnotation.defaultArrow_StartLineStyle(.arrow)
  221. var endLineStyle = CPDFLineAnnotation.defaultArrow_EndLineStyle(.arrow)
  222. if annotations.count == 0 {
  223. var annotationType: CAnnotationType = .line
  224. if viewManager?.subToolMode == .Arrow {
  225. annotationType = .arrow
  226. }
  227. colorGroup.currentColor = CPDFLineAnnotation.defaultColor(annotationType)
  228. colorGroup.refreshUI()
  229. let opacity = CPDFLineAnnotation.defaultOpacity(annotationType)
  230. colorSlider.properties.percent = opacity
  231. colorSlider.reloadData()
  232. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  233. colorOpacitySelect.reloadData()
  234. let border: CPDFBorder = CPDFLineAnnotation.defaultBorder(annotationType)
  235. dashProperty.state = .normal
  236. solidProperty.state = .normal
  237. if border.style == .dashed {
  238. dashProperty.state = .pressed
  239. } else if border.style == .solid {
  240. solidProperty.state = .pressed
  241. }
  242. lineTypeSelector.reloadData()
  243. let percent = (border.lineWidth - 1)/17
  244. lineWidthSlider.properties.percent = percent
  245. lineWidthSlider.reloadData()
  246. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  247. lineWidthSelect.reloadData()
  248. linedashInfoView.isHidden = true
  249. if border.style == .dashed {
  250. linedashInfoView.isHidden = false
  251. var dash = 1.0
  252. for dashPattern in border.dashPattern {
  253. if let value = dashPattern as? CGFloat {
  254. dash = value
  255. break
  256. }
  257. }
  258. let percent: CGFloat = (CGFloat(dash) - 1)/17
  259. lineDashSlider.properties.percent = percent
  260. lineDashSlider.reloadData()
  261. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  262. lineDashSelect.reloadData()
  263. }
  264. } else if annotations.count == 1, let annotation = firstAnnotation {
  265. colorGroup.currentColor = annotation.color
  266. colorGroup.refreshUI()
  267. let opacity = annotation.opacity
  268. colorSlider.properties.percent = opacity
  269. colorSlider.reloadData()
  270. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  271. colorOpacitySelect.reloadData()
  272. let border: CPDFBorder = annotation.border ?? CPDFBorder()
  273. dashProperty.state = .normal
  274. solidProperty.state = .normal
  275. if border.style == .dashed {
  276. dashProperty.state = .pressed
  277. } else if border.style == .solid {
  278. solidProperty.state = .pressed
  279. }
  280. lineTypeSelector.reloadData()
  281. let percent = (border.lineWidth - 1)/17
  282. lineWidthSlider.properties.percent = percent
  283. lineWidthSlider.reloadData()
  284. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  285. lineWidthSelect.reloadData()
  286. linedashInfoView.isHidden = true
  287. if border.style == .dashed {
  288. linedashInfoView.isHidden = false
  289. var dash = 1.0
  290. for dashPattern in border.dashPattern {
  291. if let value = dashPattern as? CGFloat {
  292. dash = value
  293. break
  294. }
  295. }
  296. let percent: CGFloat = (CGFloat(dash) - 1)/17
  297. lineDashSlider.properties.percent = percent
  298. lineDashSlider.reloadData()
  299. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  300. lineDashSelect.reloadData()
  301. }
  302. startLineStyle = annotation.startLineStyle
  303. endLineStyle = annotation.endLineStyle
  304. } else {
  305. guard let annotation = firstAnnotation else {
  306. return
  307. }
  308. if true {
  309. let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color)
  310. if multiColor == true {
  311. colorGroup.currentColor = NSColor.clear
  312. } else {
  313. colorGroup.currentColor = annotation.color
  314. }
  315. colorGroup.refreshUI()
  316. }
  317. if true {
  318. let multiOpacity: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .opacity)
  319. if multiOpacity {
  320. colorSlider.properties.percent = 0
  321. colorSlider.reloadData()
  322. colorOpacitySelect.resetText("-")
  323. } else {
  324. let opacity = annotation.opacity
  325. colorSlider.properties.percent = opacity
  326. colorSlider.reloadData()
  327. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  328. colorOpacitySelect.reloadData()
  329. }
  330. }
  331. if true {
  332. let multiStyle: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .border_Style)
  333. linedashInfoView.isHidden = true
  334. if multiStyle {
  335. dashProperty.state = .normal
  336. solidProperty.state = .normal
  337. lineTypeSelector.reloadData()
  338. } else {
  339. let style = annotation.border?.style
  340. dashProperty.state = .normal
  341. solidProperty.state = .normal
  342. if style == .dashed {
  343. dashProperty.state = .pressed
  344. } else if style == .solid {
  345. solidProperty.state = .pressed
  346. }
  347. lineTypeSelector.reloadData()
  348. if style == .dashed {
  349. linedashInfoView.isHidden = false
  350. }
  351. }
  352. }
  353. if true {
  354. let multiLineWidth: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .line_Width)
  355. if multiLineWidth {
  356. lineWidthSlider.properties.percent = 0
  357. lineWidthSlider.reloadData()
  358. lineWidthSelect.resetText("-")
  359. } else {
  360. let border: CPDFBorder = annotation.border ?? CPDFBorder()
  361. let percent = (border.lineWidth - 1)/17
  362. lineWidthSlider.properties.percent = percent
  363. lineWidthSlider.reloadData()
  364. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  365. lineWidthSelect.reloadData()
  366. }
  367. }
  368. if true {
  369. let multiLineDash: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .dash_Pattern)
  370. if multiLineDash {
  371. lineDashSlider.properties.percent = 0
  372. lineDashSlider.reloadData()
  373. lineDashSelect.resetText("-")
  374. } else {
  375. var dashA = 1.0
  376. for dashPattern in annotation.border?.dashPattern ?? [] {
  377. if let value = dashPattern as? CGFloat {
  378. dashA = value
  379. break
  380. }
  381. }
  382. let percent = (dashA - 1)/17
  383. lineDashSlider.properties.percent = percent
  384. lineDashSlider.reloadData()
  385. lineDashSelect.properties.text = String(format: "%.0f%@", dashA, " pt")
  386. lineDashSelect.reloadData()
  387. }
  388. }
  389. startLineStyle = annotation.startLineStyle
  390. endLineStyle = annotation.endLineStyle
  391. }
  392. //ArrowType
  393. arrowTypeView.isHidden = true
  394. if let annotation = firstAnnotation, annotation.isArrowLine() == true {
  395. arrowTypeView.isHidden = false
  396. } else if viewManager?.subToolMode == .Arrow {
  397. arrowTypeView.isHidden = false
  398. }
  399. if linedashInfoView.isHidden {
  400. arrowTypeTopConst.constant = 8
  401. } else {
  402. arrowTypeTopConst.constant = 48
  403. }
  404. arrow_leftTypeSelect.selectItemAtIndex(startLineStyle.rawValue)
  405. arrow_RightTypeSelect.selectItemAtIndex(endLineStyle.rawValue)
  406. }
  407. //MARK: - Mouse
  408. override func mouseDown(with event: NSEvent) {
  409. super.mouseDown(with: event)
  410. view.window?.makeFirstResponder(nil)
  411. }
  412. }
  413. //MARK: - ComponentCColorDelegate
  414. extension KMLineController: ComponentCColorDelegate {
  415. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  416. self.componentCColorDidChooseColor(view, color, true)
  417. }
  418. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?, _ refresh: Bool) {
  419. CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView)
  420. CPDFAnnotation.updateAnnotations(annotations, newInteriorColor: color, withPDFView: pdfView)
  421. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  422. CPDFLineAnnotation.updateDefault_Color(color, .line)
  423. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  424. CPDFLineAnnotation.updateDefault_Color(color, .arrow)
  425. }
  426. if refresh == true {
  427. reloadData()
  428. }
  429. }
  430. func componentCColorGroupColorsUpdates(_ view: NSView, _ colors: [NSColor]) {
  431. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  432. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMLinePropertColorsKey)
  433. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  434. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMArrowPropertColorsKey)
  435. }
  436. }
  437. func componentCColorDidRightMouseUpWithStrings(_ view: NSView) -> [String] {
  438. return [KMLocalizedString("Change Color"), KMLocalizedString("Restore default color")]
  439. }
  440. func componentCColorDidRightMenuItemClicked(_ view: NSView, menuItemProperty: ComponentMenuitemProperty?) {
  441. if menuItemProperty?.text == KMLocalizedString("Change Color") {
  442. if NSColorPanel.sharedColorPanelExists {
  443. NSColorPanel.shared.setTarget(nil)
  444. NSColorPanel.shared.setAction(nil)
  445. }
  446. NSColorPanel.shared.setTarget(self)
  447. NSColorPanel.shared.setAction(#selector(colorChoose(_:)))
  448. if let colorItem = view as? ComponentCColorItem, let color = colorItem.properties?.color {
  449. NSColorPanel.shared.color = color
  450. }
  451. let viewRect = view.superview?.convert(view.frame, to: nil) ?? CGRectZero
  452. var rect = NSColorPanel.shared.frame
  453. rect.origin.x = viewRect.minX
  454. rect.origin.y = viewRect.minY - 30
  455. NSColorPanel.shared.setFrame(rect, display: true)
  456. NSColorPanel.shared.orderFront(nil)
  457. } else if menuItemProperty?.text == KMLocalizedString("Restore default color") {
  458. if let colorItem = colorGroup.rightClickedItem {
  459. var colors: [NSColor] = []
  460. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  461. colors = KMAnnotationPropertiesColorManager.manager.defaultColors(key: KMLinePropertColorsKey)
  462. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  463. colors = KMAnnotationPropertiesColorManager.manager.defaultColors(key: KMArrowPropertColorsKey)
  464. }
  465. if colors.count > 4, colorItem.itemIndex >= 0, colorItem.itemIndex < colors.count {
  466. colorItem.properties?.color = colors[colorItem.itemIndex]
  467. colorItem.reloadData()
  468. colorGroup.componentCColorDidChoosePanelColor(colorGroup, nil)
  469. self.componentCColorDidChooseColor(colorGroup, colorItem.properties?.color, false)
  470. }
  471. }
  472. }
  473. }
  474. @objc func colorChoose(_ sender: Any) {
  475. if let colorItem = colorGroup.rightClickedItem {
  476. let color = NSColorPanel.shared.color
  477. colorItem.properties?.color = color
  478. colorItem.reloadData()
  479. colorGroup.componentCColorDidChoosePanelColor(colorGroup, nil)
  480. self.componentCColorDidChooseColor(colorGroup, color, false)
  481. }
  482. }
  483. }
  484. //MARK: - ComponentSliderDelegate
  485. extension KMLineController: ComponentSliderDelegate {
  486. func componentSliderDidUpdate(_ view: ComponentSlider) {
  487. if view == colorSlider {
  488. let value = view.properties.percent
  489. CPDFAnnotation.updateAnnotations(annotations, newOpacity: value, withPDFView: pdfView)
  490. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  491. CPDFLineAnnotation.updateDefault_Opacity(value, .line)
  492. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  493. CPDFLineAnnotation.updateDefault_Opacity(value, .arrow)
  494. }
  495. } else if view == lineWidthSlider {
  496. let value = view.properties.percent * 17 + 1
  497. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  498. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  499. CPDFLineAnnotation.updateDefault_LineWidth(value, .line)
  500. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  501. CPDFLineAnnotation.updateDefault_LineWidth(value, .arrow)
  502. }
  503. } else if view == lineDashSlider {
  504. let value = view.properties.percent * 17 + 1
  505. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  506. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  507. CPDFLineAnnotation.updateDefault_DashPattern(value, .line)
  508. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  509. CPDFLineAnnotation.updateDefault_DashPattern(value, .arrow)
  510. }
  511. }
  512. reloadData()
  513. }
  514. }
  515. //MARK: - ComponentSelectDelegate
  516. extension KMLineController: ComponentSelectDelegate {
  517. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  518. if let result = text {
  519. if view == colorOpacitySelect {
  520. let value = max(0, min(1, result.stringToCGFloat()/100))
  521. CPDFAnnotation.updateAnnotations(annotations, newOpacity: value, withPDFView: pdfView)
  522. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  523. CPDFLineAnnotation.updateDefault_Opacity(value, .line)
  524. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  525. CPDFLineAnnotation.updateDefault_Opacity(value, .arrow)
  526. }
  527. } else if view == lineWidthSelect {
  528. var value = result.stringToCGFloat()
  529. if value > 18 {
  530. value = 18
  531. } else if value < 1 {
  532. value = 1
  533. }
  534. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  535. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  536. CPDFLineAnnotation.updateDefault_LineWidth(value, .line)
  537. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  538. CPDFLineAnnotation.updateDefault_LineWidth(value, .arrow)
  539. }
  540. } else if view == lineDashSelect {
  541. var value = result.stringToCGFloat()
  542. if value > 18 {
  543. value = 18
  544. } else if value < 1 {
  545. value = 1
  546. }
  547. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  548. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  549. CPDFLineAnnotation.updateDefault_DashPattern(value, .line)
  550. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  551. CPDFLineAnnotation.updateDefault_DashPattern(value, .arrow)
  552. }
  553. }
  554. reloadData()
  555. }
  556. }
  557. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  558. if var result = menuItemProperty?.text {
  559. if let textUnit = view?.properties.textUnit {
  560. result = result.stringByDeleteCharString(textUnit)
  561. }
  562. if view == colorOpacitySelect {
  563. let value = max(0, min(1, result.stringToCGFloat()/100))
  564. CPDFAnnotation.updateAnnotations(annotations, newOpacity: value, withPDFView: pdfView)
  565. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  566. CPDFLineAnnotation.updateDefault_Opacity(value, .line)
  567. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  568. CPDFLineAnnotation.updateDefault_Opacity(value, .arrow)
  569. }
  570. } else if view == lineWidthSelect {
  571. var value = result.stringToCGFloat()
  572. if value > 18 {
  573. value = 18
  574. } else if value < 1 {
  575. value = 1
  576. }
  577. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  578. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  579. CPDFLineAnnotation.updateDefault_LineWidth(value, .line)
  580. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  581. CPDFLineAnnotation.updateDefault_LineWidth(value, .arrow)
  582. }
  583. } else if view == lineDashSelect {
  584. var value = result.stringToCGFloat()
  585. if value > 18 {
  586. value = 18
  587. } else if value < 1 {
  588. value = 1
  589. }
  590. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  591. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  592. CPDFLineAnnotation.updateDefault_DashPattern(value, .line)
  593. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  594. CPDFLineAnnotation.updateDefault_DashPattern(value, .arrow)
  595. }
  596. } else if view == arrow_leftTypeSelect {
  597. let idx = view?.indexOfSelect() ?? 0
  598. var lineStype = CPDFLineStyle(rawValue: idx) ?? .none
  599. CPDFAnnotation.updateAnnotations(annotations, newStartLineStyle: lineStype, withPDFView: pdfView)
  600. CPDFLineAnnotation.updateDefault_StartLineStyle(lineStype, .arrow)
  601. } else if view == arrow_RightTypeSelect {
  602. let idx = view?.indexOfSelect() ?? 0
  603. var lineStype = CPDFLineStyle(rawValue: idx) ?? .none
  604. CPDFAnnotation.updateAnnotations(annotations, newEndLineStyle: lineStype, withPDFView: pdfView)
  605. CPDFLineAnnotation.updateDefault_EndLineStyle(lineStype, .arrow)
  606. }
  607. reloadData()
  608. }
  609. }
  610. }
  611. //MARK: - ComponentCSelectorGroupDelegate
  612. extension KMLineController: ComponentCSelectorGroupDelegate {
  613. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  614. if item.properties == solidProperty {
  615. CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .solid, withPDFView: pdfView)
  616. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  617. CPDFLineAnnotation.updateDefault_Style(.solid, .line)
  618. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  619. CPDFLineAnnotation.updateDefault_Style(.solid, .arrow)
  620. }
  621. } else if item.properties == dashProperty {
  622. CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .dashed, withPDFView: pdfView)
  623. if self.lineAnnotations.count > 0 || viewManager?.subToolMode == .Line {
  624. CPDFLineAnnotation.updateDefault_Style(.dashed, .line)
  625. } else if self.arrowAnnotations.count > 0 || viewManager?.subToolMode == .Arrow {
  626. CPDFLineAnnotation.updateDefault_Style(.dashed, .arrow)
  627. }
  628. }
  629. reloadData()
  630. }
  631. }