KMRectangleController.swift 30 KB

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