123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804 |
- //
- // KMAnnotationTableViewController.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2023/11/7.
- //
- import Cocoa
- class KMLineStyleButtonCell: NSButtonCell {
- override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) {
- if flag {
- print("111")
- controlView.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- } else {
- print("111")
- }
- }
- }
- @objcMembers
- class KMAnnotationTableViewController: NSViewController {
- var annotation: KMTableAnnotation?
- weak var pdfView: CPDFListView?
- var _annotationModel: CPDFAnnotationModel?
- var _annotations: [KMTableAnnotation] = []
- var point: NSPoint = NSPoint.zero
- @IBOutlet weak var styleLabel: NSTextField!
- @IBOutlet weak var styleBox0: KMBox!
- @IBOutlet weak var styleBox1: KMBox!
- @IBOutlet weak var styleBox2: KMBox!
- @IBOutlet weak var styleBox3: KMBox!
- @IBOutlet weak var styleBox4: KMBox!
- @IBOutlet weak var styleBox5: KMBox!
- @IBOutlet weak var cellSizeLabel: NSTextField!
- @IBOutlet weak var cellWidthLabel: NSTextField!
- @IBOutlet weak var cellHeightLabel: NSTextField!
- @IBOutlet weak var cellWidthTextField: NSTextField!
- @IBOutlet weak var cellHeightTextField: NSTextField!
- @IBOutlet weak var cellWidthStepper: NSStepper!
- @IBOutlet weak var cellHeightStepper: NSStepper!
- @IBOutlet weak var titleEndTableLabel: NSTextField!
- @IBOutlet weak var sidebarPopUpButton: KMPopUpButton!
- @IBOutlet weak var headerPopUpButton: KMPopUpButton!
- @IBOutlet weak var footerPopUpButton: KMPopUpButton!
- @IBOutlet weak var bordersAndColorsLabel: NSTextField!
- @IBOutlet weak var backgroundColorLabel: NSTextField!
- @IBOutlet weak var backgroudColorBox: KMBox!
- @IBOutlet weak var backgrourdColorView: NSView!
- @IBOutlet weak var borderColorLabel: NSTextField!
- @IBOutlet weak var borderColorBox: KMBox!
- @IBOutlet weak var borderColorView: NSView!
- @IBOutlet weak var widthLabel: NSTextField!
- @IBOutlet weak var widthSlider: NSSlider!
- @IBOutlet weak var widthPopUpButton: KMPopUpButton!
- @IBOutlet weak var lineStyleLabel: NSTextField!
- @IBOutlet weak var lineStyleButton: KMButton!
- @IBOutlet weak var dottedLineStyleButton: KMButton!
- @IBOutlet weak var rightArrowStyleButton: KMButton!
- @IBOutlet weak var alternateColorButton: NSButton!
- @IBOutlet weak var alternateColorLabel: NSTextField!
- @IBOutlet weak var alternateColorBox: KMBox!
- @IBOutlet weak var alternateColorView: NSView!
- var backgroudColorBoxIsEnabled: Bool = false
- var notUpdateBackgroundColor: Bool = false
- var lineStyleCount: Int = 0
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- DistributedNotificationCenter.default().removeObserver(self)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- self.styleLabel.stringValue = NSLocalizedString("Table Styles", comment: "")
- self.styleLabel.textColor = KMAppearance.Layout.h0Color()
- self.cellSizeLabel.stringValue = NSLocalizedString("Cell Size", comment: "")
- self.cellSizeLabel.textColor = KMAppearance.Layout.h0Color()
- self.cellWidthLabel.stringValue = "\(NSLocalizedString("Cell Width", comment: ""))(cm)"
- self.cellWidthLabel.toolTip = "\(NSLocalizedString("Cell Width", comment: ""))(cm)"
- self.cellWidthLabel.allowsExpansionToolTips = true
- self.cellWidthLabel.textColor = KMAppearance.Layout.h1Color()
- self.cellHeightLabel.stringValue = "\(NSLocalizedString("Cell Height", comment: ""))(cm)"
- self.cellHeightLabel.toolTip = "\(NSLocalizedString("Cell Height", comment: ""))(cm)"
- self.cellHeightLabel.allowsExpansionToolTips = true
- self.cellHeightLabel.textColor = KMAppearance.Layout.h1Color()
- self.titleEndTableLabel.stringValue = NSLocalizedString("Headers & Footers", comment: "")
- self.titleEndTableLabel.textColor = KMAppearance.Layout.h0Color()
- self.bordersAndColorsLabel.stringValue = NSLocalizedString("Borders & Colors", comment: "")
- self.bordersAndColorsLabel.textColor = KMAppearance.Layout.h0Color()
- self.backgroundColorLabel.stringValue = NSLocalizedString("Background Color", comment: "")
- self.backgroundColorLabel.textColor = KMAppearance.Layout.h1Color()
- self.borderColorLabel.stringValue = NSLocalizedString("Border Color", comment: "")
- self.borderColorLabel.textColor = KMAppearance.Layout.h1Color()
- self.widthLabel.stringValue = NSLocalizedString("Line Width", comment: "")
- self.widthLabel.textColor = KMAppearance.Layout.h1Color()
- self.lineStyleLabel.stringValue = NSLocalizedString("Line Style", comment: "")
- self.lineStyleLabel.textColor = KMAppearance.Layout.h1Color()
- self.alternateColorLabel.stringValue = NSLocalizedString("Alternate", comment: "")
- self.alternateColorLabel.textColor = KMAppearance.Layout.h0Color()
- let styleBoxArr: [KMBox] = [self.styleBox0, self.styleBox1, self.styleBox2, self.styleBox3, self.styleBox4, self.styleBox5]
- for box in styleBoxArr {
- box.moveCallback = { mouseEntered, mouseBox in
- if mouseEntered {
- mouseBox.fillColor = KMAppearance.Status.selColor()
- } else {
- mouseBox.fillColor = NSColor.clear
- }
- }
- }
- self.widthPopUpButton.removeAllItems()
- 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"])
- self.widthPopUpButton.type = .arrowDown
- self.backgroudColorBox.borderWidth = 1.0
- self.backgroudColorBox.borderColor = KMAppearance.Interactive.s0Color()
- self.backgroudColorBox.fillColor = KMAppearance.Layout.l1Color()
- self.backgroudColorBox.cornerRadius = 1.0
- self.backgroudColorBox.downCallback = {downEntered, mouseBox, event in
- if downEntered {
- if self.backgroudColorBoxIsEnabled {
- NSColorPanel.shared.setTarget(self)
- NSColorPanel.shared.setAction(#selector(self.backgroudColorPanelColorDidChange(_:)))
- NSColorPanel.shared.orderFront(nil)
- } else {
- NSSound.beep()
- }
- }
- }
- self.backgrourdColorView.wantsLayer = true
- self.borderColorView.wantsLayer = true
- self.alternateColorView.wantsLayer = true
- self.backgrourdColorView.layer?.backgroundColor = NSColor.white.cgColor
- self.backgrourdColorView.layer?.borderWidth = 0.5
- self.borderColorView.layer?.borderWidth = 0.5
- self.alternateColorView.layer?.borderWidth = 0.5
-
- self.backgrourdColorView.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
- self.borderColorView.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
- self.alternateColorView.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
- self.borderColorBox.borderWidth = 1.0
- self.borderColorBox.borderColor = KMAppearance.Interactive.s0Color()
- self.borderColorBox.fillColor = KMAppearance.Layout.l1Color()
- self.borderColorBox.cornerRadius = 1.0
- self.borderColorBox.downCallback = { downEntered, mouseBox, event in
- if downEntered {
- NSColorPanel.shared.setTarget(self)
- NSColorPanel.shared.setAction(#selector(self.borderColorPanelColorDidChange(_:)))
- NSColorPanel.shared.orderFront(nil)
- }
- }
- self.alternateColorBox.borderWidth = 1.0
- self.alternateColorBox.borderColor = KMAppearance.Interactive.s0Color()
- self.alternateColorBox.fillColor = KMAppearance.Layout.l1Color()
- self.alternateColorBox.cornerRadius = 1.0
- self.alternateColorBox.downCallback = { downEntered, mouseBox, event in
- if downEntered {
- NSColorPanel.shared.setTarget(self)
- NSColorPanel.shared.setAction(#selector(self.alternateColorPanelColorDidChange(_:)))
- NSColorPanel.shared.orderFront(nil)
- }
- }
- self.lineStyleButton.image = self.borderStyleSolid(isSelect: false)
- self.lineStyleButton.wantsLayer = true
- self.dottedLineStyleButton.image = self.borderStyleDashed(isSelect: false)
- self.dottedLineStyleButton.wantsLayer = true
- self.rightArrowStyleButton.image = self.borderStyleDashed(isSelect: false)
- self.rightArrowStyleButton.wantsLayer = true
- let buttonArray: [KMButton] = [self.lineStyleButton, self.dottedLineStyleButton, self.rightArrowStyleButton]
- for button in buttonArray {
- button.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
- button.layer?.borderWidth = 1.0
- button.layer?.cornerRadius = 1.0
- if button.tag == self.lineStyleCount {
- button.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- } else {
- button.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
- }
- button.mouseMoveCallback = {[weak self] mouseEntered in
- if button.tag == self?.lineStyleCount {
- return
- }
- if mouseEntered {
- button.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- button.layer?.borderWidth = 0.0
- if button.tag == 0 {
- self?.lineStyleButton.image = self?.borderStyleSolid(isSelect: true)
- } else if button.tag == 1 {
- self?.dottedLineStyleButton.image = self?.borderStyleDashed(isSelect: true)
- }
- } else {
- button.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
- button.layer?.borderWidth = 1.0
- if button.tag == 0 {
- self?.lineStyleButton.image = self?.borderStyleSolid(isSelect: false)
- } else {
- self?.dottedLineStyleButton.image = self?.borderStyleDashed(isSelect: false)
- }
- }
- }
- }
- self.rightArrowStyleButton.isHidden = true
-
- self.sidebarPopUpButton.buttonImage = NSImage(named: KMImageNameUXIconPropertybarTableTitleLeft)
- self.sidebarPopUpButton.type = .table
- self.headerPopUpButton.buttonImage = NSImage(named: KMImageNameUXIconPropertybarTableTitleAbove)
- self.headerPopUpButton.type = .table;
-
- self.footerPopUpButton.buttonImage = NSImage(named: KMImageNameUXIconPropertybarTableBottom)
- self.footerPopUpButton.type = .table;
- self.cellWidthTextField.backgroundColor = KMAppearance.Layout.l1Color()
- self.cellHeightTextField.backgroundColor = KMAppearance.Layout.l1Color()
- self.cellWidthTextField.wantsLayer = true
- self.cellHeightTextField.wantsLayer = true
- self.sidebarPopUpButton.wantsLayer = true
- self.widthPopUpButton.wantsLayer = true
- self.headerPopUpButton.wantsLayer = true
- self.footerPopUpButton.wantsLayer = true
- self.cellWidthTextField.layer!.borderWidth = 1.0
- self.cellHeightTextField.layer?.borderWidth = 1.0
- self.sidebarPopUpButton.layer!.borderWidth = 1.0
- self.widthPopUpButton.layer!.borderWidth = 1.0
- self.headerPopUpButton.layer!.borderWidth = 1.0
- self.footerPopUpButton.layer!.borderWidth = 1.0
-
- self.cellWidthTextField.layer?.cornerRadius = 1.0
- self.cellHeightTextField.layer?.cornerRadius = 1.0
- self.sidebarPopUpButton.layer?.cornerRadius = 1.0
- self.widthPopUpButton.layer?.cornerRadius = 1.0
- self.headerPopUpButton.layer?.cornerRadius = 1.0
- self.updateViewColor()
- //
- NotificationCenter.default.addObserver(self, selector: #selector(PDFViewTableAnnotationDidChangeNotification(_:)), name: Notification.Name("KMPDFViewTableAnnotationDidChangeNotification"), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(controlTextDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: self.cellWidthTextField)
- NotificationCenter.default.addObserver(self, selector: #selector(controlTextDidEndEditingNotification(_:)), name: NSControl.textDidEndEditingNotification, object: self.cellHeightTextField)
- DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(_:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
- }
-
- override func viewWillAppear() {
- super.viewWillAppear()
-
- reloadData(with: self.point)
- }
-
- // MARK: Set & Get
-
- var annotations: [KMTableAnnotation] {
- set {
- _annotations = newValue
-
- self.annotation = newValue.first
- }
- get {
- return _annotations
- }
- }
-
- var annotationModel: CPDFAnnotationModel {
- get {
- return _annotationModel!
- }
- set {
- _annotationModel = newValue
- if _annotationModel?.annotations != nil {
- if annotationModel.annotations.count > 0 {
- annotations = _annotationModel?.annotations as! [KMTableAnnotation]
- }
- }
- }
- }
-
- // MARK: Private Methods
-
- func reloadData(with point: NSPoint) {
- let rect = annotation?.isInCell(with: point) ?? .zero
- if !NSEqualRects(rect, NSRectFromCGRect(CGRect.zero)) {
- var color = annotation?.getCellData(withRow: annotation!.currentCell.row, andColumn: annotation!.currentCell.column).backgroundColor
-
- var maxWidth: CGFloat = 10
- let minHeight: CGFloat = 10
-
- if annotation!.currentCell.column == annotation!.columnNumber - 1 {
- maxWidth = CGFloat(MAXFLOAT)
- } else {
- if let rect1 = annotation?.getCellBounds(withRow: annotation!.currentCell.row, column: annotation!.currentCell.column + 1) {
- maxWidth = rect1.size.width + rect.size.width - 10
- }
- }
-
- cellWidthStepper.maxValue = Double(maxWidth)
- cellHeightStepper.minValue = Double(minHeight)
-
- if color == nil {
- color = NSColor(deviceRed: 1, green: 1, blue: 1, alpha: 1)
- }
-
- backgroudColorBoxIsEnabled = true
- backgrourdColorView.wantsLayer = true
- backgrourdColorView.layer?.backgroundColor = color!.cgColor
- } else {
- backgroudColorBoxIsEnabled = false
- backgrourdColorView.wantsLayer = true
- backgrourdColorView.layer?.backgroundColor = NSColor.white.cgColor
- }
-
- self.setCell(width: rect.size.width, height: rect.size.height)
- var lineWidth: Float = 0.0
- if let data = self.annotation?.border {
- lineWidth = Float(data.lineWidth)
- } else {
- lineWidth = Float(self.annotation?.lineWidth() ?? 0)
- }
- self.widthSlider.floatValue = lineWidth
- let sliderString = String(format: "%.1f pt", lineWidth)
-
- if widthPopUpButton.itemTitles.contains(sliderString) {
- if let index = widthPopUpButton.itemTitles.firstIndex(of: sliderString) {
- widthPopUpButton.selectItem(at: index)
- }
- }
-
- widthSlider.toolTip = sliderString
-
- lineStyleCount = 0
-
- if annotation?.border?.style == .solid {
- lineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- lineStyleButton.layer?.borderWidth = 0.0
- lineStyleButton.image = borderStyleSolid(isSelect: true)
- dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
- dottedLineStyleButton.layer?.borderWidth = 1.0
- lineStyleButton.image = borderStyleSolid(isSelect: false)
- } else if annotation?.border?.style == .dashed {
- dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- dottedLineStyleButton.layer?.borderWidth = 0.0
- dottedLineStyleButton.image = borderStyleDashed(isSelect: true)
-
- lineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
- lineStyleButton.layer?.borderWidth = 1.0
- lineStyleButton.image = borderStyleSolid(isSelect: false)
-
- lineStyleCount = 1
- }
-
- borderColorView.wantsLayer = true
- borderColorView.layer?.backgroundColor = annotation?.color.cgColor
-
- if let alternateColor = annotation?.alternatingColor {
- alternateColorView.wantsLayer = true
- alternateColorView.layer?.backgroundColor = alternateColor.cgColor
- }
-
- alternateColorButton.state = (annotation?.isAlternateColor ?? false) ? .on : .off
-
- sidebarPopUpButton.selectItem(at: (annotation?.sidebarCount())!)
- headerPopUpButton.selectItem(at: (annotation?.headerCount())!)
- footerPopUpButton.selectItem(at: (annotation?.footerCount())!)
- }
- func setCell(width cellWidth: CGFloat, height cellHeight: CGFloat) {
- if cellWidth == 0 && cellHeight == 0 {
- cellWidthStepper.isEnabled = false
- cellHeightStepper.isEnabled = false
- cellWidthTextField.isEnabled = false
- cellHeightTextField.isEnabled = false
- } else {
- cellWidthStepper.isEnabled = true
- cellHeightStepper.isEnabled = true
- cellWidthTextField.isEnabled = true
- cellHeightTextField.isEnabled = true
- }
-
- let widthText = String(format: "%0.2f", cellWidth * 0.04)
- cellWidthTextField.stringValue = widthText
- cellWidthStepper.doubleValue = Double(cellWidth)
-
- let heightText = String(format: "%0.2f", cellHeight * 0.04)
- cellHeightTextField.stringValue = heightText
- cellHeightStepper.doubleValue = Double(cellHeight)
- }
-
- func reloadDataStepper(_ stepper: NSStepper) {
- guard let table = self.annotation else {
- return
- }
- if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
- let row = table.currentCell.row
- let column = table.currentCell.column
-
- if stepper == self.cellWidthStepper {
- let distance = self.cellWidthStepper.doubleValue - table.getCellBounds(withRow: row, column: column).size.width
- table.moveVerticalLine(of: column + 1, withDistance: distance)
- } else if stepper == self.cellHeightStepper {
- let distance = table.getCellBounds(withRow: row, column: column).size.height - self.cellHeightStepper.doubleValue
- table.moveCrossLine(of: table.rowNumber - row - 1, withDistance: distance)
- table.setOldRowHeights()
- }
-
- table.afreshDrawLine()
- table.updateAppearanceInk(withIsAdd: false)
- }
- pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
- }
-
- func colorWithCGColor(_ cgColor: CGColor?) -> NSColor? {
- guard let cgColor = cgColor else { return nil }
- return NSColor(cgColor: cgColor)
- }
-
- func borderStyleSolid(isSelect: Bool) -> NSImage {
- let size = NSSize(width: 49.0, height: 12.0)
-
- let image = NSImage.image(with: size) { rect in
- let path = NSBezierPath(rect: NSRect(x: 15.0, y: 5.0, width: 20.0, height: 1.0))
- path.lineWidth = 2.0
- if isSelect {
- KMAppearance.KM_273C62_ColorDarkChanged02().setStroke()
- } else {
- KMAppearance.Layout.h2Color().setStroke()
- }
- path.stroke()
- return true
- }
- return image
- }
-
- func borderStyleDashed(isSelect: Bool) -> NSImage {
- let size = NSSize(width: 49.0, height: 12.0)
- let image = NSImage(size: size) { rect in
- let path = NSBezierPath()
- path.move(to: NSPoint(x: 15.0, y: 5.0))
- path.line(to: NSPoint(x: 19.0, y: 5.0))
- path.move(to: NSPoint(x: 23.0, y: 5.0))
- path.line(to: NSPoint(x: 27.0, y: 5.0))
- path.move(to: NSPoint(x: 31.0, y: 5.0))
- path.line(to: NSPoint(x: 35.0, y: 5.0))
- path.lineWidth = 2.0
- if isSelect {
- KMAppearance.KM_273C62_ColorDarkChanged02().setStroke()
- } else {
- KMAppearance.Layout.h2Color().setStroke()
- }
- path.stroke()
- return true
- }
- return image!
- }
-
- //MARK: Button Actions
-
- @IBAction func styleButtonAction(_ sender: NSButton) {
- var tableStyle: KMTableStyle = .default
- if let button = sender as? NSButton {
- switch button.tag {
- case 300:
- tableStyle = .default
- case 301:
- tableStyle = .style1
- case 302:
- tableStyle = .style2
- case 303:
- tableStyle = .style3
- case 304:
- tableStyle = .style4
- case 305:
- tableStyle = .style5
- default:
- break
- }
- }
-
- annotation?.changeTableStyle(tableStyle)
- reloadData(with: point)
- pdfView?.needsDisplay = true
- }
-
- @IBAction func stepperAction(_ sender: Any) {
- guard let stepper = sender as? NSStepper else {
- return
- }
- var text: String
- switch stepper.tag {
- case 201:
- text = String(format: "%0.2f", self.cellWidthStepper.doubleValue * 0.04)
- self.cellWidthTextField.stringValue = text
- self.reloadDataStepper(self.cellWidthStepper)
- case 202:
- text = String(format: "%0.2f", self.cellHeightStepper.doubleValue * 0.04)
- self.cellHeightTextField.stringValue = text
- self.reloadDataStepper(self.cellHeightStepper)
- default:
- break
- }
- }
-
- @IBAction func colorWellAction(_ sender: Any) {
- guard let table = self.annotation else {
- return
- }
- table.borderWidth = CGFloat(self.widthSlider.floatValue)
- let sliderString = String(format: "%.1f pt", self.widthSlider.floatValue)
- if widthPopUpButton.itemTitles.contains(sliderString) {
- if let index = self.widthPopUpButton.itemTitles.firstIndex(of: sliderString) {
- self.widthPopUpButton.selectItem(at: index)
- }
- }
- table.color = self.colorWithCGColor(self.borderColorView.layer!.backgroundColor)
-
- if table.currentCell.row >= 0 && table.currentCell.column >= 0 {
- if self.backgroudColorBoxIsEnabled {
- if !self.notUpdateBackgroundColor {
- table.getCellData(withRow: table.currentCell.row, andColumn: table.currentCell.column).backgroundColor = self.colorWithCGColor(self.backgrourdColorView.layer?.backgroundColor)
- } else {
- var color = self.annotation?.getCellData(withRow: self.annotation!.currentCell.row, andColumn: self.annotation!.currentCell.column).backgroundColor
- if color == nil {
- color = NSColor(deviceRed: 1, green: 1, blue: 1, alpha: 1)
- }
- self.backgrourdColorView.layer?.backgroundColor = color?.cgColor
- }
- }
- }
-
- table.isAlternateColor = self.alternateColorButton.state == .on
- table.alternatingColor = self.colorWithCGColor(self.alternateColorView.layer!.backgroundColor)!
- table.writeFormDataToContents()
-
- if lineStyleCount == 1 {
- let lineType = table.dashPattern()
- if lineType.count == 0 {
- let number = NSNumber(value: Float(4))
- table.setDashPattern([number])
- }
- }
- table.setBorderStyle(CPDFBorderStyle(rawValue: lineStyleCount) ?? .solid)
-
- if self.notUpdateBackgroundColor {
- self.notUpdateBackgroundColor = false
- }
- pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
- }
- @IBAction func titleEndTablePopUpButtonAction(_ sender: KMPopUpButton) {
- guard let table = self.annotation else {
- return
- }
-
- let bounds = self.annotation!.bounds
- guard let popButton = sender as? KMPopUpButton else {
- return
- }
-
- switch popButton.tag {
- case 100:
- let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.sidebarCount()
- if count > 0 {
- for _ in 0..<count {
- table.insertColumnAtIndex(before: 0, with: .round)
- }
- } else if count < 0 {
- for _ in count..<0 {
- table.removeColumn(at: 0)
- }
- }
- table.bounds = NSRectFromCGRect(CGRect(x: table.bounds.origin.x - (bounds.size.width - table.bounds.size.width),
- y: table.bounds.origin.y,
- width: table.bounds.size.width, height: table.bounds.size.height))
- case 101:
- let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.headerCount()
- if count > 0 {
- for _ in 0..<count {
- table.insertRowAtIndex(before: 0, with: .round)
- }
- } else if count < 0 {
- for _ in count..<0 {
- table.removeRow(at: 0)
- }
- }
- table.bounds = NSRectFromCGRect(CGRect(x: table.bounds.origin.x,
- y: table.bounds.origin.y - (table.bounds.size.height - bounds.size.height),
- width: table.bounds.size.width, height: table.bounds.size.height))
- case 102:
- let count = (Int(popButton.titleOfSelectedItem ?? "0") ?? 0) - table.footerCount()
- if count > 0 {
- for _ in 0..<count {
- table.insertRow(atIndexBehind: table.rowNumber - 1, with: .bevel)
- }
- } else if count < 0 {
- for _ in count..<0 {
- table.removeRow(atIndexBehindLine: table.rowNumber - 1)
- }
- }
- default:
- break
- }
-
- table.drawLine(self.point)
- reloadData(with: self.point)
- pdfView?.setNeedsDisplayAnnotationViewFor(table.page)
- }
-
- @IBAction func widthPopUpButtonAction(_ sender: NSPopUpButton) {
- if let widthString = sender.titleOfSelectedItem, let width = Float(widthString.prefix(3)) {
- widthSlider.floatValue = width
- }
-
- colorWellAction(sender)
- }
- @objc func backgroudColorPanelColorDidChange(_ sender: Any) {
- guard NSColorPanel.sharedColorPanelExists, NSColorPanel.shared.isVisible else {
- return
- }
-
- if let color = (sender as? NSColorPanel)?.color {
- var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
- // if let rgbColor = color.usingColorSpace(NSColorSpace.sRGB) {
- // rgbColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- // }
- color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- backgrourdColorView.wantsLayer = true
- backgrourdColorView.layer?.backgroundColor = color.cgColor
-
- colorWellAction(sender)
- }
- }
-
- @objc func borderColorPanelColorDidChange(_ sender: Any) {
- guard NSColorPanel.sharedColorPanelExists, NSColorPanel.shared.isVisible else {
- return
- }
- if let color = (sender as? NSColorPanel)?.color {
- var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
- color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- borderColorView.wantsLayer = true
- borderColorView.layer?.backgroundColor = color.cgColor
- colorWellAction(sender)
- }
- }
- @objc func alternateColorPanelColorDidChange(_ sender: Any) {
- if let color = (sender as? NSColorPanel)?.color {
- var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
- color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- alternateColorView.wantsLayer = true
- alternateColorView.layer?.backgroundColor = color.cgColor
- colorWellAction(sender)
- }
- }
-
- @IBAction func lineTypeButtonAction(_ sender: NSButton) {
- let tag = sender.tag
-
- switch tag {
- case 0:
- lineStyleCount = 0
- lineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- lineStyleButton.layer?.borderWidth = 0.0
- dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
- dottedLineStyleButton.layer?.borderWidth = 1.0
-
- lineStyleButton.image = borderStyleSolid(isSelect: true)
- dottedLineStyleButton.image = borderStyleDashed(isSelect: false)
- colorWellAction(sender)
- case 1:
- lineStyleCount = 1
- lineStyleButton.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
- lineStyleButton.layer?.borderWidth = 1.0
- dottedLineStyleButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
- dottedLineStyleButton.layer?.borderWidth = 0.0
-
- lineStyleButton.image = borderStyleSolid(isSelect: false)
- dottedLineStyleButton.image = borderStyleDashed(isSelect: true)
- colorWellAction(sender)
- case 3:
- break
-
- default:
- break
- }
- }
-
- //MARK: Notification
-
- @objc func PDFViewTableAnnotationDidChangeNotification(_ notification: Notification) {
- if let pdfView = notification.object as? CPDFListView, pdfView == pdfView {
- self.notUpdateBackgroundColor = true
- if let pointValue = notification.userInfo?["point"] as? NSValue {
- let point = pointValue.pointValue
- self.point = point
- if NSColorPanel.sharedColorPanelExists {
- NSColorPanel.shared.close()
- }
- self.reloadData(with: point)
- }
- }
- }
-
- @objc func controlTextDidEndEditingNotification(_ notification: Notification) {
- guard let textField = notification.object as? NSTextField else {
- return
- }
- if textField == cellWidthTextField {
- let width = (textField.stringValue as NSString).doubleValue / 0.04
- if fabs(width - cellWidthStepper.doubleValue) < 0.01 {
- return
- }
- if width <= 0 {
- let text = String(format: "%0.2f", cellWidthStepper.doubleValue * 0.04)
- cellWidthTextField.stringValue = text
- } else if width > 0 && width < cellWidthStepper.minValue {
- let text = String(format: "%0.2f", cellWidthStepper.minValue * 0.04)
- cellWidthTextField.stringValue = text
- cellWidthStepper.doubleValue = cellWidthStepper.minValue
- reloadDataStepper(cellWidthStepper)
- } else if width >= cellWidthStepper.minValue && width <= cellWidthStepper.maxValue {
- let text = String(format: "%0.2f", (textField.stringValue as NSString).doubleValue)
- cellWidthTextField.stringValue = text
- cellWidthStepper.doubleValue = width
- reloadDataStepper(cellWidthStepper)
- } else {
- let text = String(format: "%0.2f", cellWidthStepper.maxValue * 0.04)
- cellWidthTextField.stringValue = text
- cellWidthStepper.doubleValue = cellWidthStepper.maxValue
- reloadDataStepper(cellWidthStepper)
- }
- } else if textField == cellHeightTextField {
- let height = (textField.stringValue as NSString).doubleValue / 0.04
- if height == cellHeightStepper.doubleValue {
- return
- }
- if height <= 0 {
- let text = String(format: "%0.2f", cellHeightStepper.doubleValue * 0.04)
- cellHeightTextField.stringValue = text
- } else if height > 0 && height < cellHeightStepper.minValue {
- let text = String(format: "%0.2f", cellHeightStepper.minValue * 0.04)
- cellHeightTextField.stringValue = text
- cellHeightStepper.doubleValue = cellHeightStepper.minValue
- reloadDataStepper(cellHeightStepper)
- } else {
- let text = String(format: "%0.2f", (textField.stringValue as NSString).doubleValue)
- cellHeightTextField.stringValue = text
- cellHeightStepper.doubleValue = height
- reloadDataStepper(cellHeightStepper)
- }
- }
- }
-
- @objc func themeChanged(_ notification: Notification) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
- self?.updateViewColor()
- }
- }
- func updateViewColor() {
- if KMAppearance.isDarkMode() {
- widthPopUpButton.layer?.backgroundColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
- cellWidthTextField.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
- cellHeightTextField.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
- sidebarPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
- widthPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
- headerPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
- footerPopUpButton.layer?.borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
- } else {
- widthPopUpButton.layer?.backgroundColor = NSColor.white.cgColor
- cellWidthTextField.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
- cellHeightTextField.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
- sidebarPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
- widthPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
- headerPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
- footerPopUpButton.layer?.borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
- }
- }
- }
|