KMCompressSettingViewController.swift 4.9 KB

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