KMAnnotationTableViewController.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. lineStyleButton.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. guard let table = self.annotation else {
  337. return
  338. }
  339. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  340. let row = table.currentCell.row
  341. let column = table.currentCell.column
  342. if stepper == self.cellWidthStepper {
  343. let distance = self.cellWidthStepper.doubleValue - table.getCellBounds(withRow: row, column: column).size.width
  344. table.moveVerticalLine(of: column + 1, withDistance: distance)
  345. } else if stepper == self.cellHeightStepper {
  346. let distance = table.getCellBounds(withRow: row, column: column).size.height - self.cellHeightStepper.doubleValue
  347. table.moveCrossLine(of: table.rowNumber - row - 1, withDistance: distance)
  348. table.setOldRowHeights()
  349. }
  350. table.afreshDrawLine()
  351. table.updateAppearanceInk(withIsAdd: false)
  352. }
  353. pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
  354. }
  355. func colorWithCGColor(_ cgColor: CGColor?) -> NSColor? {
  356. guard let cgColor = cgColor else { return nil }
  357. return NSColor(cgColor: cgColor)
  358. }
  359. func borderStyleSolid(isSelect: Bool) -> NSImage {
  360. let size = NSSize(width: 49.0, height: 12.0)
  361. let image = NSImage.image(with: size) { rect in
  362. let path = NSBezierPath(rect: NSRect(x: 15.0, y: 5.0, width: 20.0, height: 1.0))
  363. path.lineWidth = 2.0
  364. if isSelect {
  365. KMAppearance.KM_273C62_ColorDarkChanged02().setStroke()
  366. } else {
  367. KMAppearance.Layout.h2Color().setStroke()
  368. }
  369. path.stroke()
  370. return true
  371. }
  372. return image
  373. }
  374. func borderStyleDashed(isSelect: Bool) -> NSImage {
  375. let size = NSSize(width: 49.0, height: 12.0)
  376. let image = NSImage(size: size) { rect in
  377. let path = NSBezierPath()
  378. path.move(to: NSPoint(x: 15.0, y: 5.0))
  379. path.line(to: NSPoint(x: 19.0, y: 5.0))
  380. path.move(to: NSPoint(x: 23.0, y: 5.0))
  381. path.line(to: NSPoint(x: 27.0, y: 5.0))
  382. path.move(to: NSPoint(x: 31.0, y: 5.0))
  383. path.line(to: NSPoint(x: 35.0, y: 5.0))
  384. path.lineWidth = 2.0
  385. if isSelect {
  386. KMAppearance.KM_273C62_ColorDarkChanged02().setStroke()
  387. } else {
  388. KMAppearance.Layout.h2Color().setStroke()
  389. }
  390. path.stroke()
  391. return true
  392. }
  393. return image!
  394. }
  395. //MARK: Button Actions
  396. @IBAction func styleButtonAction(_ sender: NSButton) {
  397. var tableStyle: KMTableStyle = .default
  398. if let button = sender as? NSButton {
  399. switch button.tag {
  400. case 300:
  401. tableStyle = .default
  402. case 301:
  403. tableStyle = .style1
  404. case 302:
  405. tableStyle = .style2
  406. case 303:
  407. tableStyle = .style3
  408. case 304:
  409. tableStyle = .style4
  410. case 305:
  411. tableStyle = .style5
  412. default:
  413. break
  414. }
  415. }
  416. annotation?.changeTableStyle(tableStyle)
  417. reloadData(with: point)
  418. pdfView?.needsDisplay = true
  419. }
  420. @IBAction func stepperAction(_ sender: Any) {
  421. guard let stepper = sender as? NSStepper else {
  422. return
  423. }
  424. var text: String
  425. switch stepper.tag {
  426. case 201:
  427. text = String(format: "%0.2f", self.cellWidthStepper.doubleValue * 0.04)
  428. self.cellWidthTextField.stringValue = text
  429. self.reloadDataStepper(self.cellWidthStepper)
  430. case 202:
  431. text = String(format: "%0.2f", self.cellHeightStepper.doubleValue * 0.04)
  432. self.cellHeightTextField.stringValue = text
  433. self.reloadDataStepper(self.cellHeightStepper)
  434. default:
  435. break
  436. }
  437. }
  438. @IBAction func colorWellAction(_ sender: Any) {
  439. guard let table = self.annotation else {
  440. return
  441. }
  442. table.borderWidth = CGFloat(self.widthSlider.floatValue)
  443. let sliderString = String(format: "%.1f pt", self.widthSlider.floatValue)
  444. if widthPopUpButton.itemTitles.contains(sliderString) {
  445. if let index = self.widthPopUpButton.itemTitles.firstIndex(of: sliderString) {
  446. self.widthPopUpButton.selectItem(at: index)
  447. }
  448. }
  449. table.color = self.colorWithCGColor(self.borderColorView.layer!.backgroundColor)
  450. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  451. if self.backgroudColorBoxIsEnabled {
  452. if !self.notUpdateBackgroundColor {
  453. table.getCellData(withRow: table.currentCell.row, andColumn: table.currentCell.column).backgroundColor = self.colorWithCGColor(self.backgrourdColorView.layer?.backgroundColor)
  454. } else {
  455. var color = self.annotation?.getCellData(withRow: self.annotation!.currentCell.row, andColumn: self.annotation!.currentCell.column).backgroundColor
  456. if color == nil {
  457. color = NSColor(deviceRed: 1, green: 1, blue: 1, alpha: 1)
  458. }
  459. self.backgrourdColorView.layer?.backgroundColor = color?.cgColor
  460. }
  461. }
  462. }
  463. table.isAlternateColor = self.alternateColorButton.state == .on
  464. table.alternatingColor = self.colorWithCGColor(self.alternateColorView.layer!.backgroundColor)!
  465. table.writeFormDataToContents()
  466. if let tableInk = table as? CPDFAnnotation {
  467. if lineStyleCount == 1 {
  468. let lineType = tableInk.border.dashPattern ?? []
  469. if lineType.count == 0 {
  470. let number = NSNumber(value: Float(4))
  471. tableInk.setDashPattern([number])
  472. }
  473. }
  474. tableInk.setBorderStyle(CPDFBorderStyle(rawValue: lineStyleCount) ?? .solid)
  475. }
  476. if self.notUpdateBackgroundColor {
  477. self.notUpdateBackgroundColor = false
  478. }
  479. pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
  480. }
  481. @IBAction func titleEndTablePopUpButtonAction(_ sender: KMPopUpButton) {
  482. guard let table = self.annotation else {
  483. return
  484. }
  485. let bounds = self.annotation!.bounds
  486. guard let popButton = sender as? KMPopUpButton else {
  487. return
  488. }
  489. switch popButton.tag {
  490. case 100:
  491. let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.sidebarCount()
  492. if count > 0 {
  493. for _ in 0..<count {
  494. table.insertColumnAtIndex(before: 0, with: .round)
  495. }
  496. } else if count < 0 {
  497. for _ in count..<0 {
  498. table.removeColumn(at: 0)
  499. }
  500. }
  501. table.bounds = NSRectFromCGRect(CGRect(x: table.bounds.origin.x - (bounds.size.width - table.bounds.size.width),
  502. y: table.bounds.origin.y,
  503. width: table.bounds.size.width, height: table.bounds.size.height))
  504. case 101:
  505. let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.headerCount()
  506. if count > 0 {
  507. for _ in 0..<count {
  508. table.insertRowAtIndex(before: 0, with: .round)
  509. }
  510. } else if count < 0 {
  511. for _ in count..<0 {
  512. table.removeRow(at: 0)
  513. }
  514. }
  515. table.bounds = NSRectFromCGRect(CGRect(x: table.bounds.origin.x,
  516. y: table.bounds.origin.y - (table.bounds.size.height - bounds.size.height),
  517. width: table.bounds.size.width, height: table.bounds.size.height))
  518. case 102:
  519. let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.footerCount()
  520. if count > 0 {
  521. for _ in 0..<count {
  522. table.insertRow(atIndexBehind: table.rowNumber - 1, with: .bevel)
  523. }
  524. } else if count < 0 {
  525. for _ in count..<0 {
  526. table.removeRow(atIndexBehindLine: table.rowNumber - 1)
  527. }
  528. }
  529. default:
  530. break
  531. }
  532. table.drawLine(self.point)
  533. reloadData(with: self.point)
  534. pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
  535. }
  536. @IBAction func widthPopUpButtonAction(_ sender: NSPopUpButton) {
  537. if let widthString = sender.titleOfSelectedItem, let width = Float(widthString.prefix(3)) {
  538. widthSlider.floatValue = width
  539. }
  540. colorWellAction(sender)
  541. }
  542. @objc func backgroudColorPanelColorDidChange(_ sender: Any) {
  543. guard NSColorPanel.sharedColorPanelExists, NSColorPanel.shared.isVisible else {
  544. return
  545. }
  546. if let color = (sender as? NSColorPanel)?.color {
  547. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  548. // if let rgbColor = color.usingColorSpace(NSColorSpace.sRGB) {
  549. // rgbColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  550. // }
  551. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  552. backgrourdColorView.wantsLayer = true
  553. backgrourdColorView.layer?.backgroundColor = color.cgColor
  554. colorWellAction(sender)
  555. }
  556. }
  557. @objc func borderColorPanelColorDidChange(_ sender: Any) {
  558. guard NSColorPanel.sharedColorPanelExists, NSColorPanel.shared.isVisible else {
  559. return
  560. }
  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. borderColorView.wantsLayer = true
  565. borderColorView.layer?.backgroundColor = color.cgColor
  566. colorWellAction(sender)
  567. }
  568. }
  569. @objc func alternateColorPanelColorDidChange(_ sender: Any) {
  570. if let color = (sender as? NSColorPanel)?.color {
  571. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  572. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  573. alternateColorView.wantsLayer = true
  574. alternateColorView.layer?.backgroundColor = color.cgColor
  575. colorWellAction(sender)
  576. }
  577. }
  578. @IBAction func lineTypeButtonAction(_ sender: NSButton) {
  579. let tag = sender.tag
  580. switch tag {
  581. case 0:
  582. lineStyleCount = 0
  583. lineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  584. lineStyleButton.layer?.borderWidth = 0.0
  585. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  586. dottedLineStyleButton.layer?.borderWidth = 1.0
  587. lineStyleButton.image = borderStyleSolid(isSelect: true)
  588. dottedLineStyleButton.image = borderStyleDashed(isSelect: false)
  589. colorWellAction(sender)
  590. case 1:
  591. lineStyleCount = 1
  592. lineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  593. lineStyleButton.layer?.borderWidth = 1.0
  594. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  595. dottedLineStyleButton.layer?.borderWidth = 0.0
  596. lineStyleButton.image = borderStyleSolid(isSelect: false)
  597. dottedLineStyleButton.image = borderStyleDashed(isSelect: true)
  598. colorWellAction(sender)
  599. case 3:
  600. break
  601. default:
  602. break
  603. }
  604. }
  605. //MARK: Notification
  606. @objc func PDFViewTableAnnotationDidChangeNotification(_ notification: Notification) {
  607. if let pdfView = notification.object as? CPDFListView, pdfView == pdfView {
  608. self.notUpdateBackgroundColor = true
  609. if let pointValue = notification.userInfo?["point"] as? NSValue {
  610. let point = pointValue.pointValue
  611. self.point = point
  612. if NSColorPanel.sharedColorPanelExists {
  613. NSColorPanel.shared.close()
  614. }
  615. self.reloadData(with: point)
  616. }
  617. }
  618. }
  619. @objc func controlTextDidEndEditingNotification(_ notification: Notification) {
  620. guard let textField = notification.object as? NSTextField else {
  621. return
  622. }
  623. if textField == cellWidthTextField {
  624. let width = (textField.stringValue as NSString).doubleValue / 0.04
  625. if fabs(width - cellWidthStepper.doubleValue) < 0.01 {
  626. return
  627. }
  628. if width <= 0 {
  629. let text = String(format: "%0.2f", cellWidthStepper.doubleValue * 0.04)
  630. cellWidthTextField.stringValue = text
  631. } else if width > 0 && width < cellWidthStepper.minValue {
  632. let text = String(format: "%0.2f", cellWidthStepper.minValue * 0.04)
  633. cellWidthTextField.stringValue = text
  634. cellWidthStepper.doubleValue = cellWidthStepper.minValue
  635. reloadDataStepper(cellWidthStepper)
  636. } else if width >= cellWidthStepper.minValue && width <= cellWidthStepper.maxValue {
  637. let text = String(format: "%0.2f", (textField.stringValue as NSString).doubleValue)
  638. cellWidthTextField.stringValue = text
  639. cellWidthStepper.doubleValue = width
  640. reloadDataStepper(cellWidthStepper)
  641. } else {
  642. let text = String(format: "%0.2f", cellWidthStepper.maxValue * 0.04)
  643. cellWidthTextField.stringValue = text
  644. cellWidthStepper.doubleValue = cellWidthStepper.maxValue
  645. reloadDataStepper(cellWidthStepper)
  646. }
  647. } else if textField == cellHeightTextField {
  648. let height = (textField.stringValue as NSString).doubleValue / 0.04
  649. if height == cellHeightStepper.doubleValue {
  650. return
  651. }
  652. if height <= 0 {
  653. let text = String(format: "%0.2f", cellHeightStepper.doubleValue * 0.04)
  654. cellHeightTextField.stringValue = text
  655. } else if height > 0 && height < cellHeightStepper.minValue {
  656. let text = String(format: "%0.2f", cellHeightStepper.minValue * 0.04)
  657. cellHeightTextField.stringValue = text
  658. cellHeightStepper.doubleValue = cellHeightStepper.minValue
  659. reloadDataStepper(cellHeightStepper)
  660. } else {
  661. let text = String(format: "%0.2f", (textField.stringValue as NSString).doubleValue)
  662. cellHeightTextField.stringValue = text
  663. cellHeightStepper.doubleValue = height
  664. reloadDataStepper(cellHeightStepper)
  665. }
  666. }
  667. }
  668. @objc func themeChanged(_ notification: Notification) {
  669. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
  670. self?.updateViewColor()
  671. }
  672. }
  673. func updateViewColor() {
  674. if KMAppearance.isDarkMode() {
  675. widthPopUpButton.layer?.backgroundColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
  676. cellWidthTextField.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  677. cellHeightTextField.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  678. sidebarPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  679. widthPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  680. headerPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  681. footerPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  682. } else {
  683. widthPopUpButton.layer?.backgroundColor = NSColor.white.cgColor
  684. cellWidthTextField.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  685. cellHeightTextField.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  686. sidebarPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  687. widthPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  688. headerPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  689. footerPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  690. }
  691. }
  692. }