123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- //
- // KMOutlineEditViewController.swift
- // PDF Reader Pro
- //
- // 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!
-
- weak var outline : CPDFOutline?
- weak var pdfView : CPDFListView?
- var originalURLString : String = ""
- weak var originalDestination : CPDFDestination?
- var originalPageIndex = -1
- var originalLabel : String = ""
- var currentPageIndex : Int = 0
-
- convenience init(outline: CPDFOutline?, document: CPDFListView?) {
- self.init()
-
- self.outline = outline
- self.pdfView = document
- }
-
- override func loadView() {
- super.loadView()
-
- self.localizedLanguage()
-
- self.setONButton(button: self.pageButton)
- self.enableTextField(textField: self.outlineTargetPageIndexTextField)
-
- self.setDelegateAndFormatter()
- if let action = self.outline?.action {
- self.adjustUIWithAction(action: action)
- }
- }
-
- //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!) {
- guard let doc = self.pdfView?.document else {
- return
- }
-
- if action == nil {
- self.totalPageCountLabel.stringValue = " \(doc.pageCount)"
- self.outlineTargetPageIndexTextField.stringValue = " \(self.currentPageIndex + 1)"
- } else {
- guard let outL = self.outline else {
- return
- }
- if action.isKind(of: CPDFURLAction.self) {
- var newAction : CPDFURLAction = self.outline?.action as! CPDFURLAction
- var urlString : String = newAction.url()
- self.originalURLString = urlString
-
- 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(doc.index(for: doc.page(at: UInt(self.pdfView!.currentPageIndex))))
- } else {
- self.setONButton(button: self.pageButton)
- self.enableTextField(textField: self.outlineTargetPageIndexTextField)
- self.currentPageIndex = Int(doc.index(for: doc.page(at: UInt(outL.destination.pageIndex))))
- self.originalDestination = outL.destination
- self.originalPageIndex = self.originalDestination?.pageIndex ?? -1
- }
- self.originalLabel = outL.label
- self.totalPageCountLabel.stringValue = " /\(doc.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 ?? 0)
- 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 = KMAppearance.Layout.h0Color()
- self.outlineURLTextField.textColor = KMAppearance.Layout.h0Color()
- self.mailAddressTextField.textColor = KMAppearance.Layout.h0Color()
-
- textField.isEditable = true
- textField.isSelectable = true
- textField.textColor = KMAppearance.Layout.h0Color()
- }
-
- //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 ?? ""
- }
- }
- }
|