KMPDFMergePageRangeTabelViewCell.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // KMPDFMergePageRangeTabelViewCell.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/10/11.
  6. //
  7. import Cocoa
  8. @objcMembers class KMPDFMergePageRangeTabelViewCell: NSTableCellView {
  9. var pageCount: Int = 0
  10. var callback: ((Int) -> Void)?
  11. var textDidChange: ((String) -> Void)?
  12. var textDidEndEdit: ((String, KMPDFMergePageRangeTabelViewCell) -> Void)?
  13. var comboBox: KMMergeSelect?
  14. override init(frame frameRect: NSRect) {
  15. super.init(frame: frameRect)
  16. self.initSubviews()
  17. }
  18. required init?(coder: NSCoder) {
  19. super.init(coder: coder)
  20. self.initSubviews()
  21. }
  22. override var isFlipped: Bool {
  23. return true
  24. }
  25. func initSubviews() {
  26. let comboBox = KMMergeSelect(withType: .Combox)
  27. self.addSubview(comboBox.view)
  28. self.comboBox = comboBox
  29. self.comboBox?.removeAllItems()
  30. self.comboBox?.addItems(withObjectValues: [KMLocalizedString("All Pages", nil),KMLocalizedString("Odd Pages", nil),KMLocalizedString("Even Pages", nil),KMLocalizedString("Customized", nil)])
  31. self.comboBox?.stringValue = KMLocalizedString("All Pages", nil)
  32. self.comboBox?.editable = false
  33. self.comboBox?.delete = self
  34. self.comboBox?.placeholderString = KMLocalizedString("eg .1,3-5,10", nil)
  35. }
  36. override func layout() {
  37. super.layout()
  38. let width = NSWidth(self.bounds)
  39. let height = NSHeight(self.bounds)
  40. let comboBoxH = 26.0
  41. self.comboBox?.view.frame = NSMakeRect(10, (height-comboBoxH)*0.5, width-10, comboBoxH)
  42. }
  43. func updatePageRange(_ pageRange: KMPageRange, pageStrings: String, isFirstResponser: Bool = true) {
  44. if (pageRange == .all) {
  45. self.comboBox?.stringValue = KMLocalizedString("All Pages", nil)
  46. self.comboBox?.editable = false
  47. self.comboBox?.alert = false
  48. } else if (pageRange == .odd) {
  49. self.comboBox?.stringValue = KMLocalizedString("Odd Pages", nil)
  50. self.comboBox?.editable = false
  51. self.comboBox?.alert = false
  52. } else if (pageRange == .even) {
  53. self.comboBox?.stringValue = KMLocalizedString("Even Pages", nil)
  54. self.comboBox?.editable = false
  55. self.comboBox?.alert = false
  56. } else {
  57. self.comboBox?.stringValue = pageStrings
  58. self.comboBox?.editable = true
  59. if (pageStrings.isEmpty) {
  60. self.comboBox?.alert = true
  61. } else {
  62. self.comboBox?.alert = false
  63. }
  64. if (isFirstResponser || pageStrings.isEmpty) {
  65. if let data = self.window?.firstResponder, data.isEqual(to: self.comboBox?.textField) == false {
  66. if (Thread.isMainThread) {
  67. self.window?.makeFirstResponder(self.comboBox?.textField)
  68. } else {
  69. DispatchQueue.main.async {
  70. self.window?.makeFirstResponder(self.comboBox?.textField)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. /*
  78. - (void)updateRageRangeState:(NSInteger)state {
  79. if (state == 9) {
  80. self.comboBox.state = KMDesignTokenStateError_def;
  81. } else if (state == 10) {
  82. self.comboBox.state = KMDesignTokenStateError_focus;
  83. }
  84. }
  85. */
  86. }
  87. extension KMPDFMergePageRangeTabelViewCell: KMSelectPopButtonDelegate {
  88. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  89. guard let data = self.callback else {
  90. return
  91. }
  92. var index = obj.indexOfSelectedItem
  93. if (index < 0) {
  94. index = 0
  95. }
  96. if (index == obj.numberOfItems-1) {
  97. self.comboBox?.editable = true
  98. DispatchQueue.main.async {
  99. self.comboBox?.stringValue = ""
  100. }
  101. self.window?.makeFirstResponder(obj.textField)
  102. } else {
  103. self.comboBox?.editable = false
  104. self.comboBox?.alert = false
  105. }
  106. data(index)
  107. }
  108. func km_controlTextDidChange(_ obj: KMDesignSelect) {
  109. guard let data = self.textDidChange else {
  110. return
  111. }
  112. data(obj.stringValue)
  113. }
  114. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  115. guard let data = self.textDidEndEdit else {
  116. return
  117. }
  118. data(obj.stringValue, self)
  119. }
  120. func km_SelectPopoverWillShow(_ obj: KMDesignSelect) {
  121. if (obj.isEqual(to: self.comboBox)) {
  122. var disableItems: [String] = []
  123. if (self.pageCount <= 1) {
  124. disableItems.append(KMLocalizedString("Even Pages", nil))
  125. }
  126. self.comboBox?.disItems = disableItems
  127. }
  128. }
  129. }