KMFileInsertPositionWindowController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // KMFileInsertPositionWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/6/12.
  6. //
  7. import Cocoa
  8. class KMFileInsertPositionWindowController: NSWindowController {
  9. @IBOutlet var positionLabel: NSTextField!
  10. @IBOutlet var firstButton: NSButton!
  11. @IBOutlet var lastButton: NSButton!
  12. @IBOutlet var pageButton: NSButton!
  13. @IBOutlet var pageTextField: NSTextField!
  14. @IBOutlet var byPageStepper: NSStepper!
  15. @IBOutlet var pageCountLabel: NSTextField!
  16. @IBOutlet var locationRangePopUpButton: NSPopUpButton!
  17. @IBOutlet var cancelButton: NSButton!
  18. @IBOutlet var insertButton: NSButton!
  19. var pdfDocument: CPDFDocument?
  20. var currentPage: Int = 1
  21. var insertLocation: Int = 0
  22. private var _password: String?
  23. private var _insertPageIndex: Int = 0
  24. var callback: ((CPDFDocument?, String?, _ pageIndex: Int) -> Void)?
  25. deinit {
  26. #if DEBUG
  27. print("\(self.className) deinit")
  28. #endif
  29. }
  30. convenience init(document: CPDFDocument) {
  31. self.init(windowNibName: "KMFileInsertPositionWindowController")
  32. // self.type = KMPDFEditPageEdit;
  33. self.pdfDocument = document;
  34. }
  35. override func windowDidLoad() {
  36. super.windowDidLoad()
  37. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  38. self.positionLabel.stringValue = KMLocalizedString("Position", nil)
  39. self.firstButton.title = KMLocalizedString("First", nil)
  40. self.lastButton.title = KMLocalizedString("Last", nil)
  41. self.pageButton.title = KMLocalizedString("Page", nil)
  42. let menu = NSMenu()
  43. _ = menu.addItem(title: KMLocalizedString("After", nil), action: #selector(after_Action), target: self)
  44. _ = menu.addItem(title: KMLocalizedString("Before", nil), action: #selector(before_Action), target: self)
  45. self.locationRangePopUpButton.menu = menu
  46. self.locationRangePopUpButton.isEnabled = false
  47. self.locationRangePopUpButton.isEnabled = false
  48. self.pageTextField.isEditable = false
  49. self.byPageStepper.isEnabled = false
  50. self.insertLocation = 1
  51. if (self.insertLocation == 2) {
  52. self.lastButton.state = .on
  53. } else if (self.insertLocation == 3) {
  54. self.pageButton.state = .on
  55. self.locationRangePopUpButton.isEnabled = true
  56. self.pageTextField.isEditable = true
  57. self.byPageStepper.isEnabled = true
  58. } else {
  59. self.firstButton.state = .on
  60. }
  61. if let data = self.pdfDocument?.isLocked, data {
  62. DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
  63. }
  64. } else {
  65. self.byPageStepper.minValue = 1.0
  66. self.byPageStepper.maxValue = Double(self.pdfDocument?.pageCount ?? 0)
  67. self.pageCountLabel.stringValue = String(format: "/%ld", self.pdfDocument?.pageCount ?? 0)
  68. }
  69. self.pageTextField.formatter = TextFieldFormatter()
  70. self.pageTextField.delegate = self
  71. }
  72. //MARK: IBAction
  73. @IBAction func buttonItemClicked_Location(_ sender: AnyObject?) {
  74. if (self.pageButton.isEqual(to: sender)) {
  75. self.pageTextField.isEditable = true
  76. self.locationRangePopUpButton.isEnabled = true
  77. self.byPageStepper.isEnabled = true
  78. } else{
  79. self.pageTextField.isEditable = false
  80. self.locationRangePopUpButton.isEnabled = false
  81. self.byPageStepper.isEnabled = false
  82. }
  83. }
  84. @IBAction func byPageStepperAction(_ sender: AnyObject?) {
  85. self.pageTextField.stringValue = String(format: "%ld", self.byPageStepper.integerValue)
  86. }
  87. @IBAction func buttonItemClicked_Cancel(_ sender: AnyObject?) {
  88. self.pdfDocument = nil
  89. guard let block = self.callback else {
  90. return
  91. }
  92. block(self.pdfDocument, nil, -1)
  93. }
  94. @IBAction func buttonItemClicked_Insert(_ sender: AnyObject?) {
  95. self.window?.makeFirstResponder(nil)
  96. var index = 0
  97. if (self.firstButton.state == .on) {
  98. index = 0
  99. } else if (self.lastButton.state == .on) {
  100. index = Int(self.pdfDocument?.pageCount ?? 0)
  101. } else {
  102. index = Int(self.pageTextField.stringValue) ?? 0
  103. if ((1 == self.locationRangePopUpButton.indexOfSelectedItem)) {
  104. index = index - 1
  105. }
  106. }
  107. self._insertPageIndex = index
  108. guard let block = self.callback else {
  109. return
  110. }
  111. block(self.pdfDocument, self._password, self._insertPageIndex)
  112. }
  113. @IBAction func after_Action(_ sender: AnyObject?) {
  114. }
  115. @IBAction func before_Action(_ sender: AnyObject?) {
  116. }
  117. }
  118. extension KMFileInsertPositionWindowController: NSTextFieldDelegate {
  119. func controlTextDidChange(_ obj: Notification) {
  120. let textField = obj.object as? NSTextField
  121. if (textField == self.pageTextField) {
  122. if let sValue = Int(textField!.stringValue), sValue <= 0 {
  123. self.pageTextField.stringValue = "1"
  124. self.byPageStepper.integerValue = 1
  125. } else if let sValue = Double(textField!.stringValue), sValue > self.byPageStepper.maxValue {
  126. self.pageTextField.stringValue = String(format: "%.0f", self.byPageStepper.maxValue)
  127. self.byPageStepper.doubleValue = self.byPageStepper.maxValue
  128. } else {
  129. self.byPageStepper.integerValue = Int(self.pageTextField.stringValue) ?? 0
  130. }
  131. }
  132. }
  133. }