Açıklama Yok

PremiumPlansWindowController.swift 59KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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)
  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)
  1139. }
  1140. }
  1141. }
  1142. private func presentPurchaseError(_ error: Error) {
  1143. let alert = NSAlert()
  1144. alert.messageText = L("Something went wrong")
  1145. if let localized = error as? LocalizedError {
  1146. var parts: [String] = []
  1147. if let description = localized.errorDescription {
  1148. parts.append(description)
  1149. }
  1150. if let recovery = localized.recoverySuggestion {
  1151. parts.append(recovery)
  1152. }
  1153. alert.informativeText = parts.isEmpty ? error.localizedDescription : parts.joined(separator: "\n\n")
  1154. } else {
  1155. alert.informativeText = error.localizedDescription
  1156. }
  1157. alert.alertStyle = .warning
  1158. alert.addButton(withTitle: L("OK"))
  1159. if let window = view.window {
  1160. alert.beginSheetModal(for: window)
  1161. } else {
  1162. alert.runModal()
  1163. }
  1164. }
  1165. private func applyLocalizedStrings() {
  1166. view.window?.title = L("Premium Plans")
  1167. premiumTitleLabel?.stringValue = L("Upgrade to Pro")
  1168. premiumSubtitleLabel?.stringValue = L("Unlock unlimited access to premium tools and boost your productivity.")
  1169. for target in pricingCardTargets {
  1170. guard let plan = plans.first(where: { $0.id == target.planId }) else { continue }
  1171. target.titleLabel.stringValue = plan.title
  1172. target.subtitleLabel.stringValue = plan.subtitle
  1173. if subscriptionStore.product(forPlanKey: plan.id) == nil {
  1174. target.periodLabel.stringValue = plan.period
  1175. }
  1176. target.billedPillLabel?.stringValue = plan.billedPill
  1177. target.billedPillLabel?.isHidden = plan.billedPill.isEmpty
  1178. for (index, label) in target.featureLabels.enumerated() where index < plan.features.count {
  1179. label.stringValue = plan.features[index]
  1180. }
  1181. target.purchaseButton.title = String(format: L("Get %@"), plan.title)
  1182. }
  1183. if let trustBadgesRow {
  1184. let trustData: [(String, String)] = [
  1185. (L("Secure Payments"), L("Your payment is 100% secure.")),
  1186. (L("Cancel Anytime"), L("No commitment, cancel anytime.")),
  1187. (L("24/7 Support"), L("We're here to help you anytime.")),
  1188. (L("Privacy First"), L("Your data is safe with us."))
  1189. ]
  1190. for (index, subview) in trustBadgesRow.arrangedSubviews.enumerated() {
  1191. guard let badge = subview as? NSStackView, index < trustData.count else { continue }
  1192. for case let textStack as NSStackView in badge.arrangedSubviews {
  1193. let labels = textStack.arrangedSubviews.compactMap { $0 as? NSTextField }
  1194. if labels.count >= 2 {
  1195. labels[0].stringValue = trustData[index].0
  1196. labels[1].stringValue = trustData[index].1
  1197. }
  1198. }
  1199. }
  1200. }
  1201. let footerTitles = [
  1202. subscriptionPrimaryFooterTitle(),
  1203. L("Restore Purchase"),
  1204. L("Privacy Policy"),
  1205. L("Terms of Use"),
  1206. L("Support")
  1207. ]
  1208. for (index, button) in footerLinkButtons.enumerated() where index < footerTitles.count {
  1209. button.title = footerTitles[index]
  1210. }
  1211. updateSubscriptionPrimaryFooter()
  1212. applyStorePricing()
  1213. }
  1214. private func dismissPremiumSheetFromParentIfNeeded() {
  1215. guard let sheet = view.window, let parent = sheet.sheetParent else { return }
  1216. parent.endSheet(sheet)
  1217. }
  1218. @objc private func didTapClose() {
  1219. guard let window = view.window else { return }
  1220. if let parent = window.sheetParent {
  1221. parent.endSheet(window)
  1222. return
  1223. }
  1224. window.close()
  1225. }
  1226. private func applyCurrentAppearance() {
  1227. view.window?.backgroundColor = PremiumPlansWindowController.paywallSheetBackground
  1228. pageGradient.colors = [Theme.pageStart.cgColor, Theme.pageEnd.cgColor]
  1229. premiumTitleLabel?.textColor = Theme.primaryText
  1230. premiumSubtitleLabel?.textColor = Theme.secondaryText
  1231. for target in pricingCardTargets {
  1232. target.card.updateBorderColors(base: Theme.cardBorder, hover: Theme.accent)
  1233. target.card.layer?.backgroundColor = Theme.cardBackground.cgColor
  1234. target.iconWell.layer?.backgroundColor = Theme.bottomStrip.cgColor
  1235. target.planIconView.contentTintColor = planIconTint(planId: target.planId)
  1236. target.titleLabel.textColor = Theme.primaryText
  1237. target.subtitleLabel.textColor = Theme.secondaryText
  1238. target.priceLabel.textColor = Theme.primaryText
  1239. target.periodLabel.textColor = Theme.secondaryText
  1240. target.billingLabel?.textColor = Theme.secondaryText
  1241. target.divider.borderColor = Theme.divider
  1242. for label in target.featureLabels {
  1243. label.textColor = Theme.primaryText
  1244. }
  1245. for icon in target.featureIcons {
  1246. icon.contentTintColor = Theme.iconTint
  1247. }
  1248. target.purchaseButton.refreshAppearance()
  1249. }
  1250. if let trustBadgesRow {
  1251. trustBadgesRow.layer?.backgroundColor = Theme.bottomStrip.cgColor
  1252. trustBadgesRow.layer?.borderColor = Theme.divider.cgColor
  1253. for case let badge as NSStackView in trustBadgesRow.arrangedSubviews {
  1254. for case let image as NSImageView in badge.arrangedSubviews {
  1255. image.contentTintColor = Theme.primaryText
  1256. }
  1257. for case let textStack as NSStackView in badge.arrangedSubviews {
  1258. let labels = textStack.arrangedSubviews.compactMap { $0 as? NSTextField }
  1259. if labels.count >= 2 {
  1260. labels[0].textColor = Theme.primaryText
  1261. labels[1].textColor = Theme.secondaryText
  1262. }
  1263. }
  1264. }
  1265. }
  1266. for button in footerLinkButtons {
  1267. button.contentTintColor = Theme.secondaryText
  1268. }
  1269. premiumCloseButton?.refreshAppearance(hovered: false)
  1270. }
  1271. private func planIconTint(planId: String) -> NSColor {
  1272. switch planId {
  1273. case "monthly": Theme.accent
  1274. case "yearly": Theme.successText
  1275. default: Theme.iconTint
  1276. }
  1277. }
  1278. }