// // KMHeaderFooterManagerWindowController.swift // PDF Reader Pro // // Created by liujiajie on 2023/11/6. // import Cocoa typealias HeaderFooterOperateCallBack = (_ obj: KMHeaderFooterObject) -> () class KMHeaderFooterManagerWindowController: NSWindowController{ var operateCallBack: HeaderFooterOperateCallBack? var isBates: Bool = false var onlyManagerTemplate: Bool = false lazy var filePath: String = { var str = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") return str ?? "" }() var password: String = "" var type:KMBatchModifyTemplateType = .Add var pdfdocument: CPDFDocument? lazy var headerFooterObj: KMHeaderFooterObject? = { let obj = KMHeaderFooterObject() obj.isBates = self.isBates if self.isBates { obj.id = KMHeaderFooterManager.defaultManager.fetchBatesAvailableName() }else { obj.id = KMHeaderFooterManager.defaultManager.fetchHeaderFooterAvailableName() } initialID = obj.id return obj }() var originalHeaderFooterObj: KMHeaderFooterObject? lazy var dateFormatArray: Array = { let arr = KMHeaderFooterManager.defaultManager.dateFormatArray return arr }() var initialID: String? @IBOutlet var pdfView: KMWatermarkPDFView! @IBOutlet var currentPageIndexTextField: NSTextField! @IBOutlet var totalPageCountLabel: NSTextField! @IBOutlet var textBox: NSBox! @IBOutlet var textLabel: NSTextField! @IBOutlet var fontSizeLabel: NSTextField! @IBOutlet var fontSizeCombobox: NSComboBox! @IBOutlet var textColorLabel: NSTextField! @IBOutlet var textColorWell: NSColorWell! @IBOutlet var marginBox: NSBox! @IBOutlet var marginLabel: NSTextField! @IBOutlet var topMarginLabel: NSTextField! @IBOutlet var topMargintextField: NSTextField! @IBOutlet var topMarginStepper: NSStepper! @IBOutlet var bottomMarginLabel: NSTextField! @IBOutlet var bottomMarginTextField: NSTextField! @IBOutlet var bottomMarginStepper: NSStepper! @IBOutlet var leftMarginLabel: NSTextField! @IBOutlet var leftMarginTextField: NSTextField! @IBOutlet var leftMarginStepper: NSStepper! @IBOutlet var rightMarginLabel: NSTextField! @IBOutlet var rightMarginTextField: NSTextField! @IBOutlet var rightMarginStepper: NSStepper! @IBOutlet var formatBox: NSBox! @IBOutlet var formatLabel: NSTextField! @IBOutlet var dateFormateLabel: NSTextField! @IBOutlet var dateFormateCombobox: NSComboBox! @IBOutlet var pageLabel: NSTextField! @IBOutlet var pageCombobox: NSComboBox! @IBOutlet var beginPageLabel: NSTextField! @IBOutlet var beginPageCombobox: NSComboBox! @IBOutlet var topLeftLabel: NSTextField! @IBOutlet var topLeftTextView: NSTextView! @IBOutlet var topCenterLabel: NSTextField! @IBOutlet var topCenterTextView: NSTextView! @IBOutlet var topRightLabel: NSTextField! @IBOutlet var topRightTextView: NSTextView! @IBOutlet var bottomLeftLabel: NSTextField! @IBOutlet var bottomLeftTextView: NSTextView! @IBOutlet var bottomCenterLabel: NSTextField! @IBOutlet var BottomCenterTextView: NSTextView! @IBOutlet var bottomRightLabel: NSTextField! @IBOutlet var bottomRightTextView: NSTextView! @IBOutlet var pageRangeLabel: NSTextField! @IBOutlet var pageRangeCombobox: NSComboBox! @IBOutlet var saveToTemplateButton: NSButton! @IBOutlet var templateNameLabel: NSTextField! @IBOutlet var templateNameTextField: NSTextField! @IBOutlet var topLeftButton: NSButton! @IBOutlet var topCentButton: NSButton! @IBOutlet var topRightButton: NSButton! @IBOutlet var bottomLeftButton: NSButton! @IBOutlet var bottomCenterButton: NSButton! @IBOutlet var bottomRightButton: NSButton! @IBOutlet var batchButton: NSButton! @IBOutlet var cancelButton: NSButton! @IBOutlet var applyButton: NSButton! @IBOutlet var batesBox: NSBox! @IBOutlet var prefixLabel: NSTextField! @IBOutlet var prefixTextField: NSTextField! @IBOutlet var suffixLabel: NSTextField! @IBOutlet var suffixTextField: NSTextField! @IBOutlet var batesNumberLabel: NSTextField! @IBOutlet var batesNumberTextField: NSTextField! @IBOutlet var batesNumberStepper: NSStepper! @IBOutlet var batesStartPageLabel: NSTextField! @IBOutlet var batesStartpageCombobox: NSComboBox! @IBOutlet var constraint: NSLayoutConstraint! deinit { NotificationCenter.default.removeObserver(self) DistributedNotificationCenter.default().removeObserver(self) } convenience init(baseFile:String, obj:KMHeaderFooterObject, password:String, type:KMBatchModifyTemplateType) { self.init(windowNibName: "KMHeaderFooterManagerWindowController") self.filePath = baseFile self.password = password self.headerFooterObj = obj self.originalHeaderFooterObj = obj self.initialID = obj.id self.type = type self.pdfdocument = CPDFDocument(url: URL(fileURLWithPath: self.filePath)) if let data = self.pdfdocument?.isLocked, data { self.pdfdocument?.unlock(withPassword: password) } if let data = self.pdfdocument?.isLocked, data { return } } override func windowDidLoad() { super.windowDidLoad() configuUI() loadData() NotificationCenter.default.addObserver(self, selector: #selector(pageChangeNotification(notification:)), name: NSNotification.Name.CPDFViewPageChanged, object: nil) // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil) DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil) } func configuUI() { topLeftButton.wantsLayer = true topCentButton.wantsLayer = true topRightButton.wantsLayer = true bottomLeftButton.wantsLayer = true bottomCenterButton.wantsLayer = true bottomRightButton.wantsLayer = true topLeftButton.layer?.borderWidth = 1.0 topCentButton.layer?.borderWidth = 1.0 topRightButton.layer?.borderWidth = 1.0 bottomLeftButton.layer?.borderWidth = 1.0 bottomCenterButton.layer?.borderWidth = 1.0 bottomRightButton.layer?.borderWidth = 1.0 topLeftButton.layer?.cornerRadius = 1.0 topCentButton.layer?.cornerRadius = 1.0 topRightButton.layer?.cornerRadius = 1.0 bottomLeftButton.layer?.cornerRadius = 1.0 bottomCenterButton.layer?.cornerRadius = 1.0 bottomRightButton.layer?.cornerRadius = 1.0 updateViewColor() let textViewArr: [NSTextView] = [topLeftTextView, topCenterTextView, topRightTextView, bottomLeftTextView, BottomCenterTextView, bottomRightTextView] for textView in textViewArr { textView.wantsLayer = true // textView.layer?.borderWidth = 1.0 // textView.layer?.borderColor = NSColor.gridColor.cgColor } textColorWell.color = headerFooterObj?.getTextColor() ?? .black pdfView.document = pdfdocument // pdfView. = headerFooterObj pdfView.autoScales = true // pdfView.documentView?.enclosingScrollView?.hasVerticalScroller = false // pdfView.documentView?.enclosingScrollView?.hasHorizontalScroller = false // // currentPageIndexTextField.stringValue = NSLocalizedString("1", comment: "") // batesStartPageLabel.stringValue = String(format: "%@:", NSLocalizedString("Start Page Number", comment: "")) // let numberFormatter = currentPageIndexTextField.formatter as? NSNumberFormatter // numberFormatter?.maximum = NSNumber(value: pdfdocument.pageCount) // totalPageCountLabel.stringValue = String(format: "/ %ld", pdfdocument.pageCount) // // previewLabel.stringValue = NSLocalizedString("Preview", comment: "") textLabel.stringValue = NSLocalizedString("Font Size", comment: "") fontSizeLabel.stringValue = String(format: "%@:", NSLocalizedString("Size", comment: "")) textColorLabel.stringValue = String(format: "%@:", NSLocalizedString("Color", comment: "")) marginLabel.stringValue = NSLocalizedString("Margin", comment: "") topMarginLabel.stringValue = String(format: "%@:", NSLocalizedString("Top", comment: "")) bottomMarginLabel.stringValue = String(format: "%@:", NSLocalizedString("Bottom", comment: "")) leftMarginLabel.stringValue = String(format: "%@:", NSLocalizedString("Left", comment: "")) rightMarginLabel.stringValue = String(format: "%@:", NSLocalizedString("Right", comment: "")) topMarginLabel.toolTip = String(format: "%@:", NSLocalizedString("Top", comment: "")) bottomMarginLabel.toolTip = String(format: "%@:", NSLocalizedString("Bottom", comment: "")) leftMarginLabel.toolTip = String(format: "%@:", NSLocalizedString("Left", comment: "")) rightMarginLabel.toolTip = String(format: "%@:", NSLocalizedString("Right", comment: "")) topMarginLabel.allowsExpansionToolTips = true bottomMarginLabel.allowsExpansionToolTips = true leftMarginLabel.allowsExpansionToolTips = true rightMarginLabel.allowsExpansionToolTips = true formatLabel.stringValue = NSLocalizedString("Page Number and Date Format", comment: "") dateFormateLabel.stringValue = String(format: "%@:", NSLocalizedString("Date", comment: "")) pageLabel.stringValue = String(format: "%@:", NSLocalizedString("Page", comment: "")) beginPageLabel.stringValue = String(format: "%@:", NSLocalizedString("Start Page Number", comment: "")) formatLabel.toolTip = NSLocalizedString("Page Number and Date Format", comment: "") dateFormateLabel.toolTip = String(format: "%@:", NSLocalizedString("Date", comment: "")) pageLabel.toolTip = String(format: "%@:", NSLocalizedString("Page", comment: "")) beginPageLabel.toolTip = String(format: "%@:", NSLocalizedString("Start Page Number", comment: "")) topLeftLabel.stringValue = NSLocalizedString("Left Header Text", comment: "") topCenterLabel.stringValue = NSLocalizedString("Center Header Text", comment: "") topRightLabel.stringValue = NSLocalizedString("Right Header Text", comment: "") bottomLeftLabel.stringValue = NSLocalizedString("Left Footer Text", comment: "") bottomCenterLabel.stringValue = NSLocalizedString("Center Footer Text", comment: "") bottomRightLabel.stringValue = NSLocalizedString("Right Footer Text", comment: "") topLeftLabel.toolTip = NSLocalizedString("Left Header Text", comment: "") topCenterLabel.toolTip = NSLocalizedString("Center Header Text", comment: "") topRightLabel.toolTip = NSLocalizedString("Right Header Text", comment: "") bottomLeftLabel.toolTip = NSLocalizedString("Left Footer Text", comment: "") } func loadData(){ self.leftMarginTextField.stringValue = String(format: "%ld", self.headerFooterObj?.leftMargin ?? 0) self.rightMarginTextField.stringValue = String(format: "%ld", self.headerFooterObj?.rightMargin ?? 0) self.topMargintextField.stringValue = String(format: "%ld", self.headerFooterObj?.topMargin ?? 0) self.bottomMarginTextField.stringValue = String(format: "%ld", self.headerFooterObj?.bottomMargin ?? 0) self.leftMarginStepper.integerValue = Int(self.headerFooterObj?.leftMargin ?? 0) self.rightMarginStepper.integerValue = Int(self.headerFooterObj?.rightMargin ?? 0) self.topMarginStepper.integerValue = Int(self.headerFooterObj?.topMargin ?? 0) self.bottomMarginStepper.integerValue = Int(self.headerFooterObj?.bottomMargin ?? 0) var fontSizeArray = NSMutableArray() for i in 0.. 0 { let array = sting?.components(separatedBy: ",") ?? [] var pagesString = "" for (i, index) in array.enumerated() { if i == array.count - 1 { pagesString += "(Int(index)! + 1)" } else { pagesString += "(Int(index)! + 1)," } } self.pageRangeCombobox.stringValue = pagesString } } self.topLeftTextView.string = self.headerFooterObj?.topLeftString ?? "" self.topCenterTextView.string = self.headerFooterObj?.topCenterString ?? "" self.topRightTextView.string = self.headerFooterObj?.topRightString ?? "" self.bottomLeftTextView.string = self.headerFooterObj?.bottomLeftString ?? "" self.BottomCenterTextView.string = self.headerFooterObj?.bottomCenterString ?? "" self.bottomRightTextView.string = self.headerFooterObj?.bottomRightString ?? "" self.templateNameTextField.stringValue = self.headerFooterObj?.id ?? "" if self.topLeftTextView.string.count <= 0 && self.topCenterTextView.string.count <= 0 && self.topRightTextView.string.count <= 0 && self.bottomLeftTextView.string.count <= 0 && self.BottomCenterTextView.string.count <= 0 && self.bottomRightTextView.string.count <= 0 { self.applyButton.isEnabled = false } else { self.applyButton.isEnabled = true } } @objc func pageChangeNotification(notification: Notification) { currentPageIndexTextField.stringValue = "\(pdfView.currentPage().pageIndex() + 1)" } @objc func themeChanged(notification: Notification) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self.updateViewColor() } } func updateViewColor() { if KMAppearance.isDarkMode() { topLeftButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor topCentButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor topRightButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor bottomLeftButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor bottomCenterButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor bottomRightButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor topLeftButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor topCentButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor topRightButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor bottomLeftButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor bottomCenterButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor bottomRightButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor } else { topLeftButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor topCentButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor topRightButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor bottomLeftButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor bottomCenterButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor bottomRightButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor topLeftButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor topCentButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor topRightButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor bottomLeftButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor bottomCenterButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor bottomRightButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor } } }