KMConvertPageRangeSettingItemView.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // KMConvertPageRangeSettingItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/6.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMConvertPageRangeSettingItemViewTextDidEndEditing = (_ textString: String) -> ()
  10. class KMConvertPageRangeSettingItemView: KMConvertBaseView {
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var pageSizeComboBox: NSComboBox!
  13. @IBOutlet weak var pageSizeBox: NSBox!
  14. @IBOutlet weak var pagesizeBoxHeight: NSLayoutConstraint!
  15. var pageSizeVC: KMDesignSelect?
  16. var pageRangeValue: String = ""
  17. var itemClick: KMCommonClickBlock?
  18. var textDidEndEditingCallback: KMConvertPageRangeSettingItemViewTextDidEndEditing?
  19. var isTextDidEndCallback: Bool = false
  20. var pageCount = 0
  21. private lazy var pageRangeView_: ComponentSelect = {
  22. let view = ComponentSelect()
  23. view.properties = ComponentSelectProperties(size: .s,
  24. state: .normal,
  25. isDisabled: false,
  26. isError: false,
  27. leftIcon: false,
  28. placeholder: nil,
  29. errorText: nil,
  30. creatable: false,
  31. text: KMLocalizedString("Odd Pages Only", comment: ""))
  32. view.delegate = self
  33. return view
  34. }()
  35. override func initSubViews() {
  36. super.initSubViews()
  37. self.pageSizeBox.contentView = self.pageRangeView_
  38. let strings = [KMTools.pageRangeTypeString(pageRange: .all),
  39. KMTools.pageRangeTypeString(pageRange: .current),
  40. KMTools.pageRangeTypeString(pageRange: .odd),
  41. KMTools.pageRangeTypeString(pageRange: .even),
  42. KMTools.pageRangeTypeString(pageRange: .custom)]
  43. var menuItemArr: [ComponentMenuitemProperty] = []
  44. for str in strings {
  45. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  46. itemSelected: false,
  47. isDisabled: false,
  48. keyEquivalent: nil,
  49. text: str)
  50. menuItemArr.append(itemProperty)
  51. }
  52. pageRangeView_.updateMenuItemsArr(menuItemArr)
  53. pageRangeView_.selectItemAtIndex(0)
  54. pageRangeView_.properties.creatable = false
  55. pageRangeView_.properties.text = menuItemArr.first?.text
  56. pageRangeView_.reloadData()
  57. }
  58. override func initDefaultVlaue() {
  59. super.initDefaultVlaue()
  60. self.pageSizeBox.fillColor = .clear
  61. self.titleLabel.stringValue = NSLocalizedString("Page Range", comment: "")
  62. self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
  63. self.titleLabel.font = .SFProTextSemiboldFont(12)
  64. }
  65. }
  66. extension KMConvertPageRangeSettingItemView: KMSelectPopButtonDelegate {
  67. func km_controlTextDidChange(_ obj: KMDesignSelect) {
  68. if let vc = self.pageSizeVC {
  69. self.pageRangeValue = vc.stringValue
  70. }
  71. }
  72. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  73. guard let vc = self.pageSizeVC else {
  74. return
  75. }
  76. if (!self.isTextDidEndCallback) {
  77. return
  78. }
  79. if (vc.indexOfSelectedItem != (vc.numberOfItems-1)) {
  80. /// 其他item切换
  81. return
  82. }
  83. guard let callback = self.textDidEndEditingCallback else {
  84. return
  85. }
  86. callback(vc.stringValue)
  87. }
  88. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  89. guard let vc = self.pageSizeVC else {
  90. return
  91. }
  92. let index = vc.indexOfSelectedItem
  93. if index < 0 {
  94. return
  95. }
  96. if index == vc.numberOfItems-1 {
  97. self.pageSizeVC?.editable = true
  98. DispatchQueue.main.async {
  99. self.pageSizeVC?.stringValue = ""
  100. }
  101. self.window?.makeFirstResponder(self.pageSizeVC?.textField)
  102. } else {
  103. self.window?.makeFirstResponder(nil)
  104. self.pageSizeVC?.editable = false
  105. }
  106. guard let callback = self.itemClick else {
  107. return
  108. }
  109. callback(index)
  110. }
  111. func km_SelectPopoverWillShow(_ obj: KMDesignSelect) {
  112. if (self.pageCount <= 1) {
  113. self.pageSizeVC?.disItems = [KMTools.pageRangeTypeString(pageRange: .even)]
  114. } else {
  115. self.pageSizeVC?.disItems = []
  116. }
  117. }
  118. }
  119. extension KMConvertPageRangeSettingItemView: ComponentSelectDelegate {
  120. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  121. if(view == pageRangeView_) {
  122. let pageRangeSelectIndex = pageRangeView_.indexOfSelect()
  123. var isEditIndex:Int = 4
  124. if (pageRangeSelectIndex == isEditIndex) {
  125. pageRangeView_.properties.text = ""
  126. pageRangeView_.properties.placeholder = KMLocalizedString("e.g. 1,3-5,10")
  127. pageRangeView_.properties.creatable = true
  128. } else {
  129. pageRangeView_.properties.placeholder = ""
  130. pageRangeView_.properties.creatable = false
  131. }
  132. pageRangeView_.reloadData()
  133. guard let callback = self.itemClick else {
  134. return
  135. }
  136. callback(pageRangeSelectIndex)
  137. }
  138. }
  139. func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
  140. guard let callback = self.textDidEndEditingCallback else {
  141. return
  142. }
  143. callback(view.properties.text ?? "")
  144. }
  145. }