KMOutlineEditViewController.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // KMOutlineEditViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/11/9.
  6. //
  7. import Cocoa
  8. class KMOutlineEditViewController: NSViewController {
  9. @IBOutlet var outlineNameTextView: NSTextView!
  10. @IBOutlet weak var outlineNameLabel: NSTextField!
  11. @IBOutlet weak var outlineTargetPageIndexTextField: NSTextField!
  12. @IBOutlet weak var pageButton: NSButton!
  13. @IBOutlet weak var totalPageCountLabel: NSTextField!
  14. @IBOutlet weak var outlineURLTextField: NSTextField!
  15. @IBOutlet weak var urlButton: NSButton!
  16. @IBOutlet weak var mailAddressTextField: NSTextField!
  17. @IBOutlet weak var mailButton: NSButton!
  18. weak var outline : CPDFOutline?
  19. weak var pdfView : CPDFListView?
  20. var originalURLString : String = ""
  21. weak var originalDestination : CPDFDestination?
  22. var originalPageIndex = -1
  23. var originalLabel : String = ""
  24. var currentPageIndex : Int = 0
  25. convenience init(outline: CPDFOutline?, document: CPDFListView?) {
  26. self.init()
  27. self.outline = outline
  28. self.pdfView = document
  29. }
  30. override func loadView() {
  31. super.loadView()
  32. self.localizedLanguage()
  33. self.setONButton(button: self.pageButton)
  34. self.enableTextField(textField: self.outlineTargetPageIndexTextField)
  35. self.setDelegateAndFormatter()
  36. if let action = self.outline?.action {
  37. self.adjustUIWithAction(action: action)
  38. }
  39. }
  40. //MARK: - Private Methods
  41. private func localizedLanguage () {
  42. self.outlineNameLabel.stringValue = "\(NSLocalizedString("Label", comment: ""))"
  43. self.pageButton.title = "\(NSLocalizedString("Page", comment: ""))"
  44. self.urlButton.title = "\(NSLocalizedString("URL:", comment: ""))"
  45. self.outlineURLTextField.placeholderString = "\(NSLocalizedString("https://www.pdfreaderpro.com", comment: ""))"
  46. self.mailButton.title = "Email:"
  47. self.mailAddressTextField.placeholderString = "\(NSLocalizedString("support@pdfreaderpro.com", comment: ""))"
  48. self.outlineNameTextView.string = self.outline?.label ?? ""
  49. }
  50. private func adjustUIWithAction(action:CPDFAction!) {
  51. guard let doc = self.pdfView?.document else {
  52. return
  53. }
  54. if action == nil {
  55. self.totalPageCountLabel.stringValue = " \(doc.pageCount)"
  56. self.outlineTargetPageIndexTextField.stringValue = " \(self.currentPageIndex + 1)"
  57. } else {
  58. guard let outL = self.outline else {
  59. return
  60. }
  61. if action.isKind(of: CPDFURLAction.self) {
  62. var newAction : CPDFURLAction = self.outline?.action as! CPDFURLAction
  63. var urlString = newAction.url() ?? ""
  64. self.originalURLString = urlString
  65. if urlString.hasPrefix("mailto:") {
  66. self.setONButton(button: self.mailButton)
  67. self.enableTextField(textField: self.mailAddressTextField)
  68. urlString = String(urlString.suffix(from: "mailto:".endIndex))
  69. self.mailAddressTextField.stringValue = urlString
  70. } else {
  71. self.setONButton(button: self.urlButton)
  72. self.enableTextField(textField: self.outlineURLTextField)
  73. self.outlineURLTextField.stringValue = urlString
  74. }
  75. self.currentPageIndex = Int(doc.index(for: doc.page(at: UInt(self.pdfView!.currentPageIndex))))
  76. } else {
  77. self.setONButton(button: self.pageButton)
  78. self.enableTextField(textField: self.outlineTargetPageIndexTextField)
  79. self.currentPageIndex = Int(doc.index(for: doc.page(at: UInt(outL.destination.pageIndex))))
  80. self.originalDestination = outL.destination
  81. self.originalPageIndex = self.originalDestination?.pageIndex ?? -1
  82. }
  83. self.originalLabel = outL.label ?? ""
  84. self.totalPageCountLabel.stringValue = " /\(doc.pageCount)"
  85. self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
  86. }
  87. }
  88. private func setDelegateAndFormatter () {
  89. self.outlineTargetPageIndexTextField.delegate = self
  90. self.outlineNameTextView.delegate = self
  91. var formatter = NumberFormatter()
  92. formatter.numberStyle = .none
  93. formatter.maximum = NSNumber(value: self.pdfView?.document?.pageCount ?? 0)
  94. formatter.minimum = NSNumber(value: 1)
  95. self.outlineTargetPageIndexTextField.formatter = formatter
  96. }
  97. private func setONButton(button:NSButton) {
  98. self.pageButton.state = NSControl.StateValue.off
  99. self.urlButton.state = NSControl.StateValue.off
  100. self.mailButton.state = NSControl.StateValue.off
  101. button.state = NSControl.StateValue.on
  102. }
  103. private func enableTextField(textField:NSTextField) {
  104. self.outlineTargetPageIndexTextField.isEditable = false
  105. self.outlineTargetPageIndexTextField.isSelectable = false
  106. self.outlineURLTextField.isEditable = false
  107. self.outlineURLTextField.isSelectable = false
  108. self.mailAddressTextField.isEditable = false
  109. self.mailAddressTextField.isSelectable = false
  110. self.outlineTargetPageIndexTextField.textColor = KMAppearance.Layout.h0Color()
  111. self.outlineURLTextField.textColor = KMAppearance.Layout.h0Color()
  112. self.mailAddressTextField.textColor = KMAppearance.Layout.h0Color()
  113. textField.isEditable = true
  114. textField.isSelectable = true
  115. textField.textColor = KMAppearance.Layout.h0Color()
  116. }
  117. //MARK: Button Action
  118. @IBAction func buttonClicked_GotoAction(_ sender: Any) {
  119. self.setONButton(button: self.pageButton)
  120. self.enableTextField(textField: self.outlineTargetPageIndexTextField)
  121. self.view.window?.makeFirstResponder(self.outlineTargetPageIndexTextField)
  122. }
  123. @IBAction func buttonClicked_URLAction(_ sender: Any) {
  124. self.setONButton(button: self.urlButton)
  125. self.enableTextField(textField: self.outlineURLTextField)
  126. self.view.window?.makeFirstResponder(self.outlineURLTextField)
  127. if (self.outlineTargetPageIndexTextField.stringValue == "0" || self.outlineTargetPageIndexTextField.stringValue.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) < 1){
  128. self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
  129. }
  130. }
  131. @IBAction func buttonClicked_MailAction(_ sender: Any) {
  132. self.setONButton(button: self.mailButton)
  133. self.enableTextField(textField: self.mailAddressTextField)
  134. self.view.window?.makeFirstResponder(self.mailAddressTextField)
  135. if (self.outlineTargetPageIndexTextField.stringValue == "0" || self.outlineTargetPageIndexTextField.stringValue.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) < 1){
  136. self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
  137. }
  138. }
  139. }
  140. extension KMOutlineEditViewController : NSTextFieldDelegate,NSTextViewDelegate {
  141. func controlTextDidChange(_ obj: Notification) {
  142. if self.outlineTargetPageIndexTextField.isEqual(obj.object) {
  143. let number = NSNumber(value: Int(self.outlineTargetPageIndexTextField.stringValue) ?? 0)
  144. let string = self.outlineTargetPageIndexTextField.formatter?.string(for: number)
  145. let s = Int(string ?? "")
  146. if s != 0 {
  147. self.currentPageIndex = (s ?? 0) - 1
  148. }
  149. self.outlineTargetPageIndexTextField.stringValue = string ?? ""
  150. }
  151. }
  152. }