KMMergeSettingWindowController.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // KMMergeSettingWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/11/25.
  6. //
  7. import Cocoa
  8. typealias KMMergeSettingWindowControllerCancelClick = (_ window: NSWindow)->()
  9. typealias KMMergeSettingWindowControllerMergeClick = (_ windowController: NSWindowController)->()
  10. class KMMergeSettingWindowController: NSWindowController {
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var odd_even_mergeComboBox: NSButton!
  13. @IBOutlet weak var helpButton: NSButton!
  14. @IBOutlet weak var pageSizeLabel: NSTextField!
  15. @IBOutlet weak var pageSizeComboBox: NSComboBox!
  16. @IBOutlet weak var customSizeView: NSView!
  17. @IBOutlet weak var customWitdhView: NSView!
  18. @IBOutlet weak var customWidthTextField: NSTextField!
  19. @IBOutlet weak var customHeigthView: NSView!
  20. @IBOutlet weak var customHeightTextField: NSTextField!
  21. @IBOutlet weak var mergeButton: NSButton!
  22. @IBOutlet weak var cancelButton: NSButton!
  23. var selectedIndex: Int = 0
  24. var cancelClick: KMMergeSettingWindowControllerCancelClick!
  25. var mergeClick: KMMergeSettingWindowControllerMergeClick!
  26. override func windowDidLoad() {
  27. super.windowDidLoad()
  28. titleLabel.stringValue = NSLocalizedString("合并设置", comment: "")
  29. odd_even_mergeComboBox.title = NSLocalizedString("奇偶页面穿插合并", comment: "")
  30. odd_even_mergeComboBox.state = .off
  31. helpButton.target = self
  32. helpButton.action = #selector(helpButtonAction)
  33. pageSizeLabel.stringValue = NSLocalizedString("页面大小", comment: "")
  34. pageSizeComboBox.stringValue = NSLocalizedString("原始页面大小", comment: "")
  35. pageSizeComboBox.removeAllItems()
  36. pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("原始页面大小", comment: ""),
  37. NSLocalizedString("A4", comment: ""),
  38. NSLocalizedString("A3", comment: ""),
  39. NSLocalizedString("U.S.Letter", comment: ""),
  40. NSLocalizedString("U.S.Legal", comment: ""),
  41. NSLocalizedString("自定义(595*841mm)", comment: "")])
  42. pageSizeComboBox.delegate = self
  43. pageSizeComboBox.isEditable = false
  44. customSizeView.isHidden = true
  45. for view in [customWitdhView, customHeigthView] {
  46. view?.wantsLayer = true
  47. view?.layer?.borderWidth = 1
  48. view?.layer?.borderColor = NSColor.black.cgColor
  49. view?.layer?.cornerRadius = 4
  50. }
  51. for textField in [customWidthTextField, customHeightTextField] {
  52. textField?.focusRingType = .none
  53. textField?.delegate = self
  54. }
  55. mergeButton.title = NSLocalizedString("合并", comment: "")
  56. mergeButton.target = self
  57. mergeButton.action = #selector(mergeButtonAction)
  58. cancelButton.title = NSLocalizedString("取消", comment: "")
  59. cancelButton.target = self
  60. cancelButton.action = #selector(cancelButtonAction)
  61. }
  62. @objc func mergeButtonAction() {
  63. guard let callback = mergeClick else {
  64. return
  65. }
  66. callback(self)
  67. }
  68. @objc func cancelButtonAction() {
  69. guard let callback = cancelClick else {
  70. return
  71. }
  72. callback(self.window!)
  73. }
  74. @objc func helpButtonAction() {
  75. let popover = NSPopover()
  76. popover.behavior = .transient
  77. let controller = KMMergePopoverViewController.init(nibName: "KMMergePopoverViewController", bundle: nil)
  78. popover.contentViewController = controller
  79. popover .show(relativeTo: helpButton.bounds, of: helpButton, preferredEdge: .maxY)
  80. }
  81. }
  82. extension KMMergeSettingWindowController: NSComboBoxDelegate {
  83. func comboBoxSelectionDidChange(_ notification: Notification) {
  84. if notification.object as! NSComboBox != pageSizeComboBox {
  85. return
  86. }
  87. let box: NSComboBox = notification.object as! NSComboBox
  88. let index = box.indexOfSelectedItem
  89. if index < 0 {
  90. return
  91. }
  92. selectedIndex = index
  93. if index == 5 {
  94. self.customSizeView.isHidden = false
  95. let value: String = pageSizeComboBox.itemObjectValue(at: pageSizeComboBox.numberOfItems-1) as! String
  96. let array = value.components(separatedBy: "*")
  97. if array.count <= 1 {
  98. self.customWidthTextField.stringValue = "595"
  99. self.customHeightTextField.stringValue = "841"
  100. return
  101. }
  102. var heigth: String = ""
  103. heigth = (array.last?.components(separatedBy: "mm").first!)!
  104. var width: String = ""
  105. width = (array.first?.components(separatedBy: NSLocalizedString("(", comment: "")).last)!
  106. self.customWidthTextField.stringValue = width
  107. self.customHeightTextField.stringValue = heigth
  108. } else {
  109. self.customSizeView.isHidden = true
  110. }
  111. }
  112. func controlTextDidChange(_ obj: Notification) {
  113. if customWidthTextField.isEqual(to: obj.object) {
  114. let textField: NSTextField = obj.object as! NSTextField
  115. var value: String = ""
  116. for ch in textField.stringValue {
  117. if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" {
  118. } else {
  119. if value.isEmpty && ch == "0" {
  120. } else {
  121. value.append(ch)
  122. }
  123. }
  124. }
  125. customWidthTextField.stringValue = value
  126. pageSizeComboBox.removeAllItems()
  127. var string: String = ""
  128. string.append(NSLocalizedString("自定义", comment: ""))
  129. string.append("(")
  130. string.append(customWidthTextField.stringValue)
  131. string.append("*")
  132. string.append(customHeightTextField.stringValue)
  133. string.append("mm)")
  134. pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("原始页面大小", comment: ""),
  135. "A4",
  136. "A3",
  137. "U.S.Letter",
  138. "U.S.Legal",
  139. string])
  140. pageSizeComboBox.stringValue = string
  141. } else if customHeightTextField.isEqual(to: obj.object) {
  142. let textField: NSTextField = obj.object as! NSTextField
  143. var value: String = ""
  144. for ch in textField.stringValue {
  145. if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" {
  146. } else {
  147. if value.isEmpty && ch == "0" {
  148. } else {
  149. value.append(ch)
  150. }
  151. }
  152. }
  153. customHeightTextField.stringValue = value
  154. pageSizeComboBox.removeAllItems()
  155. var string: String = ""
  156. string.append(NSLocalizedString("自定义", comment: ""))
  157. string.append("(")
  158. string.append(customWidthTextField.stringValue)
  159. string.append("*")
  160. string.append(customHeightTextField.stringValue)
  161. string.append("mm)")
  162. pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("原始页面大小", comment: ""),
  163. "A4",
  164. "A3",
  165. "U.S.Letter",
  166. "U.S.Legal",
  167. string])
  168. pageSizeComboBox.stringValue = string
  169. }
  170. }
  171. }