KMOutlineEditViewController.swift 7.1 KB

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