KMRectangleController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. //
  2. // KMRectangleController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMRectangleController: 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. @IBOutlet var fillColorGroup: ComponentCColorGroup!
  17. //Line
  18. @IBOutlet var lineBGView: NSView!
  19. @IBOutlet var lineLabel: NSTextField!
  20. @IBOutlet var lineTypeSelector: ComponentCSelectorGroup!
  21. @IBOutlet var lineWidthSlider: ComponentSlider!
  22. @IBOutlet var lineWidthSelect: ComponentSelect!
  23. @IBOutlet var linedashInfoView: NSView!
  24. @IBOutlet var lineDashSlider: ComponentSlider!
  25. @IBOutlet var lineDashSelect: ComponentSelect!
  26. private let solidProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_solid"))
  27. private let dashProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_dash"))
  28. private var annotations: [CPDFAnnotation] = [] //CPDFCircleAnnotation, CPDFSquareAnnotation
  29. private var circleAnnotations: [CPDFCircleAnnotation] {
  30. get {
  31. var validAnnotations: [CPDFCircleAnnotation] = []
  32. for annotation in annotations {
  33. if annotation is CPDFCircleAnnotation {
  34. validAnnotations.append((annotation as! CPDFCircleAnnotation))
  35. }
  36. }
  37. return validAnnotations
  38. }
  39. }
  40. private var squareAnnotations: [CPDFSquareAnnotation] {
  41. get {
  42. var validAnnotations: [CPDFSquareAnnotation] = []
  43. for annotation in annotations {
  44. if annotation is CPDFSquareAnnotation {
  45. validAnnotations.append((annotation as! CPDFSquareAnnotation))
  46. }
  47. }
  48. return validAnnotations
  49. }
  50. }
  51. var pdfView: CPDFListView?
  52. var viewManager: KMPDFViewManager?
  53. //MARK: - func
  54. override func viewDidAppear() {
  55. super.viewDidAppear()
  56. colorSlider.reloadData()
  57. lineWidthSlider.reloadData()
  58. lineDashSlider.reloadData()
  59. }
  60. override func viewDidLoad() {
  61. super.viewDidLoad()
  62. // Do view setup here.
  63. setupProperty()
  64. }
  65. func setupProperty() {
  66. //Color
  67. colorLabel.stringValue = KMLocalizedString("Color")
  68. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  69. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  70. colorGroup.delegate = self
  71. fillColorGroup.delegate = self
  72. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  73. colorSlider.delegate = self
  74. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  75. state: .normal,
  76. creatable: true,
  77. text: "100%",
  78. textUnit: "%",
  79. regexString: "0123456789%")
  80. if true {
  81. var opacityItems: [ComponentMenuitemProperty] = []
  82. for string in ["25%", "50%", "75%", "100%"] {
  83. let item = ComponentMenuitemProperty(type: .normal, text: string)
  84. opacityItems.append(item)
  85. }
  86. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  87. }
  88. colorOpacitySelect.delegate = self
  89. //Line
  90. lineLabel.stringValue = KMLocalizedString("Line")
  91. lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  92. lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  93. lineTypeSelector.updateItemProperty([solidProperty, dashProperty])
  94. lineTypeSelector.delegate = self
  95. lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  96. lineWidthSlider.delegate = self
  97. lineWidthSelect.properties = ComponentSelectProperties(size: .s,
  98. state: .normal,
  99. creatable: true,
  100. text: "2",
  101. textUnit: " pt",
  102. regexString: "0123456789 pt")
  103. if true {
  104. var opacityItems: [ComponentMenuitemProperty] = []
  105. for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
  106. let item = ComponentMenuitemProperty(type: .normal, text: string)
  107. opacityItems.append(item)
  108. }
  109. lineWidthSelect.updateMenuItemsArr(opacityItems)
  110. }
  111. lineWidthSelect.delegate = self
  112. lineDashSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  113. lineDashSlider.delegate = self
  114. lineDashSelect.properties = ComponentSelectProperties(size: .s,
  115. state: .normal,
  116. creatable: true,
  117. text: "2",
  118. textUnit: " pt",
  119. regexString: "0123456789 pt")
  120. if true {
  121. var opacityItems: [ComponentMenuitemProperty] = []
  122. for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
  123. let item = ComponentMenuitemProperty(type: .normal, text: string)
  124. opacityItems.append(item)
  125. }
  126. lineDashSelect.updateMenuItemsArr(opacityItems)
  127. }
  128. lineDashSelect.delegate = self
  129. }
  130. func reloadData() {
  131. guard let pdfView = self.pdfView else {
  132. return
  133. }
  134. var firstAnnotation: CPDFAnnotation?
  135. self.annotations.removeAll()
  136. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  137. for annotation in allAnnotations {
  138. if annotation is CPDFCircleAnnotation {
  139. annotations.append((annotation as! CPDFCircleAnnotation))
  140. } else if annotation is CPDFSquareAnnotation {
  141. annotations.append((annotation as! CPDFSquareAnnotation))
  142. }
  143. }
  144. if annotations.count > 0 {
  145. firstAnnotation = annotations.first
  146. }
  147. if true {
  148. var colors: [NSColor] = []
  149. var fillColors: [NSColor] = []
  150. if let annotation = firstAnnotation {
  151. if annotation is CPDFSquareAnnotation {
  152. colors = KMAnnotationPropertiesColorManager.manager.rectangle_Colors
  153. fillColors = KMAnnotationPropertiesColorManager.manager.rectangle_Fill_Colors
  154. } else if annotation is CPDFCircleAnnotation {
  155. colors = KMAnnotationPropertiesColorManager.manager.circle_Colors
  156. fillColors = KMAnnotationPropertiesColorManager.manager.circle_Fill_Colors
  157. }
  158. } else if viewManager?.subToolMode == .Rectangle {
  159. colors = KMAnnotationPropertiesColorManager.manager.rectangle_Colors
  160. fillColors = KMAnnotationPropertiesColorManager.manager.rectangle_Fill_Colors
  161. } else if viewManager?.subToolMode == .Circle {
  162. colors = KMAnnotationPropertiesColorManager.manager.circle_Colors
  163. fillColors = KMAnnotationPropertiesColorManager.manager.circle_Fill_Colors
  164. }
  165. if colors.count > 4 {
  166. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  167. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  168. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  169. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  170. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  171. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  172. }
  173. if fillColors.count > 4 {
  174. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: fillColors[0])
  175. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: fillColors[1])
  176. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: fillColors[2])
  177. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: fillColors[3])
  178. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: fillColors[4])
  179. fillColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  180. }
  181. }
  182. if annotations.count == 0 {
  183. if viewManager?.subToolMode == .Rectangle {
  184. colorGroup.currentColor = CPDFSquareAnnotation.defaultColor()
  185. fillColorGroup.currentColor = CPDFSquareAnnotation.defaultInteriorColor()
  186. let opacity = CPDFSquareAnnotation.defaultOpacity()
  187. colorSlider.properties.percent = opacity
  188. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  189. let border: CPDFBorder = CPDFSquareAnnotation.defaultBorder()
  190. dashProperty.state = .normal
  191. solidProperty.state = .normal
  192. if border.style == .dashed {
  193. dashProperty.state = .pressed
  194. } else if border.style == .solid {
  195. solidProperty.state = .pressed
  196. }
  197. let percent = (border.lineWidth - 1)/17
  198. lineWidthSlider.properties.percent = percent
  199. lineWidthSlider.reloadData()
  200. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  201. lineWidthSelect.reloadData()
  202. linedashInfoView.isHidden = true
  203. if border.style == .dashed {
  204. linedashInfoView.isHidden = false
  205. var dash = 1.0
  206. for dashPattern in border.dashPattern {
  207. if let value = dashPattern as? CGFloat {
  208. dash = value
  209. break
  210. }
  211. }
  212. let percent: CGFloat = (CGFloat(dash) - 1)/17
  213. lineDashSlider.properties.percent = percent
  214. lineDashSlider.reloadData()
  215. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  216. lineDashSelect.reloadData()
  217. }
  218. } else if viewManager?.subToolMode == .Circle {
  219. colorGroup.currentColor = CPDFCircleAnnotation.defaultColor()
  220. fillColorGroup.currentColor = CPDFCircleAnnotation.defaultInteriorColor()
  221. let opacity = CPDFCircleAnnotation.defaultOpacity()
  222. colorSlider.properties.percent = opacity
  223. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  224. let border: CPDFBorder = CPDFCircleAnnotation.defaultBorder()
  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. let percent = (border.lineWidth - 1)/17
  233. lineWidthSlider.properties.percent = percent
  234. lineWidthSlider.reloadData()
  235. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  236. lineWidthSelect.reloadData()
  237. linedashInfoView.isHidden = true
  238. if border.style == .dashed {
  239. linedashInfoView.isHidden = false
  240. var dash = 1.0
  241. for dashPattern in border.dashPattern {
  242. if let value = dashPattern as? CGFloat {
  243. dash = value
  244. break
  245. }
  246. }
  247. let percent: CGFloat = (CGFloat(dash) - 1)/17
  248. lineDashSlider.properties.percent = percent
  249. lineDashSlider.reloadData()
  250. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  251. lineDashSelect.reloadData()
  252. }
  253. }
  254. } else if annotations.count == 1, let annotation = firstAnnotation {
  255. colorGroup.currentColor = annotation.color
  256. if firstAnnotation is CPDFCircleAnnotation {
  257. fillColorGroup.currentColor = (firstAnnotation as! CPDFCircleAnnotation).interiorColor
  258. } else if firstAnnotation is CPDFSquareAnnotation {
  259. fillColorGroup.currentColor = (firstAnnotation as! CPDFSquareAnnotation).interiorColor
  260. }
  261. let opacity = annotation.opacity
  262. colorSlider.properties.percent = opacity
  263. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  264. let border: CPDFBorder = annotation.border
  265. dashProperty.state = .normal
  266. solidProperty.state = .normal
  267. if border.style == .dashed {
  268. dashProperty.state = .pressed
  269. } else if border.style == .solid {
  270. solidProperty.state = .pressed
  271. }
  272. let percent = (border.lineWidth - 1)/17
  273. lineWidthSlider.properties.percent = percent
  274. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  275. linedashInfoView.isHidden = true
  276. if border.style == .dashed {
  277. linedashInfoView.isHidden = false
  278. var dash = 1.0
  279. for dashPattern in border.dashPattern {
  280. if let value = dashPattern as? CGFloat {
  281. dash = value
  282. break
  283. }
  284. }
  285. let percent: CGFloat = (CGFloat(dash) - 1)/17
  286. lineDashSlider.properties.percent = percent
  287. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  288. }
  289. } else {
  290. guard let annotation = firstAnnotation else {
  291. return
  292. }
  293. if true {
  294. let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color)
  295. if multiColor == true {
  296. colorGroup.currentColor = NSColor.clear
  297. } else {
  298. colorGroup.currentColor = annotation.color
  299. }
  300. }
  301. if true {
  302. let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .interiorColor)
  303. if multiColor == true {
  304. fillColorGroup.currentColor = NSColor.clear
  305. } else {
  306. if firstAnnotation is CPDFSquareAnnotation {
  307. fillColorGroup.currentColor = (firstAnnotation as! CPDFSquareAnnotation).interiorColor
  308. } else if firstAnnotation is CPDFCircleAnnotation {
  309. fillColorGroup.currentColor = (firstAnnotation as! CPDFCircleAnnotation).interiorColor
  310. }
  311. }
  312. }
  313. if true {
  314. let multiOpacity: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .opacity)
  315. if multiOpacity {
  316. colorSlider.properties.percent = 0
  317. colorOpacitySelect.resetText("-")
  318. } else {
  319. let opacity = annotation.opacity
  320. colorSlider.properties.percent = opacity
  321. colorSlider.reloadData()
  322. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  323. colorOpacitySelect.reloadData()
  324. }
  325. }
  326. if true {
  327. let multiStyle: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .border_Style)
  328. linedashInfoView.isHidden = true
  329. if multiStyle {
  330. dashProperty.state = .normal
  331. solidProperty.state = .normal
  332. lineTypeSelector.reloadData()
  333. } else {
  334. let style = annotation.border.style
  335. dashProperty.state = .normal
  336. solidProperty.state = .normal
  337. if style == .dashed {
  338. dashProperty.state = .pressed
  339. } else if style == .solid {
  340. solidProperty.state = .pressed
  341. }
  342. lineTypeSelector.reloadData()
  343. if style == .dashed {
  344. linedashInfoView.isHidden = false
  345. }
  346. }
  347. }
  348. if true {
  349. let multiLineWidth: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .line_Width)
  350. if multiLineWidth {
  351. lineWidthSlider.properties.percent = 0
  352. lineWidthSlider.reloadData()
  353. lineWidthSelect.resetText("-")
  354. } else {
  355. let border: CPDFBorder = annotation.border
  356. let percent = (border.lineWidth - 1)/17
  357. lineWidthSlider.properties.percent = percent
  358. lineWidthSlider.reloadData()
  359. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  360. lineWidthSelect.reloadData()
  361. }
  362. }
  363. if true {
  364. let multiLineDash: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .dash_Pattern)
  365. if multiLineDash {
  366. lineDashSlider.properties.percent = 0
  367. lineDashSlider.reloadData()
  368. lineDashSelect.resetText("-")
  369. } else {
  370. var dashA = 1.0
  371. for dashPattern in annotation.border.dashPattern {
  372. if let value = dashPattern as? CGFloat {
  373. dashA = value
  374. break
  375. }
  376. }
  377. let percent = (dashA - 1)/17
  378. lineDashSlider.properties.percent = percent
  379. lineDashSlider.reloadData()
  380. lineDashSelect.properties.text = String(format: "%.0f%@", dashA, " pt")
  381. lineDashSelect.reloadData()
  382. }
  383. }
  384. }
  385. colorGroup.refreshUI()
  386. fillColorGroup.refreshUI()
  387. colorSlider.reloadData()
  388. colorOpacitySelect.reloadData()
  389. lineTypeSelector.reloadData()
  390. lineWidthSlider.reloadData()
  391. lineWidthSelect.reloadData()
  392. lineDashSlider.reloadData()
  393. lineDashSelect.reloadData()
  394. }
  395. //MARK: - Mouse
  396. override func mouseDown(with event: NSEvent) {
  397. super.mouseDown(with: event)
  398. view.window?.makeFirstResponder(nil)
  399. }
  400. }
  401. //MARK: - ComponentCColorDelegate
  402. extension KMRectangleController: ComponentCColorDelegate {
  403. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  404. if view == colorGroup {
  405. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  406. CPDFCircleAnnotation.updateColor(circleAnnotations, color, withPDFView: pdfView)
  407. }
  408. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  409. CPDFSquareAnnotation.updateColor(squareAnnotations, color, withPDFView: pdfView)
  410. }
  411. } else if view == fillColorGroup {
  412. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  413. CPDFCircleAnnotation.updateInteriorColor(circleAnnotations, color, withPDFView: pdfView)
  414. }
  415. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  416. CPDFSquareAnnotation.updateInteriorColor(squareAnnotations, color, withPDFView: pdfView)
  417. }
  418. }
  419. reloadData()
  420. }
  421. }
  422. //MARK: - ComponentSliderDelegate
  423. extension KMRectangleController: ComponentSliderDelegate {
  424. func componentSliderDidUpdate(_ view: ComponentSlider) {
  425. if view == colorSlider {
  426. let percent = view.properties.percent
  427. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  428. CPDFCircleAnnotation.updateOpacity(circleAnnotations, percent, withPDFView: pdfView)
  429. }
  430. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  431. CPDFSquareAnnotation.updateOpacity(squareAnnotations, percent, withPDFView: pdfView)
  432. }
  433. } else if view == lineWidthSlider {
  434. let percent = view.properties.percent * 17 + 1
  435. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  436. CPDFCircleAnnotation.updateLineWidth(circleAnnotations, percent, withPDFView: pdfView)
  437. }
  438. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  439. CPDFSquareAnnotation.updateLineWidth(squareAnnotations, percent, withPDFView: pdfView)
  440. }
  441. } else if view == lineDashSlider {
  442. let percent = view.properties.percent * 17 + 1
  443. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle{
  444. CPDFCircleAnnotation.updateDashPattern(circleAnnotations, percent, withPDFView: pdfView)
  445. }
  446. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  447. CPDFSquareAnnotation.updateDashPattern(squareAnnotations, percent, withPDFView: pdfView)
  448. }
  449. }
  450. reloadData()
  451. }
  452. }
  453. //MARK: - ComponentSelectDelegate
  454. extension KMRectangleController: ComponentSelectDelegate {
  455. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  456. if let result = text {
  457. if view == colorOpacitySelect {
  458. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  459. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  460. CPDFCircleAnnotation.updateOpacity(circleAnnotations, opacity, withPDFView: pdfView)
  461. }
  462. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  463. CPDFSquareAnnotation.updateOpacity(squareAnnotations, opacity, withPDFView: pdfView)
  464. }
  465. } else if view == lineWidthSelect {
  466. var value = result.stringToCGFloat()
  467. if value > 18 {
  468. value = 18
  469. } else if value < 1 {
  470. value = 1
  471. }
  472. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle{
  473. CPDFCircleAnnotation.updateLineWidth(circleAnnotations, value, withPDFView: pdfView)
  474. }
  475. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  476. CPDFSquareAnnotation.updateLineWidth(squareAnnotations, value, withPDFView: pdfView)
  477. }
  478. } else if view == lineDashSelect {
  479. var value = result.stringToCGFloat()
  480. if value > 18 {
  481. value = 18
  482. } else if value < 1 {
  483. value = 1
  484. }
  485. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  486. CPDFCircleAnnotation.updateDashPattern(circleAnnotations, value, withPDFView: pdfView)
  487. }
  488. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  489. CPDFSquareAnnotation.updateDashPattern(squareAnnotations, value, withPDFView: pdfView)
  490. }
  491. }
  492. reloadData()
  493. }
  494. }
  495. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  496. if var result = menuItemProperty?.text {
  497. if let textUnit = view?.properties.textUnit {
  498. result = result.stringByDeleteCharString(textUnit)
  499. }
  500. if view == colorOpacitySelect {
  501. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  502. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  503. CPDFCircleAnnotation.updateOpacity(circleAnnotations, opacity, withPDFView: pdfView)
  504. }
  505. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  506. CPDFSquareAnnotation.updateOpacity(squareAnnotations, opacity, withPDFView: pdfView)
  507. }
  508. } else if view == lineWidthSelect {
  509. var value = result.stringToCGFloat()
  510. if value > 18 {
  511. value = 18
  512. } else if value < 1 {
  513. value = 1
  514. }
  515. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  516. CPDFCircleAnnotation.updateLineWidth(circleAnnotations, value, withPDFView: pdfView)
  517. }
  518. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  519. CPDFSquareAnnotation.updateLineWidth(squareAnnotations, value, withPDFView: pdfView)
  520. }
  521. } else if view == lineDashSelect {
  522. var value = result.stringToCGFloat()
  523. if value > 18 {
  524. value = 18
  525. } else if value < 1 {
  526. value = 1
  527. }
  528. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  529. CPDFCircleAnnotation.updateDashPattern(circleAnnotations, value, withPDFView: pdfView)
  530. }
  531. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  532. CPDFSquareAnnotation.updateDashPattern(squareAnnotations, value, withPDFView: pdfView)
  533. }
  534. }
  535. reloadData()
  536. }
  537. }
  538. }
  539. //MARK: - ComponentCSelectorGroupDelegate
  540. extension KMRectangleController: ComponentCSelectorGroupDelegate {
  541. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  542. if item.properties == solidProperty {
  543. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  544. CPDFCircleAnnotation.updateStyle(circleAnnotations, .solid, withPDFView: pdfView)
  545. }
  546. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  547. CPDFSquareAnnotation.updateStyle(squareAnnotations, .solid, withPDFView: pdfView)
  548. }
  549. } else if item.properties == dashProperty {
  550. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  551. CPDFCircleAnnotation.updateStyle(circleAnnotations, .dashed, withPDFView: pdfView)
  552. }
  553. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  554. CPDFSquareAnnotation.updateStyle(squareAnnotations, .dashed, withPDFView: pdfView)
  555. }
  556. }
  557. reloadData()
  558. }
  559. }