123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- //
- // 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 ?? ""
- }
- }
- }
|