KMPageRangePickerWindowController.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // KMPageRangePickerWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/11/25.
  6. //
  7. import Cocoa
  8. typealias KMPageRangePickerWindowControllerCancelClick = (_ window: NSWindow)->()
  9. typealias KMPageRangePickerWindowControllerConfirmClick = (_ windowController: NSWindowController)->()
  10. class KMPageRangePickerWindowController: NSWindowController {
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var allPageButton: NSButton!
  13. @IBOutlet weak var oddPagesButton: NSButton!
  14. @IBOutlet weak var evenPagesButton: NSButton!
  15. @IBOutlet weak var customPagesButton: NSButton!
  16. @IBOutlet weak var customPagesTextField: NSTextField!
  17. @IBOutlet weak var cancelButton: NSButton!
  18. @IBOutlet weak var confirmButton: NSButton!
  19. var fileModel: KMMergeFileModel!
  20. var selectIndex: Int = 0
  21. var cancelClick: KMPageRangePickerWindowControllerCancelClick!
  22. var confirmClick: KMPageRangePickerWindowControllerConfirmClick!
  23. override func windowDidLoad() {
  24. super.windowDidLoad()
  25. titleLabel.stringValue = NSLocalizedString("页面范围", comment: "")
  26. allPageButton.title = NSLocalizedString("全部页面", comment: "")
  27. allPageButton.target = self
  28. allPageButton.action = #selector(allPageButtonAction)
  29. oddPagesButton.title = NSLocalizedString("奇数页面", comment: "")
  30. oddPagesButton.target = self
  31. oddPagesButton.action = #selector(oddPagesButtonAction)
  32. evenPagesButton.title = NSLocalizedString("偶数页面", comment: "")
  33. evenPagesButton.target = self
  34. evenPagesButton.action = #selector(evenPagesButtonAction)
  35. customPagesButton.title = NSLocalizedString("自定义", comment: "")
  36. customPagesButton.target = self
  37. customPagesButton.action = #selector(customPagesButtonAction)
  38. customPagesTextField.placeholderString = NSLocalizedString("如1,3-5,10", comment: "")
  39. customPagesTextField.isEnabled = false
  40. cancelButton.title = NSLocalizedString("Cancel", comment: "")
  41. cancelButton.target = self
  42. cancelButton.action = #selector(cancelButtonAction)
  43. confirmButton.title = NSLocalizedString("Confirm", comment: "")
  44. confirmButton.target = self
  45. confirmButton.action = #selector(confirmButtonAction)
  46. selectIndex = 0;
  47. allPageButton.state = .on
  48. }
  49. @objc func allPageButtonAction() {
  50. for button in [allPageButton, oddPagesButton, evenPagesButton, customPagesButton] {
  51. button?.state = .off
  52. }
  53. allPageButton.state = .on
  54. window?.makeFirstResponder(nil)
  55. customPagesTextField.isEnabled = false
  56. selectIndex = 0
  57. }
  58. @objc func oddPagesButtonAction() {
  59. for button in [allPageButton, oddPagesButton, evenPagesButton, customPagesButton] {
  60. button?.state = .off
  61. }
  62. oddPagesButton.state = .on
  63. window?.makeFirstResponder(nil)
  64. customPagesTextField.isEnabled = false
  65. selectIndex = 1
  66. }
  67. @objc func evenPagesButtonAction() {
  68. for button in [allPageButton, oddPagesButton, evenPagesButton, customPagesButton] {
  69. button?.state = .off
  70. }
  71. evenPagesButton.state = .on
  72. window?.makeFirstResponder(nil)
  73. customPagesTextField.isEnabled = false
  74. selectIndex = 2
  75. }
  76. @objc func customPagesButtonAction() {
  77. for button in [allPageButton, oddPagesButton, evenPagesButton, customPagesButton] {
  78. button?.state = .off
  79. }
  80. customPagesButton.state = .on
  81. customPagesTextField.isEnabled = true
  82. window?.makeFirstResponder(customPagesTextField)
  83. selectIndex = 3
  84. }
  85. @objc func cancelButtonAction() {
  86. guard let callback = cancelClick else {
  87. return
  88. }
  89. callback(window!)
  90. }
  91. @objc func confirmButtonAction() {
  92. if selectIndex == 3 && findSelectPage(pagesString: self.customPagesTextField.stringValue).count == 0 {
  93. let alert = NSAlert()
  94. alert.alertStyle = .warning
  95. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  96. alert.runModal()
  97. return
  98. }
  99. guard let callback = confirmClick else {
  100. return
  101. }
  102. callback(self)
  103. }
  104. func isValidPagesString(pagesString: String)-> Bool {
  105. var valid = false
  106. for ch in pagesString {
  107. if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" && ch != "," && ch != "-" {
  108. valid = false
  109. break
  110. } else {
  111. valid = true
  112. }
  113. }
  114. return valid
  115. }
  116. func findSelectPage(pagesString: String) -> ([Int]) {
  117. if !isValidPagesString(pagesString: pagesString) {
  118. return []
  119. }
  120. var result: [Int] = []
  121. let array = pagesString.components(separatedBy: ",")
  122. for string in array {
  123. if string.isEmpty {
  124. return []
  125. } else {
  126. let pages = string .components(separatedBy: "-")
  127. if pages.count > 2 {
  128. return []
  129. } else if pages.count == 1 {
  130. let page = pages[0]
  131. if page.isEmpty || Int(page)! > fileModel.document.pageCount || Int(page)! == 0 {
  132. return []
  133. } else {
  134. var hasSame: Bool = false
  135. for i in result {
  136. if i == Int(page)! {
  137. hasSame = true
  138. return []
  139. }
  140. }
  141. if !hasSame {
  142. result.append(Int(page)!)
  143. }
  144. }
  145. } else if pages.count == 2 {
  146. let page1 = pages[0]
  147. let page2 = pages[1]
  148. if page1.isEmpty || page2.isEmpty || page1 >= page2 || Int(page2)! > fileModel.myDocument.pageCount || Int(page1)! == 0 {
  149. return []
  150. } else {
  151. var hasSame: Bool = false
  152. for i in Int(page1)! ... Int(page2)! {
  153. for j in result {
  154. if j == i {
  155. hasSame = true
  156. return []
  157. }
  158. }
  159. }
  160. if !hasSame {
  161. for i in Int(page1)! ... Int(page2)! {
  162. result.append(i)
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. return result
  170. }
  171. }