KMConvertImageSettingView.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // KMConvertImageSettingView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/6.
  6. //
  7. import KMComponentLibrary
  8. typealias KMConvertImageSettingViewImageTypeAction = (_ selectedIndex: Int) -> Void
  9. typealias KMConvertImageSettingViewResolutionAction = (_ selectedIndex: Int) -> Void
  10. class KMConvertImageSettingView: KMConvertSettingView {
  11. @IBOutlet weak var fileTypeTitleLabel: NSTextField!
  12. @IBOutlet weak var formatBox: NSBox!
  13. @IBOutlet weak var pageRangeBox: NSBox!
  14. @IBOutlet weak var dpiTitleLabel: NSTextField!
  15. @IBOutlet weak var dpiSelectBox: NSBox!
  16. var selectedFormatIndex = 0
  17. var selectedResolutionIndex = 3
  18. var imageTypeAction: KMConvertImageSettingViewImageTypeAction?
  19. var resolutionAction: KMConvertImageSettingViewResolutionAction?
  20. private lazy var formatSelect_: ComponentSelect = {
  21. let view = ComponentSelect()
  22. view.properties = ComponentSelectProperties(size: .s,
  23. state: .normal,
  24. isDisabled: false,
  25. isError: false,
  26. leftIcon: false,
  27. placeholder: nil,
  28. errorText: nil,
  29. creatable: false,
  30. text: NSLocalizedString("", comment: ""))
  31. view.delegate = self
  32. return view
  33. }()
  34. private lazy var dpiSelect_: ComponentSelect = {
  35. let view = ComponentSelect()
  36. view.properties = ComponentSelectProperties(size: .s,
  37. state: .normal,
  38. isDisabled: false,
  39. isError: false,
  40. leftIcon: false,
  41. placeholder: nil,
  42. errorText: nil,
  43. creatable: false,
  44. text: NSLocalizedString("", comment: ""))
  45. view.delegate = self
  46. return view
  47. }()
  48. override func initSubViews() {
  49. super.initSubViews()
  50. self.formatBox.contentView = self.formatSelect_
  51. var menuItemArr: [ComponentMenuitemProperty] = []
  52. for string in ["JPEG","JPG","PNG","GIF","TIFF","TGA","BMP","JPEG-2000"] {
  53. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  54. itemSelected: false,
  55. isDisabled: false,
  56. keyEquivalent: nil,
  57. text: string)
  58. menuItemArr.append(itemProperty)
  59. }
  60. formatSelect_.updateMenuItemsArr(menuItemArr)
  61. formatSelect_.selectItemAtIndex(0)
  62. formatSelect_.properties.creatable = false
  63. formatSelect_.properties.text = "JPEG"
  64. formatSelect_.reloadData()
  65. self.dpiSelectBox.contentView = self.dpiSelect_
  66. var menuItemArr2: [ComponentMenuitemProperty] = []
  67. for string in ["50 dpi", "72 dpi", "96 dpi", "150 dpi", "300 dpi", "600 dpi"] {
  68. let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  69. itemSelected: false,
  70. isDisabled: false,
  71. keyEquivalent: nil,
  72. text: string)
  73. menuItemArr2.append(itemProperty)
  74. }
  75. dpiSelect_.updateMenuItemsArr(menuItemArr2)
  76. dpiSelect_.selectItemAtIndex(3)
  77. dpiSelect_.properties.creatable = false
  78. dpiSelect_.properties.text = "150 dpi"
  79. dpiSelect_.reloadData()
  80. self.tipView?.km_add_left_constraint(constant: 8)
  81. self.tipView?.km_add_top_constraint(equalTo: self.pageRangeBox, attribute: .bottom, constant: -5)
  82. self.tipView?.km_add_right_constraint()
  83. self.tipView?.km_add_height_constraint(constant: 16+20+16)
  84. }
  85. override func initDefaultVlaue() {
  86. self.formatBox.fillColor = .clear
  87. self.fileTypeTitleLabel.stringValue = NSLocalizedString("Format", comment: "")
  88. self.fileTypeTitleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  89. self.formatBox.fillColor = .clear
  90. }
  91. override func viewDidMoveToWindow() {
  92. super.viewDidMoveToWindow()
  93. if (self.pageRangeItemView == nil) {
  94. self.pageRangeItemView = KMConvertPageRangeSettingItemView.createFromNib()
  95. self.pageRangeBox.contentView = self.pageRangeItemView
  96. self.pageRangeItemView?.titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  97. self.updateViewColor()
  98. }
  99. self.pageRangeItemView?.pageCount = self.pageCount
  100. self.pageRangeItemView?.isHidden = self.isBatch
  101. }
  102. override func getPageRangeString() -> String {
  103. if let view = self.pageRangeItemView {
  104. return view.pageRangeValue
  105. }
  106. return ""
  107. }
  108. override func updateViewColor() {
  109. super.updateViewColor()
  110. KMMainThreadExecute {
  111. self.fileTypeTitleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  112. self.pageRangeItemView?.titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  113. }
  114. }
  115. override func reloadData() {
  116. formatSelect_.properties.isDisabled = self.isDisable
  117. dpiSelect_.properties.isDisabled = self.isDisable
  118. }
  119. func selectConvertType(convertType: KMPDFConvertType) {
  120. if convertType == .jpeg {
  121. self.selectedFormatIndex = 0
  122. } else if convertType == .jpg {
  123. self.selectedFormatIndex = 1
  124. } else if convertType == .png {
  125. self.selectedFormatIndex = 2
  126. } else if convertType == .gif {
  127. self.selectedFormatIndex = 3
  128. } else if convertType == .tiff {
  129. self.selectedFormatIndex = 4
  130. } else if convertType == .tga {
  131. self.selectedFormatIndex = 5
  132. } else if convertType == .bmp {
  133. self.selectedFormatIndex = 6
  134. } else if convertType == .jp2 {
  135. self.selectedFormatIndex = 7
  136. }
  137. formatSelect_.selectItemAtIndex(self.selectedFormatIndex)
  138. }
  139. }
  140. extension KMConvertImageSettingView: ComponentSelectDelegate {
  141. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  142. guard let index = view?.indexOfSelect(), index >= 0 else {
  143. return
  144. }
  145. if self.formatSelect_.isEqual(to: view) {
  146. self.selectedFormatIndex = index
  147. imageTypeAction?(self.selectedFormatIndex)
  148. } else if self.dpiSelect_.isEqual(to: view) {
  149. self.selectedResolutionIndex = max(index, 0)
  150. resolutionAction?(self.selectedResolutionIndex)
  151. }
  152. }
  153. }