KMRectangleController.swift 30 KB

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