KMAlertTool.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // KMAlertTool.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/7.
  6. //
  7. import Cocoa
  8. class KMAlertTool: NSObject {
  9. /*
  10. * NSAlert 弹窗封装
  11. * 主线程执行
  12. */
  13. public class func runModel(style: NSAlert.Style = .critical, message: String, informative: String = "", buttons: [String] = [], callback: @escaping ((NSApplication.ModalResponse)->Void)) {
  14. let block = {
  15. let result = Self._runModelForMainThread(style: style, message: message, informative: informative, buttons: buttons)
  16. callback(result)
  17. }
  18. if Thread.isMainThread {
  19. block()
  20. } else {
  21. Task { @MainActor in
  22. block()
  23. }
  24. }
  25. }
  26. /*
  27. * NSAlert 弹窗封装
  28. * 主线程执行
  29. * 异步函数
  30. */
  31. @available(macOS 10.15.0, iOS 13.0, *)
  32. public class func runModel(style: NSAlert.Style = .critical, message: String, informative: String = "", buttons: [String] = []) async -> NSApplication.ModalResponse {
  33. return await withCheckedContinuation({ continuation in
  34. self.runModel(style: style, message: message, informative: informative, buttons: buttons) { response in
  35. continuation.resume(returning: response)
  36. }
  37. })
  38. }
  39. /*
  40. * NSAlert 弹窗封装
  41. * 主线程调用
  42. * 无返回值
  43. */
  44. public class func runModelForMainThread(style: NSAlert.Style = .critical, message: String, informative: String = "", buttons: [String] = []) {
  45. _ = self._runModelForMainThread(style: style, message: message, informative: informative, buttons: buttons)
  46. }
  47. /*
  48. * NSAlert 弹窗封装
  49. * 主线程调用
  50. * 有返回值
  51. */
  52. public class func runModelForMainThread_r(style: NSAlert.Style = .critical, message: String, informative: String = "", buttons: [String] = []) -> NSApplication.ModalResponse {
  53. return self._runModelForMainThread(style: style, message: message, informative: informative, buttons: buttons)
  54. }
  55. // MARK: - Private Methods
  56. /*
  57. * NSAlert 弹窗封装
  58. * 主线程调用
  59. */
  60. private class func _runModelForMainThread(style: NSAlert.Style = .critical, message: String, informative: String = "", buttons: [String] = []) -> NSApplication.ModalResponse {
  61. if Thread.isMainThread == false {
  62. #if DEBUG
  63. assert(false, "need main thread doing ...")
  64. #endif
  65. return .stop
  66. }
  67. let alert = NSAlert()
  68. alert.alertStyle = style
  69. alert.messageText = message
  70. alert.informativeText = informative
  71. for title in buttons {
  72. alert.addButton(withTitle: title)
  73. }
  74. return alert.runModal()
  75. }
  76. }