KMSubscribeSuccessWindowController.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // KMSubscribeSuccessWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/6/26.
  6. //
  7. import Cocoa
  8. var subscribeSuccessController: KMSubscribeSuccessWindowController?
  9. var subscribeSuccessMainWindow: NSWindow?
  10. typealias KMSubscribeSuccessWindowControllerCloseAction = (_ controller: KMSubscribeSuccessWindowController) -> Void
  11. class KMSubscribeSuccessWindowController: NSWindowController {
  12. @IBOutlet weak var successView: KMSubscribeSuccessView!
  13. var closeAction: KMSubscribeSuccessWindowControllerCloseAction?
  14. deinit {
  15. KMPrint("KMSubscribeSuccessWindowController 释放")
  16. }
  17. override func windowDidLoad() {
  18. super.windowDidLoad()
  19. self.setup()
  20. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  21. }
  22. //MARK: 打开文件
  23. static func show(window: NSWindow) -> KMSubscribeSuccessWindowController {
  24. if let controller: KMSubscribeSuccessWindowController = KMSubscribeSuccessWindowController.fetchSampleController() {
  25. return controller
  26. } else {
  27. let subscribeSuccessWindowController: KMSubscribeSuccessWindowController = KMSubscribeSuccessWindowController.init(windowNibName: "KMSubscribeSuccessWindowController")
  28. window.beginSheet(subscribeSuccessWindowController.window!)
  29. subscribeSuccessWindowController.window?.center()
  30. subscribeSuccessWindowController.closeAction = { controller in
  31. controller.closeWindow()
  32. }
  33. subscribeSuccessController = subscribeSuccessWindowController
  34. subscribeSuccessMainWindow = window
  35. return subscribeSuccessWindowController
  36. }
  37. }
  38. func setup() {
  39. self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
  40. self.successView.doneAction = { [weak self] view in
  41. guard let callBack = self?.closeAction else { return }
  42. callBack((self!))
  43. }
  44. }
  45. func closeWindow() {
  46. if subscribeSuccessMainWindow != nil {
  47. subscribeSuccessMainWindow?.endSheet((subscribeSuccessController?.window)!)
  48. }
  49. if subscribeSuccessController != nil {
  50. subscribeSuccessController?.window?.close()
  51. }
  52. subscribeSuccessController = nil
  53. subscribeSuccessMainWindow = nil
  54. }
  55. static func fetchSampleController() -> KMSubscribeSuccessWindowController? {
  56. for window in NSApp.windows {
  57. let controller = window.windowController
  58. if controller is KMSubscribeSuccessWindowController {
  59. return controller as! KMSubscribeSuccessWindowController
  60. }
  61. }
  62. return nil
  63. }
  64. }