KMCompressSettingViewController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // KMCompressSettingViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/11/15.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. typealias KMCompressSettingViewControllerCancelAction = (_ controller: KMCompressSettingViewController ) -> Void
  10. typealias KMCompressSettingViewControllerDoneAction = (_ controller: KMCompressSettingViewController) -> Void
  11. typealias KMCompressSettingViewControllerModelChangeAction = (_ controller: KMCompressSettingViewController, _ model: KMCompressSettingModel) -> Void
  12. class KMCompressSettingViewController: KMBaseViewController {
  13. @IBOutlet weak var doneButton: ComponentButton!
  14. @IBOutlet weak var cancelButton: ComponentButton!
  15. @IBOutlet weak var settingView: KMCompressSettingTableView!
  16. @IBOutlet weak var titleLabel: NSTextField!
  17. private var toastViews: [NSView] = [] // 用于管理多个 alertView
  18. var cancelAction: KMCompressSettingViewControllerCancelAction?
  19. var doneAction: KMCompressSettingViewControllerDoneAction?
  20. var modelChangeAction: KMCompressSettingViewControllerModelChangeAction?
  21. var model: KMCompressSettingModel = KMCompressSettingModel(modelsType: .standard) {
  22. didSet {
  23. self.reloadData()
  24. }
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. // Do view setup here.
  29. // 将按钮设置为第一响应者
  30. // if let window = view.window {
  31. //
  32. // window.makeFirstResponder(doneButton.nextResponder)
  33. // window.defaultButtonCell = doneButton.cell as? NSButtonCell
  34. //
  35. // window.contentMinSize = CGSizeMake(624, 513)
  36. // window.contentMaxSize = CGSizeMake(624, 513)
  37. // }
  38. self.updateUI()
  39. self.settingView.valueChangeAction = { [unowned self] view, tModel in
  40. if tModel.fontUnembed == false {
  41. if KMCompressSettingManager.shared.fetchFontCount() == 1 {
  42. self.showAlert("Unembed any font may result in incomplete display of text")
  43. }
  44. }
  45. let count = tModel.objectOptions.numberOfSetOptions() + tModel.userDataOptions.numberOfSetOptions() + tModel.otherDataOptions.numberOfSetOptions()
  46. if count < 7 {
  47. if KMCompressSettingManager.shared.fetchLimitAlert() == 1 {
  48. self.showAlert("Less than 7 items selected; compression quality may be affected")
  49. }
  50. }
  51. guard let callBack = modelChangeAction else { return }
  52. callBack(self, tModel)
  53. }
  54. }
  55. func updateUI() {
  56. doneButton.properties = ComponentButtonProperty(type: .primary, size: .xs, buttonText: KMLocalizedString("Compress"), keepPressState: false)
  57. doneButton.setTarget(self, action: #selector(doneButtonAction(_:)))
  58. doneButton.reloadData()
  59. cancelButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .xs, buttonText: KMLocalizedString("Cancel"), keepPressState: false)
  60. cancelButton.setTarget(self, action: #selector(cancelButtonAction(_:)))
  61. cancelButton.reloadData()
  62. titleLabel.stringValue = KMLocalizedString("Advanced Compress Settings", comment: "")
  63. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  64. //ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  65. }
  66. func reloadData() {
  67. self.settingView.model = model
  68. }
  69. func showAlert(_ string: String) {
  70. let centerYOffset = CGRectGetHeight(self.view.frame) / 2 + CGFloat((46 + 12) * (toastViews.count - 1)) // 每个 Toast 间隔 30px
  71. let alertView = ComponentMessage()
  72. alertView.properties = ComponentMessageProperty(messageType: .warning, title: KMLocalizedString(string))
  73. alertView.frame = CGRectMake((CGRectGetWidth(self.view.frame) - alertView.properties.propertyInfo.viewWidth)/2,
  74. CGRectGetHeight(self.view.frame) - alertView.properties.propertyInfo.viewHeight - centerYOffset,
  75. alertView.properties.propertyInfo.viewWidth,
  76. alertView.properties.propertyInfo.viewHeight)
  77. alertView.reloadData()
  78. self.view.addSubview(alertView)
  79. // 添加到管理数组
  80. toastViews.append(alertView)
  81. // 自动移除视图
  82. DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
  83. self.removeAlert(alertView)
  84. }
  85. }
  86. private func removeAlert(_ alertView: NSView) {
  87. // 淡出动画
  88. NSAnimationContext.runAnimationGroup({ context in
  89. context.duration = 0.3
  90. alertView.animator().alphaValue = 0
  91. }, completionHandler: {
  92. // 从视图和数组中移除
  93. alertView.removeFromSuperview()
  94. if let index = self.toastViews.firstIndex(of: alertView) {
  95. self.toastViews.remove(at: index)
  96. }
  97. // 重新布局其他 Toast
  98. self.rearrangeToasts()
  99. })
  100. }
  101. private func rearrangeToasts() {
  102. for (index, toastView) in toastViews.enumerated() {
  103. let newYOffset = CGFloat(-46 * index)
  104. if let constraint = toastView.constraints.first(where: { $0.firstAttribute == .centerY }) {
  105. constraint.constant = newYOffset
  106. }
  107. }
  108. }
  109. }
  110. extension KMCompressSettingViewController {
  111. @objc func cancelButtonAction(_ sender: Any) {
  112. guard let callBack = cancelAction else { return }
  113. callBack(self)
  114. }
  115. @objc func doneButtonAction(_ sender: Any) {
  116. guard let callBack = doneAction else { return }
  117. callBack(self)
  118. }
  119. }