KMPenController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //
  2. // KMPenController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMPenController: NSViewController {
  10. //Color
  11. @IBOutlet var colorBGView: NSView!
  12. @IBOutlet var colorLabel: NSTextField!
  13. @IBOutlet var colorGroup: ComponentCColorGroup!
  14. @IBOutlet var colorSlider: ComponentSlider!
  15. @IBOutlet var colorOpacitySelect: ComponentSelect!
  16. //Line
  17. @IBOutlet var lineBGView: NSView!
  18. @IBOutlet var lineLabel: NSTextField!
  19. @IBOutlet var lineTypeSelector: ComponentCSelectorGroup!
  20. @IBOutlet var lineWidthSlider: ComponentSlider!
  21. @IBOutlet var lineWidthSelect: ComponentSelect!
  22. @IBOutlet var linedashInfoView: NSView!
  23. @IBOutlet var lineDashSlider: ComponentSlider!
  24. @IBOutlet var lineDashSelect: ComponentSelect!
  25. private let solidProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_solid"))
  26. private let dashProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_dash"))
  27. private var annotations: [CPDFInkAnnotation] = []
  28. var pdfView: CPDFListView?
  29. //MARK: - func
  30. override func viewDidAppear() {
  31. super.viewDidAppear()
  32. colorSlider.reloadData()
  33. lineWidthSlider.reloadData()
  34. lineDashSlider.reloadData()
  35. }
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. // Do view setup here.
  39. setupProperty()
  40. }
  41. func setupProperty() {
  42. //Color
  43. colorLabel.stringValue = KMLocalizedString("Color")
  44. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  45. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  46. colorGroup.delegate = self
  47. colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  48. colorSlider.delegate = self
  49. colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
  50. state: .normal,
  51. creatable: true,
  52. text: "100%",
  53. textUnit: "%",
  54. regexString: "0123456789%")
  55. if true {
  56. var opacityItems: [ComponentMenuitemProperty] = []
  57. for string in ["25%", "50%", "75%", "100%"] {
  58. let item = ComponentMenuitemProperty(type: .normal, text: string)
  59. opacityItems.append(item)
  60. }
  61. colorOpacitySelect.updateMenuItemsArr(opacityItems)
  62. }
  63. colorOpacitySelect.delegate = self
  64. //Line
  65. lineLabel.stringValue = KMLocalizedString("Line")
  66. lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  67. lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  68. lineTypeSelector.updateItemProperty([solidProperty, dashProperty])
  69. lineTypeSelector.delegate = self
  70. lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  71. lineWidthSlider.delegate = self
  72. lineWidthSelect.properties = ComponentSelectProperties(size: .s,
  73. state: .normal,
  74. creatable: true,
  75. text: "2",
  76. textUnit: " pt",
  77. regexString: "0123456789 pt")
  78. if true {
  79. var opacityItems: [ComponentMenuitemProperty] = []
  80. for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
  81. let item = ComponentMenuitemProperty(type: .normal, text: string)
  82. opacityItems.append(item)
  83. }
  84. lineWidthSelect.updateMenuItemsArr(opacityItems)
  85. }
  86. lineWidthSelect.delegate = self
  87. lineDashSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  88. lineDashSlider.delegate = self
  89. lineDashSelect.properties = ComponentSelectProperties(size: .s,
  90. state: .normal,
  91. creatable: true,
  92. text: "2",
  93. textUnit: " pt",
  94. regexString: "0123456789 pt")
  95. if true {
  96. var opacityItems: [ComponentMenuitemProperty] = []
  97. for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
  98. let item = ComponentMenuitemProperty(type: .normal, text: string)
  99. opacityItems.append(item)
  100. }
  101. lineDashSelect.updateMenuItemsArr(opacityItems)
  102. }
  103. lineDashSelect.delegate = self
  104. }
  105. func reloadData() {
  106. guard let pdfView = self.pdfView else {
  107. return
  108. }
  109. if true {
  110. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.inkColors
  111. if colors.count > 4 {
  112. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  113. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  114. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  115. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  116. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  117. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  118. }
  119. }
  120. var firstAnnotation: CPDFInkAnnotation?
  121. self.annotations.removeAll()
  122. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  123. for annotation in allAnnotations {
  124. if annotation is CPDFInkAnnotation {
  125. if firstAnnotation == nil {
  126. firstAnnotation = (annotation as! CPDFInkAnnotation)
  127. }
  128. annotations.append((annotation as! CPDFInkAnnotation))
  129. }
  130. }
  131. if annotations.count == 0 {
  132. colorGroup.currentColor = CPDFInkAnnotation.defaultColor()
  133. colorGroup.refreshUI()
  134. let opacity = CPDFInkAnnotation.defaultOpacity()
  135. colorSlider.properties.percent = opacity
  136. colorSlider.reloadData()
  137. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  138. colorOpacitySelect.reloadData()
  139. let border: CPDFBorder = CPDFInkAnnotation.defaultBorder()
  140. dashProperty.state = .normal
  141. solidProperty.state = .normal
  142. if border.style == .dashed {
  143. dashProperty.state = .pressed
  144. } else if border.style == .solid {
  145. solidProperty.state = .pressed
  146. }
  147. lineTypeSelector.reloadData()
  148. let percent = (border.lineWidth - 1)/17
  149. lineWidthSlider.properties.percent = percent
  150. lineWidthSlider.reloadData()
  151. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  152. lineWidthSelect.reloadData()
  153. linedashInfoView.isHidden = true
  154. if border.style == .dashed {
  155. linedashInfoView.isHidden = false
  156. var dash = 1.0
  157. for dashPattern in border.dashPattern {
  158. if let value = dashPattern as? CGFloat {
  159. dash = value
  160. break
  161. }
  162. }
  163. let percent: CGFloat = (CGFloat(dash) - 1)/17
  164. lineDashSlider.properties.percent = percent
  165. lineDashSlider.reloadData()
  166. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  167. lineDashSelect.reloadData()
  168. }
  169. } else if annotations.count == 1, let annotation = firstAnnotation {
  170. colorGroup.currentColor = annotation.color
  171. colorGroup.refreshUI()
  172. let opacity = annotation.opacity
  173. colorSlider.properties.percent = opacity
  174. colorSlider.reloadData()
  175. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  176. colorOpacitySelect.reloadData()
  177. let border: CPDFBorder = annotation.border
  178. dashProperty.state = .normal
  179. solidProperty.state = .normal
  180. if border.style == .dashed {
  181. dashProperty.state = .pressed
  182. } else if border.style == .solid {
  183. solidProperty.state = .pressed
  184. }
  185. lineTypeSelector.reloadData()
  186. let percent = (border.lineWidth - 1)/17
  187. lineWidthSlider.properties.percent = percent
  188. lineWidthSlider.reloadData()
  189. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  190. lineWidthSelect.reloadData()
  191. linedashInfoView.isHidden = true
  192. if border.style == .dashed {
  193. linedashInfoView.isHidden = false
  194. var dash = 1.0
  195. for dashPattern in border.dashPattern {
  196. if let value = dashPattern as? CGFloat {
  197. dash = value
  198. break
  199. }
  200. }
  201. let percent: CGFloat = (CGFloat(dash) - 1)/17
  202. lineDashSlider.properties.percent = percent
  203. lineDashSlider.reloadData()
  204. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  205. lineDashSelect.reloadData()
  206. }
  207. } else {
  208. guard let annotation = firstAnnotation else {
  209. return
  210. }
  211. if true {
  212. let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color)
  213. if multiColor == true {
  214. colorGroup.currentColor = NSColor.clear
  215. } else {
  216. colorGroup.currentColor = annotation.color
  217. }
  218. colorGroup.refreshUI()
  219. }
  220. if true {
  221. let multiOpacity: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .opacity)
  222. if multiOpacity {
  223. colorSlider.properties.percent = 0
  224. colorSlider.reloadData()
  225. colorOpacitySelect.resetText("-")
  226. } else {
  227. let opacity = annotation.opacity
  228. colorSlider.properties.percent = opacity
  229. colorSlider.reloadData()
  230. colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  231. colorOpacitySelect.reloadData()
  232. }
  233. }
  234. if true {
  235. var multiStyle: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .border_Style)
  236. linedashInfoView.isHidden = true
  237. if multiStyle {
  238. dashProperty.state = .normal
  239. solidProperty.state = .normal
  240. lineTypeSelector.reloadData()
  241. } else {
  242. let style = annotation.border.style
  243. dashProperty.state = .normal
  244. solidProperty.state = .normal
  245. if style == .dashed {
  246. dashProperty.state = .pressed
  247. } else if style == .solid {
  248. solidProperty.state = .pressed
  249. }
  250. lineTypeSelector.reloadData()
  251. if style == .dashed {
  252. linedashInfoView.isHidden = false
  253. }
  254. }
  255. }
  256. if true {
  257. var multiLineWidth: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .line_Width)
  258. if multiLineWidth {
  259. lineWidthSlider.properties.percent = 0
  260. lineWidthSlider.reloadData()
  261. lineWidthSelect.resetText("-")
  262. } else {
  263. let border: CPDFBorder = annotation.border
  264. let percent = (border.lineWidth - 1)/17
  265. lineWidthSlider.properties.percent = percent
  266. lineWidthSlider.reloadData()
  267. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  268. lineWidthSelect.reloadData()
  269. }
  270. }
  271. if true {
  272. var multiLineDash: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .dash_Pattern)
  273. if multiLineDash {
  274. lineDashSlider.properties.percent = 0
  275. lineDashSlider.reloadData()
  276. lineDashSelect.resetText("-")
  277. } else {
  278. var dashA = 1.0
  279. for dashPattern in annotation.border.dashPattern {
  280. if let value = dashPattern as? CGFloat {
  281. dashA = value
  282. break
  283. }
  284. }
  285. let percent = (dashA - 1)/17
  286. lineDashSlider.properties.percent = percent
  287. lineDashSlider.reloadData()
  288. lineDashSelect.properties.text = String(format: "%.0f%@", dashA, " pt")
  289. lineDashSelect.reloadData()
  290. }
  291. }
  292. }
  293. }
  294. //MARK: - Mouse
  295. override func mouseDown(with event: NSEvent) {
  296. super.mouseDown(with: event)
  297. view.window?.makeFirstResponder(nil)
  298. }
  299. }
  300. //MARK: - ComponentCColorDelegate
  301. extension KMPenController: ComponentCColorDelegate {
  302. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  303. CPDFInkAnnotation.updateColor(annotations, color, withPDFView: pdfView)
  304. reloadData()
  305. }
  306. }
  307. //MARK: - ComponentSliderDelegate
  308. extension KMPenController: ComponentSliderDelegate {
  309. func componentSliderDidUpdate(_ view: ComponentSlider) {
  310. if view == colorSlider {
  311. let percent = view.properties.percent
  312. CPDFInkAnnotation.updateOpacity(annotations, percent, withPDFView: pdfView)
  313. } else if view == lineWidthSlider {
  314. let percent = view.properties.percent * 17 + 1
  315. CPDFInkAnnotation.updateLineWidth(annotations, percent, withPDFView: pdfView)
  316. } else if view == lineDashSlider {
  317. let percent = view.properties.percent * 17 + 1
  318. CPDFInkAnnotation.updateDashPattern(annotations, percent, withPDFView: pdfView)
  319. }
  320. reloadData()
  321. }
  322. }
  323. //MARK: - ComponentSelectDelegate
  324. extension KMPenController: ComponentSelectDelegate {
  325. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  326. if let result = text {
  327. if view == colorOpacitySelect {
  328. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  329. CPDFInkAnnotation.updateOpacity(annotations, opacity, withPDFView: pdfView)
  330. } else if view == lineWidthSelect {
  331. var value = result.stringToCGFloat()
  332. if value > 18 {
  333. value = 18
  334. } else if value < 1 {
  335. value = 1
  336. }
  337. CPDFInkAnnotation.updateLineWidth(annotations, value, withPDFView: pdfView)
  338. } else if view == lineDashSelect {
  339. var value = result.stringToCGFloat()
  340. if value > 18 {
  341. value = 18
  342. } else if value < 1 {
  343. value = 1
  344. }
  345. CPDFInkAnnotation.updateDashPattern(annotations, value, withPDFView: pdfView)
  346. }
  347. reloadData()
  348. }
  349. }
  350. }
  351. //MARK: - ComponentCSelectorGroupDelegate
  352. extension KMPenController: ComponentCSelectorGroupDelegate {
  353. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  354. if item.properties == solidProperty {
  355. CPDFInkAnnotation.updateStyle(annotations, .solid, withPDFView: pdfView)
  356. } else if item.properties == dashProperty {
  357. CPDFInkAnnotation.updateStyle(annotations, .dashed, withPDFView: pdfView)
  358. }
  359. reloadData()
  360. }
  361. }