KMLineController.swift 33 KB

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