KMCompressSettingViewController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // KMCompressSettingViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/11/15.
  6. //
  7. import Cocoa
  8. typealias KMCompressSettingViewControllerCancelAction = (_ controller: KMCompressSettingViewController ) -> Void
  9. typealias KMCompressSettingViewControllerDoneAction = (_ controller: KMCompressSettingViewController ) -> Void
  10. class KMCompressSettingViewController: KMBaseViewController {
  11. @IBOutlet weak var doneButton: KMButton!
  12. @IBOutlet weak var cancelButton: KMButton!
  13. @IBOutlet weak var settingView: KMCompressSettingTableView!
  14. private var toastViews: [NSView] = [] // 用于管理多个 alertView
  15. var cancelAction: KMCompressSettingViewControllerCancelAction?
  16. var doneAction: KMCompressSettingViewControllerDoneAction?
  17. var model: KMCompressSettingModel = KMCompressSettingModel(modelsType: .standard) {
  18. didSet {
  19. self.reloadData()
  20. }
  21. }
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. // Do view setup here.
  25. // 将按钮设置为第一响应者
  26. // if let window = view.window {
  27. //
  28. // window.makeFirstResponder(doneButton.nextResponder)
  29. // window.defaultButtonCell = doneButton.cell as? NSButtonCell
  30. //
  31. // window.contentMinSize = CGSizeMake(624, 513)
  32. // window.contentMaxSize = CGSizeMake(624, 513)
  33. // }
  34. // self.showAlert("Unembed any font may result in incomplete display of text")
  35. //
  36. // // 自动移除视图
  37. // DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  38. // self.showAlert("Less than 7 items selected; compression quality may be affected")
  39. // }
  40. }
  41. func reloadData() {
  42. self.settingView.model = model
  43. }
  44. func showAlert(_ string: String) {
  45. // 创建 alertView
  46. let alertView = KMBatchOperateCompressSettingAlertView()
  47. alertView.titleString = string
  48. alertView.translatesAutoresizingMaskIntoConstraints = false
  49. self.view.addSubview(alertView)
  50. // 添加到管理数组
  51. toastViews.append(alertView)
  52. // 设置约束
  53. let centerYOffset = CGFloat((46 + 12) * (toastViews.count - 1) + 68) // 每个 Toast 间隔 30px
  54. NSLayoutConstraint.activate([
  55. alertView.widthAnchor.constraint(lessThanOrEqualTo: self.view.widthAnchor, multiplier: 0.8),
  56. alertView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
  57. alertView.centerYAnchor.constraint(equalTo: self.view.topAnchor, constant: centerYOffset),
  58. ])
  59. // 自动移除视图
  60. DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
  61. self.removeAlert(alertView)
  62. }
  63. }
  64. private func removeAlert(_ alertView: NSView) {
  65. // 淡出动画
  66. NSAnimationContext.runAnimationGroup({ context in
  67. context.duration = 0.3
  68. alertView.animator().alphaValue = 0
  69. }, completionHandler: {
  70. // 从视图和数组中移除
  71. alertView.removeFromSuperview()
  72. if let index = self.toastViews.firstIndex(of: alertView) {
  73. self.toastViews.remove(at: index)
  74. }
  75. // 重新布局其他 Toast
  76. self.rearrangeToasts()
  77. })
  78. }
  79. private func rearrangeToasts() {
  80. for (index, toastView) in toastViews.enumerated() {
  81. let newYOffset = CGFloat(-46 * index)
  82. if let constraint = toastView.constraints.first(where: { $0.firstAttribute == .centerY }) {
  83. constraint.constant = newYOffset
  84. }
  85. }
  86. }
  87. }
  88. extension KMCompressSettingViewController {
  89. @IBAction func cancelButtonAction(_ sender: Any) {
  90. guard let callBack = cancelAction else { return }
  91. callBack(self)
  92. }
  93. @IBAction func doneButtonAction(_ sender: Any) {
  94. guard let callBack = doneAction else { return }
  95. callBack(self)
  96. }
  97. }