KMTextBoxController.swift 35 KB

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