KMConvertPageRangeSettingItemView.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // KMConvertPageRangeSettingItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/6.
  6. //
  7. import Cocoa
  8. typealias KMConvertPageRangeSettingItemViewTextDidEndEditing = (_ textString: String) -> ()
  9. class KMConvertPageRangeSettingItemView: KMConvertBaseView {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var pageSizeComboBox: NSComboBox!
  12. @IBOutlet weak var pageSizeBox: NSBox!
  13. @IBOutlet weak var pagesizeBoxHeight: NSLayoutConstraint!
  14. var pageSizeVC: KMDesignSelect?
  15. var pageRangeValue: String = ""
  16. var itemClick: KMCommonClickBlock?
  17. var textDidEndEditingCallback: KMConvertPageRangeSettingItemViewTextDidEndEditing?
  18. var isTextDidEndCallback: Bool = false
  19. var pageCount = 0
  20. override func initSubViews() {
  21. super.initSubViews()
  22. self.pageSizeVC = KMDesignSelect.init(withType: .Combox)
  23. self.pageSizeBox.contentView = self.pageSizeVC!.view
  24. self.pageSizeVC?.removeAllItems()
  25. self.pageSizeVC?.addItems(withObjectValues: [KMTools.pageRangeTypeString(pageRange: .all),
  26. KMTools.pageRangeTypeString(pageRange: .current),
  27. KMTools.pageRangeTypeString(pageRange: .odd),
  28. KMTools.pageRangeTypeString(pageRange: .even),
  29. KMTools.pageRangeTypeString(pageRange: .custom)])
  30. self.pageSizeVC?.selectItem(at: 0)
  31. self.pageSizeVC?.editable = false //默认属性,初始化可不设置
  32. self.pageSizeVC?.delete = self
  33. self.pageSizeVC?.placeholderString = NSLocalizedString("eg. 1,3-5,10", comment: "")
  34. self.pageSizeVC?.updateUI()
  35. }
  36. override func initDefaultVlaue() {
  37. super.initDefaultVlaue()
  38. self.pageSizeBox.fillColor = .clear
  39. self.titleLabel.stringValue = NSLocalizedString("Page Range", comment: "")
  40. self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
  41. self.titleLabel.font = .SFProTextSemiboldFont(12)
  42. }
  43. }
  44. extension KMConvertPageRangeSettingItemView: KMSelectPopButtonDelegate {
  45. func km_controlTextDidChange(_ obj: KMDesignSelect) {
  46. if let vc = self.pageSizeVC {
  47. self.pageRangeValue = vc.stringValue
  48. }
  49. }
  50. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  51. guard let vc = self.pageSizeVC else {
  52. return
  53. }
  54. if (!self.isTextDidEndCallback) {
  55. return
  56. }
  57. if (vc.indexOfSelectedItem != (vc.numberOfItems-1)) {
  58. /// 其他item切换
  59. return
  60. }
  61. guard let callback = self.textDidEndEditingCallback else {
  62. return
  63. }
  64. callback(vc.stringValue)
  65. }
  66. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  67. guard let vc = self.pageSizeVC else {
  68. return
  69. }
  70. let index = vc.indexOfSelectedItem
  71. if index < 0 {
  72. return
  73. }
  74. if index == vc.numberOfItems-1 {
  75. self.pageSizeVC?.editable = true
  76. DispatchQueue.main.async {
  77. self.pageSizeVC?.stringValue = ""
  78. }
  79. self.window?.makeFirstResponder(self.pageSizeVC?.textField)
  80. } else {
  81. self.window?.makeFirstResponder(nil)
  82. self.pageSizeVC?.editable = false
  83. }
  84. guard let callback = self.itemClick else {
  85. return
  86. }
  87. callback(index)
  88. }
  89. func km_SelectPopoverWillShow(_ obj: KMDesignSelect) {
  90. if (self.pageCount <= 1) {
  91. self.pageSizeVC?.disItems = [KMTools.pageRangeTypeString(pageRange: .even)]
  92. } else {
  93. self.pageSizeVC?.disItems = []
  94. }
  95. }
  96. }