KMAnnotationTableViewController.swift 36 KB

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