|
@@ -0,0 +1,606 @@
|
|
|
+//
|
|
|
+// KMAnnotationFromViewController.swift
|
|
|
+// PDF Reader Pro
|
|
|
+//
|
|
|
+// Created by wanjun on 2024/1/18.
|
|
|
+//
|
|
|
+
|
|
|
+import Cocoa
|
|
|
+
|
|
|
+@objcMembers class KMAnnotationFromViewController: NSViewController {
|
|
|
+
|
|
|
+ private var _annotations: [CPDFWidgetAnnotation] = []
|
|
|
+ private var _formMode: CAnnotationType = .radioButton
|
|
|
+ var pdfView: CPDFListView?
|
|
|
+ var annotationModel: CPDFAnnotationModel?
|
|
|
+
|
|
|
+ @IBOutlet private var colorLabel: NSTextField!
|
|
|
+ @IBOutlet private var colorPickerView: KMColorPickerView!
|
|
|
+ @IBOutlet private var fillColorPickerView: KMColorPickerView!
|
|
|
+
|
|
|
+ @IBOutlet private var fontViewBottom: NSLayoutConstraint!
|
|
|
+ @IBOutlet private var fontLabel: NSTextField!
|
|
|
+
|
|
|
+ @IBOutlet private var fontPopUpButton: KMPopUpButton!
|
|
|
+ @IBOutlet private var fontStylePopUpButton: KMPopUpButton!
|
|
|
+ @IBOutlet private var fontSizeComboBox: KMComboBox!
|
|
|
+
|
|
|
+ private var annotation: CPDFWidgetAnnotation?
|
|
|
+ private var isFromMode: Bool = false
|
|
|
+ private var annotationFont: NSFont?
|
|
|
+
|
|
|
+ deinit {
|
|
|
+ NotificationCenter.default.removeObserver(self)
|
|
|
+ DistributedNotificationCenter.default.removeObserver(self)
|
|
|
+ annotation = nil
|
|
|
+ colorPickerView.target = nil
|
|
|
+ colorPickerView.action = nil
|
|
|
+ fillColorPickerView.target = nil
|
|
|
+ fillColorPickerView.action = nil
|
|
|
+ NSFontManager.shared.target = nil
|
|
|
+ }
|
|
|
+
|
|
|
+ override func loadView() {
|
|
|
+ super.loadView()
|
|
|
+
|
|
|
+ colorLabel.stringValue = NSLocalizedString("Colors", comment: "")
|
|
|
+ colorLabel.textColor = KMAppearance.Layout.h0Color()
|
|
|
+ fontLabel.stringValue = NSLocalizedString("Fonts", comment: "")
|
|
|
+ fontLabel.textColor = KMAppearance.Layout.h0Color()
|
|
|
+
|
|
|
+ colorPickerView.noContentString = true
|
|
|
+ colorPickerView.annotationTypeString = NSLocalizedString("Text Color", comment: "")
|
|
|
+ fillColorPickerView.annotationTypeString = NSLocalizedString("Background Color", comment: "")
|
|
|
+ fillColorPickerView.noContentString = true
|
|
|
+
|
|
|
+ colorPickerView.annotationType = .fromColors
|
|
|
+ fillColorPickerView.annotationType = .fromFillColors
|
|
|
+ fillColorPickerView.isFillColor = true
|
|
|
+ colorPickerView.isCallColorPanelAction = true
|
|
|
+ fillColorPickerView.isCallColorPanelAction = true
|
|
|
+
|
|
|
+ fontPopUpButton.type = .arrowUpDown
|
|
|
+ fontStylePopUpButton.type = .arrowDown
|
|
|
+ fontSizeComboBox.type = .none
|
|
|
+ fontSizeComboBox.comboxRect = fontSizeComboBox.bounds
|
|
|
+
|
|
|
+ let views: [NSView] = [fontPopUpButton, fontStylePopUpButton, fontSizeComboBox]
|
|
|
+ for view in views {
|
|
|
+ view.wantsLayer = true
|
|
|
+ view.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
|
|
|
+ view.layer?.borderWidth = 1.0
|
|
|
+ view.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
|
|
|
+ view.layer?.cornerRadius = 1.0
|
|
|
+ }
|
|
|
+
|
|
|
+ updateViewColor()
|
|
|
+ reloadData()
|
|
|
+ }
|
|
|
+
|
|
|
+ override func viewDidAppear() {
|
|
|
+ super.viewDidAppear()
|
|
|
+
|
|
|
+ let showConvertDetails = KMPropertiesViewPopController.showChangeColorDetails()
|
|
|
+ if showConvertDetails, let document = self.view.window?.windowController?.document as? NSDocument, document.fileURL != nil {
|
|
|
+ KMPropertiesViewPopController.defaultManager().showChangeColorDetailsView(colorPickerView.firstButton)
|
|
|
+ }
|
|
|
+
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(_:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: Get & Set
|
|
|
+
|
|
|
+ var annotations: [CPDFWidgetAnnotation] {
|
|
|
+ get {
|
|
|
+ return _annotations
|
|
|
+ }
|
|
|
+ set {
|
|
|
+ _annotations = newValue
|
|
|
+ annotation = _annotations.first
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var formMode: CAnnotationType {
|
|
|
+ get {
|
|
|
+ return _formMode
|
|
|
+ }
|
|
|
+ set {
|
|
|
+ _formMode = newValue
|
|
|
+ isFromMode = true
|
|
|
+
|
|
|
+ let defaults = UserDefaults.standard
|
|
|
+ var annotation: CPDFWidgetAnnotation?
|
|
|
+ var backgroundColor: NSColor?
|
|
|
+ var fontColor: NSColor?
|
|
|
+
|
|
|
+ if formMode == .textField {
|
|
|
+ annotation = CPDFTextWidgetAnnotation(PDFListViewNoteWith: (pdfView?.document)!)
|
|
|
+ backgroundColor = defaults.color(forKey: SKAnnotationTextWidgetBackgroundColorKey) ?? NSColor.clear
|
|
|
+ annotation?.backgroundColor = backgroundColor
|
|
|
+ fontColor = defaults.color(forKey: SKAnnotationTextWidgetFontColorKey)
|
|
|
+ annotation?.fontColor = fontColor
|
|
|
+ (annotation as! CPDFTextWidgetAnnotation).alignment = NSTextAlignment(rawValue: defaults.integer(forKey: SKAnnotationTextWidgetAlignmenKey)) ?? .left
|
|
|
+ if let font = defaults.font(forNameKey: CAnnotationTextWidgetFontNameKey, sizeKey: CAnnotationTextWidgetFontSizeKey) {
|
|
|
+ annotation?.font = font
|
|
|
+ }
|
|
|
+ if let tAnnotation = annotation as? CPDFTextWidgetAnnotation {
|
|
|
+ (annotation as! CPDFTextWidgetAnnotation).isMultiline = defaults.bool(forKey: SKAnnotationTextMultilineKey)
|
|
|
+ }
|
|
|
+ } else if formMode == .listMenu {
|
|
|
+ annotation = CPDFChoiceWidgetAnnotation(PDFListViewNoteWith: (pdfView?.document)!, listChoice: true)
|
|
|
+ backgroundColor = defaults.color(forKey: SKAnnotationChoiceListWidgetBackgroundColorKey) ?? NSColor.clear
|
|
|
+ annotation?.backgroundColor = backgroundColor
|
|
|
+ fontColor = defaults.color(forKey: SKAnnotationChoiceListWidgetFontColorKey)
|
|
|
+ annotation?.fontColor = fontColor
|
|
|
+ if let font = defaults.font(forNameKey: SKAnnotationChoiceListWidgetFontNameKey, sizeKey: SKAnnotationChoiceListWidgetFontSizeKey) {
|
|
|
+ annotation?.font = font
|
|
|
+ }
|
|
|
+ } else if formMode == .comboBox {
|
|
|
+ annotation = CPDFChoiceWidgetAnnotation(PDFListViewNoteWith: (pdfView?.document)!, listChoice: false)
|
|
|
+ backgroundColor = defaults.color(forKey: SKAnnotationChoiceWidgetBackgroundColorKey) ?? NSColor.clear
|
|
|
+ annotation?.backgroundColor = backgroundColor
|
|
|
+ fontColor = defaults.color(forKey: SKAnnotationChoiceWidgetFontColorKey)
|
|
|
+ annotation?.fontColor = fontColor
|
|
|
+ if let font = defaults.font(forNameKey: SKAnnotationChoiceWidgetFontNameKey, sizeKey: SKAnnotationChoiceWidgetFontSizeKey) {
|
|
|
+ annotation?.font = font
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ annotation = CPDFWidgetAnnotation(document: (pdfView?.document)!)
|
|
|
+ }
|
|
|
+
|
|
|
+ self.annotations = [annotation].compactMap { $0 }
|
|
|
+ self.annotation = annotations.first
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: Private Method
|
|
|
+
|
|
|
+ func reloadData() {
|
|
|
+ if annotations.count > 0 {
|
|
|
+ if let presetValue = annotation?.buttonWidgetStateString() {
|
|
|
+ annotation?.setButtonWidgetStateString(presetValue)
|
|
|
+ }
|
|
|
+ var opacity: CGFloat = 1
|
|
|
+ let color = annotation!.backgroundColor
|
|
|
+ if color != nil {
|
|
|
+ color!.usingColorSpaceName(.calibratedRGB)?.getRed(nil, green:nil, blue: nil, alpha: &opacity)
|
|
|
+ }
|
|
|
+
|
|
|
+ let fontColor = self.annotation!.fontColor
|
|
|
+ if let isTextWidget = (annotation?.isKind(of: CPDFTextWidgetAnnotation.self)), isTextWidget {
|
|
|
+ fillColorPickerView!.color = color
|
|
|
+ colorPickerView.color = fontColor
|
|
|
+
|
|
|
+ fontSizeComboBox.stringValue = "\((annotation as! CPDFTextWidgetAnnotation).font.pointSize) pt"
|
|
|
+
|
|
|
+ DispatchQueue.global(qos: .default).async { [self] in
|
|
|
+ let fonts = NSFontManager.shared.availableFontFamilies
|
|
|
+ var selectedIndex = 0
|
|
|
+ if let family = (annotation as! CPDFTextWidgetAnnotation).font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
|
|
|
+ let style = (annotation as! CPDFTextWidgetAnnotation).font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
|
|
|
+
|
|
|
+ let menu = NSMenu()
|
|
|
+ for (index, fontName) in fonts.enumerated() {
|
|
|
+ if let font = NSFont(name: fontName, size: annotation!.fontSize()) {
|
|
|
+ let attributes: [NSAttributedString.Key: Any] = [.font: font]
|
|
|
+ let attributedString = NSAttributedString(string: fontName, attributes: attributes)
|
|
|
+ let item = NSMenuItem()
|
|
|
+ item.attributedTitle = attributedString
|
|
|
+ menu.addItem(item)
|
|
|
+
|
|
|
+ if (annotation as! CPDFTextWidgetAnnotation).font.fontName == font.fontName {
|
|
|
+ selectedIndex = index
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DispatchQueue.main.async { [self] in
|
|
|
+ fontPopUpButton.menu = menu
|
|
|
+ fontPopUpButton.selectItem(at: selectedIndex)
|
|
|
+ let selectedStyleIndex = setFontStyle(withFontName: family, currentStyle: style)
|
|
|
+ fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ annotationFont = (annotation as! CPDFTextWidgetAnnotation).font
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if let presetValue = annotationModel?.stringValue() {
|
|
|
+ annotationModel?.setStringValue(presetValue)
|
|
|
+ }
|
|
|
+ var opacity: CGFloat = 1
|
|
|
+ let color = annotationModel?.backgroundColor()
|
|
|
+ if color != nil {
|
|
|
+ color!.usingColorSpaceName(.calibratedRGB)?.getRed(nil, green:nil, blue: nil, alpha: &opacity)
|
|
|
+ }
|
|
|
+
|
|
|
+ let fontColor = annotationModel?.fontColor()
|
|
|
+ fillColorPickerView!.color = color
|
|
|
+ colorPickerView.color = fontColor
|
|
|
+ fontSizeComboBox.stringValue = "\(annotationModel?.fontSize() ?? 12.0) pt"
|
|
|
+
|
|
|
+ var annotationFont = NSFont.systemFont(ofSize: (annotationModel?.fontSize())!)
|
|
|
+ if annotationModel?.fontName() != nil {
|
|
|
+ annotationFont = NSFont(name: (annotationModel?.fontName())!, size: (annotationModel?.fontSize())!)!
|
|
|
+ }
|
|
|
+ DispatchQueue.global(qos: .default).async { [self] in
|
|
|
+ let fonts = NSFontManager.shared.availableFontFamilies
|
|
|
+ var selectedIndex = 0
|
|
|
+ let family = annotationFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String
|
|
|
+ let style = annotationFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String
|
|
|
+ let menu = NSMenu()
|
|
|
+ for (index, fontName) in fonts.enumerated() {
|
|
|
+ if let font = NSFont(name: fontName, size: (annotationModel?.fontSize())!) {
|
|
|
+ let attributes: [NSAttributedString.Key: Any] = [.font: font]
|
|
|
+ let attributedString = NSAttributedString(string: fontName, attributes: attributes)
|
|
|
+ let item = NSMenuItem()
|
|
|
+ item.attributedTitle = attributedString
|
|
|
+ menu.addItem(item)
|
|
|
+
|
|
|
+ if annotationModel?.fontName() != nil {
|
|
|
+ if annotationModel?.fontName() == font.fontName {
|
|
|
+ selectedIndex = index
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DispatchQueue.main.async { [self] in
|
|
|
+ fontPopUpButton.menu = menu
|
|
|
+ fontPopUpButton.selectItem(at: selectedIndex)
|
|
|
+ let selectedStyleIndex = setFontStyle(withFontName: family!, currentStyle: style)
|
|
|
+ fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self.annotationFont = annotationFont
|
|
|
+ }
|
|
|
+
|
|
|
+ let fontManager = NSFontManager.shared
|
|
|
+ fontManager.target = self
|
|
|
+ fontManager.action = #selector(changeFont(_:))
|
|
|
+ }
|
|
|
+
|
|
|
+ func updateAnnotationMode() {
|
|
|
+ let userDefaults = UserDefaults.standard
|
|
|
+ guard let annotation = self.annotation else { return }
|
|
|
+
|
|
|
+ if formMode == .textField {
|
|
|
+ userDefaults.setColor(annotation.backgroundColor, forKey: SKAnnotationTextWidgetBackgroundColorKey)
|
|
|
+ userDefaults.setColor(annotation.fontColor, forKey: SKAnnotationTextWidgetFontColorKey)
|
|
|
+ userDefaults.set((annotation as! CPDFTextWidgetAnnotation).alignment.rawValue, forKey: SKAnnotationTextWidgetAlignmenKey)
|
|
|
+ userDefaults.set(annotation.font.fontName, forKey: CAnnotationTextWidgetFontNameKey)
|
|
|
+ userDefaults.set(annotation.font.pointSize, forKey: CAnnotationTextWidgetFontSizeKey)
|
|
|
+ userDefaults.set((annotation as! CPDFTextWidgetAnnotation).isMultiline, forKey: SKAnnotationTextMultilineKey)
|
|
|
+ } else if formMode == .comboBox {
|
|
|
+ userDefaults.setColor(annotation.backgroundColor, forKey: SKAnnotationChoiceWidgetBackgroundColorKey)
|
|
|
+ userDefaults.setColor(annotation.fontColor, forKey: SKAnnotationChoiceWidgetFontColorKey)
|
|
|
+ userDefaults.set(annotation.font.fontName, forKey: SKAnnotationChoiceWidgetFontNameKey)
|
|
|
+ userDefaults.set(annotation.font.pointSize, forKey: SKAnnotationChoiceWidgetFontSizeKey)
|
|
|
+ } else if formMode == .comboBox {
|
|
|
+ userDefaults.setColor(annotation.backgroundColor, forKey: SKAnnotationChoiceListWidgetBackgroundColorKey)
|
|
|
+ userDefaults.setColor(annotation.fontColor, forKey: SKAnnotationChoiceListWidgetFontColorKey)
|
|
|
+ userDefaults.set(annotation.font.fontName, forKey: SKAnnotationChoiceListWidgetFontNameKey)
|
|
|
+ userDefaults.set(annotation.font.pointSize, forKey: SKAnnotationChoiceListWidgetFontSizeKey)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func colorWithCGColor(_ CGColor: CGColor?) -> NSColor? {
|
|
|
+ guard let CGColor = CGColor else { return nil }
|
|
|
+ return NSColor(cgColor: CIColor(cgColor: CGColor) as! CGColor)
|
|
|
+ }
|
|
|
+
|
|
|
+ private func updateAnnotation() {
|
|
|
+ if annotationModel?.annotation != nil {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ pdfView?.setNeedsDisplayAnnotationViewFor(tAnnotation.page)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func setFontStyle(withFontName fontName: String, currentStyle style: String?) -> UInt {
|
|
|
+ var selectIndex: UInt = 0
|
|
|
+ let menu = NSMenu()
|
|
|
+ let fontFamily = NSFontManager.shared.availableMembers(ofFontFamily: fontName)
|
|
|
+ if fontFamily != nil {
|
|
|
+ for (index, array) in fontFamily!.enumerated() {
|
|
|
+ let styleName = array[1]
|
|
|
+ if let currentStyle = style, styleName as! String == currentStyle {
|
|
|
+ selectIndex = UInt(index)
|
|
|
+ }
|
|
|
+ let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: fontName, NSFontDescriptor.AttributeName.face: styleName])
|
|
|
+ if let font = NSFont(descriptor: attributeFontDescriptor, size: 12.0) {
|
|
|
+ let attrited = [NSAttributedString.Key.font: font]
|
|
|
+ let string = NSAttributedString(string: styleName as! String, attributes: attrited)
|
|
|
+ let item = NSMenuItem()
|
|
|
+ item.attributedTitle = string
|
|
|
+ menu.addItem(item)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if style == nil {
|
|
|
+ selectIndex = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ fontStylePopUpButton.menu = menu
|
|
|
+ return selectIndex
|
|
|
+ }
|
|
|
+
|
|
|
+ func changeStoredFontInfo(_ sender: Any) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+// if let font = (sender as AnyObject).convertFont(tAnnotation.font) {
|
|
|
+// tAnnotation.font = font
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func changeFont(_ sender: Any) {
|
|
|
+// if let convertedFont = (sender as AnyObject).convertFont(annotationFont) {
|
|
|
+// annotationFont = convertedFont
|
|
|
+// }
|
|
|
+
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ var annotationString = ""
|
|
|
+
|
|
|
+ if tAnnotation.buttonWidgetStateString().isEmpty {
|
|
|
+ annotationString = " "
|
|
|
+ } else {
|
|
|
+ let lastString = String(tAnnotation.buttonWidgetStateString().suffix(1))
|
|
|
+ if lastString == " " {
|
|
|
+ annotationString = String(tAnnotation.buttonWidgetStateString().prefix(tAnnotation.buttonWidgetStateString().count - 1))
|
|
|
+ } else {
|
|
|
+ annotationString = "\(tAnnotation.buttonWidgetStateString()) "
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ tAnnotation.font = annotationFont
|
|
|
+ tAnnotation.setButtonWidgetStateString(annotationString)
|
|
|
+ }
|
|
|
+
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: UI Action
|
|
|
+
|
|
|
+ @IBAction func colorPickerViewAction(_ sender: Any) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ var annotationString = ""
|
|
|
+ if tAnnotation.buttonWidgetStateString().isEmpty {
|
|
|
+ annotationString = " "
|
|
|
+ } else {
|
|
|
+ let lastString = String(tAnnotation.buttonWidgetStateString().suffix(1))
|
|
|
+ if lastString == " " {
|
|
|
+ annotationString = String(tAnnotation.buttonWidgetStateString().prefix(tAnnotation.buttonWidgetStateString().count - 1))
|
|
|
+ } else {
|
|
|
+ annotationString = "\(tAnnotation.buttonWidgetStateString()) "
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tAnnotation.fontColor = colorPickerView.color
|
|
|
+ tAnnotation.setButtonWidgetStateString(annotationString)
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fillColorPickerViewAction(_ sender: Any) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+// tAnnotation.removeAllAppearanceStreams()
|
|
|
+ tAnnotation.backgroundColor = fillColorPickerView.color
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fontSizeComboBoxAction(_ sender: Any) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ var annotationString = ""
|
|
|
+ if tAnnotation.buttonWidgetStateString().isEmpty {
|
|
|
+ annotationString = " "
|
|
|
+ } else {
|
|
|
+ let lastString = String(tAnnotation.buttonWidgetStateString().suffix(1))
|
|
|
+ if lastString == " " {
|
|
|
+ annotationString = String(tAnnotation.buttonWidgetStateString().prefix(tAnnotation.buttonWidgetStateString().count - 1))
|
|
|
+ } else {
|
|
|
+ annotationString = "\(tAnnotation.buttonWidgetStateString()) "
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // tAnnotation.removeAllAppearanceStreams()
|
|
|
+ if let font = tAnnotation.font {
|
|
|
+ tAnnotation.font = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
|
|
|
+ }
|
|
|
+ tAnnotation.setButtonWidgetStateString(annotationString)
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fontColorButtonAction(_ sender: NSButton) {
|
|
|
+ NSColorPanel.shared.setTarget(self)
|
|
|
+ NSColorPanel.shared.setAction(#selector(colorPanelAction(_:)))
|
|
|
+ NSColorPanel.shared.orderFront(nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func colorPanelAction(_ sender: Any) {
|
|
|
+ let color = NSColorPanel.shared.color
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ var annotationString = ""
|
|
|
+ if tAnnotation.buttonWidgetStateString().isEmpty {
|
|
|
+ annotationString = " "
|
|
|
+ } else {
|
|
|
+ let lastString = String(tAnnotation.buttonWidgetStateString().suffix(1))
|
|
|
+ if lastString == " " {
|
|
|
+ annotationString = String(tAnnotation.buttonWidgetStateString().prefix(tAnnotation.buttonWidgetStateString().count - 1))
|
|
|
+ } else {
|
|
|
+ annotationString = "\(tAnnotation.buttonWidgetStateString()) "
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ tAnnotation.fontColor = color
|
|
|
+ tAnnotation.setButtonWidgetStateString(annotationString)
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func currentFontColorButtonAction(_ sender: NSButton) {
|
|
|
+ if let color = colorWithCGColor(sender.layer?.backgroundColor) {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+// tAnnotation.removeAllAppearanceStreams()
|
|
|
+ tAnnotation.fontColor = color
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
|
|
|
+ if let selectedItem = fontPopUpButton.selectedItem {
|
|
|
+ let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
|
|
|
+ let familyString = resultAtt.string
|
|
|
+ let selectIndex = setFontStyle(withFontName: familyString, currentStyle: nil)
|
|
|
+ let styleString = fontStylePopUpButton.selectedItem?.title ?? ""
|
|
|
+ fontStylePopUpButton.selectItem(at: Int(selectIndex))
|
|
|
+
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ var annotationString = ""
|
|
|
+
|
|
|
+ if tAnnotation.buttonWidgetStateString().isEmpty {
|
|
|
+ annotationString = " "
|
|
|
+ } else {
|
|
|
+ let lastString = String(tAnnotation.buttonWidgetStateString().suffix(1))
|
|
|
+ if lastString == " " {
|
|
|
+ annotationString = String(tAnnotation.buttonWidgetStateString().prefix(tAnnotation.buttonWidgetStateString().count - 1))
|
|
|
+ } else {
|
|
|
+ annotationString = tAnnotation.buttonWidgetStateString() + " "
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if let font = (annotation as! CPDFTextWidgetAnnotation).font {
|
|
|
+ let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? CGFloat ?? 12.0
|
|
|
+
|
|
|
+ let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
|
|
|
+ if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize) {
|
|
|
+ tAnnotation.font = newFont
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ tAnnotation.setButtonWidgetStateString(annotationString)
|
|
|
+ }
|
|
|
+
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
|
|
|
+ guard let styleString = sender.selectedItem?.title else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ var annotationString = ""
|
|
|
+ if tAnnotation.buttonWidgetStateString().isEmpty {
|
|
|
+ annotationString = " "
|
|
|
+ } else {
|
|
|
+ let lastString = String(tAnnotation.buttonWidgetStateString().suffix(1))
|
|
|
+ if lastString == " " {
|
|
|
+ annotationString = String(tAnnotation.buttonWidgetStateString().dropLast())
|
|
|
+ } else {
|
|
|
+ annotationString = "\(tAnnotation.buttonWidgetStateString()) "
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if let font = tAnnotation.font {
|
|
|
+ let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? NSNumber
|
|
|
+ let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String
|
|
|
+
|
|
|
+ let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString as Any, NSFontDescriptor.AttributeName.face: styleString])
|
|
|
+ if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize?.floatValue ?? 12.0)) {
|
|
|
+ tAnnotation.font = newFont
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tAnnotation.setButtonWidgetStateString(annotationString)
|
|
|
+ }
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func buttonClicked_ChangeFont(_ sender: Any) {
|
|
|
+ let fontWindowController = KMAnnotationFontWindowController.sharedAnnotationFont
|
|
|
+ let window = fontWindowController.window
|
|
|
+ if fontWindowController != nil && window != nil {
|
|
|
+ fontWindowController.annotations = annotations
|
|
|
+
|
|
|
+ fontWindowController.annotationAlignCallback = { [weak self] selectedCount in
|
|
|
+ guard let self = self else { return }
|
|
|
+
|
|
|
+ for tAnnotation in self.annotations {
|
|
|
+ var annotationString = ""
|
|
|
+
|
|
|
+
|
|
|
+ if tAnnotation.buttonWidgetStateString().isEmpty {
|
|
|
+ annotationString = " "
|
|
|
+ } else {
|
|
|
+ let lastString = String(tAnnotation.buttonWidgetStateString().suffix(1))
|
|
|
+
|
|
|
+ if lastString == " " {
|
|
|
+ annotationString = String(tAnnotation.buttonWidgetStateString().prefix(tAnnotation.buttonWidgetStateString().count - 1))
|
|
|
+ } else {
|
|
|
+ annotationString = "\(tAnnotation.buttonWidgetStateString()) "
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ switch selectedCount {
|
|
|
+ case 0:
|
|
|
+ (tAnnotation as! CPDFTextWidgetAnnotation).alignment = .left
|
|
|
+ case 2:
|
|
|
+ (tAnnotation as! CPDFTextWidgetAnnotation).alignment = .center
|
|
|
+ case 1:
|
|
|
+ (tAnnotation as! CPDFTextWidgetAnnotation).alignment = .right
|
|
|
+ case 3:
|
|
|
+ (tAnnotation as! CPDFTextWidgetAnnotation).alignment = .justified
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ tAnnotation.setButtonWidgetStateString(annotationString)
|
|
|
+ }
|
|
|
+
|
|
|
+ updateAnnotation()
|
|
|
+ }
|
|
|
+
|
|
|
+// fontWindowController.annotationCallback = {
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+ window!.orderFront(sender)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: NSNotification Action
|
|
|
+
|
|
|
+ @objc func themeChanged(_ notification: Notification) {
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
|
|
|
+ self?.updateViewColor()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func updateViewColor() {
|
|
|
+ if KMAppearance.isDarkMode() {
|
|
|
+ let darkModeColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
|
|
|
+
|
|
|
+ fontPopUpButton.layer?.backgroundColor = darkModeColor
|
|
|
+ fontStylePopUpButton.layer?.backgroundColor = darkModeColor
|
|
|
+ fontSizeComboBox.layer?.backgroundColor = darkModeColor
|
|
|
+
|
|
|
+ fontPopUpButton.layer?.borderColor = darkModeColor
|
|
|
+ fontStylePopUpButton.layer?.borderColor = darkModeColor
|
|
|
+ fontSizeComboBox.layer?.borderColor = darkModeColor
|
|
|
+ } else {
|
|
|
+ let lightModeColor = NSColor.white.cgColor
|
|
|
+ let borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
|
|
|
+
|
|
|
+ fontPopUpButton.layer?.backgroundColor = lightModeColor
|
|
|
+ fontStylePopUpButton.layer?.backgroundColor = lightModeColor
|
|
|
+ fontSizeComboBox.layer?.backgroundColor = lightModeColor
|
|
|
+
|
|
|
+ fontPopUpButton.layer?.borderColor = borderColor
|
|
|
+ fontStylePopUpButton.layer?.borderColor = borderColor
|
|
|
+ fontSizeComboBox.layer?.borderColor = borderColor
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|