KMAnnotationTableViewController.swift 34 KB

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