KMRectangleController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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", "2 pt", "4 pt", "6 pt", "8 pt", "10 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", "2 pt", "4 pt", "6 pt", "8 pt", "10 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/18
  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)/18
  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/18
  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)/18
  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 ?? CPDFBorder()
  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/18
  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)/18
  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 = nil
  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 = nil
  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 ?? CPDFBorder()
  356. let percent = border.lineWidth/18
  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 / 18
  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. CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView)
  406. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  407. CPDFCircleAnnotation.updateDefault_Color(color)
  408. }
  409. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  410. CPDFSquareAnnotation.updateDefault_Color(color)
  411. }
  412. } else if view == fillColorGroup {
  413. CPDFAnnotation.updateAnnotations(annotations, newInteriorColor: color, withPDFView: pdfView)
  414. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  415. CPDFCircleAnnotation.updateDefault_InteriorColor(color)
  416. }
  417. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  418. CPDFSquareAnnotation.updateDefault_Color(color)
  419. }
  420. }
  421. reloadData()
  422. }
  423. }
  424. //MARK: - ComponentSliderDelegate
  425. extension KMRectangleController: ComponentSliderDelegate {
  426. func componentSliderDidUpdate(_ view: ComponentSlider) {
  427. if view == colorSlider {
  428. let opacity = view.properties.percent
  429. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  430. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  431. CPDFCircleAnnotation.updateDefault_Opacity(opacity)
  432. }
  433. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  434. CPDFSquareAnnotation.updateDefault_Opacity(opacity)
  435. }
  436. } else if view == lineWidthSlider {
  437. let value = view.properties.percent * 18
  438. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  439. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  440. CPDFCircleAnnotation.updateDefault_LineWidth(value)
  441. }
  442. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  443. CPDFSquareAnnotation.updateDefault_LineWidth(value)
  444. }
  445. } else if view == lineDashSlider {
  446. let value = view.properties.percent * 18
  447. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  448. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle{
  449. CPDFCircleAnnotation.updateDefault_DashPattern(value)
  450. }
  451. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  452. CPDFSquareAnnotation.updateDefault_DashPattern(value)
  453. }
  454. }
  455. reloadData()
  456. }
  457. }
  458. //MARK: - ComponentSelectDelegate
  459. extension KMRectangleController: ComponentSelectDelegate {
  460. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  461. if let result = text {
  462. if view == colorOpacitySelect {
  463. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  464. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  465. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  466. CPDFCircleAnnotation.updateDefault_Opacity(opacity)
  467. }
  468. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  469. CPDFSquareAnnotation.updateDefault_Opacity(opacity)
  470. }
  471. } else if view == lineWidthSelect {
  472. var value = result.stringToCGFloat()
  473. if value > 18 {
  474. value = 18
  475. } else if value < 0 {
  476. value = 0
  477. }
  478. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  479. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  480. CPDFCircleAnnotation.updateDefault_LineWidth(value)
  481. }
  482. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  483. CPDFSquareAnnotation.updateDefault_LineWidth(value)
  484. }
  485. } else if view == lineDashSelect {
  486. var value = result.stringToCGFloat()
  487. if value > 18 {
  488. value = 18
  489. } else if value < 0 {
  490. value = 0
  491. }
  492. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  493. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle{
  494. CPDFCircleAnnotation.updateDefault_DashPattern(value)
  495. }
  496. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  497. CPDFSquareAnnotation.updateDefault_DashPattern(value)
  498. }
  499. }
  500. reloadData()
  501. }
  502. }
  503. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  504. if var result = menuItemProperty?.text {
  505. if let textUnit = view?.properties.textUnit {
  506. result = result.stringByDeleteCharString(textUnit)
  507. }
  508. if view == colorOpacitySelect {
  509. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  510. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  511. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  512. CPDFCircleAnnotation.updateDefault_Opacity(opacity)
  513. }
  514. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  515. CPDFSquareAnnotation.updateDefault_Opacity(opacity)
  516. }
  517. } else if view == lineWidthSelect {
  518. var value = result.stringToCGFloat()
  519. if value > 18 {
  520. value = 18
  521. } else if value < 0 {
  522. value = 0
  523. }
  524. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  525. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  526. CPDFCircleAnnotation.updateDefault_LineWidth(value)
  527. }
  528. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  529. CPDFSquareAnnotation.updateDefault_LineWidth(value)
  530. }
  531. } else if view == lineDashSelect {
  532. var value = result.stringToCGFloat()
  533. if value > 18 {
  534. value = 18
  535. } else if value < 0 {
  536. value = 0
  537. }
  538. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  539. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle{
  540. CPDFCircleAnnotation.updateDefault_DashPattern(value)
  541. }
  542. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  543. CPDFSquareAnnotation.updateDefault_DashPattern(value)
  544. }
  545. }
  546. reloadData()
  547. }
  548. }
  549. }
  550. //MARK: - ComponentCSelectorGroupDelegate
  551. extension KMRectangleController: ComponentCSelectorGroupDelegate {
  552. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  553. if item.properties == solidProperty {
  554. CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .solid, withPDFView: pdfView)
  555. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  556. CPDFCircleAnnotation.updateDefault_Style(.solid)
  557. }
  558. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  559. CPDFSquareAnnotation.updateDefault_Style(.solid)
  560. }
  561. } else if item.properties == dashProperty {
  562. CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .solid, withPDFView: pdfView)
  563. if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle {
  564. CPDFCircleAnnotation.updateDefault_Style(.dashed)
  565. }
  566. if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle {
  567. CPDFSquareAnnotation.updateDefault_Style(.dashed)
  568. }
  569. }
  570. reloadData()
  571. }
  572. }