KMAnnotationTableViewController.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. //
  2. // KMAnnotationTableViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/11/7.
  6. //
  7. import Cocoa
  8. class KMLineStyleButtonCell: NSButtonCell {
  9. override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) {
  10. if flag {
  11. print("111")
  12. controlView.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  13. } else {
  14. print("111")
  15. }
  16. }
  17. }
  18. @objcMembers
  19. class KMAnnotationTableViewController: NSViewController {
  20. var annotation: KMTableAnnotation?
  21. weak var pdfView: CPDFListView?
  22. var _annotationModel: CPDFAnnotationModel?
  23. var _annotations: [KMTableAnnotation] = []
  24. var point: NSPoint = NSPoint.zero
  25. @IBOutlet weak var styleLabel: NSTextField!
  26. @IBOutlet weak var styleBox0: KMBox!
  27. @IBOutlet weak var styleBox1: KMBox!
  28. @IBOutlet weak var styleBox2: KMBox!
  29. @IBOutlet weak var styleBox3: KMBox!
  30. @IBOutlet weak var styleBox4: KMBox!
  31. @IBOutlet weak var styleBox5: KMBox!
  32. @IBOutlet weak var cellSizeLabel: NSTextField!
  33. @IBOutlet weak var cellWidthLabel: NSTextField!
  34. @IBOutlet weak var cellHeightLabel: NSTextField!
  35. @IBOutlet weak var cellWidthTextField: NSTextField!
  36. @IBOutlet weak var cellHeightTextField: NSTextField!
  37. @IBOutlet weak var cellWidthStepper: NSStepper!
  38. @IBOutlet weak var cellHeightStepper: NSStepper!
  39. @IBOutlet weak var titleEndTableLabel: NSTextField!
  40. @IBOutlet weak var sidebarPopUpButton: KMPopUpButton!
  41. @IBOutlet weak var headerPopUpButton: KMPopUpButton!
  42. @IBOutlet weak var footerPopUpButton: KMPopUpButton!
  43. @IBOutlet weak var bordersAndColorsLabel: NSTextField!
  44. @IBOutlet weak var backgroundColorLabel: NSTextField!
  45. @IBOutlet weak var backgroudColorBox: KMBox!
  46. @IBOutlet weak var backgrourdColorView: NSView!
  47. @IBOutlet weak var borderColorLabel: NSTextField!
  48. @IBOutlet weak var borderColorBox: KMBox!
  49. @IBOutlet weak var borderColorView: NSView!
  50. @IBOutlet weak var widthLabel: NSTextField!
  51. @IBOutlet weak var widthSlider: NSSlider!
  52. @IBOutlet weak var widthPopUpButton: KMPopUpButton!
  53. @IBOutlet weak var lineStyleLabel: NSTextField!
  54. @IBOutlet weak var lineStyleButton: KMButton!
  55. @IBOutlet weak var dottedLineStyleButton: KMButton!
  56. @IBOutlet weak var rightArrowStyleButton: KMButton!
  57. @IBOutlet weak var alternateColorButton: NSButton!
  58. @IBOutlet weak var alternateColorLabel: NSTextField!
  59. @IBOutlet weak var alternateColorBox: KMBox!
  60. @IBOutlet weak var alternateColorView: NSView!
  61. var backgroudColorBoxIsEnabled: Bool = false
  62. var notUpdateBackgroundColor: Bool = false
  63. var lineStyleCount: Int = 0
  64. deinit {
  65. NotificationCenter.default.removeObserver(self)
  66. DistributedNotificationCenter.default().removeObserver(self)
  67. }
  68. override func viewDidLoad() {
  69. super.viewDidLoad()
  70. // Do view setup here.
  71. self.styleLabel.stringValue = NSLocalizedString("Table Styles", comment: "")
  72. self.styleLabel.textColor = KMAppearance.Layout.h0Color()
  73. self.cellSizeLabel.stringValue = NSLocalizedString("Cell Size", comment: "")
  74. self.cellSizeLabel.textColor = KMAppearance.Layout.h0Color()
  75. self.cellWidthLabel.stringValue = "\(NSLocalizedString("Cell Width", comment: ""))(cm)"
  76. self.cellWidthLabel.toolTip = "\(NSLocalizedString("Cell Width", comment: ""))(cm)"
  77. self.cellWidthLabel.allowsExpansionToolTips = true
  78. self.cellWidthLabel.textColor = KMAppearance.Layout.h1Color()
  79. self.cellHeightLabel.stringValue = "\(NSLocalizedString("Cell Height", comment: ""))(cm)"
  80. self.cellHeightLabel.toolTip = "\(NSLocalizedString("Cell Height", comment: ""))(cm)"
  81. self.cellHeightLabel.allowsExpansionToolTips = true
  82. self.cellHeightLabel.textColor = KMAppearance.Layout.h1Color()
  83. self.titleEndTableLabel.stringValue = NSLocalizedString("Headers & Footers", comment: "")
  84. self.titleEndTableLabel.textColor = KMAppearance.Layout.h0Color()
  85. self.bordersAndColorsLabel.stringValue = NSLocalizedString("Borders & Colors", comment: "")
  86. self.bordersAndColorsLabel.textColor = KMAppearance.Layout.h0Color()
  87. self.backgroundColorLabel.stringValue = NSLocalizedString("Background Color", comment: "")
  88. self.backgroundColorLabel.textColor = KMAppearance.Layout.h1Color()
  89. self.borderColorLabel.stringValue = NSLocalizedString("Border Color", comment: "")
  90. self.borderColorLabel.textColor = KMAppearance.Layout.h1Color()
  91. self.widthLabel.stringValue = NSLocalizedString("Line Width", comment: "")
  92. self.widthLabel.textColor = KMAppearance.Layout.h1Color()
  93. self.lineStyleLabel.stringValue = NSLocalizedString("Line Style", comment: "")
  94. self.lineStyleLabel.textColor = KMAppearance.Layout.h1Color()
  95. self.alternateColorLabel.stringValue = NSLocalizedString("Alternate", comment: "")
  96. self.alternateColorLabel.textColor = KMAppearance.Layout.h0Color()
  97. let styleBoxArr: [KMBox] = [self.styleBox0, self.styleBox1, self.styleBox2, self.styleBox3, self.styleBox4, self.styleBox5]
  98. for box in styleBoxArr {
  99. box.moveCallback = { mouseEntered, mouseBox in
  100. if mouseEntered {
  101. mouseBox.fillColor = KMAppearance.Status.selColor()
  102. } else {
  103. mouseBox.fillColor = NSColor.clear
  104. }
  105. }
  106. }
  107. self.widthPopUpButton.removeAllItems()
  108. self.widthPopUpButton.addItems(withTitles: ["0.1 pt","0.2 pt","0.3 pt","0.4 pt","0.5 pt","0.6 pt","0.7 pt","0.8 pt","0.9 pt","1.0 pt","1.1 pt","1.2 pt","1.3 pt","1.4 pt","1.5 pt","1.6 pt","1.7 pt","1.8 pt","1.9 pt","2.0 pt","2.1 pt","2.2 pt","2.3 pt","2.4 pt","2.5 pt","2.6 pt","2.7 pt","2.8 pt","2.9 pt","3.0 pt"])
  109. self.widthPopUpButton.type = .arrowDown
  110. self.backgroudColorBox.borderWidth = 1.0
  111. self.backgroudColorBox.borderColor = KMAppearance.Interactive.s0Color()
  112. self.backgroudColorBox.fillColor = KMAppearance.Layout.l1Color()
  113. self.backgroudColorBox.cornerRadius = 1.0
  114. self.backgroudColorBox.downCallback = {downEntered, mouseBox, event in
  115. if downEntered {
  116. if self.backgroudColorBoxIsEnabled {
  117. NSColorPanel.shared.setTarget(self)
  118. NSColorPanel.shared.setAction(#selector(self.backgroudColorPanelColorDidChange(_:)))
  119. NSColorPanel.shared.orderFront(nil)
  120. } else {
  121. NSSound.beep()
  122. }
  123. }
  124. }
  125. self.backgrourdColorView.wantsLayer = true
  126. self.borderColorView.wantsLayer = true
  127. self.alternateColorView.wantsLayer = true
  128. self.backgrourdColorView.layer?.backgroundColor = NSColor.white.cgColor
  129. self.backgrourdColorView.layer?.borderWidth = 0.5
  130. self.borderColorView.layer?.borderWidth = 0.5
  131. self.alternateColorView.layer?.borderWidth = 0.5
  132. self.backgrourdColorView.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
  133. self.borderColorView.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
  134. self.alternateColorView.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
  135. self.borderColorBox.borderWidth = 1.0
  136. self.borderColorBox.borderColor = KMAppearance.Interactive.s0Color()
  137. self.borderColorBox.fillColor = KMAppearance.Layout.l1Color()
  138. self.borderColorBox.cornerRadius = 1.0
  139. self.borderColorBox.downCallback = { downEntered, mouseBox, event in
  140. if downEntered {
  141. NSColorPanel.shared.setTarget(self)
  142. NSColorPanel.shared.setAction(#selector(self.borderColorPanelColorDidChange(_:)))
  143. NSColorPanel.shared.orderFront(nil)
  144. }
  145. }
  146. self.alternateColorBox.borderWidth = 1.0
  147. self.alternateColorBox.borderColor = KMAppearance.Interactive.s0Color()
  148. self.alternateColorBox.fillColor = KMAppearance.Layout.l1Color()
  149. self.alternateColorBox.cornerRadius = 1.0
  150. self.alternateColorBox.downCallback = { downEntered, mouseBox, event in
  151. if downEntered {
  152. NSColorPanel.shared.setTarget(self)
  153. NSColorPanel.shared.setAction(#selector(self.alternateColorPanelColorDidChange(_:)))
  154. NSColorPanel.shared.orderFront(nil)
  155. }
  156. }
  157. self.lineStyleButton.image = self.borderStyleSolid(isSelect: false)
  158. self.lineStyleButton.wantsLayer = true
  159. self.dottedLineStyleButton.image = self.borderStyleDashed(isSelect: false)
  160. self.dottedLineStyleButton.wantsLayer = true
  161. self.rightArrowStyleButton.image = self.borderStyleDashed(isSelect: false)
  162. self.rightArrowStyleButton.wantsLayer = true
  163. let buttonArray: [KMButton] = [self.lineStyleButton, self.dottedLineStyleButton, self.rightArrowStyleButton]
  164. for button in buttonArray {
  165. button.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
  166. button.layer?.borderWidth = 1.0
  167. button.layer?.cornerRadius = 1.0
  168. if button.tag == self.lineStyleCount {
  169. button.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  170. } else {
  171. button.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  172. }
  173. button.mouseMoveCallback = {[weak self] mouseEntered in
  174. if button.tag == self?.lineStyleCount {
  175. return
  176. }
  177. if mouseEntered {
  178. button.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  179. button.layer?.borderWidth = 0.0
  180. if button.tag == 0 {
  181. self?.lineStyleButton.image = self?.borderStyleSolid(isSelect: true)
  182. } else if button.tag == 1 {
  183. self?.dottedLineStyleButton.image = self?.borderStyleDashed(isSelect: true)
  184. }
  185. } else {
  186. button.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  187. button.layer?.borderWidth = 1.0
  188. if button.tag == 0 {
  189. self?.lineStyleButton.image = self?.borderStyleSolid(isSelect: false)
  190. } else {
  191. self?.dottedLineStyleButton.image = self?.borderStyleDashed(isSelect: false)
  192. }
  193. }
  194. }
  195. }
  196. self.rightArrowStyleButton.isHidden = true
  197. self.sidebarPopUpButton.buttonImage = NSImage(named: KMImageNameUXIconPropertybarTableTitleLeft)
  198. self.sidebarPopUpButton.type = .table
  199. self.headerPopUpButton.buttonImage = NSImage(named: KMImageNameUXIconPropertybarTableTitleAbove)
  200. self.headerPopUpButton.type = .table;
  201. self.footerPopUpButton.buttonImage = NSImage(named: KMImageNameUXIconPropertybarTableBottom)
  202. self.footerPopUpButton.type = .table;
  203. self.cellWidthTextField.backgroundColor = KMAppearance.Layout.l1Color()
  204. self.cellHeightTextField.backgroundColor = KMAppearance.Layout.l1Color()
  205. self.cellWidthTextField.wantsLayer = true
  206. self.cellHeightTextField.wantsLayer = true
  207. self.sidebarPopUpButton.wantsLayer = true
  208. self.widthPopUpButton.wantsLayer = true
  209. self.headerPopUpButton.wantsLayer = true
  210. self.footerPopUpButton.wantsLayer = true
  211. self.cellWidthTextField.layer!.borderWidth = 1.0
  212. self.cellHeightTextField.layer?.borderWidth = 1.0
  213. self.sidebarPopUpButton.layer!.borderWidth = 1.0
  214. self.widthPopUpButton.layer!.borderWidth = 1.0
  215. self.headerPopUpButton.layer!.borderWidth = 1.0
  216. self.footerPopUpButton.layer!.borderWidth = 1.0
  217. self.cellWidthTextField.layer?.cornerRadius = 1.0
  218. self.cellHeightTextField.layer?.cornerRadius = 1.0
  219. self.sidebarPopUpButton.layer?.cornerRadius = 1.0
  220. self.widthPopUpButton.layer?.cornerRadius = 1.0
  221. self.headerPopUpButton.layer?.cornerRadius = 1.0
  222. self.updateViewColor()
  223. //
  224. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewTableAnnotationDidChangeNotification(_:)), name: Notification.Name("KMPDFViewTableAnnotationDidChangeNotification"), object: nil)
  225. NotificationCenter.default.addObserver(self, selector: #selector(controlTextDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: self.cellWidthTextField)
  226. NotificationCenter.default.addObserver(self, selector: #selector(controlTextDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: self.cellHeightTextField)
  227. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(_:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  228. }
  229. // MARK: Set & Get
  230. var annotations: [KMTableAnnotation] {
  231. set {
  232. _annotations = newValue
  233. self.annotation = newValue.first
  234. }
  235. get {
  236. return _annotations
  237. }
  238. }
  239. var annotationModel: CPDFAnnotationModel {
  240. get {
  241. return _annotationModel!
  242. }
  243. set {
  244. _annotationModel = newValue
  245. if _annotationModel?.annotations != nil {
  246. if annotationModel.annotations.count > 0 {
  247. annotations = _annotationModel?.annotations as! [KMTableAnnotation]
  248. }
  249. }
  250. }
  251. }
  252. // MARK: Private Methods
  253. func reloadData(with point: NSPoint) {
  254. let rect = annotation!.isInCell(with: point)
  255. if !NSEqualRects(rect, NSRectFromCGRect(CGRect.zero)) {
  256. var color = annotation?.getCellData(withRow: annotation!.currentCell.row, andColumn: annotation!.currentCell.column).backgroundColor
  257. var maxWidth: CGFloat = 10
  258. let minHeight: CGFloat = 10
  259. if annotation!.currentCell.column == annotation!.columnNumber - 1 {
  260. maxWidth = CGFloat(MAXFLOAT)
  261. } else {
  262. if let rect1 = annotation?.getCellBounds(withRow: annotation!.currentCell.row, column: annotation!.currentCell.column + 1) {
  263. maxWidth = rect1.size.width + rect.size.width - 10
  264. }
  265. }
  266. cellWidthStepper.maxValue = Double(maxWidth)
  267. cellHeightStepper.minValue = Double(minHeight)
  268. if color == nil {
  269. color = NSColor(deviceRed: 1, green: 1, blue: 1, alpha: 1)
  270. }
  271. backgroudColorBoxIsEnabled = true
  272. backgrourdColorView.wantsLayer = true
  273. backgrourdColorView.layer?.backgroundColor = color!.cgColor
  274. } else {
  275. backgroudColorBoxIsEnabled = false
  276. backgrourdColorView.wantsLayer = true
  277. backgrourdColorView.layer?.backgroundColor = NSColor.white.cgColor
  278. }
  279. self.setCell(width: rect.size.width, height: rect.size.height)
  280. widthSlider.floatValue = Float((annotation?.border.lineWidth)!)
  281. let sliderString = String(format: "%.1f pt", (annotation?.border.lineWidth)!)
  282. if widthPopUpButton.itemTitles.contains(sliderString) {
  283. if let index = widthPopUpButton.itemTitles.firstIndex(of: sliderString) {
  284. widthPopUpButton.selectItem(at: index)
  285. }
  286. }
  287. widthSlider.toolTip = sliderString
  288. lineStyleCount = 0
  289. if annotation?.border.style == .solid {
  290. lineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  291. lineStyleButton.layer?.borderWidth = 0.0
  292. lineStyleButton.image = borderStyleSolid(isSelect: true)
  293. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  294. dottedLineStyleButton.layer?.borderWidth = 1.0
  295. dottedLineStyleButton.image = borderStyleSolid(isSelect: false)
  296. } else if annotation?.border.style == .dashed {
  297. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  298. dottedLineStyleButton.layer?.borderWidth = 0.0
  299. dottedLineStyleButton.image = borderStyleDashed(isSelect: true)
  300. lineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  301. lineStyleButton.layer?.borderWidth = 1.0
  302. lineStyleButton.image = borderStyleSolid(isSelect: false)
  303. lineStyleCount = 1
  304. }
  305. borderColorView.wantsLayer = true
  306. borderColorView.layer?.backgroundColor = annotation?.color.cgColor
  307. if let alternateColor = annotation?.alternatingColor {
  308. alternateColorView.wantsLayer = true
  309. alternateColorView.layer?.backgroundColor = alternateColor.cgColor
  310. }
  311. alternateColorButton.state = annotation!.isAlternateColor ? .on : .off
  312. sidebarPopUpButton.selectItem(at: (annotation?.sidebarCount())!)
  313. headerPopUpButton.selectItem(at: (annotation?.headerCount())!)
  314. footerPopUpButton.selectItem(at: (annotation?.footerCount())!)
  315. }
  316. func setCell(width cellWidth: CGFloat, height cellHeight: CGFloat) {
  317. if cellWidth == 0 && cellHeight == 0 {
  318. cellWidthStepper.isEnabled = false
  319. cellHeightStepper.isEnabled = false
  320. cellWidthTextField.isEnabled = false
  321. cellHeightTextField.isEnabled = false
  322. } else {
  323. cellWidthStepper.isEnabled = true
  324. cellHeightStepper.isEnabled = true
  325. cellWidthTextField.isEnabled = true
  326. cellHeightTextField.isEnabled = true
  327. }
  328. let widthText = String(format: "%0.2f", cellWidth * 0.04)
  329. cellWidthTextField.stringValue = widthText
  330. cellWidthStepper.doubleValue = Double(cellWidth)
  331. let heightText = String(format: "%0.2f", cellHeight * 0.04)
  332. cellHeightTextField.stringValue = heightText
  333. cellHeightStepper.doubleValue = Double(cellHeight)
  334. }
  335. func reloadDataStepper(_ stepper: NSStepper) {
  336. let table = self.annotation
  337. if (table?.currentCell.row)! >= 0 && (table?.currentCell.column)! >= 0 {
  338. let row = table?.currentCell.row
  339. let column = table?.currentCell.column
  340. if stepper == cellWidthStepper {
  341. table?.moveVerticalLine(of: column!+1, withDistance: self.cellWidthStepper.doubleValue - (table?.getCellBounds(withRow: row!, column: column!).size.width)!)
  342. } else {
  343. table?.moveCrossLine(of: table!.rowNumber - row! - 1, withDistance: (table?.getCellBounds(withRow: row!, column: column!).size.height)! - self.cellHeightStepper.doubleValue)
  344. table?.setOldRowHeights()
  345. }
  346. table?.afreshDrawLine()
  347. }
  348. pdfView?.needsDisplay = true
  349. }
  350. func colorWithCGColor(_ cgColor: CGColor?) -> NSColor? {
  351. guard let cgColor = cgColor else { return nil }
  352. return NSColor(cgColor: cgColor)
  353. }
  354. func borderStyleSolid(isSelect: Bool) -> NSImage {
  355. let size = NSSize(width: 49.0, height: 12.0)
  356. let image = NSImage.image(with: size) { rect in
  357. let path = NSBezierPath(rect: NSRect(x: 15.0, y: 5.0, width: 20.0, height: 1.0))
  358. path.lineWidth = 2.0
  359. if isSelect {
  360. KMAppearance.KM_273C62_ColorDarkChanged02().setStroke()
  361. } else {
  362. KMAppearance.Layout.h2Color().setStroke()
  363. }
  364. path.stroke()
  365. return true
  366. }
  367. return image
  368. }
  369. func borderStyleDashed(isSelect: Bool) -> NSImage {
  370. let size = NSSize(width: 49.0, height: 12.0)
  371. let image = NSImage(size: size) { rect in
  372. let path = NSBezierPath()
  373. path.move(to: NSPoint(x: 15.0, y: 5.0))
  374. path.line(to: NSPoint(x: 19.0, y: 5.0))
  375. path.move(to: NSPoint(x: 23.0, y: 5.0))
  376. path.line(to: NSPoint(x: 27.0, y: 5.0))
  377. path.move(to: NSPoint(x: 31.0, y: 5.0))
  378. path.line(to: NSPoint(x: 35.0, y: 5.0))
  379. path.lineWidth = 2.0
  380. if isSelect {
  381. KMAppearance.KM_273C62_ColorDarkChanged02().setStroke()
  382. } else {
  383. KMAppearance.Layout.h2Color().setStroke()
  384. }
  385. path.stroke()
  386. return true
  387. }
  388. return image!
  389. }
  390. //MARK: Button Actions
  391. @IBAction func styleButtonAction(_ sender: NSButton) {
  392. var tableStyle: KMTableStyle = .default
  393. if let button = sender as? NSButton {
  394. switch button.tag {
  395. case 300:
  396. tableStyle = .default
  397. case 301:
  398. tableStyle = .style1
  399. case 302:
  400. tableStyle = .style2
  401. case 303:
  402. tableStyle = .style3
  403. case 304:
  404. tableStyle = .style4
  405. case 305:
  406. tableStyle = .style5
  407. default:
  408. break
  409. }
  410. }
  411. annotation?.changeTableStyle(tableStyle)
  412. reloadData(with: point)
  413. pdfView?.needsDisplay = true
  414. }
  415. @IBAction func stepperAction(_ sender: Any) {
  416. guard let stepper = sender as? NSStepper else {
  417. return
  418. }
  419. var text: String
  420. switch stepper.tag {
  421. case 201:
  422. text = String(format: "%0.2f", self.cellWidthStepper.doubleValue * 0.04)
  423. self.cellWidthTextField.stringValue = text
  424. self.reloadDataStepper(self.cellWidthStepper)
  425. case 202:
  426. text = String(format: "%0.2f", self.cellHeightStepper.doubleValue * 0.04)
  427. self.cellHeightTextField.stringValue = text
  428. self.reloadDataStepper(self.cellHeightStepper)
  429. default:
  430. break
  431. }
  432. }
  433. @IBAction func colorWellAction(_ sender: Any) {
  434. guard let table = self.annotation else {
  435. return
  436. }
  437. // table.border.lineWidth = CGFloat(self.widthSlider.floatValue)
  438. let sliderString = String(format: "%.1f pt", self.widthSlider.floatValue)
  439. if widthPopUpButton.itemTitles.contains(sliderString) {
  440. if let index = self.widthPopUpButton.itemTitles.firstIndex(of: sliderString) {
  441. self.widthPopUpButton.selectItem(at: index)
  442. }
  443. }
  444. table.color = self.colorWithCGColor(self.borderColorView.layer!.backgroundColor)
  445. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  446. if self.backgroudColorBoxIsEnabled {
  447. if !self.notUpdateBackgroundColor {
  448. table.getCellData(withRow: table.currentCell.row, andColumn: table.currentCell.column).backgroundColor = self.colorWithCGColor(self.backgrourdColorView.layer?.backgroundColor)
  449. } else {
  450. var color = self.annotation?.getCellData(withRow: self.annotation!.currentCell.row, andColumn: self.annotation!.currentCell.column).backgroundColor
  451. if color == nil {
  452. color = NSColor(deviceRed: 1, green: 1, blue: 1, alpha: 1)
  453. }
  454. self.backgrourdColorView.layer?.backgroundColor = color?.cgColor
  455. }
  456. }
  457. }
  458. table.isAlternateColor = self.alternateColorButton.state == .on
  459. table.alternatingColor = self.colorWithCGColor(self.alternateColorView.layer!.backgroundColor)!
  460. table.writeFormDataToContents()
  461. if let tableInk = table as? CPDFAnnotation {
  462. if lineStyleCount == 1 {
  463. let lineType = tableInk.border.dashPattern ?? []
  464. if lineType.count == 0 {
  465. let number = NSNumber(value: Float(4))
  466. tableInk.setDashPattern([number])
  467. }
  468. }
  469. tableInk.setBorderStyle(CPDFBorderStyle(rawValue: lineStyleCount) ?? .solid)
  470. }
  471. if self.notUpdateBackgroundColor {
  472. self.notUpdateBackgroundColor = false
  473. }
  474. pdfView!.needsDisplay = true
  475. }
  476. @IBAction func titleEndTablePopUpButtonAction(_ sender: NSPopUpButton) {
  477. guard let table = self.annotation else {
  478. return
  479. }
  480. let bounds = self.annotation!.bounds
  481. guard let popButton = sender as? NSPopUpButton else {
  482. return
  483. }
  484. switch popButton.tag {
  485. case 100:
  486. let count = (popButton.titleOfSelectedItem as NSString?)?.integerValue ?? 0 - table.sidebarCount()
  487. if count > 0 {
  488. for _ in 0..<count {
  489. table.insertColumnAtIndex(before: 0, with: .round)
  490. }
  491. } else if count < 0 {
  492. for _ in count..<0 {
  493. table.removeColumn(at: 0)
  494. }
  495. }
  496. table.bounds.origin.x -= (bounds.size.width - table.bounds.size.width)
  497. case 101:
  498. let count = (popButton.titleOfSelectedItem as NSString?)?.integerValue ?? 0 - table.headerCount()
  499. if count > 0 {
  500. for _ in 0..<count {
  501. table.insertColumnAtIndex(before: 0, with: .round)
  502. }
  503. } else if count < 0 {
  504. for _ in count..<0 {
  505. table.removeRow(at: 0)
  506. }
  507. }
  508. table.bounds.origin.y -= (table.bounds.size.height - bounds.size.height)
  509. case 102:
  510. let count = (popButton.titleOfSelectedItem as NSString?)?.integerValue ?? 0 - table.footerCount()
  511. if count > 0 {
  512. for _ in 0..<count {
  513. table.insertColumnAtIndex(before: table.rowNumber - 1, with: .bevel)
  514. }
  515. } else if count < 0 {
  516. for _ in count..<0 {
  517. table.removeRow(atIndexBehindLine: table.rowNumber - 1)
  518. }
  519. }
  520. default:
  521. break
  522. }
  523. table.drawLine(self.point)
  524. reloadData(with: self.point)
  525. pdfView!.needsDisplay = true
  526. }
  527. @IBAction func widthPopUpButtonAction(_ sender: NSPopUpButton) {
  528. if let widthString = sender.titleOfSelectedItem, let width = Float(widthString.prefix(3)) {
  529. widthSlider.floatValue = width
  530. }
  531. colorWellAction(sender)
  532. }
  533. @objc func backgroudColorPanelColorDidChange(_ sender: Any) {
  534. guard NSColorPanel.sharedColorPanelExists, NSColorPanel.shared.isVisible else {
  535. return
  536. }
  537. if let color = (sender as? NSColorPanel)?.color {
  538. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  539. // if let rgbColor = color.usingColorSpace(NSColorSpace.sRGB) {
  540. // rgbColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  541. // }
  542. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  543. backgrourdColorView.wantsLayer = true
  544. backgrourdColorView.layer?.backgroundColor = color.cgColor
  545. colorWellAction(sender)
  546. }
  547. }
  548. @objc func borderColorPanelColorDidChange(_ sender: Any) {
  549. guard NSColorPanel.sharedColorPanelExists, NSColorPanel.shared.isVisible else {
  550. return
  551. }
  552. if let color = (sender as? NSColorPanel)?.color {
  553. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  554. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  555. borderColorView.wantsLayer = true
  556. borderColorView.layer?.backgroundColor = color.cgColor
  557. colorWellAction(sender)
  558. }
  559. }
  560. @objc func alternateColorPanelColorDidChange(_ sender: Any) {
  561. if let color = (sender as? NSColorPanel)?.color {
  562. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  563. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  564. alternateColorView.wantsLayer = true
  565. alternateColorView.layer?.backgroundColor = color.cgColor
  566. colorWellAction(sender)
  567. }
  568. }
  569. @IBAction func lineTypeButtonAction(_ sender: NSButton) {
  570. let tag = sender.tag
  571. switch tag {
  572. case 0:
  573. lineStyleCount = 0
  574. lineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  575. lineStyleButton.layer?.borderWidth = 0.0
  576. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  577. dottedLineStyleButton.layer?.borderWidth = 1.0
  578. lineStyleButton.image = borderStyleSolid(isSelect: true)
  579. dottedLineStyleButton.image = borderStyleDashed(isSelect: false)
  580. colorWellAction(sender)
  581. case 1:
  582. lineStyleCount = 1
  583. lineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  584. lineStyleButton.layer?.borderWidth = 1.0
  585. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  586. dottedLineStyleButton.layer?.borderWidth = 0.0
  587. lineStyleButton.image = borderStyleSolid(isSelect: false)
  588. dottedLineStyleButton.image = borderStyleDashed(isSelect: true)
  589. colorWellAction(sender)
  590. case 3:
  591. break
  592. default:
  593. break
  594. }
  595. }
  596. //MARK: Notification
  597. @objc func PDFViewTableAnnotationDidChangeNotification(_ notification: Notification) {
  598. if let pdfView = notification.object as? CPDFListView, pdfView == pdfView {
  599. self.notUpdateBackgroundColor = true
  600. if let pointValue = notification.userInfo?["point"] as? NSValue {
  601. let point = pointValue.pointValue
  602. self.point = point
  603. if NSColorPanel.sharedColorPanelExists {
  604. NSColorPanel.shared.close()
  605. }
  606. self.reloadData(with: point)
  607. }
  608. }
  609. }
  610. @objc func controlTextDidEndEditingNotification(_ notification: Notification) {
  611. guard let textField = notification.object as? NSTextField else {
  612. return
  613. }
  614. if textField == cellWidthTextField {
  615. let width = (textField.stringValue as NSString).doubleValue / 0.04
  616. if fabs(width - cellWidthStepper.doubleValue) < 0.01 {
  617. return
  618. }
  619. if width <= 0 {
  620. let text = String(format: "%0.2f", cellWidthStepper.doubleValue * 0.04)
  621. cellWidthTextField.stringValue = text
  622. } else if width > 0 && width < cellWidthStepper.minValue {
  623. let text = String(format: "%0.2f", cellWidthStepper.minValue * 0.04)
  624. cellWidthTextField.stringValue = text
  625. cellWidthStepper.doubleValue = cellWidthStepper.minValue
  626. reloadDataStepper(cellWidthStepper)
  627. } else if width >= cellWidthStepper.minValue && width <= cellWidthStepper.maxValue {
  628. let text = String(format: "%0.2f", (textField.stringValue as NSString).doubleValue)
  629. cellWidthTextField.stringValue = text
  630. cellWidthStepper.doubleValue = width
  631. reloadDataStepper(cellWidthStepper)
  632. } else {
  633. let text = String(format: "%0.2f", cellWidthStepper.maxValue * 0.04)
  634. cellWidthTextField.stringValue = text
  635. cellWidthStepper.doubleValue = cellWidthStepper.maxValue
  636. reloadDataStepper(cellWidthStepper)
  637. }
  638. } else if textField == cellHeightTextField {
  639. let height = (textField.stringValue as NSString).doubleValue / 0.04
  640. if height == cellHeightStepper.doubleValue {
  641. return
  642. }
  643. if height <= 0 {
  644. let text = String(format: "%0.2f", cellHeightStepper.doubleValue * 0.04)
  645. cellHeightTextField.stringValue = text
  646. } else if height > 0 && height < cellHeightStepper.minValue {
  647. let text = String(format: "%0.2f", cellHeightStepper.minValue * 0.04)
  648. cellHeightTextField.stringValue = text
  649. cellHeightStepper.doubleValue = cellHeightStepper.minValue
  650. reloadDataStepper(cellHeightStepper)
  651. } else {
  652. let text = String(format: "%0.2f", (textField.stringValue as NSString).doubleValue)
  653. cellHeightTextField.stringValue = text
  654. cellHeightStepper.doubleValue = height
  655. reloadDataStepper(cellHeightStepper)
  656. }
  657. }
  658. }
  659. @objc func themeChanged(_ notification: Notification) {
  660. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
  661. self?.updateViewColor()
  662. }
  663. }
  664. func updateViewColor() {
  665. if KMAppearance.isDarkMode() {
  666. widthPopUpButton.layer?.backgroundColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
  667. cellWidthTextField.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  668. cellHeightTextField.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  669. sidebarPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  670. widthPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  671. headerPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  672. footerPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  673. } else {
  674. widthPopUpButton.layer?.backgroundColor = NSColor.white.cgColor
  675. cellWidthTextField.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  676. cellHeightTextField.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  677. sidebarPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  678. widthPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  679. headerPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  680. footerPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  681. }
  682. }
  683. }