KMCompressContentView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // KMCompressContentView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/11/18.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMBatchOperateCompressViewSelectTypeBlcok = (_ view: KMCompressContentView, _ model: KMCompressSettingModel) -> Void
  10. typealias KMBatchOperateCompressViewCompressAction = (_ view: KMCompressContentView, _ model: KMCompressSettingModel, _ showView: NSView) -> Void
  11. class KMCompressContentView: BaseXibView {
  12. @IBOutlet weak var standardButton: ComponentRadio!
  13. @IBOutlet weak var mobileButton: ComponentRadio!
  14. @IBOutlet weak var customButton: ComponentRadio!
  15. @IBOutlet weak var tipButton: ComponentToolTipsHelp!
  16. @IBOutlet weak var settingButton: NSButton!
  17. @IBOutlet weak var mobileWidthConstraint: NSLayoutConstraint!
  18. @IBOutlet weak var customButtonWidthConstraint: NSLayoutConstraint!
  19. var isDisable: Bool = false {
  20. didSet {
  21. self.reloadData()
  22. }
  23. }
  24. var compressSettingViewWindow: NSWindow?
  25. var faterWindow: NSWindow?
  26. var compressSettingViewController: KMCompressSettingViewController?
  27. var compressAction: KMBatchOperateCompressViewCompressAction?
  28. var popOver: NSPopover!
  29. var selectTypeAction: KMBatchOperateCompressViewSelectTypeBlcok?
  30. var model: KMCompressSettingModel = KMCompressSettingModel(modelsType: .standard) {
  31. didSet {
  32. self.reloadData()
  33. }
  34. }
  35. override func draw(_ dirtyRect: NSRect) {
  36. super.draw(dirtyRect)
  37. // Drawing code here.
  38. }
  39. override func setup() {
  40. updateUI()
  41. updateUILanguage()
  42. reloadData()
  43. }
  44. override func updateUILanguage() {
  45. standardButton.properties.text = KMLocalizedString("Standard (Recommend)", comment: "")
  46. mobileButton.properties.text = KMLocalizedString("Mobile", comment: "")
  47. customButton.properties.text = KMLocalizedString("Custom", comment: "")
  48. settingButton.stringValue = KMLocalizedString("Setting", comment: "")
  49. tipButton.toolTip = KMLocalizedString("This mode is specifically tuned for mobile devices to improve compression quality and ensure clear viewing on mobile devices.", comment: "")
  50. }
  51. override func updateUIThemeColor() {
  52. settingButton.setTitleColor(NSColor(hex: "#1EB3F9"))
  53. }
  54. func updateUI() {
  55. standardButton.properties = ComponentCheckBoxProperty(size: .s, text: KMLocalizedString("Standard (Recommend)", comment: ""), checkboxType: .normal)
  56. standardButton.setTarget(self, action: #selector(standardButtonAction(_:)))
  57. mobileButton.properties = ComponentCheckBoxProperty(size: .s, text: KMLocalizedString("Mobile", comment: ""), checkboxType: .normal)
  58. mobileButton.setTarget(self, action: #selector(mobileButtonAction(_:)))
  59. mobileWidthConstraint.constant = mobileButton.properties.propertyInfo.viewWidth
  60. customButton.properties = ComponentCheckBoxProperty(size: .s, text: KMLocalizedString("Custom", comment: ""), checkboxType: .normal)
  61. customButton.setTarget(self, action: #selector(customButtonAction(_:)))
  62. customButtonWidthConstraint.constant = mobileButton.properties.propertyInfo.viewWidth
  63. }
  64. func reloadData() {
  65. self.updateType(type: model.modelsType)
  66. self.standardButton.properties.isDisabled = self.isDisable
  67. self.standardButton.reloadData()
  68. self.mobileButton.properties.isDisabled = self.isDisable
  69. self.mobileButton.reloadData()
  70. self.customButton.properties.isDisabled = self.isDisable
  71. self.customButton.reloadData()
  72. self.settingButton.isEnabled = !self.isDisable
  73. }
  74. func updateType(type: KMCompressModelsType) {
  75. self.standardButton.properties.checkboxType = .normal
  76. self.mobileButton.properties.checkboxType = .normal
  77. self.customButton.properties.checkboxType = .normal
  78. switch type {
  79. case .standard:
  80. self.standardButton.properties.checkboxType = .selected
  81. case .custom:
  82. self.customButton.properties.checkboxType = .selected
  83. case .mobile:
  84. self.mobileButton.properties.checkboxType = .selected
  85. }
  86. self.standardButton.reloadData()
  87. self.mobileButton.reloadData()
  88. self.customButton.reloadData()
  89. }
  90. }
  91. extension KMCompressContentView {
  92. func standardButtonAction(_ sender: Any) {
  93. self.updateType(type: .standard)
  94. self.model = KMCompressSettingModel(modelsType: .standard)
  95. guard let callBack = selectTypeAction else { return }
  96. callBack(self, model)
  97. }
  98. func mobileButtonAction(_ sender: Any) {
  99. self.updateType(type: .mobile)
  100. self.model = KMCompressSettingModel(modelsType: .mobile)
  101. guard let callBack = selectTypeAction else { return }
  102. callBack(self, model)
  103. }
  104. func customButtonAction(_ sender: Any) {
  105. self.updateType(type: .custom)
  106. self.model = KMCompressSettingModel(modelsType: .custom)
  107. if !KMCompressSettingModel.isSaveCustomData() {
  108. self.settingButtonAction(self.settingButton)
  109. }
  110. guard let callBack = selectTypeAction else { return }
  111. callBack(self, model)
  112. }
  113. @IBAction func settingButtonAction(_ sender: Any) {
  114. compressSettingViewController = KMCompressSettingViewController(nibName: "KMCompressSettingViewController", bundle: nil)
  115. let window = NSWindow(contentViewController: compressSettingViewController!)
  116. window.minSize = CGSizeMake(584, 640)
  117. window.maxSize = CGSizeMake(584, 640)
  118. window.styleMask.remove(.resizable)
  119. compressSettingViewWindow = window
  120. let currentWindow = NSWindow.currentWindow()
  121. faterWindow = currentWindow
  122. currentWindow.beginSheet(window)
  123. compressSettingViewController?.model = self.model
  124. compressSettingViewController?.cancelAction = { resultControler in
  125. currentWindow.endSheet(window)
  126. }
  127. compressSettingViewController?.doneAction = { [unowned self] resultControler in
  128. guard let callBack = compressAction else { return }
  129. callBack(self, self.model, resultControler.view)
  130. }
  131. compressSettingViewController?.modelChangeAction = { [unowned self] resultControler, tModel in
  132. self.updateType(type: tModel.modelsType)
  133. guard let callBack = selectTypeAction else { return }
  134. callBack(self, tModel)
  135. }
  136. }
  137. func closeSettingWindow() {
  138. guard let window = compressSettingViewWindow else { return }
  139. guard let faterWindow = faterWindow else { return }
  140. faterWindow.endSheet(window)
  141. }
  142. }