123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- //
- // KMPenController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/26.
- //
- import Cocoa
- import KMComponentLibrary
- class KMPenController: NSViewController {
- //Color
- @IBOutlet var colorBGView: NSView!
- @IBOutlet var colorLabel: NSTextField!
- @IBOutlet var colorGroup: ComponentCColorGroup!
- @IBOutlet var colorSlider: ComponentSlider!
- @IBOutlet var colorOpacitySelect: ComponentSelect!
-
- //Line
- @IBOutlet var lineBGView: NSView!
- @IBOutlet var lineLabel: NSTextField!
- @IBOutlet var lineTypeSelector: ComponentCSelectorGroup!
- @IBOutlet var lineWidthSlider: ComponentSlider!
- @IBOutlet var lineWidthSelect: ComponentSelect!
-
-
-
- private var annotations: [CPDFAnnotation] = []
-
- var pdfView: CPDFListView?
-
- //MARK: - func
- override func viewDidAppear() {
- super.viewDidAppear()
-
-
- colorSlider.reloadData()
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- setupProperty()
- }
-
- func setupProperty() {
-
- //Color
- colorLabel.stringValue = KMLocalizedString("Color")
- colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
- colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
-
- let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
- let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
- let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
- let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
- let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
- colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
- colorGroup.delegate = self
-
- colorSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
- colorSlider.delegate = self
-
- colorOpacitySelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- creatable: true,
- text: "100%",
- textUnit: "%",
- regexString: "0123456789%")
- if true {
- var opacityItems: [ComponentMenuitemProperty] = []
- for string in ["25%", "50%", "75%", "100%"] {
- let item = ComponentMenuitemProperty(type: .normal, text: string)
- opacityItems.append(item)
- }
- colorOpacitySelect.updateMenuItemsArr(opacityItems)
- }
- colorOpacitySelect.delegate = self
-
- //Line
- lineLabel.stringValue = KMLocalizedString("Line")
- lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
- lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
-
- lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
- lineWidthSlider.delegate = self
-
- lineWidthSelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- creatable: true,
- text: "2",
- textUnit: " pt",
- regexString: "0123456789 pt")
- if true {
- var opacityItems: [ComponentMenuitemProperty] = []
- for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
- let item = ComponentMenuitemProperty(type: .normal, text: string)
- opacityItems.append(item)
- }
- lineWidthSelect.updateMenuItemsArr(opacityItems)
- }
- lineWidthSelect.delegate = self
-
- }
-
- func reloadData() {
- guard let pdfView = self.pdfView else {
- return
- }
- let manager = KMAnnotationPropertiesColorManager.manager
-
- self.annotations.removeAll()
- let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
- for annotation in allAnnotations {
- if annotation is CPDFMarkupAnnotation {
- annotations.append((annotation as! CPDFMarkupAnnotation))
- }
- }
- if annotations.count == 0 {
- return
- }
- let firstAnnotation = annotations.first
-
- if annotations.count == 1 {
- colorGroup.currentColor = firstAnnotation?.color
- colorGroup.refreshUI()
-
- let opacity = firstAnnotation?.opacity ?? 0
- colorSlider.properties.percent = opacity
- colorSlider.reloadData()
-
- colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
- colorOpacitySelect.reloadData()
-
-
- } else {
- var multiColor: Bool = false
- for annotationA in annotations {
- for annotationB in annotations {
- if annotationA != annotationB {
- if annotationA.color != annotationB.color {
- multiColor = true
- break
- }
- }
- }
- if multiColor == true {
- break
- }
- }
- if multiColor == true {
- colorGroup.currentColor = NSColor.clear
- } else {
- colorGroup.currentColor = firstAnnotation?.color
- }
- colorGroup.refreshUI()
-
- var multiOpacity: Bool = false
- for annotationA in annotations {
- for annotationB in annotations {
- if annotationA != annotationB {
- if annotationA.opacity != annotationB.opacity {
- multiOpacity = true
- break
- }
- }
- }
- if multiOpacity == true {
- break
- }
- }
-
- if multiOpacity {
- colorSlider.properties.percent = 0
- colorSlider.reloadData()
-
- colorOpacitySelect.resetText("-")
- } else {
- let opacity = firstAnnotation?.opacity ?? 0
-
- colorSlider.properties.percent = opacity
- colorSlider.reloadData()
-
- colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
- colorOpacitySelect.reloadData()
- }
- }
-
-
- }
-
- //MARK: - Mouse
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
-
- view.window?.makeFirstResponder(nil)
- }
-
-
- }
- //MARK: - ComponentCColorDelegate
- extension KMPenController: ComponentCColorDelegate {
- func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
-
-
- reloadData()
- }
- }
- //MARK: - ComponentSliderDelegate
- extension KMPenController: ComponentSliderDelegate {
- func componentSliderDidUpdate(_ view: ComponentSlider) {
- let percent = view.properties.percent
-
- reloadData()
- }
- }
- //MARK: - ComponentSelectDelegate
- extension KMPenController: ComponentSelectDelegate {
- func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
- if let result = text {
- let opacity = max(0, min(1, result.stringToCGFloat()/100))
-
- reloadData()
- }
- }
- }
|