Brak opisu

PremiumPlansWindowController.swift 59KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. import Cocoa
  2. import StoreKit
  3. final class PremiumPlansWindowController: NSWindowController {
  4. /// Matches `PremiumPlansViewController.Theme.pageStart` so the window backing fills sheet corners.
  5. static var paywallSheetBackground: NSColor { PremiumPlansViewController.Theme.pageStart }
  6. init() {
  7. let viewController = PremiumPlansViewController()
  8. let window = NSWindow(contentViewController: viewController)
  9. window.title = L("Premium Plans")
  10. // Borderless avoids titled-window chrome: its rounded titlebar frame often leaves dark wedges at
  11. // the corners when combined with a custom full-bleed paywall (this window is only shown as a sheet).
  12. window.styleMask = [.borderless, .closable, .resizable]
  13. window.isOpaque = true
  14. window.backgroundColor = Self.paywallSheetBackground
  15. window.setContentSize(NSSize(width: 1160, height: 760))
  16. window.minSize = NSSize(width: 980, height: 680)
  17. window.isRestorable = false
  18. window.center()
  19. super.init(window: window)
  20. }
  21. @available(*, unavailable)
  22. required init?(coder: NSCoder) {
  23. nil
  24. }
  25. /// Loads StoreKit prices into the paywall view before the sheet is shown (avoids "—" placeholders flashing in).
  26. @MainActor
  27. func prepareForPresentation() async {
  28. _ = window
  29. guard let viewController = window?.contentViewController as? PremiumPlansViewController else { return }
  30. await viewController.prepareStorePricingForDisplay()
  31. }
  32. }
  33. private final class PremiumPlansViewController: NSViewController {
  34. private final class HoverPricingCardView: NSView {
  35. private var baseBorderColor: NSColor
  36. private var hoverBorderColor: NSColor
  37. private var trackingAreaRef: NSTrackingArea?
  38. private var isHovered = false
  39. init(baseBorderColor: NSColor, hoverBorderColor: NSColor) {
  40. self.baseBorderColor = baseBorderColor
  41. self.hoverBorderColor = hoverBorderColor
  42. super.init(frame: .zero)
  43. wantsLayer = true
  44. layer?.cornerRadius = 16
  45. applyHoverStyle(isHovered: false, animated: false)
  46. }
  47. @available(*, unavailable)
  48. required init?(coder: NSCoder) {
  49. nil
  50. }
  51. override func updateTrackingAreas() {
  52. super.updateTrackingAreas()
  53. if let trackingAreaRef {
  54. removeTrackingArea(trackingAreaRef)
  55. }
  56. let options: NSTrackingArea.Options = [.activeInActiveApp, .mouseEnteredAndExited, .inVisibleRect]
  57. let area = NSTrackingArea(rect: .zero, options: options, owner: self, userInfo: nil)
  58. addTrackingArea(area)
  59. trackingAreaRef = area
  60. }
  61. override func mouseEntered(with event: NSEvent) {
  62. super.mouseEntered(with: event)
  63. isHovered = true
  64. applyHoverStyle(isHovered: true, animated: true)
  65. }
  66. override func mouseExited(with event: NSEvent) {
  67. super.mouseExited(with: event)
  68. isHovered = false
  69. applyHoverStyle(isHovered: false, animated: true)
  70. }
  71. func updateBorderColors(base: NSColor, hover: NSColor) {
  72. baseBorderColor = base
  73. hoverBorderColor = hover
  74. applyHoverStyle(isHovered: isHovered, animated: false)
  75. }
  76. private func applyHoverStyle(isHovered: Bool, animated: Bool) {
  77. guard let layer else { return }
  78. let updates = {
  79. layer.borderWidth = isHovered ? 2 : 1
  80. layer.borderColor = (isHovered ? self.hoverBorderColor : self.baseBorderColor).cgColor
  81. layer.shadowColor = self.hoverBorderColor.withAlphaComponent(0.35).cgColor
  82. layer.shadowOpacity = isHovered ? 0.22 : 0
  83. layer.shadowRadius = isHovered ? 14 : 0
  84. layer.shadowOffset = .init(width: 0, height: -2)
  85. }
  86. if animated {
  87. NSAnimationContext.runAnimationGroup { context in
  88. context.duration = 0.16
  89. updates()
  90. }
  91. } else {
  92. updates()
  93. }
  94. }
  95. }
  96. /// Footer text actions: accent color + pointing hand on hover (matches pricing-card tracking behavior).
  97. private final class FooterLinkButton: NSButton {
  98. private var trackingAreaRef: NSTrackingArea?
  99. private var didPushCursor = false
  100. override func updateTrackingAreas() {
  101. super.updateTrackingAreas()
  102. if let trackingAreaRef {
  103. removeTrackingArea(trackingAreaRef)
  104. }
  105. let options: NSTrackingArea.Options = [.activeInActiveApp, .mouseEnteredAndExited, .inVisibleRect]
  106. let area = NSTrackingArea(rect: .zero, options: options, owner: self, userInfo: nil)
  107. addTrackingArea(area)
  108. trackingAreaRef = area
  109. }
  110. override func mouseEntered(with event: NSEvent) {
  111. super.mouseEntered(with: event)
  112. setHoverVisuals(hovered: true)
  113. if !didPushCursor {
  114. NSCursor.pointingHand.push()
  115. didPushCursor = true
  116. }
  117. }
  118. override func mouseExited(with event: NSEvent) {
  119. super.mouseExited(with: event)
  120. setHoverVisuals(hovered: false)
  121. if didPushCursor {
  122. NSCursor.pop()
  123. didPushCursor = false
  124. }
  125. }
  126. override func viewWillMove(toWindow newWindow: NSWindow?) {
  127. super.viewWillMove(toWindow: newWindow)
  128. if newWindow == nil, didPushCursor {
  129. NSCursor.pop()
  130. didPushCursor = false
  131. }
  132. if newWindow == nil {
  133. setHoverVisuals(hovered: false, animated: false)
  134. }
  135. }
  136. private func setHoverVisuals(hovered: Bool, animated: Bool = true) {
  137. let color = hovered ? Theme.accent : Theme.secondaryText
  138. if animated {
  139. NSAnimationContext.runAnimationGroup { context in
  140. context.duration = 0.15
  141. self.animator().contentTintColor = color
  142. }
  143. } else {
  144. contentTintColor = color
  145. }
  146. }
  147. }
  148. /// Purchase CTAs: fill/border/shadow + pointing hand on hover.
  149. private final class PlanPurchaseHoverButton: NSButton {
  150. private var trackingAreaRef: NSTrackingArea?
  151. private var didPushCursor = false
  152. private let isPrimaryStyle: Bool
  153. private static let primaryFill = NSColor(srgbRed: 189 / 255, green: 52 / 255, blue: 255 / 255, alpha: 1)
  154. private static let primaryFillHover = NSColor(srgbRed: 205 / 255, green: 88 / 255, blue: 255 / 255, alpha: 1)
  155. init(planId: String, title: String, isPrimaryStyle: Bool, target: AnyObject?, action: Selector) {
  156. self.isPrimaryStyle = isPrimaryStyle
  157. super.init(frame: .zero)
  158. identifier = NSUserInterfaceItemIdentifier(planId)
  159. self.title = title
  160. self.target = target
  161. self.action = action
  162. isBordered = false
  163. bezelStyle = .rounded
  164. font = .systemFont(ofSize: 14, weight: .semibold)
  165. wantsLayer = true
  166. layer?.cornerRadius = 12
  167. focusRingType = .none
  168. translatesAutoresizingMaskIntoConstraints = false
  169. applyBaseStyle(hovered: false)
  170. }
  171. @available(*, unavailable)
  172. required init?(coder: NSCoder) {
  173. nil
  174. }
  175. override var isEnabled: Bool {
  176. didSet {
  177. if !isEnabled {
  178. applyBaseStyle(hovered: false, animated: true)
  179. if didPushCursor {
  180. NSCursor.pop()
  181. didPushCursor = false
  182. }
  183. }
  184. }
  185. }
  186. override func updateTrackingAreas() {
  187. super.updateTrackingAreas()
  188. if let trackingAreaRef {
  189. removeTrackingArea(trackingAreaRef)
  190. }
  191. let options: NSTrackingArea.Options = [.activeInActiveApp, .mouseEnteredAndExited, .inVisibleRect]
  192. let area = NSTrackingArea(rect: .zero, options: options, owner: self, userInfo: nil)
  193. addTrackingArea(area)
  194. trackingAreaRef = area
  195. }
  196. override func mouseEntered(with event: NSEvent) {
  197. super.mouseEntered(with: event)
  198. guard isEnabled else { return }
  199. applyBaseStyle(hovered: true, animated: true)
  200. if !didPushCursor {
  201. NSCursor.pointingHand.push()
  202. didPushCursor = true
  203. }
  204. }
  205. override func mouseExited(with event: NSEvent) {
  206. super.mouseExited(with: event)
  207. applyBaseStyle(hovered: false, animated: true)
  208. if didPushCursor {
  209. NSCursor.pop()
  210. didPushCursor = false
  211. }
  212. }
  213. override func viewWillMove(toWindow newWindow: NSWindow?) {
  214. super.viewWillMove(toWindow: newWindow)
  215. if newWindow == nil, didPushCursor {
  216. NSCursor.pop()
  217. didPushCursor = false
  218. }
  219. if newWindow == nil {
  220. applyBaseStyle(hovered: false, animated: false)
  221. }
  222. }
  223. func refreshAppearance() {
  224. applyBaseStyle(hovered: false, animated: false)
  225. }
  226. private func applyBaseStyle(hovered: Bool, animated: Bool = true) {
  227. let updates = {
  228. if self.isPrimaryStyle {
  229. self.layer?.backgroundColor = (hovered ? Self.primaryFillHover : Self.primaryFill).cgColor
  230. self.layer?.borderColor = Theme.accent.cgColor
  231. self.layer?.borderWidth = hovered ? 2 : 1
  232. self.contentTintColor = .white
  233. self.layer?.shadowColor = Self.primaryFill.cgColor
  234. self.layer?.shadowOpacity = hovered ? 0.28 : 0
  235. self.layer?.shadowRadius = hovered ? 12 : 0
  236. self.layer?.shadowOffset = CGSize(width: 0, height: -2)
  237. } else {
  238. let baseFill = Theme.mutedButtonFill
  239. let hoverFill = baseFill.blended(withFraction: 0.22, of: Theme.accent) ?? baseFill
  240. self.layer?.backgroundColor = (hovered ? hoverFill : baseFill).cgColor
  241. self.layer?.borderColor = (hovered ? Theme.accent : Theme.divider).cgColor
  242. self.layer?.borderWidth = hovered ? 2 : 1
  243. self.contentTintColor = Theme.primaryText
  244. self.layer?.shadowColor = Theme.accent.withAlphaComponent(0.35).cgColor
  245. self.layer?.shadowOpacity = hovered ? 0.18 : 0
  246. self.layer?.shadowRadius = hovered ? 10 : 0
  247. self.layer?.shadowOffset = CGSize(width: 0, height: -2)
  248. }
  249. }
  250. if animated {
  251. NSAnimationContext.runAnimationGroup { context in
  252. context.duration = 0.16
  253. updates()
  254. }
  255. } else {
  256. updates()
  257. }
  258. }
  259. }
  260. /// Close control: subtle lift + accent tint on hover.
  261. private final class PremiumCloseHoverButton: NSButton {
  262. private var trackingAreaRef: NSTrackingArea?
  263. private var didPushCursor = false
  264. init(target: AnyObject?, action: Selector) {
  265. super.init(frame: .zero)
  266. self.target = target
  267. self.action = action
  268. isBordered = false
  269. wantsLayer = true
  270. layer?.cornerRadius = 15
  271. bezelStyle = .regularSquare
  272. image = NSImage(systemSymbolName: "xmark", accessibilityDescription: L("Close"))
  273. imageScaling = .scaleProportionallyDown
  274. focusRingType = .none
  275. translatesAutoresizingMaskIntoConstraints = false
  276. applyStyle(hovered: false, animated: false)
  277. }
  278. @available(*, unavailable)
  279. required init?(coder: NSCoder) {
  280. nil
  281. }
  282. override func updateTrackingAreas() {
  283. super.updateTrackingAreas()
  284. if let trackingAreaRef {
  285. removeTrackingArea(trackingAreaRef)
  286. }
  287. let options: NSTrackingArea.Options = [.activeInActiveApp, .mouseEnteredAndExited, .inVisibleRect]
  288. let area = NSTrackingArea(rect: .zero, options: options, owner: self, userInfo: nil)
  289. addTrackingArea(area)
  290. trackingAreaRef = area
  291. }
  292. override func mouseEntered(with event: NSEvent) {
  293. super.mouseEntered(with: event)
  294. applyStyle(hovered: true, animated: true)
  295. if !didPushCursor {
  296. NSCursor.pointingHand.push()
  297. didPushCursor = true
  298. }
  299. }
  300. override func mouseExited(with event: NSEvent) {
  301. super.mouseExited(with: event)
  302. applyStyle(hovered: false, animated: true)
  303. if didPushCursor {
  304. NSCursor.pop()
  305. didPushCursor = false
  306. }
  307. }
  308. override func viewWillMove(toWindow newWindow: NSWindow?) {
  309. super.viewWillMove(toWindow: newWindow)
  310. if newWindow == nil, didPushCursor {
  311. NSCursor.pop()
  312. didPushCursor = false
  313. }
  314. if newWindow == nil {
  315. applyStyle(hovered: false, animated: false)
  316. }
  317. }
  318. func refreshAppearance(hovered: Bool) {
  319. applyStyle(hovered: hovered, animated: false)
  320. }
  321. private func applyStyle(hovered: Bool, animated: Bool) {
  322. let updates = {
  323. self.layer?.backgroundColor = (hovered
  324. ? Theme.closeButtonBackgroundHover
  325. : Theme.closeButtonBackground).cgColor
  326. self.layer?.borderColor = (hovered ? Theme.accent.withAlphaComponent(0.45) : Theme.divider).cgColor
  327. self.layer?.borderWidth = hovered ? 1.5 : 1
  328. self.contentTintColor = hovered ? Theme.accent : Theme.secondaryText
  329. self.layer?.shadowColor = Theme.accent.withAlphaComponent(0.25).cgColor
  330. self.layer?.shadowOpacity = hovered ? 0.2 : 0
  331. self.layer?.shadowRadius = hovered ? 8 : 0
  332. self.layer?.shadowOffset = CGSize(width: 0, height: -1)
  333. }
  334. if animated {
  335. NSAnimationContext.runAnimationGroup { context in
  336. context.duration = 0.15
  337. updates()
  338. }
  339. } else {
  340. updates()
  341. }
  342. }
  343. }
  344. /// Shown until StoreKit returns localized `Product.displayPrice` (never use hardcoded currency amounts).
  345. private static let unloadedPricePlaceholder = "—"
  346. private struct Plan {
  347. let id: String
  348. let title: String
  349. let subtitle: String
  350. let period: String
  351. let billedPill: String
  352. let billedLine: String
  353. let crossedPrice: String?
  354. let savingsText: String?
  355. let features: [String]
  356. let iconName: String
  357. let iconTint: NSColor
  358. let highlight: Bool
  359. }
  360. fileprivate enum Theme {
  361. static var pageStart: NSColor {
  362. AppDashboardTheme.isDark
  363. ? AppDashboardTheme.pageBackground
  364. : NSColor(srgbRed: 249 / 255, green: 252 / 255, blue: 255 / 255, alpha: 1)
  365. }
  366. static var pageEnd: NSColor {
  367. AppDashboardTheme.isDark
  368. ? AppDashboardTheme.loadingPageBackgroundBottom
  369. : NSColor(srgbRed: 238 / 255, green: 244 / 255, blue: 255 / 255, alpha: 1)
  370. }
  371. static var cardBackground: NSColor { AppDashboardTheme.cardBackground }
  372. static var primaryText: NSColor {
  373. AppDashboardTheme.isDark
  374. ? AppDashboardTheme.primaryText
  375. : NSColor(srgbRed: 27 / 255, green: 38 / 255, blue: 79 / 255, alpha: 1)
  376. }
  377. static var secondaryText: NSColor {
  378. AppDashboardTheme.isDark
  379. ? AppDashboardTheme.secondaryText
  380. : NSColor(srgbRed: 108 / 255, green: 120 / 255, blue: 157 / 255, alpha: 1)
  381. }
  382. static var cardBorder: NSColor {
  383. AppDashboardTheme.isDark
  384. ? AppDashboardTheme.border
  385. : NSColor(srgbRed: 198 / 255, green: 216 / 255, blue: 255 / 255, alpha: 1)
  386. }
  387. static var accent: NSColor { AppDashboardTheme.brandBlue }
  388. static var accentHover: NSColor { AppDashboardTheme.brandBlueHover }
  389. static var mutedButtonFill: NSColor {
  390. AppDashboardTheme.isDark
  391. ? AppDashboardTheme.profileFieldFill
  392. : NSColor(srgbRed: 238 / 255, green: 243 / 255, blue: 252 / 255, alpha: 1)
  393. }
  394. static var bottomStrip: NSColor {
  395. AppDashboardTheme.isDark
  396. ? AppDashboardTheme.proCardFill
  397. : NSColor(srgbRed: 244 / 255, green: 248 / 255, blue: 255 / 255, alpha: 1)
  398. }
  399. static var divider: NSColor {
  400. AppDashboardTheme.isDark
  401. ? AppDashboardTheme.border
  402. : NSColor(srgbRed: 218 / 255, green: 228 / 255, blue: 247 / 255, alpha: 1)
  403. }
  404. static var successText: NSColor {
  405. AppDashboardTheme.isDark
  406. ? AppDashboardTheme.welcomeHeroHeadingBlue
  407. : NSColor(srgbRed: 21 / 255, green: 154 / 255, blue: 220 / 255, alpha: 1)
  408. }
  409. static var iconTint: NSColor { AppDashboardTheme.brandBlue }
  410. static var closeButtonBackground: NSColor {
  411. AppDashboardTheme.isDark
  412. ? AppDashboardTheme.cardBackground
  413. : NSColor.white.withAlphaComponent(0.92)
  414. }
  415. static var closeButtonBackgroundHover: NSColor {
  416. AppDashboardTheme.isDark
  417. ? AppDashboardTheme.neutralHoverFill
  418. : NSColor.white.withAlphaComponent(0.98)
  419. }
  420. }
  421. private struct PricingCardAppearanceTarget {
  422. let card: HoverPricingCardView
  423. let iconWell: NSView
  424. let planIconView: NSImageView
  425. let planId: String
  426. let titleLabel: NSTextField
  427. let subtitleLabel: NSTextField
  428. let priceLabel: NSTextField
  429. let periodLabel: NSTextField
  430. let billingLabel: NSTextField?
  431. let divider: NSBox
  432. let featureLabels: [NSTextField]
  433. let featureIcons: [NSImageView]
  434. let purchaseButton: PlanPurchaseHoverButton
  435. let billedPillLabel: NSTextField?
  436. }
  437. private enum FeatureListMetrics {
  438. static let rowSpacing = CGFloat(8)
  439. static let iconLabelSpacing = CGFloat(8)
  440. static let edgeInsets = NSEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
  441. /// Caps feature list height so pricing cards do not push the footer off-screen.
  442. static let maxScrollHeight = CGFloat(168)
  443. }
  444. private let subscriptionStore = SubscriptionStore.shared
  445. private var planPriceFields: [String: (price: NSTextField, period: NSTextField)] = [:]
  446. private var planPurchaseButtons: [String: NSButton] = [:]
  447. private var subscriptionPrimaryFooterButton: NSButton?
  448. private var premiumCloseButton: PremiumCloseHoverButton?
  449. private var subscriptionStatusObservation: NSObjectProtocol?
  450. private var appearanceObserver: NSObjectProtocol?
  451. private var languageObserver: NSObjectProtocol?
  452. private var storeProductsLoadTask: Task<Void, Never>?
  453. /// Core Pro capabilities shown on every pricing card (replaces generic “All premium features”).
  454. private var proCapabilityFeatures: [String] {
  455. [
  456. L("Unlimited AI job search on Home"),
  457. L("Save jobs & open listings in-app"),
  458. L("CV Maker, profiles & PDF export"),
  459. L("Role, company & skill shortcuts")
  460. ]
  461. }
  462. private var plans: [Plan] {
  463. [
  464. Plan(
  465. id: "weekly",
  466. title: L("Weekly"),
  467. subtitle: L("Flexible and commitment-free"),
  468. period: L("/ week"),
  469. billedPill: "",
  470. billedLine: "",
  471. crossedPrice: nil,
  472. savingsText: nil,
  473. features: proCapabilityFeatures + [
  474. L("Perfect for short-term job hunts"),
  475. L("Cancel anytime")
  476. ],
  477. iconName: "paperplane.fill",
  478. iconTint: Theme.iconTint,
  479. highlight: false
  480. ),
  481. Plan(
  482. id: "monthly",
  483. title: L("Monthly"),
  484. subtitle: L("Balanced for regular productivity"),
  485. period: L("/ month"),
  486. billedPill: "",
  487. billedLine: "",
  488. crossedPrice: nil,
  489. savingsText: nil,
  490. features: proCapabilityFeatures + [
  491. L("Best for regular job seekers"),
  492. L("Priority support")
  493. ],
  494. iconName: "bolt.fill",
  495. iconTint: Theme.accent,
  496. highlight: true
  497. ),
  498. Plan(
  499. id: "yearly",
  500. title: L("Yearly"),
  501. subtitle: L("Best value for long-term users"),
  502. period: L("/ year"),
  503. billedPill: L("3 days free trial"),
  504. billedLine: "",
  505. crossedPrice: nil,
  506. savingsText: nil,
  507. features: proCapabilityFeatures + [
  508. L("Lowest effective monthly cost"),
  509. L("Ideal for long-term use")
  510. ],
  511. iconName: "crown.fill",
  512. iconTint: Theme.successText,
  513. highlight: false
  514. )
  515. ]
  516. }
  517. private let pageGradient = CAGradientLayer()
  518. private var premiumTitleLabel: NSTextField?
  519. private var premiumSubtitleLabel: NSTextField?
  520. private var pricingCardTargets: [PricingCardAppearanceTarget] = []
  521. private weak var trustBadgesRow: NSStackView?
  522. private var footerLinkButtons: [FooterLinkButton] = []
  523. deinit {
  524. if let subscriptionStatusObservation {
  525. NotificationCenter.default.removeObserver(subscriptionStatusObservation)
  526. }
  527. if let appearanceObserver {
  528. NotificationCenter.default.removeObserver(appearanceObserver)
  529. }
  530. if let languageObserver {
  531. NotificationCenter.default.removeObserver(languageObserver)
  532. }
  533. }
  534. override func viewDidLoad() {
  535. super.viewDidLoad()
  536. appearanceObserver = NotificationCenter.default.addObserver(
  537. forName: AppAppearanceManager.didChangeNotification,
  538. object: nil,
  539. queue: .main
  540. ) { [weak self] _ in
  541. self?.applyCurrentAppearance()
  542. }
  543. languageObserver = NotificationCenter.default.addObserver(
  544. forName: AppLanguageManager.didChangeNotification,
  545. object: nil,
  546. queue: .main
  547. ) { [weak self] _ in
  548. self?.applyLocalizedStrings()
  549. }
  550. subscriptionStatusObservation = NotificationCenter.default.addObserver(
  551. forName: .subscriptionStatusDidChange,
  552. object: nil,
  553. queue: .main
  554. ) { [weak self] _ in
  555. Task { @MainActor in
  556. await self?.subscriptionStore.ensureProductsLoaded()
  557. self?.applyStorePricing()
  558. self?.updateSubscriptionPrimaryFooter()
  559. self?.updatePremiumCloseButtonVisibility()
  560. }
  561. }
  562. applyStorePricing()
  563. storeProductsLoadTask = Task { @MainActor [weak self] in
  564. await self?.loadStoreProducts()
  565. self?.storeProductsLoadTask = nil
  566. }
  567. }
  568. /// Ensures localized prices are on screen; reuses an in-flight load started from `viewDidLoad`.
  569. @MainActor
  570. func prepareStorePricingForDisplay() async {
  571. applyStorePricing()
  572. if let storeProductsLoadTask {
  573. await storeProductsLoadTask.value
  574. applyStorePricing()
  575. return
  576. }
  577. await subscriptionStore.ensureProductsLoaded()
  578. applyStorePricing()
  579. }
  580. override func viewDidLayout() {
  581. super.viewDidLayout()
  582. pageGradient.frame = view.bounds
  583. }
  584. override func loadView() {
  585. view = NSView()
  586. view.wantsLayer = true
  587. pageGradient.colors = [Theme.pageStart.cgColor, Theme.pageEnd.cgColor]
  588. pageGradient.startPoint = CGPoint(x: 0, y: 1)
  589. pageGradient.endPoint = CGPoint(x: 1, y: 0)
  590. view.layer?.addSublayer(pageGradient)
  591. setupLayout()
  592. applyCurrentAppearance()
  593. }
  594. private func setupLayout() {
  595. let closeButton = PremiumCloseHoverButton(target: self, action: #selector(didTapClose))
  596. let crownIcon = NSImageView()
  597. crownIcon.translatesAutoresizingMaskIntoConstraints = false
  598. crownIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 18, weight: .semibold)
  599. crownIcon.image = NSImage(systemSymbolName: "crown.fill", accessibilityDescription: nil)
  600. crownIcon.contentTintColor = NSColor(srgbRed: 254 / 255, green: 214 / 255, blue: 92 / 255, alpha: 1)
  601. let title = NSTextField(labelWithString: L("Upgrade to Pro"))
  602. title.font = .systemFont(ofSize: 40, weight: .semibold)
  603. title.textColor = Theme.primaryText
  604. title.alignment = .center
  605. premiumTitleLabel = title
  606. let subtitle = NSTextField(labelWithString: L("Unlock unlimited access to premium tools and boost your productivity."))
  607. subtitle.font = .systemFont(ofSize: 14, weight: .regular)
  608. subtitle.textColor = Theme.secondaryText
  609. subtitle.alignment = .center
  610. premiumSubtitleLabel = subtitle
  611. pricingCardTargets = []
  612. let cardsRow = NSStackView(views: plans.map(makePricingCard(_:)))
  613. cardsRow.orientation = .horizontal
  614. cardsRow.spacing = 14
  615. cardsRow.alignment = .top
  616. cardsRow.distribution = .fillEqually
  617. cardsRow.translatesAutoresizingMaskIntoConstraints = false
  618. let trustRow = makeTrustRow()
  619. let footerRow = makeFooterRow()
  620. let headerStack = NSStackView(views: [crownIcon, title, subtitle])
  621. headerStack.orientation = .vertical
  622. headerStack.spacing = 10
  623. headerStack.alignment = .centerX
  624. headerStack.translatesAutoresizingMaskIntoConstraints = false
  625. let bodyStack = NSStackView(views: [cardsRow, trustRow])
  626. bodyStack.orientation = .vertical
  627. bodyStack.spacing = 16
  628. bodyStack.alignment = .centerX
  629. bodyStack.translatesAutoresizingMaskIntoConstraints = false
  630. let scrollView = NSScrollView()
  631. scrollView.hasVerticalScroller = true
  632. scrollView.autohidesScrollers = true
  633. scrollView.drawsBackground = false
  634. scrollView.borderType = .noBorder
  635. scrollView.translatesAutoresizingMaskIntoConstraints = false
  636. let scrollDocument = NSView()
  637. scrollDocument.translatesAutoresizingMaskIntoConstraints = false
  638. scrollView.documentView = scrollDocument
  639. scrollDocument.addSubview(bodyStack)
  640. view.addSubview(headerStack)
  641. view.addSubview(scrollView)
  642. view.addSubview(footerRow)
  643. view.addSubview(closeButton)
  644. let scrollClip = scrollView.contentView
  645. NSLayoutConstraint.activate([
  646. headerStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 24),
  647. headerStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -24),
  648. headerStack.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
  649. scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 24),
  650. scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -24),
  651. scrollView.topAnchor.constraint(equalTo: headerStack.bottomAnchor, constant: 12),
  652. scrollView.bottomAnchor.constraint(equalTo: footerRow.topAnchor, constant: -12),
  653. footerRow.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 24),
  654. footerRow.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -24),
  655. footerRow.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16),
  656. footerRow.heightAnchor.constraint(greaterThanOrEqualToConstant: 32),
  657. scrollDocument.leadingAnchor.constraint(equalTo: scrollClip.leadingAnchor),
  658. scrollDocument.trailingAnchor.constraint(equalTo: scrollClip.trailingAnchor),
  659. scrollDocument.topAnchor.constraint(equalTo: scrollClip.topAnchor),
  660. scrollDocument.widthAnchor.constraint(equalTo: scrollClip.widthAnchor),
  661. scrollDocument.bottomAnchor.constraint(equalTo: bodyStack.bottomAnchor, constant: 8),
  662. bodyStack.leadingAnchor.constraint(equalTo: scrollDocument.leadingAnchor),
  663. bodyStack.trailingAnchor.constraint(equalTo: scrollDocument.trailingAnchor),
  664. bodyStack.topAnchor.constraint(equalTo: scrollDocument.topAnchor, constant: 4),
  665. bodyStack.widthAnchor.constraint(equalTo: scrollDocument.widthAnchor),
  666. cardsRow.widthAnchor.constraint(equalTo: bodyStack.widthAnchor),
  667. trustRow.widthAnchor.constraint(equalTo: bodyStack.widthAnchor),
  668. closeButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 14),
  669. closeButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -14),
  670. closeButton.widthAnchor.constraint(equalToConstant: 30),
  671. closeButton.heightAnchor.constraint(equalToConstant: 30),
  672. crownIcon.heightAnchor.constraint(equalToConstant: 20)
  673. ])
  674. premiumCloseButton = closeButton
  675. updatePremiumCloseButtonVisibility()
  676. }
  677. private func updatePremiumCloseButtonVisibility() {
  678. premiumCloseButton?.isHidden = !subscriptionStore.isProActive
  679. }
  680. private func makePricingCard(_ plan: Plan) -> NSView {
  681. let card = HoverPricingCardView(baseBorderColor: Theme.cardBorder, hoverBorderColor: Theme.accent)
  682. card.translatesAutoresizingMaskIntoConstraints = false
  683. card.layer?.backgroundColor = Theme.cardBackground.cgColor
  684. let iconWell = NSView()
  685. iconWell.translatesAutoresizingMaskIntoConstraints = false
  686. iconWell.wantsLayer = true
  687. iconWell.layer?.cornerRadius = 10
  688. iconWell.layer?.backgroundColor = Theme.bottomStrip.cgColor
  689. iconWell.widthAnchor.constraint(equalToConstant: 24).isActive = true
  690. iconWell.heightAnchor.constraint(equalToConstant: 24).isActive = true
  691. let icon = NSImageView()
  692. icon.translatesAutoresizingMaskIntoConstraints = false
  693. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .bold)
  694. icon.image = NSImage(systemSymbolName: plan.iconName, accessibilityDescription: nil)
  695. icon.contentTintColor = plan.iconTint
  696. iconWell.addSubview(icon)
  697. NSLayoutConstraint.activate([
  698. icon.centerXAnchor.constraint(equalTo: iconWell.centerXAnchor),
  699. icon.centerYAnchor.constraint(equalTo: iconWell.centerYAnchor)
  700. ])
  701. let titleLabel = NSTextField(labelWithString: plan.title)
  702. titleLabel.font = .systemFont(ofSize: 20, weight: .semibold)
  703. titleLabel.textColor = Theme.primaryText
  704. titleLabel.alignment = .center
  705. let subtitleLabel = NSTextField(labelWithString: plan.subtitle)
  706. subtitleLabel.font = .systemFont(ofSize: 12, weight: .regular)
  707. subtitleLabel.textColor = Theme.secondaryText
  708. subtitleLabel.alignment = .center
  709. let topRightTag = pillLabel(text: plan.billedPill, tint: Theme.bottomStrip, textColor: Theme.iconTint)
  710. topRightTag.isHidden = plan.billedPill.isEmpty
  711. topRightTag.font = .systemFont(ofSize: 10, weight: .bold)
  712. let priceLabel = NSTextField(labelWithString: Self.unloadedPricePlaceholder)
  713. priceLabel.font = .systemFont(ofSize: 18, weight: .semibold)
  714. priceLabel.textColor = Theme.primaryText
  715. let periodLabel = NSTextField(labelWithString: plan.period)
  716. periodLabel.font = .systemFont(ofSize: 13, weight: .medium)
  717. periodLabel.textColor = Theme.secondaryText
  718. let priceRow = NSStackView(views: [priceLabel, periodLabel])
  719. priceRow.orientation = .horizontal
  720. priceRow.spacing = 4
  721. priceRow.alignment = .firstBaseline
  722. let billingLabel = NSTextField(labelWithString: plan.billedLine)
  723. billingLabel.font = .systemFont(ofSize: 13, weight: .medium)
  724. billingLabel.textColor = Theme.secondaryText
  725. let inlinePriceInfo = inlinePriceInfoLabel(oldPrice: plan.crossedPrice, newPrice: plan.savingsText)
  726. let divider = NSBox()
  727. divider.boxType = .separator
  728. divider.translatesAutoresizingMaskIntoConstraints = false
  729. divider.borderColor = Theme.divider
  730. let featureRows = plan.features.map(makeFeatureRow(_:))
  731. let featuresStack = NSStackView(views: featureRows)
  732. featuresStack.orientation = .vertical
  733. featuresStack.spacing = FeatureListMetrics.rowSpacing
  734. featuresStack.alignment = .leading
  735. featuresStack.edgeInsets = FeatureListMetrics.edgeInsets
  736. featuresStack.setContentCompressionResistancePriority(.required, for: .vertical)
  737. featuresStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  738. for row in featureRows {
  739. row.setContentCompressionResistancePriority(.required, for: .vertical)
  740. }
  741. let featuresScroll = makeFeatureListScroll(featuresStack: featuresStack)
  742. let selectButton = PlanPurchaseHoverButton(
  743. planId: plan.id,
  744. title: String(format: L("Get %@"), plan.title),
  745. isPrimaryStyle: plan.highlight,
  746. target: self,
  747. action: #selector(didTapSelectPlan)
  748. )
  749. planPurchaseButtons[plan.id] = selectButton
  750. planPriceFields[plan.id] = (priceLabel, periodLabel)
  751. selectButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
  752. let featureLabels = featureRows.compactMap { row -> NSTextField? in
  753. (row as? NSStackView)?.arrangedSubviews.compactMap { $0 as? NSTextField }.first
  754. }
  755. let featureIcons = featureRows.compactMap { row -> NSImageView? in
  756. (row as? NSStackView)?.arrangedSubviews.compactMap { $0 as? NSImageView }.first
  757. }
  758. pricingCardTargets.append(
  759. PricingCardAppearanceTarget(
  760. card: card,
  761. iconWell: iconWell,
  762. planIconView: icon,
  763. planId: plan.id,
  764. titleLabel: titleLabel,
  765. subtitleLabel: subtitleLabel,
  766. priceLabel: priceLabel,
  767. periodLabel: periodLabel,
  768. billingLabel: plan.billedLine.isEmpty ? nil : billingLabel,
  769. divider: divider,
  770. featureLabels: featureLabels,
  771. featureIcons: featureIcons,
  772. purchaseButton: selectButton,
  773. billedPillLabel: plan.billedPill.isEmpty ? nil : topRightTag
  774. )
  775. )
  776. var contentViews: [NSView] = [iconWell, titleLabel, subtitleLabel, priceRow]
  777. if !plan.billedLine.isEmpty {
  778. contentViews.append(billingLabel)
  779. }
  780. if plan.crossedPrice != nil, plan.savingsText != nil {
  781. contentViews.append(inlinePriceInfo)
  782. }
  783. contentViews.append(contentsOf: [divider, featuresScroll])
  784. let column = NSStackView(views: contentViews + [selectButton])
  785. column.orientation = .vertical
  786. column.spacing = 10
  787. column.alignment = .centerX
  788. column.distribution = .fill
  789. column.translatesAutoresizingMaskIntoConstraints = false
  790. card.addSubview(column)
  791. card.addSubview(topRightTag)
  792. NSLayoutConstraint.activate([
  793. divider.widthAnchor.constraint(equalTo: column.widthAnchor),
  794. featuresScroll.widthAnchor.constraint(equalTo: column.widthAnchor),
  795. column.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 18),
  796. column.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -18),
  797. column.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
  798. column.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -12),
  799. selectButton.widthAnchor.constraint(equalTo: column.widthAnchor),
  800. topRightTag.topAnchor.constraint(equalTo: card.topAnchor, constant: 12),
  801. topRightTag.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -12)
  802. ])
  803. return card
  804. }
  805. private func makeFeatureListScroll(featuresStack: NSStackView) -> NSScrollView {
  806. let scroll = NSScrollView()
  807. scroll.hasVerticalScroller = true
  808. scroll.autohidesScrollers = true
  809. scroll.drawsBackground = false
  810. scroll.borderType = .noBorder
  811. scroll.translatesAutoresizingMaskIntoConstraints = false
  812. let document = NSView()
  813. document.translatesAutoresizingMaskIntoConstraints = false
  814. featuresStack.translatesAutoresizingMaskIntoConstraints = false
  815. scroll.documentView = document
  816. document.addSubview(featuresStack)
  817. NSLayoutConstraint.activate([
  818. scroll.heightAnchor.constraint(equalToConstant: FeatureListMetrics.maxScrollHeight),
  819. document.leadingAnchor.constraint(equalTo: scroll.contentView.leadingAnchor),
  820. document.trailingAnchor.constraint(equalTo: scroll.contentView.trailingAnchor),
  821. document.topAnchor.constraint(equalTo: scroll.contentView.topAnchor),
  822. document.widthAnchor.constraint(equalTo: scroll.contentView.widthAnchor),
  823. document.bottomAnchor.constraint(equalTo: featuresStack.bottomAnchor, constant: FeatureListMetrics.edgeInsets.bottom),
  824. featuresStack.leadingAnchor.constraint(equalTo: document.leadingAnchor, constant: FeatureListMetrics.edgeInsets.left),
  825. featuresStack.trailingAnchor.constraint(equalTo: document.trailingAnchor, constant: -FeatureListMetrics.edgeInsets.right),
  826. featuresStack.topAnchor.constraint(equalTo: document.topAnchor, constant: FeatureListMetrics.edgeInsets.top),
  827. featuresStack.widthAnchor.constraint(equalTo: document.widthAnchor, constant: -(FeatureListMetrics.edgeInsets.left + FeatureListMetrics.edgeInsets.right))
  828. ])
  829. return scroll
  830. }
  831. private func makeFeatureRow(_ text: String) -> NSView {
  832. let icon = NSImageView()
  833. icon.translatesAutoresizingMaskIntoConstraints = false
  834. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .bold)
  835. icon.image = NSImage(systemSymbolName: "checkmark.circle.fill", accessibilityDescription: nil)
  836. icon.contentTintColor = Theme.iconTint
  837. icon.widthAnchor.constraint(equalToConstant: 14).isActive = true
  838. icon.heightAnchor.constraint(equalToConstant: 14).isActive = true
  839. icon.setContentHuggingPriority(.required, for: .horizontal)
  840. icon.setContentHuggingPriority(.required, for: .vertical)
  841. let label = NSTextField(wrappingLabelWithString: text)
  842. label.font = .systemFont(ofSize: 13, weight: .medium)
  843. label.textColor = Theme.primaryText
  844. label.alignment = .left
  845. label.lineBreakMode = .byWordWrapping
  846. label.maximumNumberOfLines = 0
  847. label.cell?.wraps = true
  848. label.cell?.isScrollable = false
  849. label.setContentCompressionResistancePriority(.required, for: .vertical)
  850. label.setContentHuggingPriority(.required, for: .vertical)
  851. label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  852. label.setContentHuggingPriority(.defaultLow, for: .horizontal)
  853. let row = NSStackView(views: [icon, label])
  854. row.orientation = .horizontal
  855. row.spacing = FeatureListMetrics.iconLabelSpacing
  856. row.alignment = .top
  857. row.distribution = .fill
  858. row.translatesAutoresizingMaskIntoConstraints = false
  859. return row
  860. }
  861. private func inlinePriceInfoLabel(oldPrice: String?, newPrice: String?) -> NSTextField {
  862. guard let oldPrice, let newPrice else {
  863. return NSTextField(labelWithString: "")
  864. }
  865. let full = NSMutableAttributedString()
  866. let oldAttributes: [NSAttributedString.Key: Any] = [
  867. .font: NSFont.systemFont(ofSize: 12, weight: .semibold),
  868. .foregroundColor: Theme.secondaryText,
  869. .strikethroughStyle: NSUnderlineStyle.single.rawValue
  870. ]
  871. let newAttributes: [NSAttributedString.Key: Any] = [
  872. .font: NSFont.systemFont(ofSize: 12, weight: .bold),
  873. .foregroundColor: Theme.successText
  874. ]
  875. full.append(NSAttributedString(string: "\(oldPrice) ", attributes: oldAttributes))
  876. full.append(NSAttributedString(string: newPrice, attributes: newAttributes))
  877. let label = NSTextField(labelWithAttributedString: full)
  878. return label
  879. }
  880. private func pillLabel(text: String, tint: NSColor, textColor: NSColor) -> NSTextField {
  881. let pill = NSTextField(labelWithString: text)
  882. pill.font = .systemFont(ofSize: 10, weight: .semibold)
  883. pill.textColor = textColor
  884. pill.alignment = .center
  885. pill.wantsLayer = true
  886. pill.layer?.backgroundColor = tint.cgColor
  887. pill.layer?.cornerRadius = 9
  888. pill.translatesAutoresizingMaskIntoConstraints = false
  889. pill.heightAnchor.constraint(equalToConstant: 18).isActive = true
  890. pill.widthAnchor.constraint(greaterThanOrEqualToConstant: 95).isActive = true
  891. return pill
  892. }
  893. private func makeTrustRow() -> NSView {
  894. let badges = NSStackView(views: [
  895. trustBadge(icon: "shield.fill", title: L("Secure Payments"), subtitle: L("Your payment is 100% secure.")),
  896. trustBadge(icon: "arrow.counterclockwise", title: L("Cancel Anytime"), subtitle: L("No commitment, cancel anytime.")),
  897. trustBadge(icon: "headphones", title: L("24/7 Support"), subtitle: L("We're here to help you anytime.")),
  898. trustBadge(icon: "lock.fill", title: L("Privacy First"), subtitle: L("Your data is safe with us."))
  899. ])
  900. badges.orientation = .horizontal
  901. badges.alignment = .centerY
  902. badges.distribution = .fillEqually
  903. badges.spacing = 12
  904. badges.edgeInsets = NSEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
  905. badges.translatesAutoresizingMaskIntoConstraints = false
  906. badges.wantsLayer = true
  907. badges.layer?.backgroundColor = Theme.bottomStrip.cgColor
  908. badges.layer?.borderColor = Theme.divider.cgColor
  909. badges.layer?.borderWidth = 1
  910. badges.layer?.cornerRadius = 10
  911. badges.setHuggingPriority(.defaultLow, for: .horizontal)
  912. badges.heightAnchor.constraint(equalToConstant: 72).isActive = true
  913. trustBadgesRow = badges
  914. return badges
  915. }
  916. private func makeFooterRow() -> NSView {
  917. footerLinkButtons = []
  918. let primary = footerActionCell(
  919. title: subscriptionPrimaryFooterTitle(),
  920. action: #selector(didTapPrimaryFooterSubscriptionAction),
  921. showsTrailingDivider: true
  922. )
  923. subscriptionPrimaryFooterButton = primary.button
  924. let entries: [(text: String, action: Selector)] = [
  925. (L("Restore Purchase"), #selector(didTapRestorePurchases)),
  926. (L("Privacy Policy"), #selector(didTapFooterPrivacyPolicy)),
  927. (L("Terms of Use"), #selector(didTapFooterTermsOfServices)),
  928. (L("Support"), #selector(didTapFooterSupport))
  929. ]
  930. let cells = [primary.container] + entries.enumerated().map { index, entry in
  931. footerActionCell(title: entry.text, action: entry.action, showsTrailingDivider: index < entries.count - 1).container
  932. }
  933. let links = NSStackView(views: cells)
  934. links.orientation = .horizontal
  935. links.distribution = .fillEqually
  936. links.spacing = 0
  937. links.alignment = .centerY
  938. links.translatesAutoresizingMaskIntoConstraints = false
  939. return links
  940. }
  941. private func footerActionCell(title: String, action: Selector, showsTrailingDivider: Bool) -> (container: NSView, button: NSButton) {
  942. let container = NSView()
  943. container.translatesAutoresizingMaskIntoConstraints = false
  944. let button = FooterLinkButton(title: title, target: self, action: action)
  945. button.isBordered = false
  946. button.bezelStyle = .rounded
  947. button.font = .systemFont(ofSize: 12, weight: .medium)
  948. button.contentTintColor = Theme.secondaryText
  949. button.focusRingType = .none
  950. button.translatesAutoresizingMaskIntoConstraints = false
  951. footerLinkButtons.append(button)
  952. container.addSubview(button)
  953. var constraints: [NSLayoutConstraint] = [
  954. button.centerXAnchor.constraint(equalTo: container.centerXAnchor),
  955. button.centerYAnchor.constraint(equalTo: container.centerYAnchor)
  956. ]
  957. if showsTrailingDivider {
  958. let divider = footerDivider()
  959. container.addSubview(divider)
  960. constraints.append(contentsOf: [
  961. divider.trailingAnchor.constraint(equalTo: container.trailingAnchor),
  962. divider.centerYAnchor.constraint(equalTo: container.centerYAnchor)
  963. ])
  964. }
  965. NSLayoutConstraint.activate(constraints)
  966. return (container, button)
  967. }
  968. private enum PrimaryFooterSubscriptionTitle {
  969. static var manage: String { L("Manage Subscription") }
  970. static var continueFree: String { L("Continue with free plan") }
  971. }
  972. private func subscriptionPrimaryFooterTitle() -> String {
  973. subscriptionStore.isProActive ? PrimaryFooterSubscriptionTitle.manage : PrimaryFooterSubscriptionTitle.continueFree
  974. }
  975. private func updateSubscriptionPrimaryFooter() {
  976. subscriptionPrimaryFooterButton?.title = subscriptionPrimaryFooterTitle()
  977. }
  978. private func trustBadge(icon: String, title: String, subtitle: String) -> NSView {
  979. let image = NSImageView()
  980. image.translatesAutoresizingMaskIntoConstraints = false
  981. image.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
  982. image.image = NSImage(systemSymbolName: icon, accessibilityDescription: nil)
  983. image.contentTintColor = Theme.primaryText
  984. let titleLabel = NSTextField(labelWithString: title)
  985. titleLabel.font = .systemFont(ofSize: 12, weight: .bold)
  986. titleLabel.textColor = Theme.primaryText
  987. let subtitleLabel = NSTextField(labelWithString: subtitle)
  988. subtitleLabel.font = .systemFont(ofSize: 10, weight: .medium)
  989. subtitleLabel.textColor = Theme.secondaryText
  990. let textStack = NSStackView(views: [titleLabel, subtitleLabel])
  991. textStack.orientation = .vertical
  992. textStack.spacing = 2
  993. textStack.alignment = .leading
  994. let stack = NSStackView(views: [image, textStack])
  995. stack.orientation = .horizontal
  996. stack.spacing = 8
  997. stack.alignment = .leading
  998. stack.edgeInsets = NSEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
  999. stack.wantsLayer = true
  1000. stack.layer?.backgroundColor = NSColor.clear.cgColor
  1001. return stack
  1002. }
  1003. private func footerDivider() -> NSBox {
  1004. let divider = NSBox()
  1005. divider.boxType = .separator
  1006. divider.borderColor = Theme.divider
  1007. divider.translatesAutoresizingMaskIntoConstraints = false
  1008. divider.widthAnchor.constraint(equalToConstant: 1).isActive = true
  1009. divider.heightAnchor.constraint(equalToConstant: 14).isActive = true
  1010. return divider
  1011. }
  1012. @objc private func didTapSelectPlan(_ sender: NSButton) {
  1013. guard let planKey = sender.identifier?.rawValue else { return }
  1014. Task { await purchasePlan(planKey: planKey) }
  1015. }
  1016. @objc private func didTapPrimaryFooterSubscriptionAction(_ sender: NSButton) {
  1017. let userTappedManage = (sender.title == PrimaryFooterSubscriptionTitle.manage)
  1018. Task { @MainActor [weak self] in
  1019. guard let self else { return }
  1020. await subscriptionStore.refreshEntitlements(deep: true)
  1021. updateSubscriptionPrimaryFooter()
  1022. let active = subscriptionStore.isProActive
  1023. if active || userTappedManage {
  1024. guard let url = URL(string: "https://apps.apple.com/account/subscriptions") else { return }
  1025. NSWorkspace.shared.open(url)
  1026. return
  1027. }
  1028. // Non-pro: dismiss paywall and return to the home (dashboard) window.
  1029. dismissPremiumSheetFromParentIfNeeded()
  1030. }
  1031. }
  1032. @objc private func didTapRestorePurchases() {
  1033. Task { await restorePurchases() }
  1034. }
  1035. @objc private func didTapFooterPrivacyPolicy() {
  1036. AppLegalURLs.openInSafari(AppLegalURLs.privacyPolicy)
  1037. }
  1038. @objc private func didTapFooterTermsOfServices() {
  1039. AppLegalURLs.openInSafari(AppLegalURLs.termsOfUse)
  1040. }
  1041. @objc private func didTapFooterSupport() {
  1042. AppLegalURLs.openInSafari(AppLegalURLs.support)
  1043. }
  1044. private func loadStoreProducts() async {
  1045. applyStorePricing()
  1046. await subscriptionStore.ensureProductsLoaded()
  1047. applyStorePricing()
  1048. updateSubscriptionPrimaryFooter()
  1049. updatePremiumCloseButtonVisibility()
  1050. await subscriptionStore.refreshEntitlements(deep: true)
  1051. updateSubscriptionPrimaryFooter()
  1052. updatePremiumCloseButtonVisibility()
  1053. }
  1054. private func applyStorePricing() {
  1055. for plan in plans {
  1056. guard let fields = planPriceFields[plan.id] else { continue }
  1057. guard let product = subscriptionStore.product(forPlanKey: plan.id) else {
  1058. fields.price.stringValue = Self.unloadedPricePlaceholder
  1059. continue
  1060. }
  1061. fields.price.stringValue = product.displayPrice
  1062. if let period = product.subscription?.subscriptionPeriod {
  1063. fields.period.stringValue = periodSuffix(for: period)
  1064. }
  1065. }
  1066. }
  1067. private func periodSuffix(for period: Product.SubscriptionPeriod) -> String {
  1068. let value = period.value
  1069. switch period.unit {
  1070. case .day: return value == 1 ? L("/ day") : String(format: L("/ %d days"), value)
  1071. case .week: return value == 1 ? L("/ week") : String(format: L("/ %d weeks"), value)
  1072. case .month: return value == 1 ? L("/ month") : String(format: L("/ %d months"), value)
  1073. case .year: return value == 1 ? L("/ year") : String(format: L("/ %d years"), value)
  1074. @unknown default: return ""
  1075. }
  1076. }
  1077. private func setPurchasing(_ isPurchasing: Bool) {
  1078. for button in planPurchaseButtons.values {
  1079. button.isEnabled = !isPurchasing
  1080. }
  1081. }
  1082. private func purchasePlan(planKey: String) async {
  1083. setPurchasing(true)
  1084. defer { setPurchasing(false) }
  1085. do {
  1086. let completed = try await subscriptionStore.purchase(planKey: planKey)
  1087. guard completed else { return }
  1088. AppRatingCoordinator.shared.scheduleReviewAfterSubscriptionPurchase()
  1089. let alert = NSAlert()
  1090. alert.messageText = L("You're subscribed")
  1091. alert.informativeText = L("Thank you — Pro features are now available.")
  1092. alert.alertStyle = .informational
  1093. alert.addButton(withTitle: L("OK"))
  1094. if let window = view.window {
  1095. alert.beginSheetModal(for: window) { [weak self] _ in
  1096. self?.dismissPremiumSheetFromParentIfNeeded()
  1097. }
  1098. } else {
  1099. alert.runModal()
  1100. dismissPremiumSheetFromParentIfNeeded()
  1101. }
  1102. } catch {
  1103. await MainActor.run {
  1104. self.presentPurchaseError(error, context: .purchase)
  1105. }
  1106. }
  1107. }
  1108. private func restorePurchases() async {
  1109. setPurchasing(true)
  1110. defer { setPurchasing(false) }
  1111. do {
  1112. try await subscriptionStore.restorePurchases()
  1113. let active = subscriptionStore.isProActive
  1114. let alert = NSAlert()
  1115. if active {
  1116. alert.messageText = L("Purchases restored")
  1117. alert.informativeText = L("Your subscription is active.")
  1118. } else {
  1119. alert.messageText = L("No subscription found")
  1120. alert.informativeText = L("There was nothing to restore for this Apple ID.")
  1121. }
  1122. alert.alertStyle = .informational
  1123. alert.addButton(withTitle: L("OK"))
  1124. if let window = view.window {
  1125. alert.beginSheetModal(for: window) { [weak self] _ in
  1126. if active {
  1127. self?.dismissPremiumSheetFromParentIfNeeded()
  1128. }
  1129. }
  1130. } else {
  1131. alert.runModal()
  1132. if active {
  1133. dismissPremiumSheetFromParentIfNeeded()
  1134. }
  1135. }
  1136. } catch {
  1137. await MainActor.run {
  1138. self.presentPurchaseError(error, context: .restore)
  1139. }
  1140. }
  1141. }
  1142. private func presentPurchaseError(
  1143. _ error: Error,
  1144. context: UserFacingErrorMessage.PurchaseContext
  1145. ) {
  1146. let alert = NSAlert()
  1147. alert.messageText = L("Something went wrong")
  1148. alert.informativeText = UserFacingErrorMessage.purchaseFailure(error, context: context)
  1149. alert.alertStyle = .warning
  1150. alert.addButton(withTitle: L("OK"))
  1151. if let window = view.window {
  1152. alert.beginSheetModal(for: window)
  1153. } else {
  1154. alert.runModal()
  1155. }
  1156. }
  1157. private func applyLocalizedStrings() {
  1158. view.window?.title = L("Premium Plans")
  1159. premiumTitleLabel?.stringValue = L("Upgrade to Pro")
  1160. premiumSubtitleLabel?.stringValue = L("Unlock unlimited access to premium tools and boost your productivity.")
  1161. for target in pricingCardTargets {
  1162. guard let plan = plans.first(where: { $0.id == target.planId }) else { continue }
  1163. target.titleLabel.stringValue = plan.title
  1164. target.subtitleLabel.stringValue = plan.subtitle
  1165. if subscriptionStore.product(forPlanKey: plan.id) == nil {
  1166. target.periodLabel.stringValue = plan.period
  1167. }
  1168. target.billedPillLabel?.stringValue = plan.billedPill
  1169. target.billedPillLabel?.isHidden = plan.billedPill.isEmpty
  1170. for (index, label) in target.featureLabels.enumerated() where index < plan.features.count {
  1171. label.stringValue = plan.features[index]
  1172. }
  1173. target.purchaseButton.title = String(format: L("Get %@"), plan.title)
  1174. }
  1175. if let trustBadgesRow {
  1176. let trustData: [(String, String)] = [
  1177. (L("Secure Payments"), L("Your payment is 100% secure.")),
  1178. (L("Cancel Anytime"), L("No commitment, cancel anytime.")),
  1179. (L("24/7 Support"), L("We're here to help you anytime.")),
  1180. (L("Privacy First"), L("Your data is safe with us."))
  1181. ]
  1182. for (index, subview) in trustBadgesRow.arrangedSubviews.enumerated() {
  1183. guard let badge = subview as? NSStackView, index < trustData.count else { continue }
  1184. for case let textStack as NSStackView in badge.arrangedSubviews {
  1185. let labels = textStack.arrangedSubviews.compactMap { $0 as? NSTextField }
  1186. if labels.count >= 2 {
  1187. labels[0].stringValue = trustData[index].0
  1188. labels[1].stringValue = trustData[index].1
  1189. }
  1190. }
  1191. }
  1192. }
  1193. let footerTitles = [
  1194. subscriptionPrimaryFooterTitle(),
  1195. L("Restore Purchase"),
  1196. L("Privacy Policy"),
  1197. L("Terms of Use"),
  1198. L("Support")
  1199. ]
  1200. for (index, button) in footerLinkButtons.enumerated() where index < footerTitles.count {
  1201. button.title = footerTitles[index]
  1202. }
  1203. updateSubscriptionPrimaryFooter()
  1204. applyStorePricing()
  1205. }
  1206. private func dismissPremiumSheetFromParentIfNeeded() {
  1207. guard let sheet = view.window, let parent = sheet.sheetParent else { return }
  1208. parent.endSheet(sheet)
  1209. }
  1210. @objc private func didTapClose() {
  1211. guard let window = view.window else { return }
  1212. if let parent = window.sheetParent {
  1213. parent.endSheet(window)
  1214. return
  1215. }
  1216. window.close()
  1217. }
  1218. private func applyCurrentAppearance() {
  1219. view.window?.backgroundColor = PremiumPlansWindowController.paywallSheetBackground
  1220. pageGradient.colors = [Theme.pageStart.cgColor, Theme.pageEnd.cgColor]
  1221. premiumTitleLabel?.textColor = Theme.primaryText
  1222. premiumSubtitleLabel?.textColor = Theme.secondaryText
  1223. for target in pricingCardTargets {
  1224. target.card.updateBorderColors(base: Theme.cardBorder, hover: Theme.accent)
  1225. target.card.layer?.backgroundColor = Theme.cardBackground.cgColor
  1226. target.iconWell.layer?.backgroundColor = Theme.bottomStrip.cgColor
  1227. target.planIconView.contentTintColor = planIconTint(planId: target.planId)
  1228. target.titleLabel.textColor = Theme.primaryText
  1229. target.subtitleLabel.textColor = Theme.secondaryText
  1230. target.priceLabel.textColor = Theme.primaryText
  1231. target.periodLabel.textColor = Theme.secondaryText
  1232. target.billingLabel?.textColor = Theme.secondaryText
  1233. target.divider.borderColor = Theme.divider
  1234. for label in target.featureLabels {
  1235. label.textColor = Theme.primaryText
  1236. }
  1237. for icon in target.featureIcons {
  1238. icon.contentTintColor = Theme.iconTint
  1239. }
  1240. target.purchaseButton.refreshAppearance()
  1241. }
  1242. if let trustBadgesRow {
  1243. trustBadgesRow.layer?.backgroundColor = Theme.bottomStrip.cgColor
  1244. trustBadgesRow.layer?.borderColor = Theme.divider.cgColor
  1245. for case let badge as NSStackView in trustBadgesRow.arrangedSubviews {
  1246. for case let image as NSImageView in badge.arrangedSubviews {
  1247. image.contentTintColor = Theme.primaryText
  1248. }
  1249. for case let textStack as NSStackView in badge.arrangedSubviews {
  1250. let labels = textStack.arrangedSubviews.compactMap { $0 as? NSTextField }
  1251. if labels.count >= 2 {
  1252. labels[0].textColor = Theme.primaryText
  1253. labels[1].textColor = Theme.secondaryText
  1254. }
  1255. }
  1256. }
  1257. }
  1258. for button in footerLinkButtons {
  1259. button.contentTintColor = Theme.secondaryText
  1260. }
  1261. premiumCloseButton?.refreshAppearance(hovered: false)
  1262. }
  1263. private func planIconTint(planId: String) -> NSColor {
  1264. switch planId {
  1265. case "monthly": Theme.accent
  1266. case "yearly": Theme.successText
  1267. default: Theme.iconTint
  1268. }
  1269. }
  1270. }