KMTextBoxController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. //
  2. // KMTextBoxController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMTextBoxController: NSViewController {
  10. //Font
  11. @IBOutlet var fontBGView: NSView!
  12. @IBOutlet var fontLabel: NSTextField!
  13. @IBOutlet var fontNameSelect: ComponentSelect!
  14. @IBOutlet var fontStyleSelect: ComponentSelect!
  15. @IBOutlet var fontSizeSelect: ComponentSelect!
  16. @IBOutlet var fontAlignmentGroup: ComponentCSelectorGroup!
  17. //Color
  18. @IBOutlet var colorBGView: NSView!
  19. @IBOutlet var colorLabel: NSTextField!
  20. @IBOutlet var fontColorGroup: ComponentCColorGroup!
  21. @IBOutlet var borderColorGroup: ComponentCColorGroup!
  22. @IBOutlet var fillColorGroup: ComponentCColorGroup!
  23. @IBOutlet var opacitySlider: ComponentSlider!
  24. @IBOutlet var opacitySelect: ComponentSelect!
  25. //Line
  26. @IBOutlet var lineBGView: NSView!
  27. @IBOutlet var lineLabel: NSTextField!
  28. @IBOutlet var lineTypeSelector: ComponentCSelectorGroup!
  29. @IBOutlet var lineWidthSlider: ComponentSlider!
  30. @IBOutlet var lineWidthSelect: ComponentSelect!
  31. @IBOutlet var linedashInfoView: NSView!
  32. @IBOutlet var lineDashSlider: ComponentSlider!
  33. @IBOutlet var lineDashSelect: ComponentSelect!
  34. private var familyNames = CPDFFont.familyNames
  35. private let fontAlign_leftItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_left"), identifier: "fontAlign_left")
  36. private let fontAlign_centerItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_center"), identifier: "fontAlign_center")
  37. private let fontAlign_rightItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_right"), identifier: "fontAlign_right")
  38. private let solidProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_solid"))
  39. private let dashProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_dash"))
  40. private var annotations: [CPDFFreeTextAnnotation] = []
  41. private var editColorGroup: ComponentCColorGroup?
  42. var pdfView: CPDFListView?
  43. //MARK: - func
  44. override func viewDidAppear() {
  45. super.viewDidAppear()
  46. opacitySlider.reloadData()
  47. lineWidthSlider.reloadData()
  48. lineDashSlider.reloadData()
  49. }
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. // Do view setup here.
  53. setupProperty()
  54. }
  55. func setupProperty() {
  56. //Font
  57. fontLabel.stringValue = KMLocalizedString("Font")
  58. fontLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  59. fontLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  60. fontNameSelect.properties = ComponentSelectProperties(size: .s,
  61. state: .normal,
  62. text: "")
  63. if true {
  64. var menuItemArr: [ComponentMenuitemProperty] = []
  65. for string in familyNames {
  66. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  67. menuItemArr.append(item)
  68. }
  69. fontNameSelect.updateMenuItemsArr(menuItemArr)
  70. }
  71. fontNameSelect.delegate = self
  72. fontStyleSelect.properties = ComponentSelectProperties(size: .s,
  73. state: .normal,
  74. text: "")
  75. fontStyleSelect.delegate = self
  76. fontSizeSelect.properties = ComponentSelectProperties(size: .s,
  77. state: .normal,
  78. creatable: true,
  79. text: "12 pt",
  80. textUnit: " pt",
  81. regexString: "0123456789")
  82. if true {
  83. var sizeItemArr: [ComponentMenuitemProperty] = []
  84. for string in KMHeaderFooterManager.getFontSize() {
  85. let item = ComponentMenuitemProperty(type: .normal, text: string + " pt", identifier: string)
  86. sizeItemArr.append(item)
  87. }
  88. fontSizeSelect.updateMenuItemsArr(sizeItemArr)
  89. }
  90. fontSizeSelect.delegate = self
  91. if true {
  92. let itemArr: [ComponentCSelectorProperty] = [fontAlign_leftItem, fontAlign_centerItem, fontAlign_rightItem]
  93. fontAlignmentGroup.updateItemProperty(itemArr)
  94. fontAlignmentGroup.delegate = self
  95. }
  96. //Color
  97. colorLabel.stringValue = KMLocalizedString("Color")
  98. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  99. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  100. fontColorGroup.delegate = self
  101. borderColorGroup.delegate = self
  102. fillColorGroup.delegate = self
  103. opacitySlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  104. opacitySlider.delegate = self
  105. opacitySelect.properties = ComponentSelectProperties(size: .s,
  106. state: .normal,
  107. creatable: true,
  108. text: "100%",
  109. textUnit: "%",
  110. regexString: "0123456789%")
  111. if true {
  112. var opacityItems: [ComponentMenuitemProperty] = []
  113. for string in ["25%", "50%", "75%", "100%"] {
  114. let item = ComponentMenuitemProperty(type: .normal, text: string)
  115. opacityItems.append(item)
  116. }
  117. opacitySelect.updateMenuItemsArr(opacityItems)
  118. }
  119. opacitySelect.delegate = self
  120. //Line
  121. lineLabel.stringValue = KMLocalizedString("Line")
  122. lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  123. lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  124. lineTypeSelector.updateItemProperty([solidProperty, dashProperty])
  125. lineTypeSelector.delegate = self
  126. lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  127. lineWidthSlider.delegate = self
  128. lineWidthSelect.properties = ComponentSelectProperties(size: .s,
  129. state: .normal,
  130. creatable: true,
  131. text: "2",
  132. textUnit: " pt",
  133. regexString: "0123456789 pt")
  134. if true {
  135. var opacityItems: [ComponentMenuitemProperty] = []
  136. for string in ["1 pt", "2 pt", "4 pt", "6 pt", "8 pt", "10 pt"] {
  137. let item = ComponentMenuitemProperty(type: .normal, text: string)
  138. opacityItems.append(item)
  139. }
  140. lineWidthSelect.updateMenuItemsArr(opacityItems)
  141. }
  142. lineWidthSelect.delegate = self
  143. lineDashSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  144. lineDashSlider.delegate = self
  145. lineDashSelect.properties = ComponentSelectProperties(size: .s,
  146. state: .normal,
  147. creatable: true,
  148. text: "2",
  149. textUnit: " pt",
  150. regexString: "0123456789 pt")
  151. if true {
  152. var opacityItems: [ComponentMenuitemProperty] = []
  153. for string in ["1 pt", "2 pt", "4 pt", "6 pt", "8 pt", "10 pt"] {
  154. let item = ComponentMenuitemProperty(type: .normal, text: string)
  155. opacityItems.append(item)
  156. }
  157. lineDashSelect.updateMenuItemsArr(opacityItems)
  158. }
  159. lineDashSelect.delegate = self
  160. }
  161. func reloadData() {
  162. guard let pdfView = self.pdfView else {
  163. return
  164. }
  165. if true {
  166. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.freeTextColors
  167. if colors.count > 4 {
  168. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  169. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  170. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  171. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  172. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  173. fontColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  174. }
  175. }
  176. if true {
  177. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.freeText_BorderColors
  178. if colors.count > 4 {
  179. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  180. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  181. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  182. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  183. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  184. borderColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  185. }
  186. }
  187. if true {
  188. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.freeText_FillColors
  189. if colors.count > 4 {
  190. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  191. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  192. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  193. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  194. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  195. fillColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  196. }
  197. }
  198. self.annotations.removeAll()
  199. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  200. for annotation in allAnnotations {
  201. if annotation is CPDFFreeTextAnnotation {
  202. annotations.append((annotation as! CPDFFreeTextAnnotation))
  203. }
  204. }
  205. var firstAnnotation: CPDFFreeTextAnnotation?
  206. if annotations.count > 0 {
  207. firstAnnotation = annotations.first
  208. }
  209. //Font
  210. var fontNameString: String?
  211. var fontStyleString: String?
  212. var fontSize: CGFloat?
  213. var alignment: NSTextAlignment?
  214. //Color
  215. var fontColor: NSColor?
  216. var borderColor: NSColor?
  217. var fillColor: NSColor?
  218. var opacity: CGFloat?
  219. //Line
  220. var lineStyle: CPDFBorderStyle?
  221. var lineWidth: CGFloat?
  222. var dashPattern: CGFloat?
  223. if annotations.count == 0 {
  224. //Font
  225. fontNameString = CPDFFreeTextAnnotation.defaultFontName()
  226. fontStyleString = CPDFFreeTextAnnotation.defaultFontStyle()
  227. fontSize = CPDFFreeTextAnnotation.defaultFontSize()
  228. alignment = CPDFFreeTextAnnotation.defaultFontAlignment()
  229. //Color
  230. fontColor = CPDFFreeTextAnnotation.defaultFontColor()
  231. borderColor = CPDFFreeTextAnnotation.defaultBorderColor()
  232. fillColor = CPDFFreeTextAnnotation.defaultColor()
  233. opacity = CPDFFreeTextAnnotation.defaultOpacity()
  234. //Line
  235. lineStyle = CPDFFreeTextAnnotation.defaultLineStyle()
  236. lineWidth = CPDFFreeTextAnnotation.defaultLineWidth()
  237. var dash = 1.0
  238. for dashPattern in CPDFFreeTextAnnotation.defaultDashPattern() {
  239. if let value = dashPattern as? CGFloat {
  240. dash = value
  241. break
  242. }
  243. }
  244. dashPattern = dash
  245. } else if annotations.count == 1, let annotation = firstAnnotation {
  246. //Font
  247. fontNameString = annotation.cFont.familyName
  248. fontStyleString = annotation.cFont.styleName
  249. fontSize = annotation.fontSize
  250. alignment = annotation.alignment
  251. //Color
  252. fontColor = annotation.fontColor
  253. borderColor = annotation.borderColor
  254. fillColor = annotation.color
  255. opacity = annotation.opacity
  256. //Line
  257. lineStyle = annotation.border?.style
  258. lineWidth = annotation.border?.lineWidth
  259. var dash = 1.0
  260. for dashPattern in annotation.dashPattern() {
  261. if let value = dashPattern as? CGFloat {
  262. dash = value
  263. break
  264. }
  265. }
  266. dashPattern = dash
  267. } else {
  268. guard let annotation = firstAnnotation else {
  269. return
  270. }
  271. //Font
  272. if true {
  273. let multiFontName = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .font_Name)
  274. if multiFontName {
  275. fontNameString = nil
  276. } else {
  277. fontNameString = annotation.cFont.familyName
  278. }
  279. let multiFontStyle = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .font_Style)
  280. if multiFontStyle {
  281. fontStyleString = nil
  282. } else {
  283. fontStyleString = annotation.cFont.styleName
  284. }
  285. let multifontSize = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .font_Size)
  286. if multifontSize {
  287. fontSize = nil
  288. } else {
  289. fontSize = annotation.fontSize
  290. }
  291. let multiAlignment = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .font_Alignment)
  292. if multiAlignment {
  293. alignment = nil
  294. } else {
  295. alignment = annotation.alignment
  296. }
  297. }
  298. //Color
  299. if true {
  300. let multiFontColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .font_Color)
  301. if multiFontColor == true {
  302. fontColor = nil
  303. } else {
  304. fontColor = annotation.fontColor
  305. }
  306. let multiBorderColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .border_Color)
  307. if multiBorderColor == true {
  308. borderColor = nil
  309. } else {
  310. borderColor = annotation.borderColor
  311. }
  312. let multiFillColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color)
  313. if multiFillColor == true {
  314. fillColor = nil
  315. } else {
  316. fillColor = annotation.color
  317. }
  318. let multiOpacity: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .opacity)
  319. if multiOpacity == true {
  320. opacity = nil
  321. } else {
  322. opacity = annotation.opacity
  323. }
  324. }
  325. //Line
  326. if true {
  327. let multiStyle: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .border_Style)
  328. if multiStyle == true {
  329. lineStyle = nil
  330. } else {
  331. lineStyle = annotation.border?.style
  332. }
  333. let multiLineWidth: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .line_Width)
  334. if multiLineWidth == true {
  335. lineWidth = nil
  336. } else {
  337. lineWidth = annotation.border?.lineWidth
  338. }
  339. let multiDashPattern: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .line_Width)
  340. if multiDashPattern == true {
  341. dashPattern = nil
  342. } else {
  343. if let firstValue = annotation.border?.dashPattern.first {
  344. dashPattern = firstValue as? CGFloat
  345. }
  346. }
  347. }
  348. }
  349. //Font
  350. if let value = fontNameString {
  351. fontNameSelect.properties.text = value
  352. } else {
  353. fontNameSelect.properties.text = "-"
  354. }
  355. fontNameSelect.reloadData()
  356. if let familyName = fontNameSelect.properties.text, familyName != "-" {
  357. var styleNames = CPDFFont.fontNames(forFamilyName: familyName)
  358. var menuItemArr: [ComponentMenuitemProperty] = []
  359. if styleNames.count == 0 {
  360. styleNames.append("Regular")
  361. }
  362. for string in styleNames {
  363. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  364. menuItemArr.append(item)
  365. }
  366. fontStyleSelect.updateMenuItemsArr(menuItemArr)
  367. } else {
  368. fontStyleSelect.updateMenuItemsArr([])
  369. }
  370. if let value = fontStyleString {
  371. fontStyleSelect.properties.text = value
  372. } else {
  373. fontStyleSelect.properties.text = "-"
  374. }
  375. fontStyleSelect.reloadData()
  376. if let value = fontSize {
  377. fontSizeSelect.properties.text = String(format: "%.0f", value)
  378. } else {
  379. fontSizeSelect.properties.text = "-"
  380. }
  381. fontSizeSelect.reloadData()
  382. if let value = alignment {
  383. fontAlign_leftItem.state = value == .left ? .pressed : .normal
  384. fontAlign_centerItem.state = value == .center ? .pressed : .normal
  385. fontAlign_rightItem.state = value == .right ? .pressed : .normal
  386. } else {
  387. fontAlign_leftItem.state = .normal
  388. fontAlign_centerItem.state = .normal
  389. fontAlign_rightItem.state = .normal
  390. }
  391. fontAlignmentGroup.reloadData()
  392. //Color
  393. fontColorGroup.currentColor = fontColor
  394. fontColorGroup.refreshUI()
  395. borderColorGroup.currentColor = borderColor
  396. borderColorGroup.refreshUI()
  397. fillColorGroup.currentColor = fillColor
  398. fillColorGroup.refreshUI()
  399. if let value = opacity {
  400. opacitySlider.properties.percent = value
  401. opacitySelect.properties.text = String(format: "%.0f%@", value*100, "%")
  402. } else {
  403. opacitySlider.properties.percent = 0
  404. opacitySelect.properties.text = "-"
  405. }
  406. opacitySlider.reloadData()
  407. opacitySelect.reloadData()
  408. //Line
  409. linedashInfoView.isHidden = true
  410. if let value = lineStyle {
  411. let style = value
  412. dashProperty.state = .normal
  413. solidProperty.state = .normal
  414. if style == .dashed {
  415. dashProperty.state = .pressed
  416. } else if style == .solid {
  417. solidProperty.state = .pressed
  418. }
  419. if style == .dashed {
  420. linedashInfoView.isHidden = false
  421. }
  422. } else {
  423. dashProperty.state = .normal
  424. solidProperty.state = .normal
  425. }
  426. lineTypeSelector.reloadData()
  427. if let value = lineWidth {
  428. let percent = value/18
  429. lineWidthSlider.properties.percent = percent
  430. lineWidthSlider.reloadData()
  431. lineWidthSelect.properties.text = String(format: "%.0f%@", value, " pt")
  432. lineWidthSelect.reloadData()
  433. } else {
  434. lineWidthSlider.properties.percent = 0
  435. lineWidthSlider.reloadData()
  436. lineWidthSelect.resetText("-")
  437. }
  438. if let value = dashPattern {
  439. let percent = value/18
  440. lineDashSlider.properties.percent = percent
  441. lineDashSlider.reloadData()
  442. lineDashSelect.properties.text = String(format: "%.0f%@", value, " pt")
  443. lineDashSelect.reloadData()
  444. } else {
  445. lineDashSlider.properties.percent = 0
  446. lineDashSlider.reloadData()
  447. lineDashSelect.resetText("-")
  448. }
  449. }
  450. //MARK: - Mouse
  451. override func mouseDown(with event: NSEvent) {
  452. super.mouseDown(with: event)
  453. view.window?.makeFirstResponder(nil)
  454. }
  455. }
  456. //MARK: - ComponentCColorDelegate
  457. extension KMTextBoxController: ComponentCColorDelegate {
  458. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  459. for annotation in self.annotations {
  460. pdfView!.commitEditAnnotationFreeText(annotation)
  461. }
  462. if view == fontColorGroup {
  463. CPDFAnnotation.updateAnnotations(annotations, newFontColor: color, withPDFView: pdfView)
  464. CPDFFreeTextAnnotation.update_DefaultFontColor_Info(color)
  465. } else if view == borderColorGroup {
  466. CPDFAnnotation.updateAnnotations(annotations, newBorderColor: color, withPDFView: pdfView)
  467. CPDFFreeTextAnnotation.update_DefaultBorderColor_Info(color)
  468. } else if view == fillColorGroup {
  469. CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView)
  470. CPDFFreeTextAnnotation.update_DefaultColor_Info(color)
  471. }
  472. reloadData()
  473. }
  474. func componentCColorGroupColorsUpdates(_ view: NSView, _ colors: [NSColor]) {
  475. if view == fontColorGroup {
  476. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMFreeTextPropertColorsKey)
  477. } else if view == borderColorGroup {
  478. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMFreeText_BorderColorsKey)
  479. } else if view == fillColorGroup {
  480. KMAnnotationPropertiesColorManager.manager.updateDefaultColors(colors, forKey: KMFreeText_FillColorsKey)
  481. }
  482. }
  483. func componentCColorDidRightMouseUpWithStrings(_ view: NSView) -> [String] {
  484. return [KMLocalizedString("Change Color"), KMLocalizedString("Restore default color")]
  485. }
  486. func componentCColorDidRightMenuItemClicked(_ colorGroup: ComponentCColorGroup, _ view: NSView, menuItemProperty: ComponentMenuitemProperty?) {
  487. self.editColorGroup = colorGroup
  488. if menuItemProperty?.text == KMLocalizedString("Change Color") {
  489. if NSColorPanel.sharedColorPanelExists {
  490. NSColorPanel.shared.setTarget(nil)
  491. NSColorPanel.shared.setAction(nil)
  492. }
  493. NSColorPanel.shared.setTarget(self)
  494. NSColorPanel.shared.setAction(#selector(colorChoose(_:)))
  495. if let colorItem = view as? ComponentCColorItem, let color = colorItem.properties?.color {
  496. NSColorPanel.shared.color = color
  497. }
  498. let viewRect = view.superview?.convert(view.frame, to: nil) ?? CGRectZero
  499. var rect = NSColorPanel.shared.frame
  500. rect.origin.x = viewRect.minX
  501. rect.origin.y = viewRect.minY - 30
  502. NSColorPanel.shared.setFrame(rect, display: true)
  503. NSColorPanel.shared.orderFront(nil)
  504. } else if menuItemProperty?.text == KMLocalizedString("Restore default color") {
  505. if let colorItem = colorGroup.rightClickedItem {
  506. var key: String?
  507. if colorGroup == fontColorGroup {
  508. key = KMFreeTextPropertColorsKey
  509. } else if colorGroup == borderColorGroup {
  510. key = KMFreeText_BorderColorsKey
  511. } else if colorGroup == fillColorGroup {
  512. key = KMFreeText_FillColorsKey
  513. }
  514. if let value = key {
  515. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.defaultColors(key: value)
  516. if colors.count > 4, colorItem.itemIndex >= 0, colorItem.itemIndex < colors.count {
  517. colorItem.properties?.color = colors[colorItem.itemIndex]
  518. colorItem.reloadData()
  519. colorGroup.componentCColorDidChoosePanelColor(colorGroup, nil)
  520. }
  521. }
  522. }
  523. }
  524. }
  525. @objc func colorChoose(_ sender: Any) {
  526. if let colorGroup = self.editColorGroup, let colorItem = colorGroup.rightClickedItem {
  527. let color = NSColorPanel.shared.color
  528. colorItem.properties?.color = color
  529. colorItem.reloadData()
  530. colorGroup.componentCColorDidChoosePanelColor(colorGroup, nil)
  531. }
  532. }
  533. }
  534. //MARK: - ComponentSliderDelegate
  535. extension KMTextBoxController: ComponentSliderDelegate {
  536. func componentSliderDidUpdate(_ view: ComponentSlider) {
  537. for annotation in self.annotations {
  538. pdfView!.commitEditAnnotationFreeText(annotation)
  539. }
  540. if view == opacitySlider {
  541. let opacity = view.properties.percent
  542. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  543. CPDFFreeTextAnnotation.update_DefaultOpacity_Info(opacity)
  544. } else if view == lineWidthSlider {
  545. let value = view.properties.percent * 18
  546. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  547. CPDFFreeTextAnnotation.update_DefaultLineWidth_Info(value)
  548. } else if view == lineDashSlider {
  549. let value = view.properties.percent * 18
  550. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  551. CPDFFreeTextAnnotation.update_DefaultDashPattern_Info(value)
  552. }
  553. reloadData()
  554. }
  555. }
  556. //MARK: - ComponentSelectDelegate
  557. extension KMTextBoxController: ComponentSelectDelegate {
  558. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  559. for annotation in self.annotations {
  560. pdfView!.commitEditAnnotationFreeText(annotation)
  561. }
  562. if let result = text {
  563. if view == opacitySelect {
  564. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  565. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  566. CPDFFreeTextAnnotation.update_DefaultOpacity_Info(opacity)
  567. } else if view == fontSizeSelect {
  568. var value = result.stringToCGFloat()
  569. value = min(value, 288)
  570. CPDFAnnotation.updateAnnotations(annotations, newFontSize: value, withPDFView: pdfView)
  571. CPDFFreeTextAnnotation.update_DefaultFontSize_Info(value)
  572. } else if view == lineWidthSelect {
  573. var value = result.stringToCGFloat()
  574. if value > 18 {
  575. value = 18
  576. } else if value < 1 {
  577. value = 1
  578. }
  579. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  580. CPDFFreeTextAnnotation.update_DefaultLineWidth_Info(value)
  581. } else if view == lineDashSelect {
  582. var value = result.stringToCGFloat()
  583. if value > 18 {
  584. value = 18
  585. } else if value < 1 {
  586. value = 1
  587. }
  588. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  589. CPDFFreeTextAnnotation.update_DefaultDashPattern_Info(value)
  590. }
  591. reloadData()
  592. }
  593. }
  594. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  595. for annotation in self.annotations {
  596. pdfView!.commitEditAnnotationFreeText(annotation)
  597. }
  598. if var result = menuItemProperty?.text {
  599. if let textUnit = view?.properties.textUnit {
  600. result = result.stringByDeleteCharString(textUnit)
  601. }
  602. if view == fontNameSelect {
  603. var styleName = "Regular"
  604. let styleNames = CPDFFont.fontNames(forFamilyName: result)
  605. if let first = styleNames.first {
  606. styleName = first
  607. }
  608. CPDFAnnotation.updateAnnotations(annotations, newCFont: CPDFFont(familyName: result, fontStyle: styleName), withPDFView: pdfView)
  609. CPDFFreeTextAnnotation.update_DefaultFont_Info(CPDFFont(familyName: result, fontStyle: styleName))
  610. } else if view == fontStyleSelect {
  611. let fontName = fontNameSelect.properties.text ?? "Helvetica"
  612. CPDFAnnotation.updateAnnotations(annotations, newCFont: CPDFFont(familyName: fontName, fontStyle: result), withPDFView: pdfView)
  613. CPDFFreeTextAnnotation.update_DefaultFont_Info(CPDFFont(familyName: fontName, fontStyle: result))
  614. } else if view == fontSizeSelect {
  615. let size = result.stringToCGFloat()
  616. CPDFAnnotation.updateAnnotations(annotations, newFontSize: size, withPDFView: pdfView)
  617. CPDFFreeTextAnnotation.update_DefaultFontSize_Info(size)
  618. } else if view == opacitySelect {
  619. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  620. CPDFAnnotation.updateAnnotations(annotations, newOpacity: opacity, withPDFView: pdfView)
  621. CPDFFreeTextAnnotation.update_DefaultOpacity_Info(opacity)
  622. } else if view == lineWidthSelect {
  623. var value = result.stringToCGFloat()
  624. if value > 18 {
  625. value = 18
  626. } else if value < 1 {
  627. value = 1
  628. }
  629. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  630. CPDFFreeTextAnnotation.update_DefaultLineWidth_Info(value)
  631. } else if view == lineDashSelect {
  632. var value = result.stringToCGFloat()
  633. if value > 18 {
  634. value = 18
  635. } else if value < 1 {
  636. value = 1
  637. }
  638. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  639. CPDFFreeTextAnnotation.update_DefaultDashPattern_Info(value)
  640. }
  641. reloadData()
  642. }
  643. }
  644. }
  645. //MARK: - ComponentCSelectorGroupDelegate
  646. extension KMTextBoxController: ComponentCSelectorGroupDelegate {
  647. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  648. for annotation in self.annotations {
  649. pdfView!.commitEditAnnotationFreeText(annotation)
  650. }
  651. if view == fontAlignmentGroup {
  652. if item.properties == fontAlign_leftItem {
  653. CPDFAnnotation.updateAnnotations(annotations, newAlignment: .left, withPDFView: pdfView)
  654. CPDFFreeTextAnnotation.update_DefaultFontAlignment_Info(.left)
  655. } else if item.properties == fontAlign_centerItem {
  656. CPDFAnnotation.updateAnnotations(annotations, newAlignment: .center, withPDFView: pdfView)
  657. CPDFFreeTextAnnotation.update_DefaultFontAlignment_Info(.center)
  658. } else if item.properties == fontAlign_rightItem {
  659. CPDFAnnotation.updateAnnotations(annotations, newAlignment: .right, withPDFView: pdfView)
  660. CPDFFreeTextAnnotation.update_DefaultFontAlignment_Info(.right)
  661. }
  662. } else if view == lineTypeSelector {
  663. if item.properties == solidProperty {
  664. CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .solid, withPDFView: pdfView)
  665. CPDFFreeTextAnnotation.updateBorder_DefaultStyle_Info(.solid)
  666. } else if item.properties == dashProperty {
  667. CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .dashed, withPDFView: pdfView)
  668. CPDFFreeTextAnnotation.updateBorder_DefaultStyle_Info(.dashed)
  669. }
  670. }
  671. reloadData()
  672. }
  673. }