KMOpenDMGPopupBootWC.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // KMOpenDMGPopupBootWC.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/11/6.
  6. // 打开DMG时的弹窗引导
  7. //
  8. import Cocoa
  9. class KMOpenDMGPopupBootWC: NSWindowController {
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var subTitleLabel: NSTextField!
  12. @IBOutlet weak var freeBox: KMBox!
  13. @IBOutlet weak var freeLabel: NSTextField!
  14. @IBOutlet weak var signInTextView: NSTextView!
  15. private var viewModel = KMOpenDMGPopBootModel()
  16. static let shared: KMOpenDMGPopupBootWC = {
  17. let windowC = KMOpenDMGPopupBootWC(windowNibName: "KMOpenDMGPopupBootWC")
  18. return windowC
  19. }()
  20. override func windowDidLoad() {
  21. super.windowDidLoad()
  22. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  23. languageLocalized()
  24. initializeUI()
  25. NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
  26. }
  27. @objc func changeEffectiveAppearance() {
  28. self.initializeUI()
  29. }
  30. // MARK: Private Method
  31. private func languageLocalized() -> Void {
  32. titleLabel.stringValue = NSLocalizedString("Free to Get 7-Day VIP Now", tableName: "MemberCenterLocalizable", comment: "")
  33. subTitleLabel.stringValue = String(format: " · %@", NSLocalizedString("Use all functions without restrictions", tableName: "MemberCenterLocalizable", comment: ""))
  34. freeLabel.stringValue = NSLocalizedString("Get 7-Day VIP Free", tableName: "MemberCenterLocalizable", comment: "")
  35. }
  36. private func initializeUI() -> Void {
  37. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  38. if isDarkModel {
  39. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "0E1114").cgColor;
  40. } else {
  41. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "FFFFFF").cgColor;
  42. }
  43. titleLabel.textColor = NSColor(named: "4982E6")
  44. titleLabel.font = NSFont.SFProTextSemiboldFont(24)
  45. subTitleLabel.textColor = NSColor(named: "101828")
  46. subTitleLabel.font = NSFont.SFProTextRegularFont(14)
  47. freeBox.fillColor = NSColor(named: "273C62") ?? .blue
  48. freeLabel.textColor = NSColor(named: "FFFFFF")
  49. freeLabel.font = NSFont.SFProTextRegularFont(13)
  50. signInTextView.isEditable = false
  51. signInTextView.isSelectable = true
  52. signInTextView.textColor = NSColor.black
  53. signInTextView.font = NSFont.SFProTextRegularFont(12)
  54. if let _ = signInTextView.superview {
  55. signInTextView.km_add_left_constraint()
  56. signInTextView.km_add_top_constraint()
  57. signInTextView.km_add_trailing_constraint()
  58. signInTextView.km_add_height_constraint(constant: 30)
  59. }
  60. let tipsString = NSLocalizedString("Already have an account? %@", tableName: "MemberCenterLocalizable", comment: "")
  61. let specialOffer = NSLocalizedString("Sign in", tableName: "MemberCenterLocalizable", comment: "")
  62. let fullString = String(format: tipsString, specialOffer)
  63. let paragraphStyle = NSMutableParagraphStyle()
  64. paragraphStyle.alignment = .center
  65. let attributedString = NSMutableAttributedString(string: fullString)
  66. let specialOfferRange = (fullString as NSString).range(of: specialOffer)
  67. let linkColor = NSColor(named: "4982E6") ?? NSColor.blue
  68. let font = NSFont.SFProTextRegularFont(12)
  69. attributedString.addAttributes([
  70. .foregroundColor: NSColor(named: "42464D") ?? NSColor.black as Any,
  71. .font: font,
  72. .paragraphStyle: paragraphStyle
  73. ], range: (fullString as NSString).range(of: fullString))
  74. attributedString.addAttributes([
  75. .foregroundColor: linkColor,
  76. .link: "action://openDetail",
  77. .font: font
  78. ], range: specialOfferRange)
  79. signInTextView.textStorage?.setAttributedString(attributedString)
  80. signInTextView.delegate = self
  81. freeBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  82. guard let self = self else { return }
  83. }
  84. freeBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  85. guard let self = self else { return }
  86. if downEntered {
  87. self.viewModel.getVipFree()
  88. self.window?.close()
  89. }
  90. }
  91. }
  92. }
  93. extension KMOpenDMGPopupBootWC: NSTextViewDelegate {
  94. func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool {
  95. if let urlString = link as? String, urlString == "action://openDetail" {
  96. viewModel.signUpAction()
  97. self.window?.close()
  98. return true
  99. }
  100. return false
  101. }
  102. }