12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109 |
- //
- // KMNPopContentEditWindowController.swift
- // PDF Reader Pro
- //
- // Created by 丁林圭 on 2024/12/2.
- //
- import Cocoa
- import KMComponentLibrary
- class KMNPopContentEditWindowController: KMNBaseWindowController {
- public var editType:EditContentPopType = .editNone {
- didSet {
- rightOffsetConstraint.constant = 45.0
- paneBox.isHidden = false
-
- switch editType {
- case .signTextEdit :
- textPopView.layoutSubtreeIfNeeded()
-
- operationWidthConstraint.constant = textPopView.bounds.width
- self.window?.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = textPopView
- congfigFontPopUI()
-
- fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightLeft")
- fontAlightButton.reloadData()
- break
- case .signImageEdit:
- imagePopView.layoutSubtreeIfNeeded()
- congfigSignImageUI()
-
- operationWidthConstraint.constant = imagePopView.bounds.width
- self.window?.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = imagePopView
- break
- case .multpleTextEdit:
- textPopView.layoutSubtreeIfNeeded()
- operationWidthConstraint.constant = textPopView.bounds.width
- self.window?.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = textPopView
- congfigFontPopUI()
-
- fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightLeft")
- fontAlightButton.reloadData()
- break
- case .multpleImageEdit:
- imagePopView.layoutSubtreeIfNeeded()
- congfigMultpleImageUI()
-
- operationWidthConstraint.constant = imagePopView.bounds.width
- self.window?.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = imagePopView
- break
- case .multpleImageAndText:
- alightView.layoutSubtreeIfNeeded()
- operationWidthConstraint.constant = alightView.bounds.width
- self.window?.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = alightView
- break
- case .cropImage:
- cropPopView.layoutSubtreeIfNeeded()
- rightOffsetConstraint.constant = 0
- paneBox.isHidden = true
- operationWidthConstraint.constant = cropPopView.bounds.width
- self.window?.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = cropPopView
- break
- case .editNone:
- break
- }
- }
- }
-
- public var isOpenPane = false {
- didSet {
- if isOpenPane == true {
- paneSelectorItem.properties.state = .pressed
- } else {
- paneSelectorItem.properties.state = .normal
- }
- paneSelectorItem.reloadData()
- }
- }
-
- public var editContentPopMode:KMNEditContentPopMode = KMNEditContentPopMode(currentEditAreas: []) {
- didSet {
- let editPopType = editContentPopMode.popType
- editType = editPopType
-
- if(editType == .signTextEdit || editType == .multpleTextEdit) {
- let textEditAreas = listView?.km_editingTextAreas()
- let oldColor:NSColor? = fontColor
- let firstTextEditArea = textEditAreas?.first
- let firstColor = listView?.editingSelectionFontColor(with: firstTextEditArea)
- var newColor:NSColor? = firstColor
-
- for textEditArea in textEditAreas ?? [] {
- if firstTextEditArea != textEditArea {
- let color = listView?.editingSelectionFontColor(with: firstTextEditArea)
-
- if CPDFListView.isTheSameColor(color, anotherColor: firstColor) == false{
- newColor = nil
- break
- }
- }
- }
-
- if !CPDFListView.isTheSameColor(newColor, anotherColor: oldColor) {
- fontColor = newColor
- }
-
- let oldBold:Bool = isBold
- let newBolds:[Bool] = listView?.km_editTextAreasFontBolds(textEditAreas ?? []) ?? []
- var newBold = true
- for i in 0 ..< newBolds.count {
- let boldV = newBolds[i]
- if boldV == false {
- newBold = false
- break
- }
- }
-
- if oldBold != newBold {
- isBold = newBold
- }
-
- let oldItalic:Bool = isItalic
- let newItalics:[Bool] = listView?.km_editTextAreasFontItalics(textEditAreas ?? []) ?? []
-
- var newItalic = true
- for i in 0 ..< newItalics.count {
- let boldV = newItalics[i]
- if boldV == false {
- newItalic = false
- break
- }
- }
-
- if oldItalic != newItalic {
- isItalic = newItalic
- }
-
- let oldStrikeoutLine:Bool = isStrikeoutLine
- let newStrikeoutLines:[Bool] = listView?.km_editTextAreasFontStrikeout(textEditAreas ?? []) ?? []
-
- var newStrikeoutLine = true
- for i in 0 ..< newStrikeoutLines.count {
- let boldV = newStrikeoutLines[i]
- if boldV == false {
- newStrikeoutLine = false
- break
- }
- }
-
- if oldStrikeoutLine != newStrikeoutLine {
- isStrikeoutLine = newStrikeoutLine
- }
-
- let oldUnderLine:Bool = isUnderLine
- let newUnderLines:[Bool] = listView?.km_editTextAreasFontUnder(textEditAreas ?? []) ?? []
-
- var newUnderLine = true
- for i in 0 ..< newStrikeoutLines.count {
- let boldV = newUnderLines[i]
- if boldV == false {
- newUnderLine = false
- break
- }
- }
-
- if oldUnderLine != newUnderLine {
- isUnderLine = newUnderLine
- }
-
- let alights = listView?.km_editTextAreasTextAlignments(textEditAreas ?? []) ?? []
- var newAlignment = alights.first ?? .left
- let oldAlignment :NSTextAlignment = fontAlight
- for i in 0 ..< alights.count {
- let alignmentV = alights[i]
- if oldAlignment != alignmentV {
- newAlignment = .left
- break
- }
- }
- if oldAlignment != newAlignment {
- fontAlight = newAlignment
- }
-
- let fonts = listView?.km_editTextAreasComPDFFonts(textEditAreas ?? []) ?? []
-
- var newFont:CPDFFont? = fonts.first
- for i in 0 ..< fonts.count {
- let fontV = fonts[i]
- if i != 0 && (fontV.familyName != compdfFont?.familyName || fontV.styleName != compdfFont?.styleName) {
- newFont = nil
- break
- }
- }
-
- compdfFont = newFont
- }
- }
- }
-
- private var fontColor:NSColor? {
- didSet {
- fontColorItem.properties?.color = fontColor
- fontColorItem.reloadData()
- }
- }
-
- private var compdfFont:CPDFFont?{
- didSet {
- if(compdfFont != nil) {
- let styleName = compdfFont!.styleName
- if(styleName?.isEmpty == true) {
- fontNameSelect.properties.text = compdfFont!.familyName
- } else {
- fontNameSelect.properties.text = compdfFont!.familyName + "-" + (compdfFont!.styleName ?? "")
- }
- } else {
- fontNameSelect.properties.text = "-"
- }
- fontNameSelect.reloadData()
- }
- }
-
- private var fontAlight:NSTextAlignment = .left {
- didSet {
- if fontAlight == .center {
- fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightCenter")
- } else if fontAlight == .right {
- fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightRight")
- } else if fontAlight == .justified {
- fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightJus")
- } else {
- fontAlightButton.properties.icon = NSImage(named: "KMNImageNameListViewPopAlightLeft")
- }
- fontAlightButton.reloadData()
- }
- }
-
- private var objectAlign:ObjectAlignType = .alightLeft {
- didSet {
- var zeroRect = NSRect.null
- var highestRect = NSZeroRect
- var widthestRect = NSZeroRect
-
- let objects = editContentPopMode.editAreas
- let fristObject : CPDFEditArea = objects.first ?? CPDFEditArea()
- var leftestRect = fristObject.bounds
- var rightestRect = fristObject.bounds
- var topestRect = fristObject.bounds
- var bottomestRect = fristObject.bounds
-
- var leftestObject : CPDFEditArea = fristObject
- var rightestObject : CPDFEditArea = fristObject
- var topestObject : CPDFEditArea = fristObject
- var bottomestObject: CPDFEditArea = fristObject
-
- var totalWidth = 0.0
- var totalHeight = 0.0
-
- for i in 0 ..< objects.count {
- let annotation : CPDFEditArea = objects[i]
- zeroRect = zeroRect.union(annotation.bounds)
- totalWidth = totalWidth + annotation.bounds.width
- totalHeight = totalHeight + annotation.bounds.height
-
- if annotation.bounds.height > highestRect.height {
- highestRect = annotation.bounds
- }
-
- if annotation.bounds.width > widthestRect.width {
- widthestRect = annotation.bounds
- }
-
- if leftestRect.minX > annotation.bounds.minX {
- leftestRect = annotation.bounds
- leftestObject = annotation
- }
-
- if annotation.bounds.maxX > rightestRect.maxX {
- rightestRect = annotation.bounds
- rightestObject = annotation
- }
-
- if annotation.bounds.maxY > topestRect.maxY {
- topestRect = annotation.bounds
- topestObject = annotation
- }
-
- if bottomestRect.minY > annotation.bounds.minY {
- bottomestRect = annotation.bounds
- bottomestObject = annotation
- }
- }
-
- var resultAreasArray: [CPDFEditArea] = []
- var newBoundsArray: [String] = []
-
- switch objectAlign {
- case .alightLeft :
- for i in 0 ..< objects.count {
- let object = objects[i]
- var bounds = object.bounds
- bounds.origin.x = zeroRect.origin.x
- newBoundsArray.append(NSStringFromRect(bounds))
- }
- resultAreasArray = objects
-
- alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightLeft")
- break
- case .alightRight:
- for i in 0 ..< objects.count {
- let object = objects[i]
- var bounds = object.bounds
- bounds.origin.x = zeroRect.maxX - bounds.size.width
- newBoundsArray.append(NSStringFromRect(bounds))
- }
- resultAreasArray = objects
-
- alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightRight")
- break
- case .alightTop:
- for i in 0 ..< objects.count {
- let object = objects[i]
- var bounds = object.bounds
- bounds.origin.y = zeroRect.maxY - bounds.size.height
- newBoundsArray.append(NSStringFromRect(bounds))
- }
- resultAreasArray = objects
-
- alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightTop")
- break
-
- case .alightVer:
- for i in 0 ..< objects.count {
- let object = objects[i]
- var bounds = object.bounds
- bounds.origin.x = widthestRect.midX - bounds.width/2
- newBoundsArray.append(NSStringFromRect(bounds))
- }
- resultAreasArray = objects
-
- alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightVer")
- break
- case .alightHor:
- for i in 0 ..< objects.count {
- let object = objects[i]
- var bounds = object.bounds
- bounds.origin.y = highestRect.midY - bounds.height/2
- newBoundsArray.append(NSStringFromRect(bounds))
- }
- resultAreasArray = objects
-
- alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightHor")
- break
- case .alightBottom:
- for i in 0 ..< objects.count {
- let object = objects[i]
- var bounds = object.bounds
- bounds.origin.y = zeroRect.minY
- newBoundsArray.append(NSStringFromRect(bounds))
- }
- resultAreasArray = objects
-
- alightButton.properties.icon = NSImage(named: "KMNImageNameObjectPopAlightBottom")
- break
- case .averageVer:
- let middleGap = zeroRect.height - topestRect.height - bottomestRect.height
- let otherAreasTotalHeight = totalHeight - topestRect.height - bottomestRect.height
-
- let gap = (middleGap - otherAreasTotalHeight)/CGFloat(objects.count - 1)
- var areasCopyArray : [CPDFEditArea] = objects
- areasCopyArray.sorted(by: { obj1, obj2 in
- let area1 = obj1
- let area2 = obj2
- if area1.bounds.origin.x < area2.bounds.origin.x {
- return true
- } else {
- return false
- }
- })
- if let index = areasCopyArray.firstIndex(of: topestObject) {
- areasCopyArray.remove(at: index)
- }
-
- if let index = areasCopyArray.firstIndex(of: bottomestObject) {
- areasCopyArray.remove(at: index)
- }
-
- var bottomStartY = bottomestRect.maxY + gap
- for i in 0 ..< areasCopyArray.count {
- let areas = areasCopyArray[i]
- var bounds = areas.bounds
- bounds.origin.y = bottomStartY
- newBoundsArray.append(NSStringFromRect(bounds))
- bottomStartY = bottomStartY + bounds.height + gap
- }
- resultAreasArray = areasCopyArray
-
- alightButton.properties.icon = NSImage(named: "KMNImageNameObjecAlignAverageVer")
- break
- case .averageHor:
- let middleGap = zeroRect.width - leftestRect.width - rightestRect.width
- let otherAreasTotalWidth = totalWidth - leftestRect.width - rightestRect.width
-
- let gap = (middleGap - otherAreasTotalWidth)/CGFloat(objects.count - 1)
- var areasCopyArray : [CPDFEditArea] = objects
- areasCopyArray.sorted(by: { obj1, obj2 in
- let area1 = obj1
- let area2 = obj2
- if area1.bounds.origin.x < area2.bounds.origin.x {
- return true
- } else {
- return false
- }
- })
-
- if let index = areasCopyArray.firstIndex(of: leftestObject) {
- areasCopyArray.remove(at: index)
- }
-
- if let index = areasCopyArray.firstIndex(of: rightestObject) {
- areasCopyArray.remove(at: index)
- }
-
- var leftStartX = leftestRect.maxX + gap
- for i in 0 ..< areasCopyArray.count {
- let areas = areasCopyArray[i]
- var bounds = areas.bounds
- bounds.origin.x = leftStartX
- newBoundsArray.append(NSStringFromRect(bounds))
- leftStartX = leftStartX + bounds.width + gap
- }
- resultAreasArray = areasCopyArray
-
- alightButton.properties.icon = NSImage(named: "KMNImageNameObjecAlignAverageHor")
- break
- }
- alightButton.reloadData()
-
- for i in 0 ..< resultAreasArray.count {
- let editArea : CPDFEditArea = resultAreasArray[i]
- listView?.setBoundsEditArea(editArea, withBounds: NSRectFromString(newBoundsArray[i]))
- }
- }
- }
-
- private var isBold:Bool = false {
- didSet {
- fontBoldSelectorItem.properties.state = isBold ? .pressed : .normal
- fontBoldSelectorItem.reloadData()
- }
- }
-
- private var isItalic:Bool = false {
- didSet {
- fontItalicSelectorItem.properties.state = isItalic ? .pressed : .normal
- fontItalicSelectorItem.reloadData()
- }
- }
-
- private var isStrikeoutLine:Bool = false {
- didSet {
- fontStrikeoutLineSelectorItem.properties.state = isStrikeoutLine ? .pressed : .normal
- fontStrikeoutLineSelectorItem.reloadData()
- }
- }
-
- private var isUnderLine:Bool = false {
- didSet {
- fontUnderLineSelectorItem.properties.state = isUnderLine ? .pressed : .normal
- fontUnderLineSelectorItem.reloadData()
- }
- }
-
- static let shared: KMNPopContentEditWindowController = {
- let windowC = KMNPopContentEditWindowController(windowNibName: "KMNPopContentEditWindowController")
- return windowC
- }()
-
- var paneCallback: PaneCallback?
- var updatePDFViewCallback: UpdatePDFViewCallback?
-
- @IBOutlet weak var contentBox: NSBox!
- @IBOutlet weak var operationBox: NSBox!
- @IBOutlet weak var paneBox: NSBox!
- @IBOutlet weak var lineBox: NSBox!
- @IBOutlet var paneSelectorItem: ComponentCSelectorItem!
- @IBOutlet weak var operationWidthConstraint: NSLayoutConstraint!
- @IBOutlet weak var rightOffsetConstraint: NSLayoutConstraint!
-
- @IBOutlet var textPopView: NSView!
- @IBOutlet var fontColorItem: ComponentCColorItem!
- @IBOutlet var fontNameSelect: ComponentSelect!
- @IBOutlet var fontBoldSelectorItem: ComponentCSelector!
- @IBOutlet var fontItalicSelectorItem: ComponentCSelector!
- @IBOutlet var fontStrikeoutLineSelectorItem: ComponentCSelector!
- @IBOutlet var fontUnderLineSelectorItem: ComponentCSelector!
- @IBOutlet var fontSizeZoomOutButton: ComponentButton!
- @IBOutlet var fontSizeZoomInButton: ComponentButton!
- @IBOutlet var fontAlightButton: ComponentButton!
-
- @IBOutlet var imagePopView: NSView!
- @IBOutlet var rotaLeftButton: ComponentButton!
- @IBOutlet var rotaRightButton: ComponentButton!
- @IBOutlet var horizontalMirrorButton: ComponentButton!
- @IBOutlet var verticalMirrorButton: ComponentButton!
- @IBOutlet var cropButton: ComponentButton!
- @IBOutlet var replaceButton: ComponentButton!
- @IBOutlet var extractButton: ComponentButton!
- @IBOutlet weak var rightConstraint: NSLayoutConstraint!
-
- @IBOutlet var alightView: NSView!
- @IBOutlet var alightButton: ComponentButton!
-
- @IBOutlet var cropPopView: NSView!
- @IBOutlet var cropSureButton: ComponentButton!
- @IBOutlet var cropRestButton: ComponentButton!
-
- private var extractGroupView: ComponentGroup?
-
- weak var listView: CPDFListView?
-
- private var fontPopover:NSPopover?
- private var ObjectPopover:NSPopover?
- override func windowDidLoad() {
- super.windowDidLoad()
- }
-
- override func initContentView() {
- super.initContentView()
-
- paneSelectorItem.properties = ComponentCSelectorProperty(iconImage:NSImage(named: "KMNImageNameListViewPopPane"))
- paneSelectorItem.setTarget(self, action: #selector(paneButtonClicked(_ :)))
-
- fontBoldSelectorItem.properties = ComponentCSelectorProperty(
- iconImage:NSImage(named: "KMNImageNameContentBold"))
- fontBoldSelectorItem.setTarget(self, action: #selector(fontBoldButtonClicked(_ :)))
-
- fontItalicSelectorItem.properties = ComponentCSelectorProperty(
- iconImage:NSImage(named: "KMNImageNameContentItalic"))
- fontItalicSelectorItem.setTarget(self, action: #selector(fontItalicButtonClicked(_ :)))
-
- fontStrikeoutLineSelectorItem.properties = ComponentCSelectorProperty(
- iconImage:NSImage(named: "KMNImageNameContentStrikeoutLine"))
- fontStrikeoutLineSelectorItem.setTarget(self, action: #selector(fontStrikeoutLineButtonClicked(_ :)))
-
- fontUnderLineSelectorItem.properties = ComponentCSelectorProperty(iconImage:NSImage(named: "KMNImageNameContentUnderLine"))
- fontUnderLineSelectorItem.setTarget(self, action: #selector(fontUnderLineButtonClicked(_ :)))
-
- fontSizeZoomOutButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameListViewPopFontSizeZoomOut"),
- keepPressState: false)
- fontSizeZoomOutButton.setTarget(self, action: #selector(fontSizeZoomOutButtonClicked(_ :)))
-
- fontSizeZoomInButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameListViewPopFontSizeZoomIn"),
- keepPressState: false)
- fontSizeZoomInButton.setTarget(self, action: #selector(fontSizeZoomInButtonClicked(_ :)))
-
- fontColorItem.properties = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: .clear)
- fontColorItem.delegate = self
-
- fontAlightButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameListViewPopAlightLeft"),
- keepPressState: false)
- fontAlightButton.setTarget(self, action: #selector(fontAlightButtonClicked(_ :)))
-
- rotaLeftButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentRotateLeft"),
- keepPressState: false)
- rotaLeftButton.setTarget(self, action: #selector(rotateLeftButtonClicked(_ :)))
-
- rotaRightButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentRotateRight"),
- keepPressState: false)
- rotaRightButton.setTarget(self, action: #selector(rotateRightButtonClicked(_ :)))
-
- horizontalMirrorButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentHorMirr"),
- keepPressState: false)
- horizontalMirrorButton.setTarget(self, action: #selector(horizontalMirrorButtonClicked(_ :)))
-
- verticalMirrorButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentVerMirr"),
- keepPressState: false)
- verticalMirrorButton.setTarget(self, action: #selector(verticalMirrorButtonClicked(_ :)))
-
- alightButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameObjectPopAlightLeft"))
- alightButton.setTarget(self, action: #selector(alightButtonClicked(_ :)))
-
- cropRestButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentCropRest"),keepPressState: false)
- cropRestButton.setTarget(self, action: #selector(cropRestClicked(_ :)))
-
- cropSureButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentCropSure"),keepPressState: false)
- cropSureButton.setTarget(self, action: #selector(cropSureClicked(_ :)))
- }
-
- override func updateUIThemeColor() {
- super.updateUIThemeColor()
-
- lineBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider")
- contentBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup")
- contentBox.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/3-default")
- }
-
- override func updateUILanguage() {
- super.updateUILanguage()
-
- }
-
- //MARK: - Public
- public func updateFrame(listView:CPDFListView) {
- let editingAreas = listView.km_EditingAreas()
- let page = editingAreas.first?.page
- let windowFram = listView.window?.frame ?? CGRectZero
-
- if(page != nil) {
- let pageRect = listView.selectionMultipleBounds(withEditArea: editingAreas)
- let positioningRect = listView.convert(pageRect, from: page!)
- let view: NSView? = nil
- let position = listView.convert(positioningRect, to: view)
- var positionNew = position.origin
-
- positionNew.x += windowFram.origin.x + position.width/2
- positionNew.y += windowFram.origin.y + position.height
-
- var positionRect = self.window?.frame ?? CGRectZero
- positionRect.origin.x = positionNew.x - positionRect.width/2
- positionRect.origin.y = positionNew.y + popOffSet + CGFloat(truncating: listView.editingConfig().editAreaMargin)
-
- var listViewWindRect = listView.convert(listView.frame, to: view)
- listViewWindRect.origin.x += windowFram.origin.x
- listViewWindRect.origin.y += windowFram.origin.y
- if CGRectGetMaxY(positionRect) > CGRectGetMaxY(listViewWindRect) {
- positionRect.origin.y = positionNew.y - popOffSet - position.height - positionRect.size.height
- }
-
- if CGRectGetMinX(positionRect) < CGRectGetMinX(listViewWindRect) {
- positionRect.origin.x = CGRectGetMinX(listViewWindRect)
- }
- if CGRectGetMaxX(positionRect) > CGRectGetMaxX(listViewWindRect) {
- positionRect.origin.x = CGRectGetMaxX(listViewWindRect) - positionRect.width
- }
-
- self.window?.setFrame(positionRect, display: true)
-
- updateUILanguage()
- }
- }
-
- public func closeWindow(listView:CPDFListView?) {
- if self.window?.isVisible == true {
- listView?.window?.removeChildWindow(self.window ?? NSWindow())
- self.window?.close()
- }
-
- if ObjectPopover?.isShown == true {
- ObjectPopover?.close()
- }
-
- if fontPopover?.isShown == true {
- fontPopover?.close()
- }
- }
-
- //MARK: - private
- private func congfigFontPopUI() {
- fontNameSelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- isDisabled: false,
- isError: false,
- leftIcon: false,
- placeholder: nil,
- errorText: nil,
- creatable: false,
- text: NSLocalizedString("", comment: ""))
- fontNameSelect.delegate = self
-
- var menuItemArr: [ComponentMenuitemProperty] = []
- for familyName in CPDFFont.familyNames {
- let fontNames = CPDFFont.fontNames(forFamilyName: familyName)
- let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: familyName,
- identifier: "1")
- var subMenuItemArr: [ComponentMenuitemProperty] = []
-
- for fontName in fontNames {
- let subItemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: fontName,identifier: "2")
- subMenuItemArr.append(subItemProperty)
- }
-
- itemProperty.subPropertys = subMenuItemArr
- menuItemArr.append(itemProperty)
- }
-
- fontNameSelect.updateMenuItemsArr(menuItemArr)
- }
-
- private func congfigSignImageUI() {
- rightConstraint.constant = 36
- imagePopView.display()
-
- cropButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentCrop"),
- keepPressState: false)
- cropButton.setTarget(self, action: #selector(cropImageButtonClicked(_ :)))
-
- replaceButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentReplace"),
- keepPressState: false)
- replaceButton.setTarget(self, action: #selector(replaceImageButtonClicked(_ :)))
-
- extractButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentExtract"),
- keepPressState: false)
- extractButton.setTarget(self, action: #selector(extractImageButtonClicked(_ :)))
-
- extractButton.isHidden = false
- }
-
- private func congfigMultpleImageUI() {
- rightConstraint.constant = 0
- imagePopView.display()
-
- cropButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentExtract"),
- keepPressState: false)
- cropButton.setTarget(self, action: #selector(extractImageButtonClicked(_ :)))
-
- replaceButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameContentReplace"),
- keepPressState: false)
- replaceButton.setTarget(self, action: #selector(alightButtonClicked(_ :)))
-
- extractButton.isHidden = true
- }
-
- func removeGroupView() {
- if extractGroupView != nil {
- extractGroupView?.removeFromSuperview()
- }
- extractButton.properties.state = .normal
- extractButton.reloadData()
- }
-
- //MARK: - Action
- @objc func paneButtonClicked(_ sender: NSView) {
- isOpenPane = !isOpenPane
- paneCallback?(isOpenPane)
- }
-
- @objc func fontBoldButtonClicked(_ sender: NSView) {
- listView?.setEditingTextarea_Bold()
- updatePDFViewCallback?()
- let textEditAreas = listView?.km_editingTextAreas()
- let newBolds:[Bool] = listView?.km_editTextAreasFontBolds(textEditAreas ?? []) ?? []
- if newBolds.first == false {
- fontBoldSelectorItem.properties.state = .normal
- fontBoldSelectorItem.reloadData()
- }
- }
-
- @objc func fontItalicButtonClicked(_ sender: NSView) {
- listView?.setEditingTextarea_Italic()
- updatePDFViewCallback?()
- let textEditAreas = listView?.km_editingTextAreas()
- let newItalics:[Bool] = listView?.km_editTextAreasFontItalics(textEditAreas ?? []) ?? []
- if newItalics.first == false {
- fontItalicSelectorItem.properties.state = .normal
- fontItalicSelectorItem.reloadData()
- }
- }
-
- @objc func fontUnderLineButtonClicked(_ sender: NSView) {
- listView?.setEditingTextarea_Under()
- updatePDFViewCallback?()
- let textEditAreas = listView?.km_editingTextAreas()
- let newUnders:[Bool] = listView?.km_editTextAreasFontUnder(textEditAreas ?? []) ?? []
- if newUnders.first == false {
- fontUnderLineSelectorItem.properties.state = .normal
- fontUnderLineSelectorItem.reloadData()
- }
- }
-
- @objc func fontStrikeoutLineButtonClicked(_ sender: NSView) {
- listView?.setEditingTextarea_Strikeout()
- updatePDFViewCallback?()
- let textEditAreas = listView?.km_editingTextAreas()
- let newStrikeouts:[Bool] = listView?.km_editTextAreasFontStrikeout(textEditAreas ?? []) ?? []
- if newStrikeouts.first == false {
- fontStrikeoutLineSelectorItem.properties.state = .normal
- fontStrikeoutLineSelectorItem.reloadData()
- }
- }
-
- @objc func fontSizeZoomOutButtonClicked(_ sender: NSView) {
- let textEditAreas = listView?.km_editingTextAreas()
- for i in 0 ..< (textEditAreas?.count ?? 0) {
- let textEditArea = textEditAreas?[i]
- let fontSize = listView?.editingSelectionFontSize(byRangeEdit: textEditArea) ?? 0
- if(fontSize > changeValue) {
- listView?.setEditingSelectionFontSize((fontSize - changeValue), with: textEditArea, isAutoSize: false)
- }
- }
- updatePDFViewCallback?()
- }
-
- @objc func fontSizeZoomInButtonClicked(_ sender: NSView) {
- let textEditAreas = listView?.km_editingTextAreas()
- for i in 0 ..< (textEditAreas?.count ?? 0) {
- let textEditArea = textEditAreas?[i]
- let fontSize = listView?.editingSelectionFontSize(byRangeEdit: textEditArea) ?? 0
- listView?.setEditingSelectionFontSize((fontSize + changeValue), with: textEditArea, isAutoSize: false)
-
- }
- updatePDFViewCallback?()
- }
-
- @objc func alightButtonClicked(_ sender: NSView) {
- if ObjectPopover?.isShown == true {
- ObjectPopover?.close()
- } else {
- let vc = KMNPopDetailsViewController(nibName: "KMNPopDetailsViewController", bundle: nil)
- let createFilePopover: NSPopover = NSPopover.init()
- createFilePopover.contentViewController = vc
- createFilePopover.delegate = self
- createFilePopover.animates = true
- createFilePopover.behavior = .semitransient
- createFilePopover.setValue(true, forKey: "shouldHideAnchor")
- createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 20, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
-
- if (editContentPopMode.editAreas.count == 2) {
- vc.detailPopType = .doubleObjectAlight
- } else if (editContentPopMode.editAreas.count > 2) {
- vc.detailPopType = .multpleObjectAlight
- }
- vc.objectChangeAlighCallback = { [weak self] alight in
- self?.objectAlign = alight
- self?.updatePDFViewCallback?()
- }
- ObjectPopover = createFilePopover
- vc.objectAlignType = objectAlign
- }
- }
-
- @objc func fontAlightButtonClicked(_ sender: NSView) {
- if fontPopover?.isShown == true {
- fontPopover?.close()
- } else {
- let vc = KMNPopDetailsViewController(nibName: "KMNPopDetailsViewController", bundle: nil)
- let createFilePopover: NSPopover = NSPopover.init()
- createFilePopover.contentViewController = vc
- createFilePopover.delegate = self
- createFilePopover.animates = true
- createFilePopover.behavior = .semitransient
- createFilePopover.setValue(true, forKey: "shouldHideAnchor")
- createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 20, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
-
- vc.detailPopType = .contentEditAlight
- vc.fontChangeAlighCallback = { [weak self] alight in
- self?.listView?.setEditingTextarea_Alignment(align: .right)
- self?.fontAlight = alight
- self?.updatePDFViewCallback?()
- }
- fontPopover = createFilePopover
- vc.fontAlight = fontAlight
- }
- }
-
- @objc func rotateLeftButtonClicked(_ sender: NSView) {
- listView?.leftRotateAction()
- updatePDFViewCallback?()
- }
-
- @objc func rotateRightButtonClicked(_ sender: NSView) {
- listView?.rightRotateAction()
- updatePDFViewCallback?()
- }
-
- @objc func horizontalMirrorButtonClicked(_ sender: NSView) {
- listView?.reverseXAction()
- updatePDFViewCallback?()
- }
-
- @objc func verticalMirrorButtonClicked(_ sender: NSView) {
- listView?.reverseYAction()
- updatePDFViewCallback?()
- }
-
- @objc func cropImageButtonClicked(_ sender: NSView) {
- listView?.cropAction()
- }
-
- @objc func replaceImageButtonClicked(_ sender: NSView) {
- if let areas = listView?.km_editingImageAreas() {
- let panel = NSOpenPanel()
- panel.allowsMultipleSelection = false
- panel.allowedFileTypes = ["png","jpg"]
- panel.beginSheetModal(for: NSApp.mainWindow!) { response in
- if response == .OK {
- let openPath = panel.url?.path
- for area in areas {
- self.listView?.replace(area, imagePath: openPath!)
- }
- }
- }
- }
- }
-
- @objc func extractImageButtonClicked(_ sender: NSView) {
- if extractGroupView?.superview != nil {
- removeGroupView()
- } else {
- var viewHeight: CGFloat = 8
- var menuItemArr: [ComponentMenuitemProperty] = []
- for i in ["jpg", "png", "pdf"] {
- let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: KMLocalizedString(i))
- menuItemArr.append(properties_Menuitem)
- viewHeight += 36
- }
-
- if extractGroupView == nil {
- extractGroupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
- }
- extractGroupView?.groupDelegate = self
- extractGroupView?.frame = CGRectMake(310, 0, 72, viewHeight)
- extractGroupView?.updateGroupInfo(menuItemArr)
-
- var point = CGPoint(x: sender.frame.origin.x + sender.frame.size.width/2, y: CGRectGetMaxY(sender.frame))
- extractGroupView?.showWithPoint(point, relativeTo: self.window?.contentView)
-
- extractButton.properties.state = .pressed
- extractButton.reloadData()
- }
- }
-
- @objc func cropRestClicked(_ sender: NSView) {
- listView?.cropRestAction()
- }
-
- @objc func cropSureClicked(_ sender: NSView) {
- listView?.cropComfirmAction()
- }
-
- @objc func fontColorChange(_ sender: Any) {
- if let color = (sender as? NSColorPanel)?.color {
- listView?.changeEditingTextarea_Color(color: color)
- updatePDFViewCallback?()
- }
- }
-
- }
- //MARK: - ComponentCColorDelegate
- extension KMNPopContentEditWindowController: ComponentCColorDelegate {
- func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
- if (view == fontColorItem) {
- fontColorItem.properties?.state = .normal
- fontColorItem.reloadData()
-
- let colorPanel = NSColorPanel.shared
-
- colorPanel.setTarget(self)
- colorPanel.showsAlpha = false
- colorPanel.setAction(#selector(self.fontColorChange(_:)))
- colorPanel.orderFront(nil)
- }
- }
-
- }
- //MARK: - ComponentSelectDelegate
- extension KMNPopContentEditWindowController: ComponentSelectDelegate {
- func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
- if(view == fontNameSelect) {
- if menuItemProperty?.identifier == "1" {
- let familyName = fontNameSelect.properties.text ?? "Helvetica"
- let styles = CPDFFont.fontNames(forFamilyName: familyName)
-
- compdfFont = CPDFFont(familyName: familyName, fontStyle: styles.first ?? "")
- listView?.setEditingTextarea_font(font: compdfFont ?? CPDFFont(familyName: "Helvetica", fontStyle: ""))
- } else if menuItemProperty?.identifier == "2" { //字体样式
- compdfFont = CPDFFont(familyName: compdfFont?.familyName ?? "Helvetica", fontStyle: menuItemProperty?.text ?? "")
- listView?.setEditingTextarea_font(font: compdfFont ?? CPDFFont(familyName: "Helvetica", fontStyle: ""))
- }
- }
- }
- }
- //MARK: - ComponentSelectDelegate
- extension KMNPopContentEditWindowController: NSPopoverDelegate {
- func popoverWillClose(_ notification: Notification) {
- if fontPopover == (notification.object as? NSPopover) {
- if(fontAlightButton.properties.state == .pressed) {
- fontAlightButton.properties.state = .normal
- fontAlightButton.reloadData()
- }
- } else if ObjectPopover == (notification.object as? NSPopover) {
- if(alightButton.properties.state == .pressed) {
- alightButton.properties.state = .normal
- alightButton.reloadData()
- }
- }
-
- }
- }
- extension KMNPopContentEditWindowController: ComponentGroupDelegate {
- func componentGroupDidDismiss(group: ComponentGroup?) {
- removeGroupView()
-
- extractButton.properties.state = .normal
- extractButton.reloadData()
- }
-
- func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
- if group == extractGroupView {
- if let selItem = menuItemProperty {
- let index = group?.menuItemArr.firstIndex(of: selItem)
- if index == 0 {
- listView?.exportEditingImageAreasAction(format: "jpg")
- } else if index == 1 {
- listView?.exportEditingImageAreasAction(format: "png")
- } else if index == 2 {
- listView?.exportEditingImageAreasAction(format: "pdf")
- }
- }
- }
- }
- }
|