CVMakerPageView.swift 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. //
  2. // CVMakerPageView.swift
  3. // App for Indeed
  4. //
  5. // Template gallery for the CV Maker sidebar destination: page header, category
  6. // toggle, style chips, thumbnail grid, and a sticky bottom CTA. Follows the
  7. // active dashboard light / dark appearance via `AppDashboardTheme`.
  8. //
  9. import Cocoa
  10. import QuartzCore
  11. // MARK: - Data model
  12. enum CVCategoryGroup: Hashable {
  13. case designBased
  14. case professionBased
  15. var title: String {
  16. switch self {
  17. case .designBased: return L("Design-Based")
  18. case .professionBased: return L("Profession-Based")
  19. }
  20. }
  21. }
  22. enum CVDesignFamily: String, CaseIterable, Hashable {
  23. case professional, modern, creative, minimal, executive
  24. var title: String {
  25. switch self {
  26. case .professional: return L("Professional")
  27. case .modern: return L("Modern")
  28. case .creative: return L("Creative")
  29. case .minimal: return L("Minimal")
  30. case .executive: return L("Executive")
  31. }
  32. }
  33. }
  34. /// High-level layout bucket for catalog metadata and filtering.
  35. enum CVTemplateLayoutType: String, Hashable {
  36. case atsSingleColumn
  37. case twoColumnSidebarLeading
  38. case twoColumnSidebarTrailing
  39. var gallerySubtitle: String {
  40. switch self {
  41. case .atsSingleColumn: return L("ATS layout")
  42. case .twoColumnSidebarLeading: return L("Sidebar left")
  43. case .twoColumnSidebarTrailing: return L("Sidebar right")
  44. }
  45. }
  46. }
  47. /// Visual recipe used by the mini preview renderer so every template can vary
  48. /// the headline style, accent line, and sidebar layout without bespoke views.
  49. struct CVTemplate: Hashable {
  50. enum Headline: Hashable {
  51. /// Big name centered above the body.
  52. case centered
  53. /// Name aligned to the leading edge, role beneath it.
  54. case leftAligned
  55. /// Name on the leading edge with circular initials avatar on the trailing edge.
  56. case leftWithInitials
  57. /// Initials avatar above a centered name (single column).
  58. case avatarStacked
  59. }
  60. enum Accent: Hashable {
  61. case none
  62. case redUnderline
  63. case redBar
  64. case blueBar
  65. }
  66. enum SidebarSide: Hashable { case leading, trailing }
  67. enum Layout: Hashable {
  68. case singleColumn
  69. case twoColumn(sidebar: SidebarSide, tinted: Bool)
  70. }
  71. enum SectionLabelStyle: Hashable {
  72. case uppercase
  73. case slashed // "// EXPERIENCE"
  74. case bracketed // "[ EXPERIENCE ]"
  75. }
  76. let id: String
  77. let name: String
  78. let family: CVDesignFamily
  79. let headline: Headline
  80. let accent: Accent
  81. let layout: Layout
  82. let sectionLabelStyle: SectionLabelStyle
  83. /// sRGB accent used for headers, tags, and sidebar tints in the mini preview.
  84. let themeRed: CGFloat
  85. let themeGreen: CGFloat
  86. let themeBlue: CGFloat
  87. /// Shown on cards; mirrors the design family in this build.
  88. var category: String { family.title }
  89. var layoutType: CVTemplateLayoutType {
  90. switch layout {
  91. case .singleColumn: return .atsSingleColumn
  92. case .twoColumn(sidebar: .leading, _): return .twoColumnSidebarLeading
  93. case .twoColumn(sidebar: .trailing, _): return .twoColumnSidebarTrailing
  94. }
  95. }
  96. /// Top-level gallery tab: expressive layouts for design-led roles vs. conservative ATS-friendly styles.
  97. var galleryGroup: CVCategoryGroup {
  98. switch family {
  99. case .modern, .creative: return .designBased
  100. case .minimal, .professional, .executive: return .professionBased
  101. }
  102. }
  103. var themeColor: NSColor {
  104. NSColor(srgbRed: themeRed, green: themeGreen, blue: themeBlue, alpha: 1)
  105. }
  106. /// User-facing template title for the active language (`name` is the English localization key).
  107. var localizedName: String { localizedTemplateName(name) }
  108. /// Non-empty label for gallery cards and profile rows.
  109. var displayName: String {
  110. let localized = localizedName.trimmingCharacters(in: .whitespacesAndNewlines)
  111. if !localized.isEmpty { return localized }
  112. let raw = name.trimmingCharacters(in: .whitespacesAndNewlines)
  113. if !raw.isEmpty { return raw }
  114. return id
  115. }
  116. /// Optional bundle image name; `nil` means render a live vector/text preview.
  117. var previewImageAssetName: String? { nil }
  118. init(
  119. id: String,
  120. name: String,
  121. family: CVDesignFamily,
  122. headline: Headline,
  123. accent: Accent,
  124. layout: Layout,
  125. sectionLabelStyle: SectionLabelStyle,
  126. themeRed: CGFloat? = nil,
  127. themeGreen: CGFloat? = nil,
  128. themeBlue: CGFloat? = nil
  129. ) {
  130. self.id = id
  131. self.name = name
  132. self.family = family
  133. self.headline = headline
  134. self.accent = accent
  135. self.layout = layout
  136. self.sectionLabelStyle = sectionLabelStyle
  137. if let tr = themeRed, let tg = themeGreen, let tb = themeBlue {
  138. self.themeRed = tr
  139. self.themeGreen = tg
  140. self.themeBlue = tb
  141. } else {
  142. let rgb = Self.resolvedThemeRGB(family: family, id: id)
  143. self.themeRed = rgb.0
  144. self.themeGreen = rgb.1
  145. self.themeBlue = rgb.2
  146. }
  147. }
  148. private static func resolvedThemeRGB(family: CVDesignFamily, id: String) -> (CGFloat, CGFloat, CGFloat) {
  149. var hash: UInt64 = 1469598103934665603
  150. for b in id.utf8 {
  151. hash ^= UInt64(b)
  152. hash &*= 1_099_511_628_211
  153. }
  154. let t = Double(hash % 1000) / 1000.0
  155. switch family {
  156. case .professional:
  157. let r = 0.12 + t * 0.06
  158. let g = 0.32 + t * 0.08
  159. let b = 0.58 + t * 0.12
  160. return (r, g, b)
  161. case .modern:
  162. let r = 0.0 + t * 0.08
  163. let g = 0.45 + t * 0.12
  164. let bl = 0.85 + t * 0.1
  165. return (min(r, 1), min(g, 1), min(bl, 1))
  166. case .minimal:
  167. return (0.45 + t * 0.05, 0.48 + t * 0.04, 0.55 + t * 0.06)
  168. case .executive:
  169. let r = 0.08 + t * 0.06
  170. let g = 0.12 + t * 0.05
  171. let b = 0.22 + t * 0.08
  172. return (r, g, b)
  173. case .creative:
  174. let r = 0.25 + t * 0.2
  175. let g = 0.35 + t * 0.15
  176. let b = 0.72 + t * 0.15
  177. return (min(r, 1), min(g, 1), min(b, 1))
  178. }
  179. }
  180. }
  181. extension CVTemplate {
  182. /// Same 0…11 silhouette index as `CVTemplatePreviewView` so the filled résumé matches the gallery thumbnail for that template.
  183. var galleryLayoutVariant: Int {
  184. var h: UInt64 = 1469598103934665603
  185. let layoutDesc: String
  186. switch layout {
  187. case .singleColumn: layoutDesc = "1col"
  188. case .twoColumn(let s, let t): layoutDesc = "2col_\(s)_\(t)"
  189. }
  190. let blob = "\(id)|\(family.rawValue)|\(headline)|\(accent)|\(layoutDesc)|\(sectionLabelStyle)"
  191. for b in blob.utf8 {
  192. h ^= UInt64(b)
  193. h &*= 1_099_511_628_211
  194. }
  195. return Int(h % 12)
  196. }
  197. }
  198. // MARK: - Catalog
  199. enum CVTemplateCatalog {
  200. static let all: [CVTemplate] = [
  201. // Minimal family (matches the reference screenshot)
  202. CVTemplate(
  203. id: "paper-white",
  204. name: "Paper White",
  205. family: .minimal,
  206. headline: .centered,
  207. accent: .none,
  208. layout: .singleColumn,
  209. sectionLabelStyle: .uppercase
  210. ),
  211. CVTemplate(
  212. id: "swiss",
  213. name: "Swiss",
  214. family: .minimal,
  215. headline: .centered,
  216. accent: .redUnderline,
  217. layout: .twoColumn(sidebar: .leading, tinted: false),
  218. sectionLabelStyle: .uppercase
  219. ),
  220. CVTemplate(
  221. id: "mono",
  222. name: "Mono",
  223. family: .minimal,
  224. headline: .leftAligned,
  225. accent: .redUnderline,
  226. layout: .singleColumn,
  227. sectionLabelStyle: .slashed
  228. ),
  229. CVTemplate(
  230. id: "airy",
  231. name: "Airy",
  232. family: .minimal,
  233. headline: .leftWithInitials,
  234. accent: .none,
  235. layout: .twoColumn(sidebar: .trailing, tinted: false),
  236. sectionLabelStyle: .uppercase
  237. ),
  238. CVTemplate(
  239. id: "tabular",
  240. name: "Tabular",
  241. family: .minimal,
  242. headline: .leftAligned,
  243. accent: .none,
  244. layout: .singleColumn,
  245. sectionLabelStyle: .bracketed
  246. ),
  247. CVTemplate(
  248. id: "facet",
  249. name: "Facet",
  250. family: .minimal,
  251. headline: .avatarStacked,
  252. accent: .none,
  253. layout: .twoColumn(sidebar: .leading, tinted: true),
  254. sectionLabelStyle: .uppercase
  255. ),
  256. // Professional family
  257. CVTemplate(
  258. id: "corporate",
  259. name: "Corporate",
  260. family: .professional,
  261. headline: .leftAligned,
  262. accent: .blueBar,
  263. layout: .singleColumn,
  264. sectionLabelStyle: .uppercase
  265. ),
  266. CVTemplate(
  267. id: "atlas",
  268. name: "Atlas",
  269. family: .professional,
  270. headline: .centered,
  271. accent: .blueBar,
  272. layout: .twoColumn(sidebar: .leading, tinted: true),
  273. sectionLabelStyle: .uppercase
  274. ),
  275. CVTemplate(
  276. id: "ledger",
  277. name: "Ledger",
  278. family: .professional,
  279. headline: .leftAligned,
  280. accent: .blueBar,
  281. layout: .twoColumn(sidebar: .trailing, tinted: false),
  282. sectionLabelStyle: .uppercase
  283. ),
  284. CVTemplate(
  285. id: "harbor",
  286. name: "Harbor",
  287. family: .professional,
  288. headline: .leftWithInitials,
  289. accent: .none,
  290. layout: .twoColumn(sidebar: .leading, tinted: true),
  291. sectionLabelStyle: .uppercase
  292. ),
  293. CVTemplate(
  294. id: "metro",
  295. name: "Clear Path",
  296. family: .professional,
  297. headline: .centered,
  298. accent: .blueBar,
  299. layout: .singleColumn,
  300. sectionLabelStyle: .uppercase
  301. ),
  302. CVTemplate(
  303. id: "pinstripe",
  304. name: "Pinstripe",
  305. family: .professional,
  306. headline: .leftAligned,
  307. accent: .blueBar,
  308. layout: .twoColumn(sidebar: .leading, tinted: false),
  309. sectionLabelStyle: .uppercase
  310. ),
  311. CVTemplate(
  312. id: "briefing",
  313. name: "Briefing",
  314. family: .professional,
  315. headline: .leftAligned,
  316. accent: .blueBar,
  317. layout: .twoColumn(sidebar: .leading, tinted: true),
  318. sectionLabelStyle: .uppercase
  319. ),
  320. CVTemplate(
  321. id: "quorum",
  322. name: "Quorum",
  323. family: .professional,
  324. headline: .leftWithInitials,
  325. accent: .none,
  326. layout: .singleColumn,
  327. sectionLabelStyle: .bracketed
  328. ),
  329. CVTemplate(
  330. id: "docket",
  331. name: "Docket",
  332. family: .professional,
  333. headline: .centered,
  334. accent: .blueBar,
  335. layout: .twoColumn(sidebar: .trailing, tinted: false),
  336. sectionLabelStyle: .uppercase
  337. ),
  338. CVTemplate(
  339. id: "conduit",
  340. name: "Conduit",
  341. family: .professional,
  342. headline: .leftAligned,
  343. accent: .blueBar,
  344. layout: .singleColumn,
  345. sectionLabelStyle: .slashed
  346. ),
  347. CVTemplate(
  348. id: "principal",
  349. name: "Principal",
  350. family: .professional,
  351. headline: .leftWithInitials,
  352. accent: .blueBar,
  353. layout: .twoColumn(sidebar: .trailing, tinted: true),
  354. sectionLabelStyle: .uppercase
  355. ),
  356. CVTemplate(
  357. id: "charter",
  358. name: "Charter",
  359. family: .professional,
  360. headline: .leftAligned,
  361. accent: .none,
  362. layout: .twoColumn(sidebar: .leading, tinted: false),
  363. sectionLabelStyle: .uppercase
  364. ),
  365. // Modern family
  366. CVTemplate(
  367. id: "vertex",
  368. name: "Vertex",
  369. family: .modern,
  370. headline: .leftWithInitials,
  371. accent: .blueBar,
  372. layout: .twoColumn(sidebar: .leading, tinted: true),
  373. sectionLabelStyle: .slashed
  374. ),
  375. CVTemplate(
  376. id: "linea",
  377. name: "Linea",
  378. family: .modern,
  379. headline: .leftAligned,
  380. accent: .blueBar,
  381. layout: .singleColumn,
  382. sectionLabelStyle: .slashed
  383. ),
  384. CVTemplate(
  385. id: "prism",
  386. name: "Prism",
  387. family: .modern,
  388. headline: .avatarStacked,
  389. accent: .blueBar,
  390. layout: .twoColumn(sidebar: .trailing, tinted: true),
  391. sectionLabelStyle: .uppercase
  392. ),
  393. CVTemplate(
  394. id: "circuit",
  395. name: "Circuit",
  396. family: .modern,
  397. headline: .leftAligned,
  398. accent: .blueBar,
  399. layout: .twoColumn(sidebar: .trailing, tinted: false),
  400. sectionLabelStyle: .slashed
  401. ),
  402. CVTemplate(
  403. id: "north",
  404. name: "North",
  405. family: .modern,
  406. headline: .leftWithInitials,
  407. accent: .none,
  408. layout: .twoColumn(sidebar: .leading, tinted: false),
  409. sectionLabelStyle: .uppercase
  410. ),
  411. CVTemplate(
  412. id: "axis",
  413. name: "Axis",
  414. family: .modern,
  415. headline: .centered,
  416. accent: .blueBar,
  417. layout: .singleColumn,
  418. sectionLabelStyle: .bracketed
  419. ),
  420. // Creative family
  421. CVTemplate(
  422. id: "marigold",
  423. name: "Marigold",
  424. family: .creative,
  425. headline: .avatarStacked,
  426. accent: .redBar,
  427. layout: .twoColumn(sidebar: .leading, tinted: true),
  428. sectionLabelStyle: .slashed
  429. ),
  430. CVTemplate(
  431. id: "ember",
  432. name: "Ember",
  433. family: .creative,
  434. headline: .leftWithInitials,
  435. accent: .redBar,
  436. layout: .twoColumn(sidebar: .trailing, tinted: true),
  437. sectionLabelStyle: .slashed
  438. ),
  439. CVTemplate(
  440. id: "lattice",
  441. name: "Lattice",
  442. family: .creative,
  443. headline: .leftAligned,
  444. accent: .redUnderline,
  445. layout: .singleColumn,
  446. sectionLabelStyle: .bracketed
  447. ),
  448. CVTemplate(
  449. id: "bloom",
  450. name: "Bloom",
  451. family: .creative,
  452. headline: .avatarStacked,
  453. accent: .redBar,
  454. layout: .singleColumn,
  455. sectionLabelStyle: .slashed
  456. ),
  457. CVTemplate(
  458. id: "studio",
  459. name: "Studio",
  460. family: .creative,
  461. headline: .leftWithInitials,
  462. accent: .redUnderline,
  463. layout: .twoColumn(sidebar: .leading, tinted: true),
  464. sectionLabelStyle: .uppercase
  465. ),
  466. CVTemplate(
  467. id: "kite",
  468. name: "Kite",
  469. family: .creative,
  470. headline: .centered,
  471. accent: .redBar,
  472. layout: .twoColumn(sidebar: .trailing, tinted: false),
  473. sectionLabelStyle: .slashed
  474. ),
  475. // Executive family
  476. CVTemplate(
  477. id: "regent",
  478. name: "Regent",
  479. family: .executive,
  480. headline: .centered,
  481. accent: .blueBar,
  482. layout: .twoColumn(sidebar: .leading, tinted: true),
  483. sectionLabelStyle: .uppercase
  484. ),
  485. CVTemplate(
  486. id: "monarch",
  487. name: "Monarch",
  488. family: .executive,
  489. headline: .centered,
  490. accent: .blueBar,
  491. layout: .singleColumn,
  492. sectionLabelStyle: .uppercase
  493. ),
  494. CVTemplate(
  495. id: "sterling",
  496. name: "Sterling",
  497. family: .executive,
  498. headline: .leftAligned,
  499. accent: .blueBar,
  500. layout: .twoColumn(sidebar: .trailing, tinted: false),
  501. sectionLabelStyle: .uppercase
  502. ),
  503. CVTemplate(
  504. id: "summit",
  505. name: "Summit",
  506. family: .executive,
  507. headline: .centered,
  508. accent: .redUnderline,
  509. layout: .twoColumn(sidebar: .leading, tinted: false),
  510. sectionLabelStyle: .uppercase
  511. ),
  512. CVTemplate(
  513. id: "estate",
  514. name: "Estate",
  515. family: .executive,
  516. headline: .leftWithInitials,
  517. accent: .blueBar,
  518. layout: .twoColumn(sidebar: .trailing, tinted: true),
  519. sectionLabelStyle: .uppercase
  520. ),
  521. CVTemplate(
  522. id: "chairman",
  523. name: "Chairman",
  524. family: .executive,
  525. headline: .leftAligned,
  526. accent: .blueBar,
  527. layout: .singleColumn,
  528. sectionLabelStyle: .uppercase
  529. )
  530. ]
  531. }
  532. // MARK: - View
  533. /// Standalone NSView for the CV Maker route. Renders the template gallery with
  534. /// header, segmented category groups, family chips, a thumbnail grid, and a
  535. /// bottom CTA. Hosts inside the same `nonHomeHost` slot as Saved Jobs/Settings.
  536. final class CVMakerPageView: NSView {
  537. private enum Palette {
  538. static var primaryText: NSColor { AppDashboardTheme.primaryText }
  539. static var secondaryText: NSColor { AppDashboardTheme.secondaryText }
  540. static var cardBackground: NSColor { AppDashboardTheme.cardBackground }
  541. static var cardBorder: NSColor { AppDashboardTheme.border }
  542. static var cardBorderHover: NSColor { AppDashboardTheme.cvMakerCardBorderHover }
  543. static var cardBorderSelected: NSColor { AppDashboardTheme.brandBlue }
  544. static var cardFooter: NSColor { AppDashboardTheme.cvMakerCardFooter }
  545. static var previewSurface: NSColor { AppDashboardTheme.cvMakerPreviewSurface }
  546. static var previewPaper: NSColor { CVResumeAppearance.colors().paper }
  547. static var previewSidebarTint: NSColor { CVResumeAppearance.colors().sidebarTint }
  548. static var previewInk: NSColor { CVResumeAppearance.colors().ink }
  549. static var previewMuted: NSColor { CVResumeAppearance.colors().muted }
  550. static var previewAccentRed: NSColor { CVResumeAppearance.colors().accentRed }
  551. static var previewAccentBlue: NSColor { CVResumeAppearance.colors().accentBlue }
  552. static var ctaBackground: NSColor { AppDashboardTheme.brandBlue }
  553. static var ctaHover: NSColor { AppDashboardTheme.brandBlueHover }
  554. static var ctaText: NSColor { AppDashboardTheme.proCTAText }
  555. static var selectionGlow: NSColor { AppDashboardTheme.cvMakerSelectionGlow }
  556. static var gradientTop: NSColor { AppDashboardTheme.cvMakerPageGradientTop }
  557. static var gradientBottom: NSColor { AppDashboardTheme.cvMakerPageGradientBottom }
  558. static var filterChromeBorder: NSColor { AppDashboardTheme.cvMakerFilterChromeBorder }
  559. }
  560. private var appearanceObserver: NSObjectProtocol?
  561. private var languageObserver: NSObjectProtocol?
  562. private var scrollBoundsObserver: NSObjectProtocol?
  563. /// AI catalog keyed by `AppLanguage.localeIdentifier` so switching language can restore without a network round-trip.
  564. private var aiCatalogByLocale: [String: [CVTemplate]] = [:]
  565. /// Ignores stale OpenAI responses after a newer fetch or language change started.
  566. private var aiCatalogFetchGeneration = 0
  567. private let pageGradientLayer = CAGradientLayer()
  568. private let filterChrome = NSVisualEffectView()
  569. private let filterStack = NSStackView()
  570. private static let gallerySubtitleKey = "Polished layouts with live previews — pick a style that fits your story."
  571. private let titleLabel = NSTextField(labelWithString: "")
  572. private let subtitleLabel = NSTextField(labelWithString: "")
  573. private let groupTabsRow = NSStackView()
  574. private let familyChipsRow = NSStackView()
  575. private let scrollView = NSScrollView()
  576. private let gridDocument = TopFlippedView()
  577. private let gridStack = NSStackView()
  578. private let ctaButton = CVHoverableButton(title: L("Use Template & Select Profile →"), target: nil, action: nil)
  579. private var selectedGroup: CVCategoryGroup = .professionBased
  580. private var selectedFamily: CVDesignFamily? = nil // nil == "All"
  581. private var selectedTemplateID: String? = "metro"
  582. /// Exactly one gallery card — avoids multiple highlighted cards when catalog entries share the same `template.id`.
  583. private var selectedTemplateCardToken: UUID?
  584. /// Shown immediately; replaced when `CVTemplateFetchService` returns AI-generated entries.
  585. private var activeCatalog: [CVTemplate] = CVTemplateCatalog.all
  586. private var groupTabButtons: [CVCategoryGroup: CVChipButton] = [:]
  587. private var familyChipButtons: [CVDesignFamily?: CVChipButton] = [:]
  588. /// Every visible gallery card (not keyed by id — duplicate AI ids would collapse in a dictionary and break single-selection visuals).
  589. private var templateCardsInGrid: [CVTemplateCard] = []
  590. /// Invoked when the user taps **Use Template & Select Profile** with a valid gallery selection. Delivers the same `CVTemplate` instance the card used for its thumbnail (not a re-lookup by id), so the filled résumé cannot drift from the user’s pick.
  591. var onContinueToProfileSelection: ((CVTemplate) -> Void)?
  592. func templateInGallery(withID id: String) -> CVTemplate? {
  593. resolvedTemplate(withID: id)
  594. }
  595. /// Resolves a template from the live gallery, then falls back to the built-in catalog when AI fetch replaces `activeCatalog` (so the user’s selection still previews correctly).
  596. func resolvedTemplate(withID id: String) -> CVTemplate? {
  597. if let match = activeCatalog.first(where: { $0.id == id }) { return match }
  598. return CVTemplateCatalog.all.first { $0.id == id }
  599. }
  600. private var appliedGridColumnCount: Int = 0
  601. /// Family filter row always renders this many slots so chip widths stay stable
  602. /// when switching between Design-Based (3 labels) and Profession-Based (4).
  603. private let familyChipSlotCount = 4
  604. private enum FilterChromeLayout {
  605. static let padding: CGFloat = 12
  606. static let rowGap: CGFloat = 12
  607. static let groupRowHeight: CGFloat = 38
  608. static let familyRowHeight: CGFloat = 30
  609. static var height: CGFloat {
  610. padding * 2 + groupRowHeight + rowGap + familyRowHeight
  611. }
  612. }
  613. override init(frame frameRect: NSRect) {
  614. super.init(frame: frameRect)
  615. configureLayout()
  616. reloadFamilyChips()
  617. reloadTemplateGrid()
  618. updateSelectedChipStates()
  619. appearanceObserver = NotificationCenter.default.addObserver(
  620. forName: AppAppearanceManager.didChangeNotification,
  621. object: nil,
  622. queue: .main
  623. ) { [weak self] _ in
  624. self?.applyCurrentAppearance()
  625. }
  626. languageObserver = NotificationCenter.default.addObserver(
  627. forName: AppLanguageManager.didChangeNotification,
  628. object: nil,
  629. queue: .main
  630. ) { [weak self] _ in
  631. self?.applyLocalizedStrings()
  632. self?.restoreOrLoadAICatalogForCurrentLanguage()
  633. }
  634. applyCurrentAppearance()
  635. applyLocalizedStrings()
  636. beginLoadingAICatalogIfPossible()
  637. }
  638. deinit {
  639. if let appearanceObserver {
  640. NotificationCenter.default.removeObserver(appearanceObserver)
  641. }
  642. if let languageObserver {
  643. NotificationCenter.default.removeObserver(languageObserver)
  644. }
  645. if let scrollBoundsObserver {
  646. NotificationCenter.default.removeObserver(scrollBoundsObserver)
  647. }
  648. }
  649. @available(*, unavailable)
  650. required init?(coder: NSCoder) {
  651. fatalError("init(coder:) has not been implemented")
  652. }
  653. override func viewDidChangeEffectiveAppearance() {
  654. super.viewDidChangeEffectiveAppearance()
  655. applyCurrentAppearance()
  656. }
  657. override func layout() {
  658. super.layout()
  659. pageGradientLayer.frame = bounds
  660. layoutGridCardsIfNeeded()
  661. // Keep the bottom CTA above the scroll view so template-grid scrolling cannot steal clicks.
  662. addSubview(ctaButton, positioned: .above, relativeTo: nil)
  663. }
  664. func applyCurrentAppearance() {
  665. pageGradientLayer.colors = [Palette.gradientBottom.cgColor, Palette.gradientTop.cgColor]
  666. filterChrome.layer?.borderColor = Palette.filterChromeBorder.cgColor
  667. titleLabel.textColor = Palette.primaryText
  668. subtitleLabel.textColor = Palette.secondaryText
  669. styleCTAButton(ctaButton)
  670. for chip in groupTabButtons.values { chip.applyCurrentAppearance() }
  671. for chip in familyChipButtons.values { chip.applyCurrentAppearance() }
  672. reloadTemplateGrid()
  673. updateSelectedChipStates()
  674. }
  675. private func gallerySubtitleText() -> String {
  676. L(Self.gallerySubtitleKey)
  677. }
  678. private func applyGalleryHeaderCopy() {
  679. titleLabel.stringValue = L("Templates")
  680. subtitleLabel.stringValue = gallerySubtitleText()
  681. }
  682. func applyLocalizedStrings() {
  683. applyGalleryHeaderCopy()
  684. styleCTAButton(ctaButton)
  685. configureGroupTabs()
  686. reloadFamilyChips()
  687. reloadTemplateGrid()
  688. updateSelectedChipStates()
  689. }
  690. // MARK: Setup
  691. private func configureLayout() {
  692. wantsLayer = true
  693. layer?.backgroundColor = NSColor.clear.cgColor
  694. pageGradientLayer.locations = [0, 1] as [NSNumber]
  695. pageGradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
  696. pageGradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
  697. layer?.insertSublayer(pageGradientLayer, at: 0)
  698. filterChrome.translatesAutoresizingMaskIntoConstraints = false
  699. filterChrome.material = .contentBackground
  700. filterChrome.blendingMode = .withinWindow
  701. filterChrome.state = .active
  702. filterChrome.wantsLayer = true
  703. filterChrome.layer?.cornerRadius = 18
  704. filterChrome.layer?.borderWidth = 1
  705. filterStack.orientation = .vertical
  706. filterStack.spacing = 12
  707. filterStack.alignment = .leading
  708. filterStack.translatesAutoresizingMaskIntoConstraints = false
  709. filterStack.addArrangedSubview(groupTabsRow)
  710. filterStack.addArrangedSubview(familyChipsRow)
  711. filterChrome.addSubview(filterStack)
  712. NSLayoutConstraint.activate([
  713. filterStack.leadingAnchor.constraint(equalTo: filterChrome.leadingAnchor, constant: 14),
  714. filterStack.trailingAnchor.constraint(equalTo: filterChrome.trailingAnchor, constant: -14),
  715. filterStack.topAnchor.constraint(equalTo: filterChrome.topAnchor, constant: 12),
  716. filterStack.bottomAnchor.constraint(equalTo: filterChrome.bottomAnchor, constant: -12),
  717. // On this SDK `alignment` is `NSLayoutConstraint.Attribute` (no `.fill`).
  718. // Pin row widths so group tabs stay full-width / equal split instead of
  719. // shrinking to intrinsic width when selection changes.
  720. groupTabsRow.widthAnchor.constraint(equalTo: filterStack.widthAnchor),
  721. familyChipsRow.widthAnchor.constraint(equalTo: filterStack.widthAnchor)
  722. ])
  723. titleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  724. titleLabel.textColor = Palette.primaryText
  725. titleLabel.alignment = .left
  726. subtitleLabel.font = .systemFont(ofSize: 13, weight: .regular)
  727. subtitleLabel.textColor = Palette.secondaryText
  728. subtitleLabel.alignment = .left
  729. subtitleLabel.maximumNumberOfLines = 2
  730. let headerStack = NSStackView(views: [titleLabel, subtitleLabel])
  731. headerStack.orientation = .vertical
  732. headerStack.spacing = 4
  733. headerStack.alignment = .leading
  734. headerStack.translatesAutoresizingMaskIntoConstraints = false
  735. groupTabsRow.orientation = .horizontal
  736. groupTabsRow.spacing = 8
  737. groupTabsRow.alignment = .centerY
  738. groupTabsRow.distribution = .fillEqually
  739. groupTabsRow.translatesAutoresizingMaskIntoConstraints = false
  740. groupTabsRow.heightAnchor.constraint(equalToConstant: 38).isActive = true
  741. configureGroupTabs()
  742. familyChipsRow.orientation = .horizontal
  743. familyChipsRow.spacing = 8
  744. familyChipsRow.alignment = .centerY
  745. // Match the top row: equal-width segments, evenly spaced across the row.
  746. familyChipsRow.distribution = .fillEqually
  747. familyChipsRow.translatesAutoresizingMaskIntoConstraints = false
  748. familyChipsRow.heightAnchor.constraint(equalToConstant: 30).isActive = true
  749. gridStack.orientation = .vertical
  750. gridStack.spacing = 26
  751. gridStack.alignment = .leading
  752. gridStack.distribution = .fill
  753. gridStack.translatesAutoresizingMaskIntoConstraints = false
  754. gridDocument.translatesAutoresizingMaskIntoConstraints = false
  755. gridDocument.addSubview(gridStack)
  756. NSLayoutConstraint.activate([
  757. gridStack.leadingAnchor.constraint(equalTo: gridDocument.leadingAnchor),
  758. gridStack.trailingAnchor.constraint(equalTo: gridDocument.trailingAnchor),
  759. gridStack.topAnchor.constraint(equalTo: gridDocument.topAnchor),
  760. gridStack.bottomAnchor.constraint(equalTo: gridDocument.bottomAnchor)
  761. ])
  762. scrollView.translatesAutoresizingMaskIntoConstraints = false
  763. scrollView.hasVerticalScroller = true
  764. scrollView.hasHorizontalScroller = false
  765. scrollView.scrollerStyle = .legacy
  766. scrollView.autohidesScrollers = true
  767. scrollView.drawsBackground = false
  768. scrollView.borderType = .noBorder
  769. scrollView.documentView = gridDocument
  770. scrollView.contentView.postsBoundsChangedNotifications = true
  771. scrollBoundsObserver = NotificationCenter.default.addObserver(
  772. forName: NSView.boundsDidChangeNotification,
  773. object: scrollView.contentView,
  774. queue: .main
  775. ) { [weak self] _ in
  776. self?.resetTemplateCardHoverStates()
  777. }
  778. NSLayoutConstraint.activate([
  779. gridDocument.topAnchor.constraint(equalTo: scrollView.contentView.topAnchor),
  780. gridDocument.leadingAnchor.constraint(equalTo: scrollView.contentView.leadingAnchor),
  781. gridDocument.widthAnchor.constraint(equalTo: scrollView.contentView.widthAnchor)
  782. ])
  783. ctaButton.translatesAutoresizingMaskIntoConstraints = false
  784. styleCTAButton(ctaButton)
  785. ctaButton.target = self
  786. ctaButton.action = #selector(didTapUseTemplate)
  787. addSubview(headerStack)
  788. addSubview(filterChrome)
  789. addSubview(scrollView)
  790. addSubview(ctaButton)
  791. let horizontalInset: CGFloat = 32
  792. NSLayoutConstraint.activate([
  793. headerStack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  794. headerStack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  795. headerStack.topAnchor.constraint(equalTo: topAnchor, constant: 8),
  796. filterChrome.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  797. filterChrome.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  798. filterChrome.topAnchor.constraint(equalTo: headerStack.bottomAnchor, constant: 16),
  799. filterChrome.heightAnchor.constraint(equalToConstant: FilterChromeLayout.height),
  800. scrollView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  801. scrollView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  802. scrollView.topAnchor.constraint(equalTo: filterChrome.bottomAnchor, constant: 18),
  803. scrollView.bottomAnchor.constraint(equalTo: ctaButton.topAnchor, constant: -18),
  804. ctaButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  805. ctaButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  806. ctaButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -24),
  807. ctaButton.heightAnchor.constraint(equalToConstant: 52)
  808. ])
  809. }
  810. private func configureGroupTabs() {
  811. groupTabsRow.arrangedSubviews.forEach {
  812. groupTabsRow.removeArrangedSubview($0)
  813. $0.removeFromSuperview()
  814. }
  815. groupTabButtons.removeAll()
  816. let groups: [(CVCategoryGroup, String)] = [
  817. (.designBased, "rectangle.3.group"),
  818. (.professionBased, "person.2")
  819. ]
  820. for (group, symbolName) in groups {
  821. let count = templates(forGroup: group, family: nil).count
  822. let chip = CVChipButton(
  823. title: group.title,
  824. badgeText: "\(count)",
  825. leadingSymbol: symbolName,
  826. style: .pillLarge
  827. )
  828. chip.translatesAutoresizingMaskIntoConstraints = false
  829. chip.onSelect = { [weak self] in self?.didSelectGroup(group) }
  830. groupTabsRow.addArrangedSubview(chip)
  831. groupTabButtons[group] = chip
  832. }
  833. }
  834. private func reloadFamilyChips() {
  835. familyChipsRow.arrangedSubviews.forEach {
  836. familyChipsRow.removeArrangedSubview($0)
  837. $0.removeFromSuperview()
  838. }
  839. familyChipButtons.removeAll()
  840. let allCount = templates(forGroup: selectedGroup, family: nil).count
  841. let allChip = CVChipButton(title: L("All"), badgeText: "\(allCount)", leadingSymbol: nil, style: .pillSmall)
  842. allChip.translatesAutoresizingMaskIntoConstraints = false
  843. allChip.onSelect = { [weak self] in self?.didSelectFamily(nil) }
  844. familyChipsRow.addArrangedSubview(allChip)
  845. familyChipButtons[nil] = allChip
  846. for family in CVDesignFamily.allCases {
  847. let count = templates(forGroup: selectedGroup, family: family).count
  848. guard count > 0 else { continue }
  849. let chip = CVChipButton(title: family.title, badgeText: "\(count)", leadingSymbol: nil, style: .pillSmall)
  850. chip.translatesAutoresizingMaskIntoConstraints = false
  851. chip.onSelect = { [weak self] in self?.didSelectFamily(family) }
  852. familyChipsRow.addArrangedSubview(chip)
  853. familyChipButtons[family] = chip
  854. }
  855. // Pad to a fixed slot count so `fillEqually` chip widths never change when
  856. // the visible family count differs between category groups.
  857. while familyChipsRow.arrangedSubviews.count < familyChipSlotCount {
  858. let slot = NSView()
  859. slot.translatesAutoresizingMaskIntoConstraints = false
  860. slot.setContentHuggingPriority(.defaultLow, for: .horizontal)
  861. slot.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  862. familyChipsRow.addArrangedSubview(slot)
  863. }
  864. if let f = selectedFamily, templates(forGroup: selectedGroup, family: f).isEmpty {
  865. selectedFamily = nil
  866. }
  867. }
  868. // MARK: Data filtering
  869. private func templates(forGroup group: CVCategoryGroup, family: CVDesignFamily?) -> [CVTemplate] {
  870. let base = activeCatalog.filter { $0.galleryGroup == group }
  871. guard let family else { return base }
  872. return base.filter { $0.family == family }
  873. }
  874. private var visibleTemplates: [CVTemplate] {
  875. templates(forGroup: selectedGroup, family: selectedFamily)
  876. }
  877. // MARK: Grid
  878. private func reloadTemplateGrid() {
  879. gridStack.arrangedSubviews.forEach {
  880. gridStack.removeArrangedSubview($0)
  881. $0.removeFromSuperview()
  882. }
  883. templateCardsInGrid.removeAll()
  884. let templates = visibleTemplates
  885. if templates.isEmpty {
  886. let empty = NSTextField(labelWithString: L("No templates yet for this category."))
  887. empty.font = .systemFont(ofSize: 13)
  888. empty.textColor = Palette.secondaryText
  889. gridStack.addArrangedSubview(empty)
  890. selectedTemplateCardToken = nil
  891. return
  892. }
  893. let columns = resolvedGridColumnCount()
  894. var index = 0
  895. while index < templates.count {
  896. let row = NSStackView()
  897. row.orientation = .horizontal
  898. row.spacing = 22
  899. row.distribution = .fillEqually
  900. row.alignment = .top
  901. row.translatesAutoresizingMaskIntoConstraints = false
  902. row.setHuggingPriority(.defaultLow, for: .horizontal)
  903. for column in 0..<columns {
  904. let position = index + column
  905. if position < templates.count {
  906. let template = templates[position]
  907. let card = CVTemplateCard(template: template, palette: palette())
  908. card.translatesAutoresizingMaskIntoConstraints = false
  909. let selectionToken = card.selectionToken
  910. let templateID = card.templateID
  911. card.onSelect = { [weak self] in
  912. self?.didSelectCard(selectionToken: selectionToken, templateID: templateID)
  913. }
  914. row.addArrangedSubview(card)
  915. templateCardsInGrid.append(card)
  916. } else {
  917. let filler = NSView()
  918. filler.translatesAutoresizingMaskIntoConstraints = false
  919. row.addArrangedSubview(filler)
  920. filler.heightAnchor.constraint(equalToConstant: CVTemplateCard.layoutHeight).isActive = true
  921. }
  922. }
  923. gridStack.addArrangedSubview(row)
  924. row.widthAnchor.constraint(equalTo: gridStack.widthAnchor).isActive = true
  925. row.heightAnchor.constraint(equalToConstant: CVTemplateCard.layoutHeight).isActive = true
  926. index += columns
  927. }
  928. if let sid = selectedTemplateID,
  929. let match = templateCardsInGrid.first(where: { $0.templateID == sid }) {
  930. selectedTemplateCardToken = match.selectionToken
  931. } else if let first = templateCardsInGrid.first {
  932. selectedTemplateCardToken = first.selectionToken
  933. selectedTemplateID = first.templateID
  934. } else {
  935. selectedTemplateCardToken = nil
  936. selectedTemplateID = nil
  937. }
  938. applySelectionToCards()
  939. }
  940. private func galleryLayoutWidth() -> CGFloat {
  941. if bounds.width > 8 { return bounds.width }
  942. if let s = superview, s.bounds.width > 8 { return max(s.bounds.width - 64, 400) }
  943. return 900
  944. }
  945. private func layoutGridCardsIfNeeded() {
  946. let cols = resolvedGridColumnCount()
  947. guard cols != appliedGridColumnCount else { return }
  948. appliedGridColumnCount = cols
  949. reloadTemplateGrid()
  950. updateSelectedChipStates()
  951. }
  952. private func resolvedGridColumnCount() -> Int {
  953. let w = max(galleryLayoutWidth(), 400)
  954. if w < 780 { return 2 }
  955. if w < 1080 { return 3 }
  956. return 4
  957. }
  958. private func applySelectionToCards() {
  959. let token = selectedTemplateCardToken
  960. for card in templateCardsInGrid {
  961. let shouldSelect = (card.selectionToken == token)
  962. card.isSelected = shouldSelect
  963. if !shouldSelect {
  964. card.clearHoverVisualState()
  965. }
  966. }
  967. }
  968. /// Scroll moves cards under a stationary cursor without `mouseExited`; clear stale hovers so only the selected card keeps a brand border.
  969. private func resetTemplateCardHoverStates() {
  970. for card in templateCardsInGrid where !card.isSelected {
  971. card.clearHoverVisualState()
  972. }
  973. }
  974. private func palette() -> CVTemplateCardPalette {
  975. CVTemplateCardPalette(
  976. border: Palette.cardBorder,
  977. borderHover: Palette.cardBorderHover,
  978. borderSelected: Palette.cardBorderSelected,
  979. selectionGlow: Palette.selectionGlow,
  980. cardShellBackground: Palette.cardBackground,
  981. footerBackground: Palette.cardFooter,
  982. previewSurface: Palette.previewSurface,
  983. previewPaper: Palette.previewPaper,
  984. previewSidebarTint: Palette.previewSidebarTint,
  985. previewInk: Palette.previewInk,
  986. previewMuted: Palette.previewMuted,
  987. previewAccentRed: Palette.previewAccentRed,
  988. previewAccentBlue: Palette.previewAccentBlue,
  989. primaryText: Palette.primaryText,
  990. secondaryText: Palette.secondaryText
  991. )
  992. }
  993. // MARK: Selection
  994. private func didSelectGroup(_ group: CVCategoryGroup) {
  995. guard selectedGroup != group else { return }
  996. selectedGroup = group
  997. selectedFamily = nil
  998. reloadFamilyChips()
  999. reloadTemplateGrid()
  1000. updateSelectedChipStates()
  1001. }
  1002. private func didSelectFamily(_ family: CVDesignFamily?) {
  1003. guard selectedFamily != family else { return }
  1004. selectedFamily = family
  1005. reloadTemplateGrid()
  1006. updateSelectedChipStates()
  1007. }
  1008. private func didSelectCard(selectionToken: UUID, templateID: String) {
  1009. selectedTemplateCardToken = selectionToken
  1010. selectedTemplateID = templateID
  1011. applySelectionToCards()
  1012. }
  1013. @objc private func didTapUseTemplate() {
  1014. let chosen: CVTemplate?
  1015. if let token = selectedTemplateCardToken,
  1016. let card = templateCardsInGrid.first(where: { $0.selectionToken == token }) {
  1017. chosen = card.catalogTemplate
  1018. } else if let id = selectedTemplateID {
  1019. chosen = resolvedTemplate(withID: id)
  1020. } else {
  1021. chosen = nil
  1022. }
  1023. guard let template = chosen else {
  1024. presentPlaceholderAlert(title: L("Pick a template"), message: L("Select a template first, then choose a profile to continue."))
  1025. return
  1026. }
  1027. onContinueToProfileSelection?(template)
  1028. }
  1029. private func updateSelectedChipStates() {
  1030. for (group, chip) in groupTabButtons {
  1031. chip.isSelected = (group == selectedGroup)
  1032. }
  1033. for (family, chip) in familyChipButtons {
  1034. chip.isSelected = (family == selectedFamily)
  1035. }
  1036. }
  1037. private func styleCTAButton(_ button: CVHoverableButton) {
  1038. button.title = L("Use Template & Select Profile →")
  1039. button.font = .systemFont(ofSize: 14, weight: .semibold)
  1040. button.isBordered = false
  1041. button.bezelStyle = .rounded
  1042. button.focusRingType = .none
  1043. button.contentTintColor = Palette.ctaText
  1044. button.wantsLayer = true
  1045. button.layer?.cornerRadius = 14
  1046. button.layer?.backgroundColor = Palette.ctaBackground.cgColor
  1047. button.pointerCursor = true
  1048. let attrs: [NSAttributedString.Key: Any] = [
  1049. .foregroundColor: Palette.ctaText,
  1050. .font: NSFont.systemFont(ofSize: 14, weight: .semibold)
  1051. ]
  1052. button.attributedTitle = NSAttributedString(string: button.title, attributes: attrs)
  1053. button.hoverHandler = { [weak button] hovering in
  1054. button?.layer?.backgroundColor = (hovering ? Palette.ctaHover : Palette.ctaBackground).cgColor
  1055. }
  1056. }
  1057. private func restoreOrLoadAICatalogForCurrentLanguage() {
  1058. aiCatalogFetchGeneration += 1
  1059. applyGalleryHeaderCopy()
  1060. let locale = currentAppLanguage().localeIdentifier
  1061. if let cached = aiCatalogByLocale[locale], !cached.isEmpty {
  1062. activeCatalog = cached
  1063. configureGroupTabs()
  1064. reloadFamilyChips()
  1065. reloadTemplateGrid()
  1066. updateSelectedChipStates()
  1067. return
  1068. }
  1069. beginLoadingAICatalogIfPossible()
  1070. }
  1071. private func beginLoadingAICatalogIfPossible() {
  1072. guard OpenAIConfiguration.hasAPIKey else {
  1073. applyGalleryHeaderCopy()
  1074. return
  1075. }
  1076. let language = currentAppLanguage()
  1077. let locale = language.localeIdentifier
  1078. if let cached = aiCatalogByLocale[locale], !cached.isEmpty {
  1079. activeCatalog = cached
  1080. applyGalleryHeaderCopy()
  1081. configureGroupTabs()
  1082. reloadFamilyChips()
  1083. reloadTemplateGrid()
  1084. updateSelectedChipStates()
  1085. return
  1086. }
  1087. aiCatalogFetchGeneration += 1
  1088. let generation = aiCatalogFetchGeneration
  1089. subtitleLabel.stringValue = L("Fetching AI-curated templates…")
  1090. CVTemplateFetchService.shared.fetchTemplates(language: language) { [weak self] result in
  1091. DispatchQueue.main.async {
  1092. guard let self, self.aiCatalogFetchGeneration == generation else { return }
  1093. switch result {
  1094. case .success(let list):
  1095. self.applyGalleryHeaderCopy()
  1096. if !list.isEmpty {
  1097. self.aiCatalogByLocale[locale] = list
  1098. self.activeCatalog = list
  1099. self.configureGroupTabs()
  1100. self.reloadFamilyChips()
  1101. self.reloadTemplateGrid()
  1102. self.updateSelectedChipStates()
  1103. }
  1104. case .failure:
  1105. self.subtitleLabel.stringValue = L("Couldn’t load AI templates — showing the built-in gallery.")
  1106. DispatchQueue.main.asyncAfter(deadline: .now() + 5.5) { [weak self] in
  1107. guard let self, self.aiCatalogFetchGeneration == generation else { return }
  1108. self.applyGalleryHeaderCopy()
  1109. }
  1110. }
  1111. }
  1112. }
  1113. }
  1114. private func presentPlaceholderAlert(title: String, message: String) {
  1115. let alert = NSAlert()
  1116. alert.messageText = title
  1117. alert.informativeText = message
  1118. alert.alertStyle = .informational
  1119. alert.addButton(withTitle: L("OK"))
  1120. if let window {
  1121. alert.beginSheetModal(for: window)
  1122. } else {
  1123. alert.runModal()
  1124. }
  1125. }
  1126. }
  1127. // MARK: - Chip / pill button
  1128. /// Reusable pill button used for both the top section toggle ("Design-Based"
  1129. /// vs "Profession-Based") and the family filter chips. Switches between an
  1130. /// active brand-blue state and a soft neutral state, with an inline count badge.
  1131. private final class CVChipButton: NSView {
  1132. enum Style { case pillLarge, pillSmall }
  1133. var onSelect: (() -> Void)?
  1134. var isSelected: Bool = false { didSet { applyState() } }
  1135. private let style: Style
  1136. private let titleLabel = NSTextField(labelWithString: "")
  1137. private let badgeLabel = NSTextField(labelWithString: "")
  1138. private let badgePill = NSView()
  1139. private let symbolView = NSImageView()
  1140. private let stack = NSStackView()
  1141. private var isHovering: Bool = false
  1142. private var didPushCursor: Bool = false
  1143. private var trackingArea: NSTrackingArea?
  1144. private enum Palette {
  1145. static var restFill: NSColor { AppDashboardTheme.cvMakerChipRestFill }
  1146. static var restBorder: NSColor { AppDashboardTheme.cvMakerChipRestBorder }
  1147. static var hoverFill: NSColor { AppDashboardTheme.cvMakerChipHoverFill }
  1148. static var activeFill: NSColor { AppDashboardTheme.brandBlue }
  1149. static var activeFillHover: NSColor { AppDashboardTheme.brandBlueHover }
  1150. static var activeBorder: NSColor { AppDashboardTheme.brandBlueHover }
  1151. static var restText: NSColor { AppDashboardTheme.primaryText }
  1152. static var activeText: NSColor { AppDashboardTheme.proCTAText }
  1153. static var restBadge: NSColor { AppDashboardTheme.cvMakerChipBadgeBackground }
  1154. static var restBadgeText: NSColor { AppDashboardTheme.cvMakerChipBadgeText }
  1155. static var activeBadge: NSColor { NSColor.white.withAlphaComponent(0.22) }
  1156. static var activeBadgeText: NSColor { AppDashboardTheme.proCTAText }
  1157. }
  1158. func applyCurrentAppearance() {
  1159. applyState()
  1160. }
  1161. private static let symbolSide: CGFloat = 18
  1162. private static let badgeWidthLarge: CGFloat = 28
  1163. private static let badgeWidthSmall: CGFloat = 26
  1164. init(title: String, badgeText: String, leadingSymbol: String?, style: Style) {
  1165. self.style = style
  1166. super.init(frame: .zero)
  1167. wantsLayer = true
  1168. translatesAutoresizingMaskIntoConstraints = false
  1169. let height: CGFloat = style == .pillLarge ? 38 : 30
  1170. layer?.cornerRadius = height / 2
  1171. layer?.borderWidth = 1
  1172. heightAnchor.constraint(equalToConstant: height).isActive = true
  1173. titleLabel.stringValue = title
  1174. titleLabel.font = .systemFont(ofSize: style == .pillLarge ? 13 : 12, weight: .semibold)
  1175. titleLabel.maximumNumberOfLines = 1
  1176. titleLabel.lineBreakMode = .byTruncatingTail
  1177. titleLabel.cell?.lineBreakMode = .byTruncatingTail
  1178. titleLabel.isBordered = false
  1179. titleLabel.drawsBackground = false
  1180. titleLabel.isEditable = false
  1181. titleLabel.isSelectable = false
  1182. badgeLabel.stringValue = badgeText
  1183. badgeLabel.font = .monospacedDigitSystemFont(ofSize: 10.5, weight: .semibold)
  1184. badgeLabel.alignment = .center
  1185. badgeLabel.isBordered = false
  1186. badgeLabel.drawsBackground = false
  1187. badgeLabel.isEditable = false
  1188. badgeLabel.isSelectable = false
  1189. badgePill.translatesAutoresizingMaskIntoConstraints = false
  1190. badgePill.wantsLayer = true
  1191. badgePill.layer?.cornerRadius = 9
  1192. badgePill.addSubview(badgeLabel)
  1193. badgeLabel.translatesAutoresizingMaskIntoConstraints = false
  1194. NSLayoutConstraint.activate([
  1195. badgeLabel.leadingAnchor.constraint(equalTo: badgePill.leadingAnchor, constant: 7),
  1196. badgeLabel.trailingAnchor.constraint(equalTo: badgePill.trailingAnchor, constant: -7),
  1197. badgeLabel.centerYAnchor.constraint(equalTo: badgePill.centerYAnchor),
  1198. badgePill.heightAnchor.constraint(equalToConstant: 18),
  1199. badgePill.widthAnchor.constraint(
  1200. equalToConstant: style == .pillLarge ? Self.badgeWidthLarge : Self.badgeWidthSmall
  1201. )
  1202. ])
  1203. stack.orientation = .horizontal
  1204. stack.spacing = 8
  1205. stack.alignment = .centerY
  1206. stack.translatesAutoresizingMaskIntoConstraints = false
  1207. if let symbol = leadingSymbol, style == .pillLarge {
  1208. symbolView.translatesAutoresizingMaskIntoConstraints = false
  1209. symbolView.image = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)
  1210. symbolView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  1211. symbolView.setContentHuggingPriority(.required, for: .horizontal)
  1212. symbolView.setContentCompressionResistancePriority(.required, for: .horizontal)
  1213. stack.addArrangedSubview(symbolView)
  1214. NSLayoutConstraint.activate([
  1215. symbolView.widthAnchor.constraint(equalToConstant: Self.symbolSide),
  1216. symbolView.heightAnchor.constraint(equalToConstant: Self.symbolSide)
  1217. ])
  1218. }
  1219. stack.addArrangedSubview(titleLabel)
  1220. stack.addArrangedSubview(badgePill)
  1221. addSubview(stack)
  1222. let horizontalInset: CGFloat = style == .pillLarge ? 16 : 12
  1223. // Leading alignment for every chip so selected vs unselected pills share the
  1224. // same geometry (centered stacks shift when badge digits change).
  1225. if style == .pillLarge {
  1226. NSLayoutConstraint.activate([
  1227. stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  1228. stack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  1229. stack.centerYAnchor.constraint(equalTo: centerYAnchor)
  1230. ])
  1231. setContentHuggingPriority(.defaultLow, for: .horizontal)
  1232. } else {
  1233. // Parent row uses `fillEqually`; center label + badge inside each segment.
  1234. NSLayoutConstraint.activate([
  1235. stack.centerXAnchor.constraint(equalTo: centerXAnchor),
  1236. stack.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: horizontalInset),
  1237. stack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  1238. stack.centerYAnchor.constraint(equalTo: centerYAnchor)
  1239. ])
  1240. setContentHuggingPriority(.defaultLow, for: .horizontal)
  1241. setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  1242. }
  1243. applyState()
  1244. }
  1245. @available(*, unavailable)
  1246. required init?(coder: NSCoder) {
  1247. fatalError("init(coder:) has not been implemented")
  1248. }
  1249. override func hitTest(_ point: NSPoint) -> NSView? {
  1250. guard let superview else { return super.hitTest(point) }
  1251. let local = convert(point, from: superview)
  1252. return bounds.contains(local) ? self : nil
  1253. }
  1254. override func mouseDown(with event: NSEvent) {
  1255. onSelect?()
  1256. }
  1257. override func updateTrackingAreas() {
  1258. super.updateTrackingAreas()
  1259. if let area = trackingArea { removeTrackingArea(area) }
  1260. let area = NSTrackingArea(
  1261. rect: bounds,
  1262. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1263. owner: self,
  1264. userInfo: nil
  1265. )
  1266. addTrackingArea(area)
  1267. trackingArea = area
  1268. }
  1269. override func mouseEntered(with event: NSEvent) {
  1270. super.mouseEntered(with: event)
  1271. isHovering = true
  1272. applyState()
  1273. if !didPushCursor {
  1274. NSCursor.pointingHand.push()
  1275. didPushCursor = true
  1276. }
  1277. }
  1278. override func mouseExited(with event: NSEvent) {
  1279. super.mouseExited(with: event)
  1280. isHovering = false
  1281. applyState()
  1282. if didPushCursor {
  1283. NSCursor.pop()
  1284. didPushCursor = false
  1285. }
  1286. }
  1287. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1288. super.viewWillMove(toWindow: newWindow)
  1289. if newWindow == nil, didPushCursor {
  1290. NSCursor.pop()
  1291. didPushCursor = false
  1292. isHovering = false
  1293. }
  1294. }
  1295. private func applyState() {
  1296. let fill: NSColor
  1297. let border: NSColor
  1298. let textColor: NSColor
  1299. let badgeFill: NSColor
  1300. let badgeText: NSColor
  1301. if isSelected {
  1302. fill = isHovering ? Palette.activeFillHover : Palette.activeFill
  1303. border = Palette.activeBorder
  1304. textColor = Palette.activeText
  1305. badgeFill = Palette.activeBadge
  1306. badgeText = Palette.activeBadgeText
  1307. } else {
  1308. fill = isHovering ? Palette.hoverFill : Palette.restFill
  1309. border = Palette.restBorder
  1310. textColor = Palette.restText
  1311. badgeFill = Palette.restBadge
  1312. badgeText = Palette.restBadgeText
  1313. }
  1314. layer?.backgroundColor = fill.cgColor
  1315. layer?.borderColor = border.cgColor
  1316. layer?.borderWidth = 1
  1317. titleLabel.textColor = textColor
  1318. symbolView.contentTintColor = textColor
  1319. badgePill.layer?.backgroundColor = badgeFill.cgColor
  1320. badgeLabel.textColor = badgeText
  1321. }
  1322. }
  1323. // MARK: - Template card
  1324. /// Premium gallery card: live résumé thumbnail, soft shadow, and an animated
  1325. /// brand border when selected.
  1326. private final class CVTemplateCard: NSView {
  1327. private static let footerHeight: CGFloat = 50
  1328. private static let previewHeight: CGFloat = 242
  1329. static let layoutHeight: CGFloat = footerHeight + previewHeight
  1330. var onSelect: (() -> Void)?
  1331. var isSelected: Bool = false { didSet { applyChrome() } }
  1332. /// Distinguishes this card from others that may share the same catalog `template.id`.
  1333. let selectionToken = UUID()
  1334. var templateID: String { template.id }
  1335. /// Definition used for this card’s preview; pass through on “Use template” so layout cannot diverge from a later id-only lookup.
  1336. var catalogTemplate: CVTemplate { template }
  1337. private let template: CVTemplate
  1338. private let palette: CVTemplateCardPalette
  1339. private let previewSurface = NSView()
  1340. private let footerView = NSView()
  1341. private let preview: CVTemplatePreviewView
  1342. private let nameLabel = NSTextField(labelWithString: "")
  1343. private var trackingArea: NSTrackingArea?
  1344. private var isHovering: Bool = false
  1345. private var didPushCursor: Bool = false
  1346. init(template: CVTemplate, palette: CVTemplateCardPalette) {
  1347. self.template = template
  1348. self.palette = palette
  1349. self.preview = CVTemplatePreviewView(template: template, palette: palette)
  1350. super.init(frame: .zero)
  1351. wantsLayer = true
  1352. layer?.masksToBounds = false
  1353. layer?.cornerRadius = 24
  1354. layer?.backgroundColor = palette.cardShellBackground.cgColor
  1355. translatesAutoresizingMaskIntoConstraints = false
  1356. heightAnchor.constraint(equalToConstant: Self.layoutHeight).isActive = true
  1357. previewSurface.translatesAutoresizingMaskIntoConstraints = false
  1358. previewSurface.wantsLayer = true
  1359. previewSurface.layer?.backgroundColor = palette.previewSurface.cgColor
  1360. previewSurface.layer?.masksToBounds = true
  1361. preview.translatesAutoresizingMaskIntoConstraints = false
  1362. previewSurface.addSubview(preview)
  1363. nameLabel.font = .systemFont(ofSize: 14, weight: .semibold)
  1364. nameLabel.textColor = palette.primaryText
  1365. nameLabel.isBordered = false
  1366. nameLabel.drawsBackground = false
  1367. nameLabel.isEditable = false
  1368. nameLabel.isSelectable = false
  1369. nameLabel.lineBreakMode = .byTruncatingTail
  1370. nameLabel.maximumNumberOfLines = 1
  1371. nameLabel.setContentCompressionResistancePriority(.required, for: .vertical)
  1372. nameLabel.setContentHuggingPriority(.required, for: .vertical)
  1373. refreshLocalizedTitle()
  1374. let footerStack = NSStackView(views: [nameLabel])
  1375. footerStack.orientation = .vertical
  1376. footerStack.alignment = .leading
  1377. footerStack.translatesAutoresizingMaskIntoConstraints = false
  1378. footerView.translatesAutoresizingMaskIntoConstraints = false
  1379. footerView.wantsLayer = true
  1380. footerView.layer?.backgroundColor = palette.footerBackground.cgColor
  1381. footerView.setContentCompressionResistancePriority(.required, for: .vertical)
  1382. footerView.setContentHuggingPriority(.required, for: .vertical)
  1383. footerView.addSubview(footerStack)
  1384. NSLayoutConstraint.activate([
  1385. footerStack.leadingAnchor.constraint(equalTo: footerView.leadingAnchor, constant: 16),
  1386. footerStack.trailingAnchor.constraint(lessThanOrEqualTo: footerView.trailingAnchor, constant: -16),
  1387. footerStack.centerYAnchor.constraint(equalTo: footerView.centerYAnchor)
  1388. ])
  1389. previewSurface.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  1390. previewSurface.setContentHuggingPriority(.defaultLow, for: .vertical)
  1391. addSubview(previewSurface)
  1392. addSubview(footerView)
  1393. NSLayoutConstraint.activate([
  1394. previewSurface.topAnchor.constraint(equalTo: topAnchor),
  1395. previewSurface.leadingAnchor.constraint(equalTo: leadingAnchor),
  1396. previewSurface.trailingAnchor.constraint(equalTo: trailingAnchor),
  1397. previewSurface.heightAnchor.constraint(equalToConstant: Self.previewHeight),
  1398. preview.topAnchor.constraint(equalTo: previewSurface.topAnchor, constant: 14),
  1399. preview.leadingAnchor.constraint(equalTo: previewSurface.leadingAnchor, constant: 16),
  1400. preview.trailingAnchor.constraint(equalTo: previewSurface.trailingAnchor, constant: -16),
  1401. preview.bottomAnchor.constraint(equalTo: previewSurface.bottomAnchor, constant: -14),
  1402. footerView.topAnchor.constraint(equalTo: previewSurface.bottomAnchor),
  1403. footerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  1404. footerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  1405. footerView.bottomAnchor.constraint(equalTo: bottomAnchor),
  1406. footerView.heightAnchor.constraint(equalToConstant: Self.footerHeight)
  1407. ])
  1408. applyChrome()
  1409. }
  1410. @available(*, unavailable)
  1411. required init?(coder: NSCoder) {
  1412. fatalError("init(coder:) has not been implemented")
  1413. }
  1414. func refreshLocalizedTitle() {
  1415. nameLabel.stringValue = template.displayName
  1416. }
  1417. override func layout() {
  1418. super.layout()
  1419. layer?.shadowPath = CGPath(roundedRect: bounds, cornerWidth: 24, cornerHeight: 24, transform: nil)
  1420. }
  1421. override func hitTest(_ point: NSPoint) -> NSView? {
  1422. guard let superview else { return super.hitTest(point) }
  1423. let local = convert(point, from: superview)
  1424. return bounds.contains(local) ? self : nil
  1425. }
  1426. override func mouseDown(with event: NSEvent) {
  1427. playTapPulse()
  1428. onSelect?()
  1429. }
  1430. private func playTapPulse() {
  1431. guard let l = layer else { return }
  1432. let a = CABasicAnimation(keyPath: "transform")
  1433. a.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
  1434. a.toValue = NSValue(caTransform3D: CATransform3DMakeScale(0.985, 0.985, 1))
  1435. a.duration = 0.1
  1436. a.autoreverses = true
  1437. a.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  1438. l.add(a, forKey: "tapPulse")
  1439. }
  1440. override func updateTrackingAreas() {
  1441. super.updateTrackingAreas()
  1442. if let area = trackingArea { removeTrackingArea(area) }
  1443. let area = NSTrackingArea(
  1444. rect: bounds,
  1445. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1446. owner: self,
  1447. userInfo: nil
  1448. )
  1449. addTrackingArea(area)
  1450. trackingArea = area
  1451. }
  1452. override func mouseEntered(with event: NSEvent) {
  1453. super.mouseEntered(with: event)
  1454. isHovering = true
  1455. applyChrome()
  1456. if !didPushCursor {
  1457. NSCursor.pointingHand.push()
  1458. didPushCursor = true
  1459. }
  1460. }
  1461. override func mouseExited(with event: NSEvent) {
  1462. super.mouseExited(with: event)
  1463. isHovering = false
  1464. applyChrome()
  1465. if didPushCursor {
  1466. NSCursor.pop()
  1467. didPushCursor = false
  1468. }
  1469. }
  1470. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1471. super.viewWillMove(toWindow: newWindow)
  1472. if newWindow == nil, didPushCursor {
  1473. NSCursor.pop()
  1474. didPushCursor = false
  1475. isHovering = false
  1476. }
  1477. }
  1478. /// Clears hover chrome without waiting for `mouseExited` (e.g. after scroll or when another card is selected).
  1479. func clearHoverVisualState() {
  1480. if didPushCursor {
  1481. NSCursor.pop()
  1482. didPushCursor = false
  1483. }
  1484. guard isHovering else { return }
  1485. isHovering = false
  1486. applyChrome()
  1487. }
  1488. private func applyChrome() {
  1489. // Same border + shadow metrics in every state — only color changes on select
  1490. // so thumbnails keep identical insets (stroke is drawn inside the layer).
  1491. let uniformBorder: CGFloat = 2
  1492. let borderColor: NSColor
  1493. if isSelected {
  1494. borderColor = palette.borderSelected
  1495. } else if isHovering {
  1496. borderColor = palette.borderHover
  1497. } else {
  1498. borderColor = palette.border
  1499. }
  1500. layer?.backgroundColor = palette.cardShellBackground.cgColor
  1501. previewSurface.layer?.backgroundColor = palette.previewSurface.cgColor
  1502. footerView.layer?.backgroundColor = palette.footerBackground.cgColor
  1503. nameLabel.textColor = palette.primaryText
  1504. layer?.borderColor = borderColor.cgColor
  1505. layer?.borderWidth = uniformBorder
  1506. layer?.shadowColor = NSColor.black.cgColor
  1507. layer?.shadowOpacity = isSelected ? 0.14 : (isHovering ? 0.11 : 0.08)
  1508. layer?.shadowRadius = 14
  1509. layer?.shadowOffset = CGSize(width: 0, height: 8)
  1510. }
  1511. }
  1512. // MARK: - Helpers
  1513. /// Flipped origin so the grid stacks fill from the top.
  1514. private final class TopFlippedView: NSView {
  1515. override var isFlipped: Bool { true }
  1516. }
  1517. /// Local copy of the dashboard's hoverable button so this file stays
  1518. /// self-contained without exposing the existing private classes.
  1519. private final class CVHoverableButton: NSButton {
  1520. var hoverHandler: ((Bool) -> Void)?
  1521. var pointerCursor: Bool = false
  1522. private(set) var isHovering: Bool = false
  1523. private var trackingArea: NSTrackingArea?
  1524. private var didPushCursor: Bool = false
  1525. override func updateTrackingAreas() {
  1526. super.updateTrackingAreas()
  1527. if let area = trackingArea { removeTrackingArea(area) }
  1528. let area = NSTrackingArea(
  1529. rect: bounds,
  1530. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1531. owner: self,
  1532. userInfo: nil
  1533. )
  1534. addTrackingArea(area)
  1535. trackingArea = area
  1536. }
  1537. override func mouseEntered(with event: NSEvent) {
  1538. super.mouseEntered(with: event)
  1539. isHovering = true
  1540. hoverHandler?(true)
  1541. if pointerCursor, !didPushCursor {
  1542. NSCursor.pointingHand.push()
  1543. didPushCursor = true
  1544. }
  1545. }
  1546. override func mouseExited(with event: NSEvent) {
  1547. super.mouseExited(with: event)
  1548. isHovering = false
  1549. hoverHandler?(false)
  1550. if didPushCursor {
  1551. NSCursor.pop()
  1552. didPushCursor = false
  1553. }
  1554. }
  1555. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1556. super.viewWillMove(toWindow: newWindow)
  1557. if newWindow == nil, didPushCursor {
  1558. NSCursor.pop()
  1559. didPushCursor = false
  1560. isHovering = false
  1561. }
  1562. }
  1563. }