KMAnnotationTableViewController.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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.widthSlider.trackFillColor = KMAppearance.Layout.mColor()
  223. self.widthSlider.controlSize = .small
  224. self.updateViewColor()
  225. //
  226. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewTableAnnotationDidChangeNotification(_:)), name: Notification.Name("KMPDFViewTableAnnotationDidChangeNotification"), object: nil)
  227. NotificationCenter.default.addObserver(self, selector: #selector(controlTextDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: self.cellWidthTextField)
  228. NotificationCenter.default.addObserver(self, selector: #selector(controlTextDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: self.cellHeightTextField)
  229. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(_:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  230. }
  231. override func viewWillAppear() {
  232. super.viewWillAppear()
  233. reloadData(with: self.point)
  234. }
  235. // MARK: Set & Get
  236. var annotations: [KMTableAnnotation] {
  237. set {
  238. _annotations = newValue
  239. self.annotation = newValue.first
  240. }
  241. get {
  242. return _annotations
  243. }
  244. }
  245. var annotationModel: CPDFAnnotationModel {
  246. get {
  247. return _annotationModel!
  248. }
  249. set {
  250. _annotationModel = newValue
  251. if _annotationModel?.annotations != nil {
  252. if annotationModel.annotations.count > 0 {
  253. annotations = _annotationModel?.annotations as! [KMTableAnnotation]
  254. }
  255. }
  256. }
  257. }
  258. // MARK: Private Methods
  259. func reloadData(with point: NSPoint) {
  260. let rect = annotation?.isInCell(with: point) ?? .zero
  261. if !NSEqualRects(rect, NSRectFromCGRect(CGRect.zero)) {
  262. var color = annotation?.getCellData(withRow: annotation!.currentCell.row, andColumn: annotation!.currentCell.column).backgroundColor
  263. var maxWidth: CGFloat = 10
  264. let minHeight: CGFloat = 10
  265. if annotation!.currentCell.column == annotation!.columnNumber - 1 {
  266. maxWidth = CGFloat(MAXFLOAT)
  267. } else {
  268. if let rect1 = annotation?.getCellBounds(withRow: annotation!.currentCell.row, column: annotation!.currentCell.column + 1) {
  269. maxWidth = rect1.size.width + rect.size.width - 10
  270. }
  271. }
  272. cellWidthStepper.maxValue = Double(maxWidth)
  273. cellHeightStepper.minValue = Double(minHeight)
  274. if color == nil {
  275. color = NSColor(deviceRed: 1, green: 1, blue: 1, alpha: 1)
  276. }
  277. backgroudColorBoxIsEnabled = true
  278. backgrourdColorView.wantsLayer = true
  279. backgrourdColorView.layer?.backgroundColor = color!.cgColor
  280. } else {
  281. backgroudColorBoxIsEnabled = false
  282. backgrourdColorView.wantsLayer = true
  283. backgrourdColorView.layer?.backgroundColor = NSColor.white.cgColor
  284. }
  285. self.setCell(width: rect.size.width, height: rect.size.height)
  286. var lineWidth: Float = 0.0
  287. if let data = self.annotation?.border {
  288. lineWidth = Float(data.lineWidth)
  289. } else {
  290. lineWidth = Float(self.annotation?.lineWidth() ?? 0)
  291. }
  292. self.widthSlider.floatValue = lineWidth
  293. let sliderString = String(format: "%.1f pt", lineWidth)
  294. if widthPopUpButton.itemTitles.contains(sliderString) {
  295. if let index = widthPopUpButton.itemTitles.firstIndex(of: sliderString) {
  296. widthPopUpButton.selectItem(at: index)
  297. }
  298. }
  299. widthSlider.toolTip = sliderString
  300. lineStyleCount = 0
  301. if annotation?.border?.style == .solid {
  302. lineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  303. lineStyleButton.layer?.borderWidth = 0.0
  304. lineStyleButton.image = borderStyleSolid(isSelect: true)
  305. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  306. dottedLineStyleButton.layer?.borderWidth = 1.0
  307. dottedLineStyleButton.image = borderStyleDashed(isSelect: false)
  308. } else if annotation?.border?.style == .dashed {
  309. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  310. dottedLineStyleButton.layer?.borderWidth = 0.0
  311. dottedLineStyleButton.image = borderStyleDashed(isSelect: true)
  312. lineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  313. lineStyleButton.layer?.borderWidth = 1.0
  314. lineStyleButton.image = borderStyleSolid(isSelect: false)
  315. lineStyleCount = 1
  316. }
  317. borderColorView.wantsLayer = true
  318. borderColorView.layer?.backgroundColor = annotation?.color.cgColor
  319. if let alternateColor = annotation?.alternatingColor {
  320. alternateColorView.wantsLayer = true
  321. alternateColorView.layer?.backgroundColor = alternateColor.cgColor
  322. }
  323. alternateColorButton.state = (annotation?.isAlternateColor ?? false) ? .on : .off
  324. sidebarPopUpButton.selectItem(at: (annotation?.sidebarCount())!)
  325. headerPopUpButton.selectItem(at: (annotation?.headerCount())!)
  326. footerPopUpButton.selectItem(at: (annotation?.footerCount())!)
  327. }
  328. func setCell(width cellWidth: CGFloat, height cellHeight: CGFloat) {
  329. if cellWidth == 0 && cellHeight == 0 {
  330. cellWidthStepper.isEnabled = false
  331. cellHeightStepper.isEnabled = false
  332. cellWidthTextField.isEnabled = false
  333. cellHeightTextField.isEnabled = false
  334. } else {
  335. cellWidthStepper.isEnabled = true
  336. cellHeightStepper.isEnabled = true
  337. cellWidthTextField.isEnabled = true
  338. cellHeightTextField.isEnabled = true
  339. }
  340. let widthText = String(format: "%0.2f", cellWidth * 0.04)
  341. cellWidthTextField.stringValue = widthText
  342. cellWidthStepper.doubleValue = Double(cellWidth)
  343. let heightText = String(format: "%0.2f", cellHeight * 0.04)
  344. cellHeightTextField.stringValue = heightText
  345. cellHeightStepper.doubleValue = Double(cellHeight)
  346. }
  347. func reloadDataStepper(_ stepper: NSStepper) {
  348. guard let table = self.annotation else {
  349. return
  350. }
  351. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  352. let row = table.currentCell.row
  353. let column = table.currentCell.column
  354. if stepper == self.cellWidthStepper {
  355. let distance = self.cellWidthStepper.doubleValue - table.getCellBounds(withRow: row, column: column).size.width
  356. table.moveVerticalLine(of: column + 1, withDistance: distance)
  357. } else if stepper == self.cellHeightStepper {
  358. let distance = table.getCellBounds(withRow: row, column: column).size.height - self.cellHeightStepper.doubleValue
  359. table.moveCrossLine(of: table.rowNumber - row - 1, withDistance: distance)
  360. table.setOldRowHeights()
  361. }
  362. table.afreshDrawLine()
  363. table.updateAppearanceInk(withIsAdd: false)
  364. }
  365. pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
  366. }
  367. func colorWithCGColor(_ cgColor: CGColor?) -> NSColor? {
  368. guard let cgColor = cgColor else { return nil }
  369. return NSColor(cgColor: cgColor)
  370. }
  371. func borderStyleSolid(isSelect: Bool) -> NSImage {
  372. let size = NSSize(width: 49.0, height: 12.0)
  373. let image = NSImage.image(with: size) { rect in
  374. let path = NSBezierPath(rect: NSRect(x: 15.0, y: 5.0, width: 20.0, height: 1.0))
  375. path.lineWidth = 2.0
  376. if isSelect {
  377. KMAppearance.Layout.mColor().setStroke()
  378. } else {
  379. KMAppearance.Layout.h2Color().setStroke()
  380. }
  381. path.stroke()
  382. return true
  383. }
  384. return image
  385. }
  386. func borderStyleDashed(isSelect: Bool) -> NSImage {
  387. let size = NSSize(width: 49.0, height: 12.0)
  388. let image = NSImage(size: size) { rect in
  389. let path = NSBezierPath()
  390. path.move(to: NSPoint(x: 15.0, y: 5.0))
  391. path.line(to: NSPoint(x: 19.0, y: 5.0))
  392. path.move(to: NSPoint(x: 23.0, y: 5.0))
  393. path.line(to: NSPoint(x: 27.0, y: 5.0))
  394. path.move(to: NSPoint(x: 31.0, y: 5.0))
  395. path.line(to: NSPoint(x: 35.0, y: 5.0))
  396. path.lineWidth = 2.0
  397. if isSelect {
  398. KMAppearance.Layout.mColor().setStroke()
  399. } else {
  400. KMAppearance.Layout.h2Color().setStroke()
  401. }
  402. path.stroke()
  403. return true
  404. }
  405. return image!
  406. }
  407. //MARK: Button Actions
  408. @IBAction func styleButtonAction(_ sender: NSButton) {
  409. var tableStyle: KMTableStyle = .default
  410. if let button = sender as? NSButton {
  411. switch button.tag {
  412. case 300:
  413. tableStyle = .default
  414. case 301:
  415. tableStyle = .style1
  416. case 302:
  417. tableStyle = .style2
  418. case 303:
  419. tableStyle = .style3
  420. case 304:
  421. tableStyle = .style4
  422. case 305:
  423. tableStyle = .style5
  424. default:
  425. break
  426. }
  427. }
  428. annotation?.changeTableStyle(tableStyle)
  429. reloadData(with: point)
  430. pdfView?.needsDisplay = true
  431. }
  432. @IBAction func stepperAction(_ sender: Any) {
  433. guard let stepper = sender as? NSStepper else {
  434. return
  435. }
  436. var text: String
  437. switch stepper.tag {
  438. case 201:
  439. text = String(format: "%0.2f", self.cellWidthStepper.doubleValue * 0.04)
  440. self.cellWidthTextField.stringValue = text
  441. self.reloadDataStepper(self.cellWidthStepper)
  442. case 202:
  443. text = String(format: "%0.2f", self.cellHeightStepper.doubleValue * 0.04)
  444. self.cellHeightTextField.stringValue = text
  445. self.reloadDataStepper(self.cellHeightStepper)
  446. default:
  447. break
  448. }
  449. }
  450. @IBAction func colorWellAction(_ sender: Any) {
  451. guard let table = self.annotation else {
  452. return
  453. }
  454. table.borderWidth = CGFloat(self.widthSlider.floatValue)
  455. let sliderString = String(format: "%.1f pt", self.widthSlider.floatValue)
  456. if widthPopUpButton.itemTitles.contains(sliderString) {
  457. if let index = self.widthPopUpButton.itemTitles.firstIndex(of: sliderString) {
  458. self.widthPopUpButton.selectItem(at: index)
  459. }
  460. }
  461. table.color = self.colorWithCGColor(self.borderColorView.layer!.backgroundColor)
  462. if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
  463. if self.backgroudColorBoxIsEnabled {
  464. if !self.notUpdateBackgroundColor {
  465. table.getCellData(withRow: table.currentCell.row, andColumn: table.currentCell.column).backgroundColor = self.colorWithCGColor(self.backgrourdColorView.layer?.backgroundColor)
  466. } else {
  467. var color = self.annotation?.getCellData(withRow: self.annotation!.currentCell.row, andColumn: self.annotation!.currentCell.column).backgroundColor
  468. if color == nil {
  469. color = NSColor(deviceRed: 1, green: 1, blue: 1, alpha: 1)
  470. }
  471. self.backgrourdColorView.layer?.backgroundColor = color?.cgColor
  472. }
  473. }
  474. }
  475. table.isAlternateColor = self.alternateColorButton.state == .on
  476. table.alternatingColor = self.colorWithCGColor(self.alternateColorView.layer!.backgroundColor)!
  477. table.writeFormDataToContents()
  478. if lineStyleCount == 1 {
  479. let lineType = table.dashPattern()
  480. if lineType.count == 0 {
  481. let number = NSNumber(value: Float(4))
  482. table.setDashPattern([number])
  483. }
  484. }
  485. table.setBorderStyle(CPDFBorderStyle(rawValue: lineStyleCount) ?? .solid)
  486. if self.notUpdateBackgroundColor {
  487. self.notUpdateBackgroundColor = false
  488. }
  489. pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
  490. }
  491. @IBAction func titleEndTablePopUpButtonAction(_ sender: KMPopUpButton) {
  492. guard let table = self.annotation else {
  493. return
  494. }
  495. let bounds = self.annotation!.bounds
  496. guard let popButton = sender as? KMPopUpButton else {
  497. return
  498. }
  499. switch popButton.tag {
  500. case 100:
  501. let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.sidebarCount()
  502. if count > 0 {
  503. for _ in 0..<count {
  504. table.insertColumnAtIndex(before: 0, with: .round)
  505. }
  506. } else if count < 0 {
  507. for _ in count..<0 {
  508. table.removeColumn(at: 0)
  509. }
  510. }
  511. table.bounds = NSRectFromCGRect(CGRect(x: table.bounds.origin.x - (bounds.size.width - table.bounds.size.width),
  512. y: table.bounds.origin.y,
  513. width: table.bounds.size.width, height: table.bounds.size.height))
  514. case 101:
  515. let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.headerCount()
  516. if count > 0 {
  517. for _ in 0..<count {
  518. table.insertRowAtIndex(before: 0, with: .round)
  519. }
  520. } else if count < 0 {
  521. for _ in count..<0 {
  522. table.removeRow(at: 0)
  523. }
  524. }
  525. table.bounds = NSRectFromCGRect(CGRect(x: table.bounds.origin.x,
  526. y: table.bounds.origin.y - (table.bounds.size.height - bounds.size.height),
  527. width: table.bounds.size.width, height: table.bounds.size.height))
  528. case 102:
  529. let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.footerCount()
  530. if count > 0 {
  531. for _ in 0..<count {
  532. table.insertRow(atIndexBehind: table.rowNumber - 1, with: .bevel)
  533. }
  534. } else if count < 0 {
  535. for _ in count..<0 {
  536. table.removeRow(atIndexBehindLine: table.rowNumber - 1)
  537. }
  538. }
  539. default:
  540. break
  541. }
  542. table.drawLine(self.point)
  543. reloadData(with: self.point)
  544. pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
  545. }
  546. @IBAction func widthPopUpButtonAction(_ sender: NSPopUpButton) {
  547. if let widthString = sender.titleOfSelectedItem, let width = Float(widthString.prefix(3)) {
  548. widthSlider.floatValue = width
  549. }
  550. colorWellAction(sender)
  551. }
  552. @objc func backgroudColorPanelColorDidChange(_ sender: Any) {
  553. guard NSColorPanel.sharedColorPanelExists, NSColorPanel.shared.isVisible else {
  554. return
  555. }
  556. if let color = (sender as? NSColorPanel)?.color {
  557. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  558. // if let rgbColor = color.usingColorSpace(NSColorSpace.sRGB) {
  559. // rgbColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  560. // }
  561. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  562. backgrourdColorView.wantsLayer = true
  563. backgrourdColorView.layer?.backgroundColor = color.cgColor
  564. colorWellAction(sender)
  565. }
  566. }
  567. @objc func borderColorPanelColorDidChange(_ sender: Any) {
  568. guard NSColorPanel.sharedColorPanelExists, NSColorPanel.shared.isVisible else {
  569. return
  570. }
  571. if let color = (sender as? NSColorPanel)?.color {
  572. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  573. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  574. borderColorView.wantsLayer = true
  575. borderColorView.layer?.backgroundColor = color.cgColor
  576. colorWellAction(sender)
  577. }
  578. }
  579. @objc func alternateColorPanelColorDidChange(_ sender: Any) {
  580. if let color = (sender as? NSColorPanel)?.color {
  581. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  582. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  583. alternateColorView.wantsLayer = true
  584. alternateColorView.layer?.backgroundColor = color.cgColor
  585. colorWellAction(sender)
  586. }
  587. }
  588. @IBAction func lineTypeButtonAction(_ sender: NSButton) {
  589. let tag = sender.tag
  590. switch tag {
  591. case 0:
  592. lineStyleCount = 0
  593. lineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  594. lineStyleButton.layer?.borderWidth = 0.0
  595. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  596. dottedLineStyleButton.layer?.borderWidth = 1.0
  597. lineStyleButton.image = borderStyleSolid(isSelect: true)
  598. dottedLineStyleButton.image = borderStyleDashed(isSelect: false)
  599. colorWellAction(sender)
  600. case 1:
  601. lineStyleCount = 1
  602. lineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  603. lineStyleButton.layer?.borderWidth = 1.0
  604. dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  605. dottedLineStyleButton.layer?.borderWidth = 0.0
  606. lineStyleButton.image = borderStyleSolid(isSelect: false)
  607. dottedLineStyleButton.image = borderStyleDashed(isSelect: true)
  608. colorWellAction(sender)
  609. case 3:
  610. break
  611. default:
  612. break
  613. }
  614. }
  615. //MARK: Notification
  616. @objc func PDFViewTableAnnotationDidChangeNotification(_ notification: Notification) {
  617. if let pdfView = notification.object as? CPDFListView, pdfView == pdfView {
  618. self.notUpdateBackgroundColor = true
  619. if let pointValue = notification.userInfo?["point"] as? NSValue {
  620. let point = pointValue.pointValue
  621. self.point = point
  622. if NSColorPanel.sharedColorPanelExists {
  623. NSColorPanel.shared.close()
  624. }
  625. self.reloadData(with: point)
  626. }
  627. }
  628. }
  629. @objc func controlTextDidEndEditingNotification(_ notification: Notification) {
  630. guard let textField = notification.object as? NSTextField else {
  631. return
  632. }
  633. if textField == cellWidthTextField {
  634. let width = (textField.stringValue as NSString).doubleValue / 0.04
  635. if fabs(width - cellWidthStepper.doubleValue) < 0.01 {
  636. return
  637. }
  638. if width <= 0 {
  639. let text = String(format: "%0.2f", cellWidthStepper.doubleValue * 0.04)
  640. cellWidthTextField.stringValue = text
  641. } else if width > 0 && width < cellWidthStepper.minValue {
  642. let text = String(format: "%0.2f", cellWidthStepper.minValue * 0.04)
  643. cellWidthTextField.stringValue = text
  644. cellWidthStepper.doubleValue = cellWidthStepper.minValue
  645. reloadDataStepper(cellWidthStepper)
  646. } else if width >= cellWidthStepper.minValue && width <= cellWidthStepper.maxValue {
  647. let text = String(format: "%0.2f", (textField.stringValue as NSString).doubleValue)
  648. cellWidthTextField.stringValue = text
  649. cellWidthStepper.doubleValue = width
  650. reloadDataStepper(cellWidthStepper)
  651. } else {
  652. let text = String(format: "%0.2f", cellWidthStepper.maxValue * 0.04)
  653. cellWidthTextField.stringValue = text
  654. cellWidthStepper.doubleValue = cellWidthStepper.maxValue
  655. reloadDataStepper(cellWidthStepper)
  656. }
  657. } else if textField == cellHeightTextField {
  658. let height = (textField.stringValue as NSString).doubleValue / 0.04
  659. if height == cellHeightStepper.doubleValue {
  660. return
  661. }
  662. if height <= 0 {
  663. let text = String(format: "%0.2f", cellHeightStepper.doubleValue * 0.04)
  664. cellHeightTextField.stringValue = text
  665. } else if height > 0 && height < cellHeightStepper.minValue {
  666. let text = String(format: "%0.2f", cellHeightStepper.minValue * 0.04)
  667. cellHeightTextField.stringValue = text
  668. cellHeightStepper.doubleValue = cellHeightStepper.minValue
  669. reloadDataStepper(cellHeightStepper)
  670. } else {
  671. let text = String(format: "%0.2f", (textField.stringValue as NSString).doubleValue)
  672. cellHeightTextField.stringValue = text
  673. cellHeightStepper.doubleValue = height
  674. reloadDataStepper(cellHeightStepper)
  675. }
  676. }
  677. }
  678. @objc func themeChanged(_ notification: Notification) {
  679. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
  680. self?.updateViewColor()
  681. }
  682. }
  683. func updateViewColor() {
  684. if KMAppearance.isDarkMode() {
  685. widthPopUpButton.layer?.backgroundColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
  686. cellWidthTextField.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  687. cellHeightTextField.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  688. sidebarPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  689. widthPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  690. headerPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  691. footerPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  692. } else {
  693. widthPopUpButton.layer?.backgroundColor = NSColor.white.cgColor
  694. cellWidthTextField.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  695. cellHeightTextField.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  696. sidebarPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  697. widthPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  698. headerPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  699. footerPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  700. }
  701. }
  702. }