123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- //
- // 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: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "")
- 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<Any> = {
- 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..<self.fontSizeCombobox.objectValues.count {
- let obj = self.fontSizeCombobox.objectValues[i] as! String
- fontSizeArray.add(NSNumber(integerLiteral: Int(obj) ?? 0))
- }
- self.fontSizeCombobox.removeAllItems()
- self.fontSizeCombobox.addItems(withObjectValues: fontSizeArray as! [Any])
- let index = self.fontSizeCombobox.indexOfItem(withObjectValue: self.headerFooterObj?.getTextFontSize() ?? 0.0)
- if (index == NSNotFound) {
- self.fontSizeCombobox.selectItem(at: index)
- } else {
- self.fontSizeCombobox.integerValue = Int(self.headerFooterObj?.getTextFontSize() ?? 0)
- }
- self.dateFormateCombobox.removeAllItems()
- self.dateFormateCombobox.addItems(withObjectValues: self.dateFormatArray)
- let obj = self.headerFooterObj?.dateFormatString
- var isContain: Bool = (((self.dateFormateCombobox.objectValues as? [String])?.contains(obj ?? "")) != nil)
-
- if isContain {
- self.dateFormateCombobox.selectItem(withObjectValue: obj)
- }
- let obj1 = self.headerFooterObj?.pageFormatString
- var isContain1: Bool = (((self.pageCombobox.objectValues as? [String])?.contains(obj1 ?? "")) != nil)
- if isContain1{
- self.pageCombobox.selectItem(withObjectValue: obj1)
- } else {
- self.pageCombobox.stringValue = ""
- }
- var pages = [String]()
- for i in 0..<(self.pdfdocument?.pageCount ?? 0) {
- pages.append("\(i+1)")
- }
- self.beginPageCombobox.formatter = TextFieldFormatter()
- self.beginPageCombobox.isEditable = false
- self.beginPageCombobox.removeAllItems()
- self.beginPageCombobox.addItems(withObjectValues: pages)
- let obj2 = self.headerFooterObj?.startString
- var isContain2: Bool = (((self.beginPageCombobox.objectValues as? [String])?.contains(obj2 ?? "")) != nil)
- if isContain2{
- self.beginPageCombobox.selectItem(withObjectValue: obj2)
- }
-
- self.batesStartpageCombobox.formatter = TextFieldFormatter()
- self.batesStartpageCombobox.isEditable = false
- self.batesStartpageCombobox.removeAllItems()
- self.batesStartpageCombobox.addItems(withObjectValues: pages)
- let obj3 = self.headerFooterObj?.startString
- var isContain3: Bool = (((self.batesStartpageCombobox.objectValues as? [String])?.contains(obj3 ?? "")) != nil)
- if isContain3 {
- self.batesStartpageCombobox.selectItem(withObjectValue: obj3)
- }
- if .all == self.headerFooterObj?.pageRangeType {
- self.pageRangeCombobox.isEditable = false
- self.pageRangeCombobox.selectItem(at: 0)
- } else if .odd == self.headerFooterObj?.pageRangeType {
- self.pageRangeCombobox.isEditable = false
- self.pageRangeCombobox.selectItem(at: 1)
- } else if .even == self.headerFooterObj?.pageRangeType {
- self.pageRangeCombobox.isEditable = false
- self.pageRangeCombobox.selectItem(at: 2)
- } else if .other == self.headerFooterObj?.pageRangeType {
- self.pageRangeCombobox.isEditable = true
- self.pageRangeCombobox.selectItem(at: 3)
- self.pageRangeCombobox.stringValue = self.headerFooterObj?.pageRangeString ?? ""
- let sting = self.headerFooterObj?.pageRangeString
- if sting?.count ?? 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
- }
- }
- }
|