// // KMRectangleController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/11/26. // import Cocoa import KMComponentLibrary class KMRectangleController: NSViewController { //Color @IBOutlet var colorBGView: NSView! @IBOutlet var colorLabel: NSTextField! @IBOutlet var colorGroup: ComponentCColorGroup! @IBOutlet var colorSlider: ComponentSlider! @IBOutlet var colorOpacitySelect: ComponentSelect! @IBOutlet var fillColorGroup: ComponentCColorGroup! //Line @IBOutlet var lineBGView: NSView! @IBOutlet var lineLabel: NSTextField! @IBOutlet var lineTypeSelector: ComponentCSelectorGroup! @IBOutlet var lineWidthSlider: ComponentSlider! @IBOutlet var lineWidthSelect: ComponentSelect! @IBOutlet var linedashInfoView: NSView! @IBOutlet var lineDashSlider: ComponentSlider! @IBOutlet var lineDashSelect: ComponentSelect! private let solidProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_solid")) private let dashProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_dash")) private var annotations: [CPDFAnnotation] = [] //CPDFCircleAnnotation, CPDFSquareAnnotation private var circleAnnotations: [CPDFCircleAnnotation] { get { var validAnnotations: [CPDFCircleAnnotation] = [] for annotation in annotations { if annotation is CPDFCircleAnnotation { validAnnotations.append((annotation as! CPDFCircleAnnotation)) } } return validAnnotations } } private var squareAnnotations: [CPDFSquareAnnotation] { get { var validAnnotations: [CPDFSquareAnnotation] = [] for annotation in annotations { if annotation is CPDFSquareAnnotation { validAnnotations.append((annotation as! CPDFSquareAnnotation)) } } return validAnnotations } } var pdfView: CPDFListView? var viewManager: KMPDFViewManager? //MARK: - func override func viewDidAppear() { super.viewDidAppear() colorSlider.reloadData() lineWidthSlider.reloadData() lineDashSlider.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") colorGroup.delegate = self fillColorGroup.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") lineTypeSelector.updateItemProperty([solidProperty, dashProperty]) lineTypeSelector.delegate = self 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 lineDashSlider.properties = ComponentSliderProperty(size: .m, percent: 1) lineDashSlider.delegate = self lineDashSelect.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) } lineDashSelect.updateMenuItemsArr(opacityItems) } lineDashSelect.delegate = self } func reloadData() { guard let pdfView = self.pdfView else { return } var firstAnnotation: CPDFAnnotation? self.annotations.removeAll() let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? [] for annotation in allAnnotations { if annotation is CPDFCircleAnnotation { annotations.append((annotation as! CPDFCircleAnnotation)) } else if annotation is CPDFSquareAnnotation { annotations.append((annotation as! CPDFSquareAnnotation)) } } if annotations.count > 0 { firstAnnotation = annotations.first } if true { var colors: [NSColor] = [] var fillColors: [NSColor] = [] if let annotation = firstAnnotation { if annotation is CPDFSquareAnnotation { colors = KMAnnotationPropertiesColorManager.manager.rectangle_Colors fillColors = KMAnnotationPropertiesColorManager.manager.rectangle_Fill_Colors } else if annotation is CPDFCircleAnnotation { colors = KMAnnotationPropertiesColorManager.manager.circle_Colors fillColors = KMAnnotationPropertiesColorManager.manager.circle_Fill_Colors } } else if viewManager?.subToolMode == .Rectangle { colors = KMAnnotationPropertiesColorManager.manager.rectangle_Colors fillColors = KMAnnotationPropertiesColorManager.manager.rectangle_Fill_Colors } else if viewManager?.subToolMode == .Circle { colors = KMAnnotationPropertiesColorManager.manager.circle_Colors fillColors = KMAnnotationPropertiesColorManager.manager.circle_Fill_Colors } if colors.count > 4 { let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0]) let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1]) let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2]) let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3]) let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4]) colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty) } if fillColors.count > 4 { let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: fillColors[0]) let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: fillColors[1]) let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: fillColors[2]) let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: fillColors[3]) let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: fillColors[4]) fillColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty) } } if annotations.count == 0 { if viewManager?.subToolMode == .Rectangle { colorGroup.currentColor = CPDFSquareAnnotation.defaultColor() fillColorGroup.currentColor = CPDFSquareAnnotation.defaultInteriorColor() let opacity = CPDFSquareAnnotation.defaultOpacity() colorSlider.properties.percent = opacity colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%") let border: CPDFBorder = CPDFSquareAnnotation.defaultBorder() dashProperty.state = .normal solidProperty.state = .normal if border.style == .dashed { dashProperty.state = .pressed } else if border.style == .solid { solidProperty.state = .pressed } let percent = (border.lineWidth - 1)/17 lineWidthSlider.properties.percent = percent lineWidthSlider.reloadData() lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt") lineWidthSelect.reloadData() linedashInfoView.isHidden = true if border.style == .dashed { linedashInfoView.isHidden = false var dash = 1.0 for dashPattern in border.dashPattern { if let value = dashPattern as? CGFloat { dash = value break } } let percent: CGFloat = (CGFloat(dash) - 1)/17 lineDashSlider.properties.percent = percent lineDashSlider.reloadData() lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt") lineDashSelect.reloadData() } } else if viewManager?.subToolMode == .Circle { colorGroup.currentColor = CPDFCircleAnnotation.defaultColor() fillColorGroup.currentColor = CPDFCircleAnnotation.defaultInteriorColor() let opacity = CPDFCircleAnnotation.defaultOpacity() colorSlider.properties.percent = opacity colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%") let border: CPDFBorder = CPDFCircleAnnotation.defaultBorder() dashProperty.state = .normal solidProperty.state = .normal if border.style == .dashed { dashProperty.state = .pressed } else if border.style == .solid { solidProperty.state = .pressed } let percent = (border.lineWidth - 1)/17 lineWidthSlider.properties.percent = percent lineWidthSlider.reloadData() lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt") lineWidthSelect.reloadData() linedashInfoView.isHidden = true if border.style == .dashed { linedashInfoView.isHidden = false var dash = 1.0 for dashPattern in border.dashPattern { if let value = dashPattern as? CGFloat { dash = value break } } let percent: CGFloat = (CGFloat(dash) - 1)/17 lineDashSlider.properties.percent = percent lineDashSlider.reloadData() lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt") lineDashSelect.reloadData() } } } else if annotations.count == 1, let annotation = firstAnnotation { colorGroup.currentColor = annotation.color if firstAnnotation is CPDFCircleAnnotation { fillColorGroup.currentColor = (firstAnnotation as! CPDFCircleAnnotation).interiorColor } else if firstAnnotation is CPDFSquareAnnotation { fillColorGroup.currentColor = (firstAnnotation as! CPDFSquareAnnotation).interiorColor } let opacity = annotation.opacity colorSlider.properties.percent = opacity colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%") let border: CPDFBorder = annotation.border dashProperty.state = .normal solidProperty.state = .normal if border.style == .dashed { dashProperty.state = .pressed } else if border.style == .solid { solidProperty.state = .pressed } let percent = (border.lineWidth - 1)/17 lineWidthSlider.properties.percent = percent lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt") linedashInfoView.isHidden = true if border.style == .dashed { linedashInfoView.isHidden = false var dash = 1.0 for dashPattern in border.dashPattern { if let value = dashPattern as? CGFloat { dash = value break } } let percent: CGFloat = (CGFloat(dash) - 1)/17 lineDashSlider.properties.percent = percent lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt") } } else { guard let annotation = firstAnnotation else { return } if true { let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .color) if multiColor == true { colorGroup.currentColor = NSColor.clear } else { colorGroup.currentColor = annotation.color } } if true { let multiColor: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .interiorColor) if multiColor == true { fillColorGroup.currentColor = NSColor.clear } else { if firstAnnotation is CPDFSquareAnnotation { fillColorGroup.currentColor = (firstAnnotation as! CPDFSquareAnnotation).interiorColor } else if firstAnnotation is CPDFCircleAnnotation { fillColorGroup.currentColor = (firstAnnotation as! CPDFCircleAnnotation).interiorColor } } } if true { let multiOpacity: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .opacity) if multiOpacity { colorSlider.properties.percent = 0 colorOpacitySelect.resetText("-") } else { let opacity = annotation.opacity colorSlider.properties.percent = opacity colorSlider.reloadData() colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%") colorOpacitySelect.reloadData() } } if true { let multiStyle: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .border_Style) linedashInfoView.isHidden = true if multiStyle { dashProperty.state = .normal solidProperty.state = .normal lineTypeSelector.reloadData() } else { let style = annotation.border.style dashProperty.state = .normal solidProperty.state = .normal if style == .dashed { dashProperty.state = .pressed } else if style == .solid { solidProperty.state = .pressed } lineTypeSelector.reloadData() if style == .dashed { linedashInfoView.isHidden = false } } } if true { let multiLineWidth: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .line_Width) if multiLineWidth { lineWidthSlider.properties.percent = 0 lineWidthSlider.reloadData() lineWidthSelect.resetText("-") } else { let border: CPDFBorder = annotation.border let percent = (border.lineWidth - 1)/17 lineWidthSlider.properties.percent = percent lineWidthSlider.reloadData() lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt") lineWidthSelect.reloadData() } } if true { let multiLineDash: Bool = CPDFListView.isAnnotationsContainMultiType(annotations, withType: .dash_Pattern) if multiLineDash { lineDashSlider.properties.percent = 0 lineDashSlider.reloadData() lineDashSelect.resetText("-") } else { var dashA = 1.0 for dashPattern in annotation.border.dashPattern { if let value = dashPattern as? CGFloat { dashA = value break } } let percent = (dashA - 1)/17 lineDashSlider.properties.percent = percent lineDashSlider.reloadData() lineDashSelect.properties.text = String(format: "%.0f%@", dashA, " pt") lineDashSelect.reloadData() } } } colorGroup.refreshUI() fillColorGroup.refreshUI() colorSlider.reloadData() colorOpacitySelect.reloadData() lineTypeSelector.reloadData() lineWidthSlider.reloadData() lineWidthSelect.reloadData() lineDashSlider.reloadData() lineDashSelect.reloadData() } //MARK: - Mouse override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) view.window?.makeFirstResponder(nil) } } //MARK: - ComponentCColorDelegate extension KMRectangleController: ComponentCColorDelegate { func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) { if view == colorGroup { if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateColor(circleAnnotations, color, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateColor(squareAnnotations, color, withPDFView: pdfView) } } else if view == fillColorGroup { if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateInteriorColor(circleAnnotations, color, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateInteriorColor(squareAnnotations, color, withPDFView: pdfView) } } reloadData() } } //MARK: - ComponentSliderDelegate extension KMRectangleController: ComponentSliderDelegate { func componentSliderDidUpdate(_ view: ComponentSlider) { if view == colorSlider { let percent = view.properties.percent if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateOpacity(circleAnnotations, percent, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateOpacity(squareAnnotations, percent, withPDFView: pdfView) } } else if view == lineWidthSlider { let percent = view.properties.percent * 17 + 1 if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateLineWidth(circleAnnotations, percent, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateLineWidth(squareAnnotations, percent, withPDFView: pdfView) } } else if view == lineDashSlider { let percent = view.properties.percent * 17 + 1 if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle{ CPDFCircleAnnotation.updateDashPattern(circleAnnotations, percent, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateDashPattern(squareAnnotations, percent, withPDFView: pdfView) } } reloadData() } } //MARK: - ComponentSelectDelegate extension KMRectangleController: ComponentSelectDelegate { func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) { if let result = text { if view == colorOpacitySelect { let opacity = max(0, min(1, result.stringToCGFloat()/100)) if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateOpacity(circleAnnotations, opacity, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateOpacity(squareAnnotations, opacity, withPDFView: pdfView) } } else if view == lineWidthSelect { var value = result.stringToCGFloat() if value > 18 { value = 18 } else if value < 1 { value = 1 } if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle{ CPDFCircleAnnotation.updateLineWidth(circleAnnotations, value, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateLineWidth(squareAnnotations, value, withPDFView: pdfView) } } else if view == lineDashSelect { var value = result.stringToCGFloat() if value > 18 { value = 18 } else if value < 1 { value = 1 } if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateDashPattern(circleAnnotations, value, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateDashPattern(squareAnnotations, value, withPDFView: pdfView) } } reloadData() } } func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) { if var result = menuItemProperty?.text { if let textUnit = view?.properties.textUnit { result = result.stringByDeleteCharString(textUnit) } if view == colorOpacitySelect { let opacity = max(0, min(1, result.stringToCGFloat()/100)) if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateOpacity(circleAnnotations, opacity, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateOpacity(squareAnnotations, opacity, withPDFView: pdfView) } } else if view == lineWidthSelect { var value = result.stringToCGFloat() if value > 18 { value = 18 } else if value < 1 { value = 1 } if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateLineWidth(circleAnnotations, value, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateLineWidth(squareAnnotations, value, withPDFView: pdfView) } } else if view == lineDashSelect { var value = result.stringToCGFloat() if value > 18 { value = 18 } else if value < 1 { value = 1 } if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateDashPattern(circleAnnotations, value, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateDashPattern(squareAnnotations, value, withPDFView: pdfView) } } reloadData() } } } //MARK: - ComponentCSelectorGroupDelegate extension KMRectangleController: ComponentCSelectorGroupDelegate { func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) { if item.properties == solidProperty { if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateStyle(circleAnnotations, .solid, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateStyle(squareAnnotations, .solid, withPDFView: pdfView) } } else if item.properties == dashProperty { if circleAnnotations.count > 0 || viewManager?.subToolMode == .Circle { CPDFCircleAnnotation.updateStyle(circleAnnotations, .dashed, withPDFView: pdfView) } if squareAnnotations.count > 0 || viewManager?.subToolMode == .Rectangle { CPDFSquareAnnotation.updateStyle(squareAnnotations, .dashed, withPDFView: pdfView) } } reloadData() } }