// // KMOutlineEditViewController.swift // PDF Master // // Created by lxy on 2022/11/9. // import Cocoa class KMOutlineEditViewController: NSViewController { @IBOutlet var outlineNameTextView: NSTextView! @IBOutlet weak var outlineNameLabel: NSTextField! @IBOutlet weak var outlineTargetPageIndexTextField: NSTextField! @IBOutlet weak var pageButton: NSButton! @IBOutlet weak var totalPageCountLabel: NSTextField! @IBOutlet weak var outlineURLTextField: NSTextField! @IBOutlet weak var urlButton: NSButton! @IBOutlet weak var mailAddressTextField: NSTextField! @IBOutlet weak var mailButton: NSButton! var outline : CPDFOutline! var pdfView : CPDFListView! var originalURLString : String! var originalDestination : CPDFDestination! var originalLabel : String! var currentPageIndex : Int! convenience init(outline:CPDFOutline!,document:CPDFListView!) { self.init() self.outline = outline self.pdfView = document } //MARK: Life cycle override func loadView() { super.loadView() self.localizedLanguage() } //MARK: Private Methods private func localizedLanguage () { self.outlineNameLabel.stringValue = "\(NSLocalizedString("Label", comment: ""))" self.pageButton.title = "\(NSLocalizedString("Page", comment: ""))" self.urlButton.title = "\(NSLocalizedString("URL:", comment: ""))" self.outlineURLTextField.placeholderString = "\(NSLocalizedString("https://www.pdfreaderpro.com", comment: ""))" self.mailButton.title = "Email:" self.mailAddressTextField.placeholderString = "\(NSLocalizedString("support@pdfreaderpro.com", comment: ""))" self.outlineNameTextView.string = self.outline.label } private func adjustUIWithAction(action:CPDFAction!) { if action == nil { self.totalPageCountLabel.stringValue = " \(self.pdfView.document.pageCount)" self.outlineTargetPageIndexTextField.stringValue = " \(self.currentPageIndex + 1)" } else { if action.isKind(of: CPDFURLAction.self) { var newAction : CPDFURLAction = self.outline.action as! CPDFURLAction self.originalURLString = newAction.url() var urlString : String = newAction.url() if urlString.hasPrefix("mailto:") { self.setONButton(button: self.mailButton) self.enableTextField(textField: self.mailAddressTextField) urlString = String(urlString.suffix(from: "mailto:".endIndex)) self.mailAddressTextField.stringValue = urlString } else { self.setONButton(button: self.urlButton) self.enableTextField(textField: self.outlineURLTextField) self.outlineURLTextField.stringValue = urlString } self.currentPageIndex = Int(self.pdfView.document.index(for: self.pdfView.document.page(at: UInt(self.pdfView.currentPageIndex)))) } else { self.setONButton(button: self.pageButton) self.enableTextField(textField: self.outlineTargetPageIndexTextField) self.currentPageIndex = Int(self.pdfView.document.index(for: self.pdfView.document.page(at: UInt(self.outline.destination.pageIndex)))) self.originalDestination = self.outline.destination } self.originalLabel = self.outline.label self.totalPageCountLabel.stringValue = " /\(self.pdfView.document.pageCount)" self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)" } } private func setDelegateAndFormatter () { self.outlineTargetPageIndexTextField.delegate = self self.outlineNameTextView.delegate = self var formatter = NumberFormatter() formatter.numberStyle = .none formatter.maximum = NSNumber(value: self.pdfView.document.pageCount) formatter.minimum = NSNumber(value: 1) self.outlineTargetPageIndexTextField.formatter = formatter } private func setONButton(button:NSButton) { self.pageButton.state = NSControl.StateValue.off self.urlButton.state = NSControl.StateValue.off self.mailButton.state = NSControl.StateValue.off button.state = NSControl.StateValue.on } private func enableTextField(textField:NSTextField) { self.outlineTargetPageIndexTextField.isEditable = false self.outlineTargetPageIndexTextField.isSelectable = false self.outlineURLTextField.isEditable = false self.outlineURLTextField.isSelectable = false self.mailAddressTextField.isEditable = false self.mailAddressTextField.isSelectable = false self.outlineTargetPageIndexTextField.textColor = NSColor.red self.outlineURLTextField.textColor = NSColor.red self.mailAddressTextField.textColor = NSColor.red textField.isEditable = false textField.isSelectable = false textField.textColor = NSColor.black } //MARK: Button Action @IBAction func buttonClicked_GotoAction(_ sender: Any) { self.setONButton(button: self.pageButton) self.enableTextField(textField: self.outlineTargetPageIndexTextField) self.view.window?.makeFirstResponder(self.outlineTargetPageIndexTextField) } @IBAction func buttonClicked_URLAction(_ sender: Any) { self.setONButton(button: self.urlButton) self.enableTextField(textField: self.outlineURLTextField) self.view.window?.makeFirstResponder(self.outlineURLTextField) if (self.outlineTargetPageIndexTextField.stringValue == "0" || self.outlineTargetPageIndexTextField.stringValue.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) < 1){ self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)" } } @IBAction func buttonClicked_MailAction(_ sender: Any) { self.setONButton(button: self.mailButton) self.enableTextField(textField: self.mailAddressTextField) self.view.window?.makeFirstResponder(self.mailAddressTextField) if (self.outlineTargetPageIndexTextField.stringValue == "0" || self.outlineTargetPageIndexTextField.stringValue.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) < 1){ self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)" } } } extension KMOutlineEditViewController : NSTextFieldDelegate,NSTextViewDelegate { func controlTextDidChange(_ obj: Notification) { if self.outlineTargetPageIndexTextField.isEqual(obj.object) { let number = NSNumber(value: Int(self.outlineTargetPageIndexTextField.stringValue) ?? 0) let string = self.outlineTargetPageIndexTextField.formatter?.string(for: number) let s = Int(string ?? "") if s != 0 { self.currentPageIndex = (s ?? 0) - 1 } self.outlineTargetPageIndexTextField.stringValue = string ?? "" } } }