123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092 |
- //
- // KMNPopAnnotationWindowController.swift
- // PDF Reader Pro
- //
- // Created by 丁林圭 on 2024/12/2.
- //
- import Cocoa
- import KMComponentLibrary
- let popOffSet:CGFloat = 8.0
- typealias PaneCallback = (_ isOpenPane: Bool) -> ()
- typealias UpdatePDFViewCallback = () -> ()
- typealias FrameCallback = () -> ()
- typealias CloseWindowCallback = () -> ()
- class KMNPopAnnotationViewController: KMNBaseViewController {
-
- let formInputWidth:CGFloat = 106.0
- let viewOffset:CGFloat = 4.0
-
- public var popType:ListViewPopType = .popTypeNone {
- didSet {
- rightOffsetConstraint.constant = 49.0
- operationHeightConstraint.constant = 32.0
- paneBox.isHidden = false
-
- switch popType {
- case .generaAnnotation :
- generaPopView.layoutSubtreeIfNeeded()
- operationWidthConstraint.constant = generaPopView.bounds.width
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = generaPopView
- congfigGeneraPopUI()
- break
- case .freeTextAnnotation:
- fontPopView.layoutSubtreeIfNeeded()
- operationWidthConstraint.constant = fontPopView.bounds.width
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = fontPopView
- break
- case .shapeAnnotation:
- var isContainSelfDot = false
- for i in 0..<annotationPopMode.annotations.count {
- if let selfAn = annotationPopMode.annotations[i] as? CSelfSignAnnotation {
- if selfAn.annotationType == .signDot {
- isContainSelfDot = true
- break
- }
- }
- }
-
- var width = 272.0
- if isContainSelfDot == true {
- shapeRightConstraint.constant = 0
- widthZoomOutButton.isHidden = true
- widthZoomInButton.isHidden = true
- width = 200.0
- } else {
- shapeRightConstraint.constant = 72
- widthZoomOutButton.isHidden = false
- widthZoomInButton.isHidden = false
- }
- shapeView.layoutSubtreeIfNeeded()
- // operationWidthConstraint.constant = shapeView.bounds.width //刷新后未更新最后的宽度,暂时写死宽度
- operationWidthConstraint.constant = width
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = shapeView
-
- congfigShapePopUI()
-
- updateShapeWidthButtonState()
- break
- case .linkAnnotation:
- if let newLinkAnnotation = annotationPopMode.annotation as? CPDFLinkAnnotation {
- linkAnnotation = newLinkAnnotation
- }
- break
- case .formAnnotation:
- formRightConstraint.constant = 0
- gropNameInput.isHidden = true
- formView.layoutSubtreeIfNeeded()
-
- operationWidthConstraint.constant = formInputWidth
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = formView
- break
- case .formRadioAnnotation:
- formRightConstraint.constant = formInputWidth + viewOffset
- gropNameInput.isHidden = false
- formView.layoutSubtreeIfNeeded()
-
- operationWidthConstraint.constant = formInputWidth * 2 + viewOffset
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = formView
- break
- case .multpleAnnotation:
- alightView.layoutSubtreeIfNeeded()
- operationWidthConstraint.constant = alightView.bounds.width
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = alightView
- break
- case .popTypeNone:
- break
-
- }
- }
- }
-
- public var isOpenPane = false {
- didSet {
- if isOpenPane == true {
- paneSelectorItem.properties.state = .pressed
- } else {
- paneSelectorItem.properties.state = .normal
- }
- paneSelectorItem.reloadData()
- }
- }
-
- public var annotationPopMode:KMNAnnotationPopMode = KMNAnnotationPopMode(pdfAnnotations: []) {
- didSet {
- let annotationPopType = annotationPopMode.popType
- popType = annotationPopType
- if annotationPopType == .generaAnnotation {
- let newColor = annotationPopMode.annotationColor()
- annotationColor = newColor
- } else if(annotationPopType == .shapeAnnotation) {
- let newColor = annotationPopMode.annotationColor()
- annotationColor = newColor
- } else if(annotationPopType == .freeTextAnnotation) {
- let newColor = annotationPopMode.annotationFontColor()
- fontColor = newColor
-
- let alignment = annotationPopMode.annotationAlignment()
- fontAlight = alignment
-
- let font = annotationPopMode.annotationFontName()
- compdfFont = font
- } else if annotationPopType == .linkAnnotation {
- if annotationPopMode.annotation != nil {
- linkAnnotation = annotationPopMode.annotation as! CPDFLinkAnnotation
- }
- } else if annotationPopType == .formAnnotation {
- fildNameString = annotationPopMode.annotationFieldName() ?? ""
- } else if annotationPopType == .formRadioAnnotation {
- fildNameString = annotationPopMode.annotationFieldName() ?? ""
- gropNameString = annotationPopMode.annotationGropName() ?? ""
- }
- }
- }
-
- private var annotationColor:NSColor?{
- didSet {
- if popType == .generaAnnotation {
- generaColorGroup.currentColor = annotationColor
- generaColorGroup.refreshUI()
- } else if popType == .shapeAnnotation {
- shapeColorGroup.currentColor = annotationColor
- shapeColorGroup.refreshUI()
- }
- }
- }
-
- private var fontColor:NSColor?{
- didSet {
- fontColorItem.properties?.color = fontColor
- fontColorItem.reloadData()
- }
- }
-
- private var linkAnnotation:CPDFLinkAnnotation = CPDFLinkAnnotation(){
- didSet {
- if let destination = linkAnnotation.destination() {
- linkType = .page
- } else if let annotationURL = linkAnnotation.url(){
- var urlString = annotationURL.fileURL.absoluteString
- if annotationURL.hasPrefix("mailto:") {
- linkType = .email
- } else {
- linkType = .url
- }
- } else {
- linkType = .linkSetting
- }
- }
- }
-
- private var fildNameString:String = ""{
- didSet {
- fildNameInput.properties.text = fildNameString
- fildNameInput.reloadData()
- }
- }
-
- private var gropNameString:String = ""{
- didSet {
- gropNameInput.properties.text = gropNameString
- gropNameInput.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
-
- private var objectAlignType:ObjectAlignType = .alightNone {
- didSet {
- let activeAnnotations = annotationPopMode.annotations
- switch objectAlignType {
- case .alightLeft :
- listView?.change(activeAnnotations, alignmentType: .left)
- break
- case .alightRight:
- listView?.change(activeAnnotations, alignmentType: .right)
- break
- case .alightTop:
- listView?.change(activeAnnotations, alignmentType: .top)
- break
- case .alightVer:
- listView?.change(activeAnnotations, alignmentType: .horizontally)
- break
- case .alightHor:
- listView?.change(activeAnnotations, alignmentType: .vertical)
- break
- case .alightBottom:
- listView?.change(activeAnnotations, alignmentType: .bottom)
- break
- case .averageVer:
- listView?.change(activeAnnotations, alignmentType: .disVertical)
- break
- case .averageHor:
- listView?.change(activeAnnotations, alignmentType: .disHorizontally)
- break
- case .alightNone:
- break
- }
- }
- }
-
-
- var paneCallback: PaneCallback?
- var updatePDFViewCallback: UpdatePDFViewCallback?
- var frameCallback: FrameCallback?
- var closeWindowCallback: CloseWindowCallback?
- static let shared: KMNPopAnnotationViewController = {
- let windowC = KMNPopAnnotationViewController(nibName: "KMNPopAnnotationViewController", bundle: nil)
- return windowC
- }()
-
- var linkType:linkDetailType = .linkSetting {
- didSet {
- if linkType == .linkSetting {
- linkView.layoutSubtreeIfNeeded()
-
- rightOffsetConstraint.constant = 45.0
- operationHeightConstraint.constant = 32.0
- operationWidthConstraint.constant = linkView.bounds.width
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = linkView
-
- paneBox.isHidden = false
- } else if linkType == .page {
- linkPageDetailView.layoutSubtreeIfNeeded()
- rightOffsetConstraint.constant = 8
- operationWidthConstraint.constant = linkPageDetailView.bounds.width
- operationHeightConstraint.constant = 224
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = linkPageDetailView
-
- paneBox.isHidden = true
- if let destination = linkAnnotation.destination() {
- reloadPageUrlData()
- refreshPageThum()
- } else {
- paginationView.properties.currentIndex = Int(((listView?.currentPageIndex ?? 0) + 1))
- paginationView.reloadData()
- refreshPageThum()
- }
- } else if linkType == .url {
- linkUrlDetailView.layoutSubtreeIfNeeded()
- rightOffsetConstraint.constant = 8
- operationWidthConstraint.constant = linkUrlDetailView.bounds.width
- operationHeightConstraint.constant = linkUrlDetailView.bounds.height
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = linkUrlDetailView
-
- paneBox.isHidden = true
-
- urlInput.properties.placeholder = KMNCompanyWebsite
-
- if let annotationURL = linkAnnotation.url(){
- if annotationURL.hasPrefix("mailto:") {
- urlInput.properties.text = ""
- } else {
- urlInput.properties.text = annotationURL
- }
- } else {
- urlInput.properties.text = ""
- }
- urlInput.properties.state = .pressed
- urlInput.beginEditing()
- urlInput.reloadData()
- } else if linkType == .email {
- linkUrlDetailView.layoutSubtreeIfNeeded()
- rightOffsetConstraint.constant = 8
- operationWidthConstraint.constant = linkUrlDetailView.bounds.width
- operationHeightConstraint.constant = linkUrlDetailView.bounds.height
- self.view.display() //需刷新约束才会有值,不然会变化
- operationBox.contentView = linkUrlDetailView
-
- paneBox.isHidden = true
-
- urlInput.properties.placeholder = KMNCompanyEamil
- if let annotationURL = linkAnnotation.url(){
- if annotationURL.hasPrefix("mailto:") {
- if let range = annotationURL.range(of: "mailto:") {
- let trimmedString = String(annotationURL[range.upperBound...])
- urlInput.properties.text = trimmedString
- }
- } else {
- urlInput.properties.text = ""
- }
- } else {
- urlInput.properties.text = ""
- }
- urlInput.properties.state = .pressed
- urlInput.beginEditing()
- urlInput.reloadData()
- }
- frameCallback?()
- }
- }
-
- @IBOutlet weak var contentBox: NSBox!
- @IBOutlet weak var operationBox: NSBox!
- @IBOutlet weak var paneBox: NSBox!
- @IBOutlet weak var lineBox: NSBox!
- @IBOutlet var paneSelectorItem: ComponentCSelector!
- @IBOutlet weak var operationWidthConstraint: NSLayoutConstraint!
- @IBOutlet weak var rightOffsetConstraint: NSLayoutConstraint!
- @IBOutlet weak var operationHeightConstraint: NSLayoutConstraint!
-
- @IBOutlet var generaPopView: NSView!
- @IBOutlet var generaColorGroup: ComponentCColorGroup!
-
- @IBOutlet var fontPopView: NSView!
- @IBOutlet var fontColorItem: ComponentCColorItem!
- @IBOutlet var fontNameSelect: ComponentSelect!
- @IBOutlet var fontSizeZoomOutButton: ComponentButton!
- @IBOutlet var fontSizeZoomInButton: ComponentButton!
- @IBOutlet var fontAlightButton: ComponentButton!
-
- @IBOutlet var shapeView: NSView!
- @IBOutlet var shapeColorGroup: ComponentCColorGroup!
- @IBOutlet var widthZoomOutButton: ComponentButton!
- @IBOutlet var widthZoomInButton: ComponentButton!
- @IBOutlet weak var shapeRightConstraint: NSLayoutConstraint!
- @IBOutlet var linkView: NSView!
- @IBOutlet var pageLinkButton: ComponentButton!
- @IBOutlet var urlLinkButton: ComponentButton!
- @IBOutlet var emailLinkButton: ComponentButton!
- @IBOutlet weak var pageLinkWidthConstraint: NSLayoutConstraint!
- @IBOutlet weak var urlLinkWidthConstraint: NSLayoutConstraint!
- @IBOutlet weak var emailLinkWidthConstraint: NSLayoutConstraint!
-
- @IBOutlet var formView: NSView!
- @IBOutlet var fildNameInput: ComponentInput!
- @IBOutlet var gropNameInput: ComponentInput!
- @IBOutlet var formRightConstraint: NSLayoutConstraint!
-
- @IBOutlet var alightView: NSView!
- @IBOutlet var alightButton: ComponentButton!
-
- @IBOutlet var linkPageDetailView: NSView!
- @IBOutlet var backPageLinkButton: ComponentButton!
- @IBOutlet var paginationView: ComponentPagination!
- @IBOutlet var paginationDesBox: NSBox!
- @IBOutlet var pageThumImageView: NSImageView!
- @IBOutlet var goPageLinkButton: ComponentButton!
- @IBOutlet weak var backPageWidthConstraint: NSLayoutConstraint!
-
- @IBOutlet var linkUrlDetailView: NSView!
- @IBOutlet var backUrlLinkButton: ComponentButton!
- @IBOutlet var goUrlLinkButton: ComponentButton!
- @IBOutlet var urlInput: ComponentInput!
- @IBOutlet weak var backUrlWidthConstraint: NSLayoutConstraint!
-
- private var fontPopover:NSPopover?
- private var ObjectPopover:NSPopover?
-
- weak var listView:CPDFListView? {
- didSet {
- paginationView.properties.totalCount = Int(listView?.document.pageCount ?? 1)
- paginationView.reloadData()
- }
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- }
-
- override func initContentView() {
- super.initContentView()
-
- configFormPopUI()
- congfigFontPopUI()
- congfigGeneraPopUI()
- congfigShapePopUI()
-
- paneSelectorItem.properties = ComponentCSelectorProperty(iconImage:NSImage(named: "KMNImageNameListViewPopPane"))
- paneSelectorItem.setTarget(self, action: #selector(paneButtonClicked(_ :)))
-
- 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(_ :)))
-
- fontAlightButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameListViewPopAlightLeft"))
- fontAlightButton.setTarget(self, action: #selector(fontAlightButtonClicked(_ :)))
-
- widthZoomOutButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameListViewPopWidthZoomOut"),
- keepPressState: false)
- widthZoomOutButton.setTarget(self, action: #selector(widthZoomOutButtonClicked(_ :)))
-
- widthZoomInButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameListViewPopWidthZoomIn"),
- keepPressState: false)
- widthZoomInButton.setTarget(self, action: #selector(widthZoomInButtonClicked(_ :)))
-
- pageLinkButton.setTarget(self, action: #selector(pageLinkButtonClicked(_ :)))
- urlLinkButton.setTarget(self, action: #selector(urlLinkButtonClicked(_ :)))
- emailLinkButton.setTarget(self, action: #selector(emailLinkButtonClicked(_ :)))
-
- alightButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNameObjectPopAlightLeft"))
- alightButton.setTarget(self, action: #selector(alightButtonClicked(_ :)))
-
- goUrlLinkButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNamePopLinkGO"),keepPressState: false)
- goUrlLinkButton.setTarget(self, action: #selector(linkGoButtonClicked(_ :)))
-
- goPageLinkButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: true,
- icon:NSImage(named: "KMNImageNamePopLinkGO"),keepPressState: false)
- goPageLinkButton.setTarget(self, action: #selector(linkGoButtonClicked(_ :)))
-
- paginationView.properties = ComponentPaginationProperty(doubleArrow_show: false, currentIndex: 0, totalCount: 1)
- paginationView.delegate = self
-
- let inputFiedProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
- state:.pressed ,
- isError: false,
- showPrefix: false,
- showSuffix: false,
- showClear: false,
- isDisabled: false,
- placeholder: KMNCompanyWebsite,
- text: "")
- urlInput.properties = inputFiedProperty
- urlInput.delegate = self
- }
-
- 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")
- paginationDesBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/4")
- paginationDesBox.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/4")
- pageThumImageView.wantsLayer = true
- paginationDesBox.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider").cgColor
- }
-
- override func updateUILanguage() {
- super.updateUILanguage()
-
- pageLinkButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: false,
- showLeftIcon:true,
- buttonText:KMLocalizedString("Page"),
- icon:NSImage(named: "KMNImageNameListViewLinkPage"),keepPressState: false)
-
- urlLinkButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: false,
- showLeftIcon:true,
- buttonText:KMLocalizedString("Web"),
- icon:NSImage(named: "KMNImageNameListViewLinkUrl"),keepPressState: false)
-
- emailLinkButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .s,
- state: .normal,
- onlyIcon: false,
- showLeftIcon:true,
- buttonText:KMLocalizedString("Email"),
- icon:NSImage(named: "KMNImageNameListViewLinkEmail"),keepPressState: false)
-
- pageLinkWidthConstraint.constant = pageLinkButton.properties.propertyInfo.viewWidth
- urlLinkWidthConstraint.constant = urlLinkButton.properties.propertyInfo.viewWidth
- emailLinkWidthConstraint.constant = emailLinkButton.properties.propertyInfo.viewWidth
-
- backUrlLinkButton.properties = ComponentButtonProperty(type: .text_primary,
- size: .s,
- state: .normal,
- onlyIcon: false,
- showLeftIcon: true,
- buttonText: KMLocalizedString("Back"),
- icon:NSImage(named: "KMNImageNamePopLinkBack"),keepPressState: false)
- backUrlLinkButton.setTarget(self, action: #selector(linkBackButtonClicked(_ :)))
- backUrlWidthConstraint.constant = backUrlLinkButton.properties.propertyInfo.viewWidth
- backUrlLinkButton.reloadData()
-
- backPageLinkButton.properties = ComponentButtonProperty(type: .text_primary,
- size: .s,
- onlyIcon: false,
- showLeftIcon:true,
- buttonText: KMLocalizedString("Back"),
- icon:NSImage(named: "KMNImageNamePopLinkBack"),keepPressState: false)
- backPageLinkButton.setTarget(self, action: #selector(linkBackButtonClicked(_ :)))
- backPageWidthConstraint.constant = backPageLinkButton.properties.propertyInfo.viewWidth
-
- }
-
- //MARK: - Public
- public func closePopView() {
- if ObjectPopover?.isShown == true {
- ObjectPopover?.close()
- }
-
- if fontPopover?.isShown == true {
- fontPopover?.close()
- }
- }
-
- //MARK: - private
- private func congfigGeneraPopUI() {
- var colors = KMAnnotationPropertiesColorManager.manager.markHighlightColors
- if(annotationPopMode.annotation is CPDFMarkupAnnotation) {
- let markupAnnotation:CPDFMarkupAnnotation = annotationPopMode.annotation as! CPDFMarkupAnnotation
- if markupAnnotation.markupType() == .highlight {
- colors = KMAnnotationPropertiesColorManager.manager.markHighlightColors
- } else {
- colors = KMAnnotationPropertiesColorManager.manager.markOtherColors
- }
- } else if (annotationPopMode.annotation is CPDFInkAnnotation) {
- colors = KMAnnotationPropertiesColorManager.manager.inkColors
- } else if (annotationPopMode.annotation is CPDFTextAnnotation) {
- colors = KMAnnotationPropertiesColorManager.manager.noteColors
- }
-
- 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])
- generaColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
- generaColorGroup.delegate = self
- }
-
- private func updateShapeWidthButtonState() {
- let annotations = annotationPopMode.annotations
- var isNoHaveZoomOut = true
- var isNoHaveZoomIn = true
- for i in 0 ..< annotations.count {
- let annotation = annotations[i]
- var shapeWidth = annotation.borderWidth
- if annotation.isKind(of: CSelfSignAnnotation.self) == true {
- if let signAnnotation:CSelfSignAnnotation = annotation as? CSelfSignAnnotation {
- shapeWidth = signAnnotation.lineAnnotationWidth
- }
- }
-
- if shapeWidth <= minLineValue {
- isNoHaveZoomOut = false
- }
-
- if shapeWidth >= maxLineValue {
- isNoHaveZoomIn = false
- }
- }
-
- if isNoHaveZoomOut == false {
- widthZoomOutButton.properties.isDisabled = true
- } else {
- widthZoomOutButton.properties.isDisabled = false
- }
- widthZoomOutButton.reloadData()
-
- if isNoHaveZoomIn == false {
- widthZoomInButton.properties.isDisabled = true
- } else {
- widthZoomInButton.properties.isDisabled = false
- }
- widthZoomInButton.reloadData()
- }
-
- private func congfigFontPopUI() {
- fontNameSelect.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- isDisabled: false,
- isError: false,
- leftIcon: false,
- placeholder: nil,
- errorText: nil,
- creatable: false,
- text: KMLocalizedString("", 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)
-
- fontColorItem.properties = ComponentCColorProperty(colorType: .color,
- state: .normal,
- isCustom: true,
- color: NSColor.red)
- fontColorItem.delegate = self
- fontColorItem.reloadData()
- }
-
- private func congfigShapePopUI() {
- var colors = KMAnnotationPropertiesColorManager.manager.inkColors
- let annotation = annotationPopMode.annotation ?? CPDFAnnotation()
- if annotation.isKind(of: CPDFInkAnnotation.self) {
- colors = KMAnnotationPropertiesColorManager.manager.inkColors
- } else if annotation.isKind(of: CPDFCircleAnnotation.self) ||
- annotation.isKind(of: CPDFSquareAnnotation.self) {
- colors = KMAnnotationPropertiesColorManager.manager.inkColors
- } else if annotation.isKind(of: CPDFLineAnnotation.self) {
- if let lineAnnotation:CPDFLineAnnotation = annotation as? CPDFLineAnnotation {
- if lineAnnotation.isMeasure {
- colors = KMAnnotationPropertiesColorManager.manager.measure_Border_Colors
- } else {
- if lineAnnotation.endLineStyle == .none && lineAnnotation.startPoint == .none {
- colors = KMAnnotationPropertiesColorManager.manager.lineColors
- } else {
- colors = KMAnnotationPropertiesColorManager.manager.arrowColors
- }
- }
- }
- } else if (annotation.isKind(of: CPDFPolygonAnnotation.self) ||
- annotation.isKind(of: CPDFPolylineAnnotation.self)) {
- colors = KMAnnotationPropertiesColorManager.manager.measure_Border_Colors
- } else if annotation.isKind(of: CPDFTextAnnotation.self) {
- colors = KMAnnotationPropertiesColorManager.manager.noteColors
- } else if annotation.isKind(of: CSelfSignAnnotation.self) {
- colors = KMAnnotationPropertiesColorManager.manager.fill_tickColors
- }
- 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])
- shapeColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
- shapeColorGroup.delegate = self
- }
-
- private func configFormPopUI() {
- let inputFiedProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
- state:.pressed ,
- isError: false,
- showPrefix: false,
- showSuffix: false,
- showClear: false,
- isDisabled: false,
- placeholder: "File Name",
- text: "")
- fildNameInput.properties = inputFiedProperty
- fildNameInput.delegate = self
-
- let inputGropProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
- state:.pressed ,
- isError: false,
- showPrefix: false,
- showSuffix: false,
- showClear: false,
- isDisabled: false,
- placeholder: "Grop Name",
- text: "")
- gropNameInput.properties = inputGropProperty
- gropNameInput.delegate = self
- }
-
- private func reloadPageUrlData() {
- if let destination = linkAnnotation.destination() {
- if let page = destination.page() {
- paginationView.properties.currentIndex = Int(page.pageIndex() + 1)
- paginationView.reloadData()
- return
- }
- }
-
- paginationView.properties.currentIndex = Int(((listView?.currentPageIndex ?? 0) + 1))
- paginationView.reloadData()
- }
-
- private func refreshPageThum() {
- guard let pdfView = listView else {
- return
- }
- let thumbnail = KMNThumbnail.init(document: pdfView.document, currentPageIndex:Int(paginationView.properties.currentIndex - 1))
- thumbnail.generateThumImage {[weak self] image in
- self?.pageThumImageView.image = image
- }
- }
-
- //MARK: - Action
- @objc func paneButtonClicked(_ sender: NSView) {
- isOpenPane = !isOpenPane
- paneCallback?(isOpenPane)
- }
-
- @objc func fontSizeZoomOutButtonClicked(_ sender: NSView) {
- if let freeText = annotationPopMode.annotation as? CPDFFreeTextAnnotation {
- if listView?.isEdit(withCurrentFreeText: freeText) == true {
- listView?.commitEditAnnotationFreeText(freeText)
- }
- if annotationPopMode.zoomOutFontSize() {
- updatePDFViewCallback?()
- }
- }
- }
-
- @objc func fontSizeZoomInButtonClicked(_ sender: NSView) {
- if let freeText = annotationPopMode.annotation as? CPDFFreeTextAnnotation {
- if listView?.isEdit(withCurrentFreeText: freeText) == true {
- listView?.commitEditAnnotationFreeText(freeText)
- }
- if annotationPopMode.zoomInFontSize() {
- updatePDFViewCallback?()
- }
- }
- }
-
- @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 = false
- 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 = .freeTextAlight
- vc.fontChangeAlighCallback = { [weak self] alight in
- if let freeText = self?.annotationPopMode.annotation as? CPDFFreeTextAnnotation {
- if self?.listView?.isEdit(withCurrentFreeText: freeText) == true {
- self?.listView?.commitEditAnnotationFreeText(freeText)
- }
- }
-
- self?.annotationPopMode.setAnnotationAlignment(annotationAlignment: alight)
- self?.updatePDFViewCallback?()
- self?.fontAlight = alight
- }
- fontPopover = createFilePopover
- vc.fontAlight = fontAlight
- }
- }
-
- @objc func widthZoomOutButtonClicked(_ sender: NSView) {
- if annotationPopMode.zoomOutShapeWidth() {
- updatePDFViewCallback?()
- }
- updateShapeWidthButtonState()
- }
-
- @objc func widthZoomInButtonClicked(_ sender: NSView) {
- if annotationPopMode.zoomInShapeWidth() {
- updatePDFViewCallback?()
- }
- updateShapeWidthButtonState()
- }
-
- @objc func pageLinkButtonClicked(_ sender: NSView) {
- linkType = .page
- }
-
- @objc func urlLinkButtonClicked(_ sender: NSView) {
- linkType = .url
- }
-
- @objc func emailLinkButtonClicked(_ sender: NSView) {
- linkType = .email
- }
-
- @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 = false
- 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 (annotationPopMode.annotations.count == 2) {
- vc.detailPopType = .freeTextDoubleAlight
- } else if (annotationPopMode.annotations.count > 2) {
- vc.detailPopType = .freeTextMultpleAlight
- }
-
- vc.objectChangeAlighCallback = { [weak self] alight in
- self?.objectAlignType = alight
- self?.updatePDFViewCallback?()
- }
- ObjectPopover = createFilePopover
- vc.objectAlignType = objectAlignType
- }
- }
-
- @objc func linkGoButtonClicked(_ sender: NSView) {
- if linkType == .url {
- guard let webString = linkAnnotation.url() else { return }
- if let data = URL(string: webString) {
- NSWorkspace.shared.open(data)
- }
- } else if linkType == .email {
- guard let emailString = linkAnnotation.url() else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = KMLocalizedString("Invalid Email. Please try again.", comment: "")
- alert.runModal()
- return
- }
- if KMNTools.isValidateEmail(emailString) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = KMLocalizedString("Invalid Email. Please try again.", comment: "")
- alert.runModal()
- } else {
- NSWorkspace.shared.open(URL(string: emailString)!)
- }
- } else if linkType == .page {
- if let destination = linkAnnotation.destination() {
- listView?.go(to: destination)
- closeWindowCallback?()
- }
- }
- }
-
- @objc func linkBackButtonClicked(_ sender: NSView) {
- linkType = .linkSetting
- }
-
- @objc func fontColorChange(_ sender: Any) {
- if let color = (sender as? NSColorPanel)?.color {
- if let freeText = annotationPopMode.annotation as? CPDFFreeTextAnnotation {
-
- if listView?.isEdit(withCurrentFreeText: freeText) == true {
- listView?.commitEditAnnotationFreeText(freeText)
- }
- }
- annotationPopMode.setAnnotationFontColor(annotationFontColor: color)
- fontColor = color
-
- updatePDFViewCallback?()
- }
- }
- }
- //MARK: - ComponentCColorDelegate
- extension KMNPopAnnotationViewController: ComponentCColorDelegate {
- func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
- if(view == generaColorGroup) {
-
- self.annotationPopMode.setAnnotationColor(annotationColor: color)
- updatePDFViewCallback?()
- } else if (view == shapeColorGroup) {
- self.annotationPopMode.setAnnotationColor(annotationColor: color)
- updatePDFViewCallback?()
- } else 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: - ComponentInputDelegate
- extension KMNPopAnnotationViewController: ComponentInputDelegate {
- func componentInputDidChanged(inputView: ComponentInput) {
- if (inputView == fildNameInput) {
- for i in 0 ..< annotationPopMode.annotations.count {
- let annotation = annotationPopMode.annotations[i]
- if let widgt = annotation as? CPDFWidgetAnnotation {
- widgt.setFieldName(inputView.properties.text)
- }
- }
- updatePDFViewCallback?()
- } else if (inputView == gropNameInput) {
- for i in 0 ..< annotationPopMode.annotations.count {
- let annotation = annotationPopMode.annotations[i]
- if let button = annotation as? CPDFButtonWidgetAnnotation {
- button.setButtonWidgetStateString(inputView.properties.text)
- }
- }
-
- updatePDFViewCallback?()
- } else if (inputView == urlInput) {
- if linkType == .email {
- let emailString = urlInput.properties.text
- if KMNTools.isValidateEmail(emailString) {
- linkAnnotation.setDestination(nil)
-
- let linkUrlPath = KMNTools.judgeEmailURL(emailString)
- linkAnnotation.setURL(linkUrlPath)
- updatePDFViewCallback?()
-
- }
- } else if linkType == .url {
- let webString = urlInput.properties.text
- linkAnnotation.setDestination(nil)
- let linkUrlPath = KMNTools.judgeWebURL(webString)
- linkAnnotation.setURL(linkUrlPath)
- updatePDFViewCallback?()
- }
- }
- }
-
- func componentInputDidEndEditing(inputView: ComponentInput) {
- if (inputView == fildNameInput) {
-
- } else if (inputView == gropNameInput) {
-
- } else if (inputView == urlInput) {
-
-
- }
- }
- }
- //MARK: - ComponentSelectDelegate
- extension KMNPopAnnotationViewController: 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 ?? "")
- annotationPopMode.setAnnotationFont(font: compdfFont ?? CPDFFont.init(familyName: "Helvetica", fontStyle: ""))
- updatePDFViewCallback?()
- } else if menuItemProperty?.identifier == "2"{ //字体样式
- compdfFont = CPDFFont(familyName: compdfFont?.familyName ?? "Helvetica", fontStyle: menuItemProperty?.text ?? "")
- annotationPopMode.setAnnotationFont(font: compdfFont ?? CPDFFont.init(familyName: "Helvetica", fontStyle: ""))
-
- updatePDFViewCallback?()
- }
- }
- }
- }
- //MARK: - ComponentSelectDelegate
- extension KMNPopAnnotationViewController: 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()
- }
- }
-
- }
- }
- //MARK: - ComponentPaginationDelegate
- extension KMNPopAnnotationViewController: ComponentPaginationDelegate {
- public func componentPaginationDidValueChanged(pagination: ComponentPagination) {
- let pageIndex = pagination.properties.currentIndex
- if let page = listView?.document.page(at: UInt(pageIndex)) {
-
- linkAnnotation.setURL(nil)
- let destination = CPDFDestination(document: listView?.document ?? CPDFDocument(), pageIndex: Int(paginationView.properties.currentIndex - 1))
- linkAnnotation.setDestination(destination)
-
- refreshPageThum()
- reloadPageUrlData()
- updatePDFViewCallback?()
- }
- }
- }
|