No Description

CVMakerPageView.swift 57KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. //
  2. // CVMakerPageView.swift
  3. // App for Indeed
  4. //
  5. // Template gallery for the CV Maker sidebar destination. Light-theme rendering
  6. // inspired by a dark reference UI: page header, category toggle, style chips,
  7. // 4-column thumbnail grid with hover Preview overlay, and a sticky bottom CTA.
  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 "Design-Based"
  18. case .professionBased: return "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 "Professional"
  27. case .modern: return "Modern"
  28. case .creative: return "Creative"
  29. case .minimal: return "Minimal"
  30. case .executive: return "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 "ATS layout"
  42. case .twoColumnSidebarLeading: return "Sidebar left"
  43. case .twoColumnSidebarTrailing: return "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. /// Optional bundle image name; `nil` means render a live vector/text preview.
  107. var previewImageAssetName: String? { nil }
  108. init(
  109. id: String,
  110. name: String,
  111. family: CVDesignFamily,
  112. headline: Headline,
  113. accent: Accent,
  114. layout: Layout,
  115. sectionLabelStyle: SectionLabelStyle,
  116. themeRed: CGFloat? = nil,
  117. themeGreen: CGFloat? = nil,
  118. themeBlue: CGFloat? = nil
  119. ) {
  120. self.id = id
  121. self.name = name
  122. self.family = family
  123. self.headline = headline
  124. self.accent = accent
  125. self.layout = layout
  126. self.sectionLabelStyle = sectionLabelStyle
  127. if let tr = themeRed, let tg = themeGreen, let tb = themeBlue {
  128. self.themeRed = tr
  129. self.themeGreen = tg
  130. self.themeBlue = tb
  131. } else {
  132. let rgb = Self.resolvedThemeRGB(family: family, id: id)
  133. self.themeRed = rgb.0
  134. self.themeGreen = rgb.1
  135. self.themeBlue = rgb.2
  136. }
  137. }
  138. private static func resolvedThemeRGB(family: CVDesignFamily, id: String) -> (CGFloat, CGFloat, CGFloat) {
  139. var hash: UInt64 = 1469598103934665603
  140. for b in id.utf8 {
  141. hash ^= UInt64(b)
  142. hash &*= 1_099_511_628_211
  143. }
  144. let t = Double(hash % 1000) / 1000.0
  145. switch family {
  146. case .professional:
  147. let r = 0.12 + t * 0.06
  148. let g = 0.32 + t * 0.08
  149. let b = 0.58 + t * 0.12
  150. return (r, g, b)
  151. case .modern:
  152. let r = 0.0 + t * 0.08
  153. let g = 0.45 + t * 0.12
  154. let bl = 0.85 + t * 0.1
  155. return (min(r, 1), min(g, 1), min(bl, 1))
  156. case .minimal:
  157. return (0.45 + t * 0.05, 0.48 + t * 0.04, 0.55 + t * 0.06)
  158. case .executive:
  159. let r = 0.08 + t * 0.06
  160. let g = 0.12 + t * 0.05
  161. let b = 0.22 + t * 0.08
  162. return (r, g, b)
  163. case .creative:
  164. let r = 0.25 + t * 0.2
  165. let g = 0.35 + t * 0.15
  166. let b = 0.72 + t * 0.15
  167. return (min(r, 1), min(g, 1), min(b, 1))
  168. }
  169. }
  170. }
  171. // MARK: - Catalog
  172. enum CVTemplateCatalog {
  173. static let all: [CVTemplate] = [
  174. // Minimal family (matches the reference screenshot)
  175. CVTemplate(
  176. id: "paper-white",
  177. name: "Paper White",
  178. family: .minimal,
  179. headline: .centered,
  180. accent: .none,
  181. layout: .singleColumn,
  182. sectionLabelStyle: .uppercase
  183. ),
  184. CVTemplate(
  185. id: "swiss",
  186. name: "Swiss",
  187. family: .minimal,
  188. headline: .centered,
  189. accent: .redUnderline,
  190. layout: .twoColumn(sidebar: .leading, tinted: false),
  191. sectionLabelStyle: .uppercase
  192. ),
  193. CVTemplate(
  194. id: "mono",
  195. name: "Mono",
  196. family: .minimal,
  197. headline: .leftAligned,
  198. accent: .redUnderline,
  199. layout: .singleColumn,
  200. sectionLabelStyle: .slashed
  201. ),
  202. CVTemplate(
  203. id: "airy",
  204. name: "Airy",
  205. family: .minimal,
  206. headline: .leftWithInitials,
  207. accent: .none,
  208. layout: .twoColumn(sidebar: .trailing, tinted: false),
  209. sectionLabelStyle: .uppercase
  210. ),
  211. CVTemplate(
  212. id: "tabular",
  213. name: "Tabular",
  214. family: .minimal,
  215. headline: .leftAligned,
  216. accent: .none,
  217. layout: .singleColumn,
  218. sectionLabelStyle: .bracketed
  219. ),
  220. CVTemplate(
  221. id: "facet",
  222. name: "Facet",
  223. family: .minimal,
  224. headline: .avatarStacked,
  225. accent: .none,
  226. layout: .twoColumn(sidebar: .leading, tinted: true),
  227. sectionLabelStyle: .uppercase
  228. ),
  229. // Professional family
  230. CVTemplate(
  231. id: "corporate",
  232. name: "Corporate",
  233. family: .professional,
  234. headline: .leftAligned,
  235. accent: .blueBar,
  236. layout: .singleColumn,
  237. sectionLabelStyle: .uppercase
  238. ),
  239. CVTemplate(
  240. id: "atlas",
  241. name: "Atlas",
  242. family: .professional,
  243. headline: .centered,
  244. accent: .blueBar,
  245. layout: .twoColumn(sidebar: .leading, tinted: true),
  246. sectionLabelStyle: .uppercase
  247. ),
  248. CVTemplate(
  249. id: "ledger",
  250. name: "Ledger",
  251. family: .professional,
  252. headline: .leftAligned,
  253. accent: .blueBar,
  254. layout: .twoColumn(sidebar: .trailing, tinted: false),
  255. sectionLabelStyle: .uppercase
  256. ),
  257. CVTemplate(
  258. id: "harbor",
  259. name: "Harbor",
  260. family: .professional,
  261. headline: .leftWithInitials,
  262. accent: .none,
  263. layout: .twoColumn(sidebar: .leading, tinted: true),
  264. sectionLabelStyle: .uppercase
  265. ),
  266. CVTemplate(
  267. id: "metro",
  268. name: "Metro",
  269. family: .professional,
  270. headline: .centered,
  271. accent: .blueBar,
  272. layout: .singleColumn,
  273. sectionLabelStyle: .uppercase
  274. ),
  275. CVTemplate(
  276. id: "pinstripe",
  277. name: "Pinstripe",
  278. family: .professional,
  279. headline: .leftAligned,
  280. accent: .blueBar,
  281. layout: .twoColumn(sidebar: .leading, tinted: false),
  282. sectionLabelStyle: .uppercase
  283. ),
  284. // Modern family
  285. CVTemplate(
  286. id: "vertex",
  287. name: "Vertex",
  288. family: .modern,
  289. headline: .leftWithInitials,
  290. accent: .blueBar,
  291. layout: .twoColumn(sidebar: .leading, tinted: true),
  292. sectionLabelStyle: .slashed
  293. ),
  294. CVTemplate(
  295. id: "linea",
  296. name: "Linea",
  297. family: .modern,
  298. headline: .leftAligned,
  299. accent: .blueBar,
  300. layout: .singleColumn,
  301. sectionLabelStyle: .slashed
  302. ),
  303. CVTemplate(
  304. id: "prism",
  305. name: "Prism",
  306. family: .modern,
  307. headline: .avatarStacked,
  308. accent: .blueBar,
  309. layout: .twoColumn(sidebar: .trailing, tinted: true),
  310. sectionLabelStyle: .uppercase
  311. ),
  312. CVTemplate(
  313. id: "circuit",
  314. name: "Circuit",
  315. family: .modern,
  316. headline: .leftAligned,
  317. accent: .blueBar,
  318. layout: .twoColumn(sidebar: .trailing, tinted: false),
  319. sectionLabelStyle: .slashed
  320. ),
  321. CVTemplate(
  322. id: "north",
  323. name: "North",
  324. family: .modern,
  325. headline: .leftWithInitials,
  326. accent: .none,
  327. layout: .twoColumn(sidebar: .leading, tinted: false),
  328. sectionLabelStyle: .uppercase
  329. ),
  330. CVTemplate(
  331. id: "axis",
  332. name: "Axis",
  333. family: .modern,
  334. headline: .centered,
  335. accent: .blueBar,
  336. layout: .singleColumn,
  337. sectionLabelStyle: .bracketed
  338. ),
  339. // Creative family
  340. CVTemplate(
  341. id: "marigold",
  342. name: "Marigold",
  343. family: .creative,
  344. headline: .avatarStacked,
  345. accent: .redBar,
  346. layout: .twoColumn(sidebar: .leading, tinted: true),
  347. sectionLabelStyle: .slashed
  348. ),
  349. CVTemplate(
  350. id: "ember",
  351. name: "Ember",
  352. family: .creative,
  353. headline: .leftWithInitials,
  354. accent: .redBar,
  355. layout: .twoColumn(sidebar: .trailing, tinted: true),
  356. sectionLabelStyle: .slashed
  357. ),
  358. CVTemplate(
  359. id: "lattice",
  360. name: "Lattice",
  361. family: .creative,
  362. headline: .leftAligned,
  363. accent: .redUnderline,
  364. layout: .singleColumn,
  365. sectionLabelStyle: .bracketed
  366. ),
  367. CVTemplate(
  368. id: "bloom",
  369. name: "Bloom",
  370. family: .creative,
  371. headline: .avatarStacked,
  372. accent: .redBar,
  373. layout: .singleColumn,
  374. sectionLabelStyle: .slashed
  375. ),
  376. CVTemplate(
  377. id: "studio",
  378. name: "Studio",
  379. family: .creative,
  380. headline: .leftWithInitials,
  381. accent: .redUnderline,
  382. layout: .twoColumn(sidebar: .leading, tinted: true),
  383. sectionLabelStyle: .uppercase
  384. ),
  385. CVTemplate(
  386. id: "kite",
  387. name: "Kite",
  388. family: .creative,
  389. headline: .centered,
  390. accent: .redBar,
  391. layout: .twoColumn(sidebar: .trailing, tinted: false),
  392. sectionLabelStyle: .slashed
  393. ),
  394. // Executive family
  395. CVTemplate(
  396. id: "regent",
  397. name: "Regent",
  398. family: .executive,
  399. headline: .centered,
  400. accent: .blueBar,
  401. layout: .twoColumn(sidebar: .leading, tinted: true),
  402. sectionLabelStyle: .uppercase
  403. ),
  404. CVTemplate(
  405. id: "monarch",
  406. name: "Monarch",
  407. family: .executive,
  408. headline: .centered,
  409. accent: .blueBar,
  410. layout: .singleColumn,
  411. sectionLabelStyle: .uppercase
  412. ),
  413. CVTemplate(
  414. id: "sterling",
  415. name: "Sterling",
  416. family: .executive,
  417. headline: .leftAligned,
  418. accent: .blueBar,
  419. layout: .twoColumn(sidebar: .trailing, tinted: false),
  420. sectionLabelStyle: .uppercase
  421. ),
  422. CVTemplate(
  423. id: "summit",
  424. name: "Summit",
  425. family: .executive,
  426. headline: .centered,
  427. accent: .redUnderline,
  428. layout: .twoColumn(sidebar: .leading, tinted: false),
  429. sectionLabelStyle: .uppercase
  430. ),
  431. CVTemplate(
  432. id: "estate",
  433. name: "Estate",
  434. family: .executive,
  435. headline: .leftWithInitials,
  436. accent: .blueBar,
  437. layout: .twoColumn(sidebar: .trailing, tinted: true),
  438. sectionLabelStyle: .uppercase
  439. ),
  440. CVTemplate(
  441. id: "chairman",
  442. name: "Chairman",
  443. family: .executive,
  444. headline: .leftAligned,
  445. accent: .blueBar,
  446. layout: .singleColumn,
  447. sectionLabelStyle: .uppercase
  448. )
  449. ]
  450. }
  451. // MARK: - View
  452. /// Standalone NSView for the CV Maker route. Renders the template gallery with
  453. /// header, segmented category groups, family chips, a thumbnail grid, and a
  454. /// bottom CTA. Hosts inside the same `nonHomeHost` slot as Saved Jobs/Settings.
  455. final class CVMakerPageView: NSView {
  456. /// Light-theme palette aligned with the rest of the dashboard (brand blue + neutral grays on white).
  457. private enum Palette {
  458. static let pageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  459. static let mutedSurface = NSColor(srgbRed: 247 / 255, green: 247 / 255, blue: 247 / 255, alpha: 1)
  460. static let chipRestFill = NSColor(srgbRed: 244 / 255, green: 246 / 255, blue: 250 / 255, alpha: 1)
  461. static let chipBorder = NSColor(srgbRed: 222 / 255, green: 226 / 255, blue: 233 / 255, alpha: 1)
  462. static let chipHoverFill = NSColor(srgbRed: 236 / 255, green: 240 / 255, blue: 246 / 255, alpha: 1)
  463. static let chipBadgeBackground = NSColor(srgbRed: 233 / 255, green: 236 / 255, blue: 241 / 255, alpha: 1)
  464. static let chipBadgeText = NSColor(srgbRed: 90 / 255, green: 102 / 255, blue: 121 / 255, alpha: 1)
  465. static let activeChipBackground = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  466. static let activeChipHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  467. static let activeChipText = NSColor.white
  468. static let activeChipBadgeBackground = NSColor.white.withAlphaComponent(0.22)
  469. static let activeChipBadgeText = NSColor.white
  470. static let primaryText = NSColor(srgbRed: 31 / 255, green: 41 / 255, blue: 55 / 255, alpha: 1)
  471. static let secondaryText = NSColor(srgbRed: 100 / 255, green: 116 / 255, blue: 139 / 255, alpha: 1)
  472. static let cardBorder = NSColor(srgbRed: 216 / 255, green: 223 / 255, blue: 233 / 255, alpha: 1)
  473. static let cardBorderHover = NSColor(srgbRed: 178 / 255, green: 196 / 255, blue: 225 / 255, alpha: 1)
  474. static let cardBorderSelected = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  475. static let cardFooter = NSColor(srgbRed: 250 / 255, green: 251 / 255, blue: 253 / 255, alpha: 1)
  476. static let previewSurface = NSColor(srgbRed: 252 / 255, green: 252 / 255, blue: 252 / 255, alpha: 1)
  477. static let previewPaper = NSColor.white
  478. static let previewSidebarTint = NSColor(srgbRed: 244 / 255, green: 246 / 255, blue: 250 / 255, alpha: 1)
  479. static let previewInk = NSColor(srgbRed: 38 / 255, green: 50 / 255, blue: 71 / 255, alpha: 1)
  480. static let previewMuted = NSColor(srgbRed: 165 / 255, green: 175 / 255, blue: 192 / 255, alpha: 1)
  481. static let previewAccentRed = NSColor(srgbRed: 207 / 255, green: 67 / 255, blue: 50 / 255, alpha: 1)
  482. static let previewAccentBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  483. static let ctaBackground = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  484. static let ctaHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  485. static let ctaText = NSColor.white
  486. static let overlayTint = NSColor.black.withAlphaComponent(0.45)
  487. static let selectionGlow = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.55)
  488. static let gradientTop = NSColor(srgbRed: 250 / 255, green: 252 / 255, blue: 1, alpha: 1)
  489. static let gradientBottom = NSColor(srgbRed: 236 / 255, green: 244 / 255, blue: 1, alpha: 1)
  490. }
  491. private let pageGradientLayer = CAGradientLayer()
  492. private let filterChrome = NSVisualEffectView()
  493. private let filterStack = NSStackView()
  494. private let titleLabel = NSTextField(labelWithString: "Templates")
  495. private let subtitleLabel = NSTextField(labelWithString: "Polished layouts with live previews — pick a style that fits your story.")
  496. private let groupTabsRow = NSStackView()
  497. private let familyChipsRow = NSStackView()
  498. private let scrollView = NSScrollView()
  499. private let gridDocument = TopFlippedView()
  500. private let gridStack = NSStackView()
  501. private let ctaButton = CVHoverableButton(title: "Use Template & Select Profile →", target: nil, action: nil)
  502. private var selectedGroup: CVCategoryGroup = .designBased
  503. private var selectedFamily: CVDesignFamily? = nil // nil == "All"
  504. private var selectedTemplateID: String? = "paper-white"
  505. /// Shown immediately; replaced when `CVTemplateFetchService` returns AI-generated entries.
  506. private var activeCatalog: [CVTemplate] = CVTemplateCatalog.all
  507. private var groupTabButtons: [CVCategoryGroup: CVChipButton] = [:]
  508. private var familyChipButtons: [CVDesignFamily?: CVChipButton] = [:]
  509. private var templateCardsByID: [String: CVTemplateCard] = [:]
  510. private var appliedGridColumnCount: Int = 0
  511. override init(frame frameRect: NSRect) {
  512. super.init(frame: frameRect)
  513. configureLayout()
  514. reloadFamilyChips()
  515. reloadTemplateGrid()
  516. updateSelectedChipStates()
  517. beginLoadingAICatalogIfPossible()
  518. }
  519. @available(*, unavailable)
  520. required init?(coder: NSCoder) {
  521. fatalError("init(coder:) has not been implemented")
  522. }
  523. override func layout() {
  524. super.layout()
  525. pageGradientLayer.frame = bounds
  526. layoutGridCardsIfNeeded()
  527. }
  528. // MARK: Setup
  529. private func configureLayout() {
  530. wantsLayer = true
  531. layer?.backgroundColor = NSColor.clear.cgColor
  532. pageGradientLayer.colors = [Palette.gradientBottom.cgColor, Palette.gradientTop.cgColor]
  533. pageGradientLayer.locations = [0, 1] as [NSNumber]
  534. pageGradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
  535. pageGradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
  536. layer?.insertSublayer(pageGradientLayer, at: 0)
  537. filterChrome.translatesAutoresizingMaskIntoConstraints = false
  538. filterChrome.material = .sidebar
  539. filterChrome.blendingMode = .withinWindow
  540. filterChrome.state = .active
  541. filterChrome.wantsLayer = true
  542. filterChrome.layer?.cornerRadius = 18
  543. filterChrome.layer?.borderWidth = 1
  544. filterChrome.layer?.borderColor = NSColor.white.withAlphaComponent(0.65).cgColor
  545. filterStack.orientation = .vertical
  546. filterStack.spacing = 12
  547. filterStack.alignment = .leading
  548. filterStack.translatesAutoresizingMaskIntoConstraints = false
  549. filterStack.addArrangedSubview(groupTabsRow)
  550. filterStack.addArrangedSubview(familyChipsRow)
  551. filterChrome.addSubview(filterStack)
  552. NSLayoutConstraint.activate([
  553. filterStack.leadingAnchor.constraint(equalTo: filterChrome.leadingAnchor, constant: 14),
  554. filterStack.trailingAnchor.constraint(equalTo: filterChrome.trailingAnchor, constant: -14),
  555. filterStack.topAnchor.constraint(equalTo: filterChrome.topAnchor, constant: 12),
  556. filterStack.bottomAnchor.constraint(equalTo: filterChrome.bottomAnchor, constant: -12)
  557. ])
  558. titleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  559. titleLabel.textColor = Palette.primaryText
  560. titleLabel.alignment = .left
  561. subtitleLabel.font = .systemFont(ofSize: 13, weight: .regular)
  562. subtitleLabel.textColor = Palette.secondaryText
  563. subtitleLabel.alignment = .left
  564. subtitleLabel.maximumNumberOfLines = 1
  565. let headerStack = NSStackView(views: [titleLabel, subtitleLabel])
  566. headerStack.orientation = .vertical
  567. headerStack.spacing = 4
  568. headerStack.alignment = .leading
  569. headerStack.translatesAutoresizingMaskIntoConstraints = false
  570. groupTabsRow.orientation = .horizontal
  571. groupTabsRow.spacing = 8
  572. groupTabsRow.alignment = .centerY
  573. groupTabsRow.distribution = .fillEqually
  574. groupTabsRow.translatesAutoresizingMaskIntoConstraints = false
  575. configureGroupTabs()
  576. familyChipsRow.orientation = .horizontal
  577. familyChipsRow.spacing = 8
  578. familyChipsRow.alignment = .centerY
  579. familyChipsRow.distribution = .fill
  580. familyChipsRow.translatesAutoresizingMaskIntoConstraints = false
  581. gridStack.orientation = .vertical
  582. gridStack.spacing = 26
  583. gridStack.alignment = .leading
  584. gridStack.distribution = .fill
  585. gridStack.translatesAutoresizingMaskIntoConstraints = false
  586. gridDocument.translatesAutoresizingMaskIntoConstraints = false
  587. gridDocument.addSubview(gridStack)
  588. NSLayoutConstraint.activate([
  589. gridStack.leadingAnchor.constraint(equalTo: gridDocument.leadingAnchor),
  590. gridStack.trailingAnchor.constraint(equalTo: gridDocument.trailingAnchor),
  591. gridStack.topAnchor.constraint(equalTo: gridDocument.topAnchor),
  592. gridStack.bottomAnchor.constraint(equalTo: gridDocument.bottomAnchor)
  593. ])
  594. scrollView.translatesAutoresizingMaskIntoConstraints = false
  595. scrollView.hasVerticalScroller = true
  596. scrollView.hasHorizontalScroller = false
  597. scrollView.scrollerStyle = .legacy
  598. scrollView.autohidesScrollers = true
  599. scrollView.drawsBackground = false
  600. scrollView.borderType = .noBorder
  601. scrollView.documentView = gridDocument
  602. NSLayoutConstraint.activate([
  603. gridDocument.topAnchor.constraint(equalTo: scrollView.contentView.topAnchor),
  604. gridDocument.leadingAnchor.constraint(equalTo: scrollView.contentView.leadingAnchor),
  605. gridDocument.widthAnchor.constraint(equalTo: scrollView.contentView.widthAnchor)
  606. ])
  607. ctaButton.translatesAutoresizingMaskIntoConstraints = false
  608. styleCTAButton(ctaButton)
  609. ctaButton.target = self
  610. ctaButton.action = #selector(didTapUseTemplate)
  611. addSubview(headerStack)
  612. addSubview(filterChrome)
  613. addSubview(scrollView)
  614. addSubview(ctaButton)
  615. let horizontalInset: CGFloat = 32
  616. NSLayoutConstraint.activate([
  617. headerStack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  618. headerStack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  619. headerStack.topAnchor.constraint(equalTo: topAnchor, constant: 8),
  620. filterChrome.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  621. filterChrome.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  622. filterChrome.topAnchor.constraint(equalTo: headerStack.bottomAnchor, constant: 16),
  623. scrollView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  624. scrollView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  625. scrollView.topAnchor.constraint(equalTo: filterChrome.bottomAnchor, constant: 18),
  626. scrollView.bottomAnchor.constraint(equalTo: ctaButton.topAnchor, constant: -18),
  627. ctaButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  628. ctaButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  629. ctaButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -24),
  630. ctaButton.heightAnchor.constraint(equalToConstant: 52)
  631. ])
  632. }
  633. private func configureGroupTabs() {
  634. groupTabsRow.arrangedSubviews.forEach {
  635. groupTabsRow.removeArrangedSubview($0)
  636. $0.removeFromSuperview()
  637. }
  638. groupTabButtons.removeAll()
  639. let groups: [(CVCategoryGroup, String)] = [
  640. (.designBased, "rectangle.3.group"),
  641. (.professionBased, "person.2")
  642. ]
  643. for (group, symbolName) in groups {
  644. let count = templates(forGroup: group, family: nil).count
  645. let chip = CVChipButton(
  646. title: group.title,
  647. badgeText: "\(count)",
  648. leadingSymbol: symbolName,
  649. style: .pillLarge
  650. )
  651. chip.translatesAutoresizingMaskIntoConstraints = false
  652. chip.onSelect = { [weak self] in self?.didSelectGroup(group) }
  653. groupTabsRow.addArrangedSubview(chip)
  654. groupTabButtons[group] = chip
  655. }
  656. }
  657. private func reloadFamilyChips() {
  658. familyChipsRow.arrangedSubviews.forEach {
  659. familyChipsRow.removeArrangedSubview($0)
  660. $0.removeFromSuperview()
  661. }
  662. familyChipButtons.removeAll()
  663. let allCount = templates(forGroup: selectedGroup, family: nil).count
  664. let allChip = CVChipButton(title: "All", badgeText: "\(allCount)", leadingSymbol: nil, style: .pillSmall)
  665. allChip.onSelect = { [weak self] in self?.didSelectFamily(nil) }
  666. familyChipsRow.addArrangedSubview(allChip)
  667. familyChipButtons[nil] = allChip
  668. for family in CVDesignFamily.allCases {
  669. let count = templates(forGroup: selectedGroup, family: family).count
  670. guard count > 0 else { continue }
  671. let chip = CVChipButton(title: family.title, badgeText: "\(count)", leadingSymbol: nil, style: .pillSmall)
  672. chip.onSelect = { [weak self] in self?.didSelectFamily(family) }
  673. familyChipsRow.addArrangedSubview(chip)
  674. familyChipButtons[family] = chip
  675. }
  676. let familyRowTailSpacer = NSView()
  677. familyRowTailSpacer.translatesAutoresizingMaskIntoConstraints = false
  678. familyRowTailSpacer.setContentHuggingPriority(.init(1), for: .horizontal)
  679. familyRowTailSpacer.setContentCompressionResistancePriority(.init(1), for: .horizontal)
  680. familyChipsRow.addArrangedSubview(familyRowTailSpacer)
  681. if let f = selectedFamily, templates(forGroup: selectedGroup, family: f).isEmpty {
  682. selectedFamily = nil
  683. }
  684. }
  685. // MARK: Data filtering
  686. private func templates(forGroup group: CVCategoryGroup, family: CVDesignFamily?) -> [CVTemplate] {
  687. let base = activeCatalog.filter { $0.galleryGroup == group }
  688. guard let family else { return base }
  689. return base.filter { $0.family == family }
  690. }
  691. private var visibleTemplates: [CVTemplate] {
  692. templates(forGroup: selectedGroup, family: selectedFamily)
  693. }
  694. // MARK: Grid
  695. private func reloadTemplateGrid() {
  696. gridStack.arrangedSubviews.forEach {
  697. gridStack.removeArrangedSubview($0)
  698. $0.removeFromSuperview()
  699. }
  700. templateCardsByID.removeAll()
  701. let templates = visibleTemplates
  702. if templates.isEmpty {
  703. let empty = NSTextField(labelWithString: "No templates yet for this category.")
  704. empty.font = .systemFont(ofSize: 13)
  705. empty.textColor = Palette.secondaryText
  706. gridStack.addArrangedSubview(empty)
  707. return
  708. }
  709. let columns = resolvedGridColumnCount()
  710. var index = 0
  711. while index < templates.count {
  712. let row = NSStackView()
  713. row.orientation = .horizontal
  714. row.spacing = 22
  715. row.distribution = .fillEqually
  716. row.alignment = .top
  717. row.translatesAutoresizingMaskIntoConstraints = false
  718. row.setHuggingPriority(.defaultLow, for: .horizontal)
  719. for column in 0..<columns {
  720. let position = index + column
  721. if position < templates.count {
  722. let template = templates[position]
  723. let card = CVTemplateCard(template: template, palette: palette())
  724. card.translatesAutoresizingMaskIntoConstraints = false
  725. card.onSelect = { [weak self] in self?.didSelectTemplate(template.id) }
  726. card.onPreview = { [weak self] in self?.didPreviewTemplate(template.id) }
  727. row.addArrangedSubview(card)
  728. templateCardsByID[template.id] = card
  729. } else {
  730. let filler = NSView()
  731. filler.translatesAutoresizingMaskIntoConstraints = false
  732. row.addArrangedSubview(filler)
  733. }
  734. }
  735. gridStack.addArrangedSubview(row)
  736. row.widthAnchor.constraint(equalTo: gridStack.widthAnchor).isActive = true
  737. index += columns
  738. }
  739. if selectedTemplateID == nil || templateCardsByID[selectedTemplateID ?? ""] == nil {
  740. selectedTemplateID = templates.first?.id
  741. }
  742. applySelectionToCards()
  743. }
  744. private func galleryLayoutWidth() -> CGFloat {
  745. if bounds.width > 8 { return bounds.width }
  746. if let s = superview, s.bounds.width > 8 { return max(s.bounds.width - 64, 400) }
  747. return 900
  748. }
  749. private func layoutGridCardsIfNeeded() {
  750. let cols = resolvedGridColumnCount()
  751. guard cols != appliedGridColumnCount else { return }
  752. appliedGridColumnCount = cols
  753. reloadTemplateGrid()
  754. updateSelectedChipStates()
  755. }
  756. private func resolvedGridColumnCount() -> Int {
  757. let w = max(galleryLayoutWidth(), 400)
  758. if w < 780 { return 2 }
  759. if w < 1080 { return 3 }
  760. return 4
  761. }
  762. private func applySelectionToCards() {
  763. for (id, card) in templateCardsByID {
  764. card.isSelected = (id == selectedTemplateID)
  765. }
  766. }
  767. private func palette() -> CVTemplateCardPalette {
  768. CVTemplateCardPalette(
  769. border: Palette.cardBorder,
  770. borderHover: Palette.cardBorderHover,
  771. borderSelected: Palette.cardBorderSelected,
  772. selectionGlow: Palette.selectionGlow,
  773. footerBackground: Palette.cardFooter,
  774. previewSurface: Palette.previewSurface,
  775. previewPaper: Palette.previewPaper,
  776. previewSidebarTint: Palette.previewSidebarTint,
  777. previewInk: Palette.previewInk,
  778. previewMuted: Palette.previewMuted,
  779. previewAccentRed: Palette.previewAccentRed,
  780. previewAccentBlue: Palette.previewAccentBlue,
  781. primaryText: Palette.primaryText,
  782. secondaryText: Palette.secondaryText,
  783. overlayTint: Palette.overlayTint
  784. )
  785. }
  786. // MARK: Selection
  787. private func didSelectGroup(_ group: CVCategoryGroup) {
  788. guard selectedGroup != group else { return }
  789. selectedGroup = group
  790. selectedFamily = nil
  791. reloadFamilyChips()
  792. reloadTemplateGrid()
  793. updateSelectedChipStates()
  794. }
  795. private func didSelectFamily(_ family: CVDesignFamily?) {
  796. guard selectedFamily != family else { return }
  797. selectedFamily = family
  798. reloadTemplateGrid()
  799. updateSelectedChipStates()
  800. }
  801. private func didSelectTemplate(_ id: String) {
  802. selectedTemplateID = id
  803. applySelectionToCards()
  804. }
  805. private func didPreviewTemplate(_ id: String) {
  806. selectedTemplateID = id
  807. applySelectionToCards()
  808. guard let template = activeCatalog.first(where: { $0.id == id }) else { return }
  809. presentPlaceholderAlert(
  810. title: "Preview \"\(template.name)\"",
  811. message: "Full-page previews and PDF export are coming soon."
  812. )
  813. }
  814. @objc private func didTapUseTemplate() {
  815. guard let id = selectedTemplateID,
  816. let template = activeCatalog.first(where: { $0.id == id }) else {
  817. presentPlaceholderAlert(title: "Pick a template", message: "Select a template first, then choose a profile to continue.")
  818. return
  819. }
  820. presentPlaceholderAlert(
  821. title: "Use \"\(template.name)\"",
  822. message: "Profile selection and CV editing are not available in this preview build yet."
  823. )
  824. }
  825. private func updateSelectedChipStates() {
  826. for (group, chip) in groupTabButtons {
  827. chip.isSelected = (group == selectedGroup)
  828. }
  829. for (family, chip) in familyChipButtons {
  830. chip.isSelected = (family == selectedFamily)
  831. }
  832. }
  833. private func styleCTAButton(_ button: CVHoverableButton) {
  834. button.title = "Use Template & Select Profile →"
  835. button.font = .systemFont(ofSize: 14, weight: .semibold)
  836. button.isBordered = false
  837. button.bezelStyle = .rounded
  838. button.focusRingType = .none
  839. button.contentTintColor = Palette.ctaText
  840. button.wantsLayer = true
  841. button.layer?.cornerRadius = 14
  842. button.layer?.backgroundColor = Palette.ctaBackground.cgColor
  843. button.pointerCursor = true
  844. let attrs: [NSAttributedString.Key: Any] = [
  845. .foregroundColor: Palette.ctaText,
  846. .font: NSFont.systemFont(ofSize: 14, weight: .semibold)
  847. ]
  848. button.attributedTitle = NSAttributedString(string: button.title, attributes: attrs)
  849. button.hoverHandler = { [weak button] hovering in
  850. button?.layer?.backgroundColor = (hovering ? Palette.ctaHover : Palette.ctaBackground).cgColor
  851. }
  852. }
  853. private func beginLoadingAICatalogIfPossible() {
  854. guard OpenAIConfiguration.hasAPIKey else { return }
  855. let defaultSubtitle = subtitleLabel.stringValue
  856. subtitleLabel.stringValue = "Fetching AI-curated templates…"
  857. CVTemplateFetchService.shared.fetchTemplates { [weak self] result in
  858. DispatchQueue.main.async {
  859. guard let self else { return }
  860. switch result {
  861. case .success(let list):
  862. self.subtitleLabel.stringValue = defaultSubtitle
  863. if !list.isEmpty {
  864. self.activeCatalog = list
  865. self.configureGroupTabs()
  866. self.reloadFamilyChips()
  867. self.reloadTemplateGrid()
  868. self.updateSelectedChipStates()
  869. }
  870. case .failure:
  871. self.subtitleLabel.stringValue = "Couldn’t load AI templates — showing the built-in gallery."
  872. DispatchQueue.main.asyncAfter(deadline: .now() + 5.5) { [weak self] in
  873. self?.subtitleLabel.stringValue = defaultSubtitle
  874. }
  875. }
  876. }
  877. }
  878. }
  879. private func presentPlaceholderAlert(title: String, message: String) {
  880. let alert = NSAlert()
  881. alert.messageText = title
  882. alert.informativeText = message
  883. alert.alertStyle = .informational
  884. alert.addButton(withTitle: "OK")
  885. if let window {
  886. alert.beginSheetModal(for: window)
  887. } else {
  888. alert.runModal()
  889. }
  890. }
  891. }
  892. // MARK: - Chip / pill button
  893. /// Reusable pill button used for both the top section toggle ("Design-Based"
  894. /// vs "Profession-Based") and the family filter chips. Switches between an
  895. /// active brand-blue state and a soft neutral state, with an inline count badge.
  896. private final class CVChipButton: NSView {
  897. enum Style { case pillLarge, pillSmall }
  898. var onSelect: (() -> Void)?
  899. var isSelected: Bool = false { didSet { applyState() } }
  900. private let style: Style
  901. private let titleLabel = NSTextField(labelWithString: "")
  902. private let badgeLabel = NSTextField(labelWithString: "")
  903. private let badgePill = NSView()
  904. private let symbolView = NSImageView()
  905. private let stack = NSStackView()
  906. private var isHovering: Bool = false
  907. private var didPushCursor: Bool = false
  908. private var trackingArea: NSTrackingArea?
  909. private enum Palette {
  910. static let restFill = NSColor(srgbRed: 247 / 255, green: 249 / 255, blue: 252 / 255, alpha: 1)
  911. static let restBorder = NSColor(srgbRed: 222 / 255, green: 226 / 255, blue: 233 / 255, alpha: 1)
  912. static let hoverFill = NSColor(srgbRed: 238 / 255, green: 241 / 255, blue: 247 / 255, alpha: 1)
  913. static let activeFill = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  914. static let activeFillHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  915. static let restText = NSColor(srgbRed: 71 / 255, green: 85 / 255, blue: 105 / 255, alpha: 1)
  916. static let activeText = NSColor.white
  917. static let restBadge = NSColor(srgbRed: 230 / 255, green: 234 / 255, blue: 240 / 255, alpha: 1)
  918. static let restBadgeText = NSColor(srgbRed: 100 / 255, green: 116 / 255, blue: 139 / 255, alpha: 1)
  919. static let activeBadge = NSColor.white.withAlphaComponent(0.22)
  920. static let activeBadgeText = NSColor.white
  921. }
  922. init(title: String, badgeText: String, leadingSymbol: String?, style: Style) {
  923. self.style = style
  924. super.init(frame: .zero)
  925. wantsLayer = true
  926. translatesAutoresizingMaskIntoConstraints = false
  927. let height: CGFloat = style == .pillLarge ? 38 : 30
  928. layer?.cornerRadius = height / 2
  929. layer?.borderWidth = 1
  930. heightAnchor.constraint(equalToConstant: height).isActive = true
  931. titleLabel.stringValue = title
  932. titleLabel.font = .systemFont(ofSize: style == .pillLarge ? 13 : 12, weight: .semibold)
  933. titleLabel.isBordered = false
  934. titleLabel.drawsBackground = false
  935. titleLabel.isEditable = false
  936. titleLabel.isSelectable = false
  937. badgeLabel.stringValue = badgeText
  938. badgeLabel.font = .systemFont(ofSize: 10.5, weight: .semibold)
  939. badgeLabel.alignment = .center
  940. badgeLabel.isBordered = false
  941. badgeLabel.drawsBackground = false
  942. badgeLabel.isEditable = false
  943. badgeLabel.isSelectable = false
  944. badgePill.translatesAutoresizingMaskIntoConstraints = false
  945. badgePill.wantsLayer = true
  946. badgePill.layer?.cornerRadius = 9
  947. badgePill.addSubview(badgeLabel)
  948. badgeLabel.translatesAutoresizingMaskIntoConstraints = false
  949. NSLayoutConstraint.activate([
  950. badgeLabel.leadingAnchor.constraint(equalTo: badgePill.leadingAnchor, constant: 7),
  951. badgeLabel.trailingAnchor.constraint(equalTo: badgePill.trailingAnchor, constant: -7),
  952. badgeLabel.centerYAnchor.constraint(equalTo: badgePill.centerYAnchor),
  953. badgePill.heightAnchor.constraint(equalToConstant: 18),
  954. badgePill.widthAnchor.constraint(greaterThanOrEqualToConstant: 22)
  955. ])
  956. stack.orientation = .horizontal
  957. stack.spacing = 8
  958. stack.alignment = .centerY
  959. stack.translatesAutoresizingMaskIntoConstraints = false
  960. if let symbol = leadingSymbol, style == .pillLarge {
  961. symbolView.translatesAutoresizingMaskIntoConstraints = false
  962. symbolView.image = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)
  963. symbolView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  964. stack.addArrangedSubview(symbolView)
  965. }
  966. stack.addArrangedSubview(titleLabel)
  967. stack.addArrangedSubview(badgePill)
  968. addSubview(stack)
  969. let horizontalInset: CGFloat = style == .pillLarge ? 16 : 12
  970. if style == .pillLarge {
  971. // Group toggle shares a row: equal widths from the parent stack; keep
  972. // icon + label + badge centered so selection state does not reshuffle width.
  973. NSLayoutConstraint.activate([
  974. stack.centerXAnchor.constraint(equalTo: centerXAnchor),
  975. stack.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: horizontalInset),
  976. stack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  977. stack.centerYAnchor.constraint(equalTo: centerYAnchor)
  978. ])
  979. setContentHuggingPriority(.defaultLow, for: .horizontal)
  980. } else {
  981. NSLayoutConstraint.activate([
  982. stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  983. stack.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  984. stack.centerYAnchor.constraint(equalTo: centerYAnchor)
  985. ])
  986. setContentHuggingPriority(.required, for: .horizontal)
  987. }
  988. applyState()
  989. }
  990. @available(*, unavailable)
  991. required init?(coder: NSCoder) {
  992. fatalError("init(coder:) has not been implemented")
  993. }
  994. override func hitTest(_ point: NSPoint) -> NSView? {
  995. guard let superview else { return super.hitTest(point) }
  996. let local = convert(point, from: superview)
  997. return bounds.contains(local) ? self : nil
  998. }
  999. override func mouseDown(with event: NSEvent) {
  1000. onSelect?()
  1001. }
  1002. override func updateTrackingAreas() {
  1003. super.updateTrackingAreas()
  1004. if let area = trackingArea { removeTrackingArea(area) }
  1005. let area = NSTrackingArea(
  1006. rect: bounds,
  1007. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1008. owner: self,
  1009. userInfo: nil
  1010. )
  1011. addTrackingArea(area)
  1012. trackingArea = area
  1013. }
  1014. override func mouseEntered(with event: NSEvent) {
  1015. super.mouseEntered(with: event)
  1016. isHovering = true
  1017. applyState()
  1018. if !didPushCursor {
  1019. NSCursor.pointingHand.push()
  1020. didPushCursor = true
  1021. }
  1022. }
  1023. override func mouseExited(with event: NSEvent) {
  1024. super.mouseExited(with: event)
  1025. isHovering = false
  1026. applyState()
  1027. if didPushCursor {
  1028. NSCursor.pop()
  1029. didPushCursor = false
  1030. }
  1031. }
  1032. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1033. super.viewWillMove(toWindow: newWindow)
  1034. if newWindow == nil, didPushCursor {
  1035. NSCursor.pop()
  1036. didPushCursor = false
  1037. isHovering = false
  1038. }
  1039. }
  1040. private func applyState() {
  1041. let fill: NSColor
  1042. let border: NSColor
  1043. let textColor: NSColor
  1044. let badgeFill: NSColor
  1045. let badgeText: NSColor
  1046. if isSelected {
  1047. fill = isHovering ? Palette.activeFillHover : Palette.activeFill
  1048. border = fill
  1049. textColor = Palette.activeText
  1050. badgeFill = Palette.activeBadge
  1051. badgeText = Palette.activeBadgeText
  1052. } else {
  1053. fill = isHovering ? Palette.hoverFill : Palette.restFill
  1054. border = Palette.restBorder
  1055. textColor = Palette.restText
  1056. badgeFill = Palette.restBadge
  1057. badgeText = Palette.restBadgeText
  1058. }
  1059. layer?.backgroundColor = fill.cgColor
  1060. layer?.borderColor = border.cgColor
  1061. titleLabel.textColor = textColor
  1062. symbolView.contentTintColor = textColor
  1063. badgePill.layer?.backgroundColor = badgeFill.cgColor
  1064. badgeLabel.textColor = badgeText
  1065. }
  1066. }
  1067. // MARK: - Template card
  1068. /// Premium gallery card: live résumé thumbnail, glass-style preview overlay, soft
  1069. /// shadow, and an animated brand border when selected.
  1070. private final class CVTemplateCard: NSView {
  1071. var onSelect: (() -> Void)?
  1072. var onPreview: (() -> Void)?
  1073. var isSelected: Bool = false { didSet { applyChrome() } }
  1074. private let template: CVTemplate
  1075. private let palette: CVTemplateCardPalette
  1076. private let previewSurface = NSView()
  1077. private let preview: CVTemplatePreviewView
  1078. private let nameLabel = NSTextField(labelWithString: "")
  1079. private let categoryLabel = NSTextField(labelWithString: "")
  1080. private let overlay = NSView()
  1081. private let overlayBadge = NSView()
  1082. private let overlayBadgeLabel = NSTextField(labelWithString: "Preview")
  1083. private let overlayBadgeIcon = NSImageView()
  1084. private var trackingArea: NSTrackingArea?
  1085. private var isHovering: Bool = false
  1086. private var didPushCursor: Bool = false
  1087. init(template: CVTemplate, palette: CVTemplateCardPalette) {
  1088. self.template = template
  1089. self.palette = palette
  1090. self.preview = CVTemplatePreviewView(template: template, palette: palette)
  1091. super.init(frame: .zero)
  1092. wantsLayer = true
  1093. layer?.masksToBounds = false
  1094. layer?.cornerRadius = 24
  1095. layer?.backgroundColor = NSColor.white.cgColor
  1096. translatesAutoresizingMaskIntoConstraints = false
  1097. heightAnchor.constraint(greaterThanOrEqualToConstant: 292).isActive = true
  1098. previewSurface.translatesAutoresizingMaskIntoConstraints = false
  1099. previewSurface.wantsLayer = true
  1100. previewSurface.layer?.backgroundColor = palette.previewSurface.cgColor
  1101. preview.translatesAutoresizingMaskIntoConstraints = false
  1102. previewSurface.addSubview(preview)
  1103. nameLabel.stringValue = template.name
  1104. nameLabel.font = .systemFont(ofSize: 14, weight: .semibold)
  1105. nameLabel.textColor = palette.primaryText
  1106. nameLabel.isBordered = false
  1107. nameLabel.drawsBackground = false
  1108. nameLabel.isEditable = false
  1109. nameLabel.isSelectable = false
  1110. categoryLabel.stringValue = "\(template.category) · \(template.layoutType.gallerySubtitle)"
  1111. categoryLabel.font = .systemFont(ofSize: 11.5, weight: .regular)
  1112. categoryLabel.textColor = palette.secondaryText
  1113. categoryLabel.isBordered = false
  1114. categoryLabel.drawsBackground = false
  1115. categoryLabel.isEditable = false
  1116. categoryLabel.isSelectable = false
  1117. let footerStack = NSStackView(views: [nameLabel, categoryLabel])
  1118. footerStack.orientation = .vertical
  1119. footerStack.spacing = 3
  1120. footerStack.alignment = .leading
  1121. footerStack.translatesAutoresizingMaskIntoConstraints = false
  1122. let footer = NSView()
  1123. footer.translatesAutoresizingMaskIntoConstraints = false
  1124. footer.wantsLayer = true
  1125. footer.layer?.backgroundColor = palette.footerBackground.cgColor
  1126. footer.addSubview(footerStack)
  1127. NSLayoutConstraint.activate([
  1128. footerStack.leadingAnchor.constraint(equalTo: footer.leadingAnchor, constant: 16),
  1129. footerStack.trailingAnchor.constraint(lessThanOrEqualTo: footer.trailingAnchor, constant: -16),
  1130. footerStack.topAnchor.constraint(equalTo: footer.topAnchor, constant: 13),
  1131. footerStack.bottomAnchor.constraint(equalTo: footer.bottomAnchor, constant: -13)
  1132. ])
  1133. addSubview(previewSurface)
  1134. addSubview(footer)
  1135. addSubview(overlay)
  1136. configureOverlay()
  1137. NSLayoutConstraint.activate([
  1138. previewSurface.topAnchor.constraint(equalTo: topAnchor),
  1139. previewSurface.leadingAnchor.constraint(equalTo: leadingAnchor),
  1140. previewSurface.trailingAnchor.constraint(equalTo: trailingAnchor),
  1141. previewSurface.heightAnchor.constraint(greaterThanOrEqualToConstant: 236),
  1142. preview.topAnchor.constraint(equalTo: previewSurface.topAnchor, constant: 14),
  1143. preview.leadingAnchor.constraint(equalTo: previewSurface.leadingAnchor, constant: 16),
  1144. preview.trailingAnchor.constraint(equalTo: previewSurface.trailingAnchor, constant: -16),
  1145. preview.bottomAnchor.constraint(equalTo: previewSurface.bottomAnchor, constant: -14),
  1146. footer.topAnchor.constraint(equalTo: previewSurface.bottomAnchor),
  1147. footer.leadingAnchor.constraint(equalTo: leadingAnchor),
  1148. footer.trailingAnchor.constraint(equalTo: trailingAnchor),
  1149. footer.bottomAnchor.constraint(equalTo: bottomAnchor),
  1150. overlay.topAnchor.constraint(equalTo: previewSurface.topAnchor),
  1151. overlay.leadingAnchor.constraint(equalTo: previewSurface.leadingAnchor),
  1152. overlay.trailingAnchor.constraint(equalTo: previewSurface.trailingAnchor),
  1153. overlay.bottomAnchor.constraint(equalTo: previewSurface.bottomAnchor)
  1154. ])
  1155. applyChrome()
  1156. }
  1157. @available(*, unavailable)
  1158. required init?(coder: NSCoder) {
  1159. fatalError("init(coder:) has not been implemented")
  1160. }
  1161. override func layout() {
  1162. super.layout()
  1163. layer?.shadowPath = CGPath(roundedRect: bounds, cornerWidth: 24, cornerHeight: 24, transform: nil)
  1164. }
  1165. private func configureOverlay() {
  1166. overlay.translatesAutoresizingMaskIntoConstraints = false
  1167. overlay.wantsLayer = true
  1168. overlay.layer?.backgroundColor = palette.overlayTint.cgColor
  1169. overlay.alphaValue = 0
  1170. overlay.isHidden = false
  1171. overlayBadge.translatesAutoresizingMaskIntoConstraints = false
  1172. overlayBadge.wantsLayer = true
  1173. overlayBadge.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.22).cgColor
  1174. overlayBadge.layer?.cornerRadius = 20
  1175. overlayBadge.layer?.borderWidth = 1
  1176. overlayBadge.layer?.borderColor = NSColor.white.withAlphaComponent(0.45).cgColor
  1177. overlayBadgeIcon.translatesAutoresizingMaskIntoConstraints = false
  1178. overlayBadgeIcon.image = NSImage(systemSymbolName: "eye", accessibilityDescription: nil)
  1179. overlayBadgeIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
  1180. overlayBadgeIcon.contentTintColor = .white
  1181. overlayBadgeLabel.font = .systemFont(ofSize: 12.5, weight: .semibold)
  1182. overlayBadgeLabel.textColor = .white
  1183. overlayBadgeLabel.isBordered = false
  1184. overlayBadgeLabel.drawsBackground = false
  1185. overlayBadgeLabel.isEditable = false
  1186. overlayBadgeLabel.isSelectable = false
  1187. let badgeStack = NSStackView(views: [overlayBadgeIcon, overlayBadgeLabel])
  1188. badgeStack.orientation = .horizontal
  1189. badgeStack.spacing = 7
  1190. badgeStack.alignment = .centerY
  1191. badgeStack.translatesAutoresizingMaskIntoConstraints = false
  1192. overlayBadge.addSubview(badgeStack)
  1193. overlay.addSubview(overlayBadge)
  1194. NSLayoutConstraint.activate([
  1195. badgeStack.leadingAnchor.constraint(equalTo: overlayBadge.leadingAnchor, constant: 16),
  1196. badgeStack.trailingAnchor.constraint(equalTo: overlayBadge.trailingAnchor, constant: -16),
  1197. badgeStack.topAnchor.constraint(equalTo: overlayBadge.topAnchor, constant: 9),
  1198. badgeStack.bottomAnchor.constraint(equalTo: overlayBadge.bottomAnchor, constant: -9),
  1199. overlayBadge.centerXAnchor.constraint(equalTo: overlay.centerXAnchor),
  1200. overlayBadge.centerYAnchor.constraint(equalTo: overlay.centerYAnchor)
  1201. ])
  1202. }
  1203. override func hitTest(_ point: NSPoint) -> NSView? {
  1204. guard let superview else { return super.hitTest(point) }
  1205. let local = convert(point, from: superview)
  1206. return bounds.contains(local) ? self : nil
  1207. }
  1208. override func mouseDown(with event: NSEvent) {
  1209. let local = convert(event.locationInWindow, from: nil)
  1210. if overlay.frame.contains(local) {
  1211. onPreview?()
  1212. } else {
  1213. playTapPulse()
  1214. onSelect?()
  1215. }
  1216. }
  1217. private func playTapPulse() {
  1218. guard let l = layer else { return }
  1219. let a = CABasicAnimation(keyPath: "transform")
  1220. a.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
  1221. a.toValue = NSValue(caTransform3D: CATransform3DMakeScale(0.985, 0.985, 1))
  1222. a.duration = 0.1
  1223. a.autoreverses = true
  1224. a.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  1225. l.add(a, forKey: "tapPulse")
  1226. }
  1227. override func updateTrackingAreas() {
  1228. super.updateTrackingAreas()
  1229. if let area = trackingArea { removeTrackingArea(area) }
  1230. let area = NSTrackingArea(
  1231. rect: bounds,
  1232. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1233. owner: self,
  1234. userInfo: nil
  1235. )
  1236. addTrackingArea(area)
  1237. trackingArea = area
  1238. }
  1239. override func mouseEntered(with event: NSEvent) {
  1240. super.mouseEntered(with: event)
  1241. isHovering = true
  1242. animateOverlay(visible: true)
  1243. applyChrome()
  1244. if !didPushCursor {
  1245. NSCursor.pointingHand.push()
  1246. didPushCursor = true
  1247. }
  1248. }
  1249. override func mouseExited(with event: NSEvent) {
  1250. super.mouseExited(with: event)
  1251. isHovering = false
  1252. animateOverlay(visible: false)
  1253. applyChrome()
  1254. if didPushCursor {
  1255. NSCursor.pop()
  1256. didPushCursor = false
  1257. }
  1258. }
  1259. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1260. super.viewWillMove(toWindow: newWindow)
  1261. if newWindow == nil, didPushCursor {
  1262. NSCursor.pop()
  1263. didPushCursor = false
  1264. isHovering = false
  1265. }
  1266. }
  1267. private func animateOverlay(visible: Bool) {
  1268. let target: CGFloat = visible ? 1 : 0
  1269. NSAnimationContext.runAnimationGroup { context in
  1270. context.duration = 0.18
  1271. context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  1272. context.allowsImplicitAnimation = true
  1273. overlay.animator().alphaValue = target
  1274. }
  1275. }
  1276. private func applyChrome() {
  1277. if isSelected {
  1278. layer?.borderColor = palette.borderSelected.cgColor
  1279. layer?.borderWidth = 2.5
  1280. layer?.shadowColor = palette.borderSelected.cgColor
  1281. layer?.shadowOpacity = 0.42
  1282. layer?.shadowRadius = 22
  1283. layer?.shadowOffset = CGSize(width: 0, height: 10)
  1284. } else if isHovering {
  1285. layer?.borderColor = palette.borderHover.cgColor
  1286. layer?.borderWidth = 1.5
  1287. layer?.shadowColor = NSColor.black.cgColor
  1288. layer?.shadowOpacity = 0.14
  1289. layer?.shadowRadius = 18
  1290. layer?.shadowOffset = CGSize(width: 0, height: 10)
  1291. } else {
  1292. layer?.borderColor = palette.border.cgColor
  1293. layer?.borderWidth = 1
  1294. layer?.shadowColor = NSColor.black.cgColor
  1295. layer?.shadowOpacity = 0.08
  1296. layer?.shadowRadius = 12
  1297. layer?.shadowOffset = CGSize(width: 0, height: 6)
  1298. }
  1299. }
  1300. }
  1301. // MARK: - Helpers
  1302. /// Flipped origin so the grid stacks fill from the top.
  1303. private final class TopFlippedView: NSView {
  1304. override var isFlipped: Bool { true }
  1305. }
  1306. /// Local copy of the dashboard's hoverable button so this file stays
  1307. /// self-contained without exposing the existing private classes.
  1308. private final class CVHoverableButton: NSButton {
  1309. var hoverHandler: ((Bool) -> Void)?
  1310. var pointerCursor: Bool = false
  1311. private(set) var isHovering: Bool = false
  1312. private var trackingArea: NSTrackingArea?
  1313. private var didPushCursor: Bool = false
  1314. override func updateTrackingAreas() {
  1315. super.updateTrackingAreas()
  1316. if let area = trackingArea { removeTrackingArea(area) }
  1317. let area = NSTrackingArea(
  1318. rect: bounds,
  1319. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1320. owner: self,
  1321. userInfo: nil
  1322. )
  1323. addTrackingArea(area)
  1324. trackingArea = area
  1325. }
  1326. override func mouseEntered(with event: NSEvent) {
  1327. super.mouseEntered(with: event)
  1328. isHovering = true
  1329. hoverHandler?(true)
  1330. if pointerCursor, !didPushCursor {
  1331. NSCursor.pointingHand.push()
  1332. didPushCursor = true
  1333. }
  1334. }
  1335. override func mouseExited(with event: NSEvent) {
  1336. super.mouseExited(with: event)
  1337. isHovering = false
  1338. hoverHandler?(false)
  1339. if didPushCursor {
  1340. NSCursor.pop()
  1341. didPushCursor = false
  1342. }
  1343. }
  1344. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1345. super.viewWillMove(toWindow: newWindow)
  1346. if newWindow == nil, didPushCursor {
  1347. NSCursor.pop()
  1348. didPushCursor = false
  1349. isHovering = false
  1350. }
  1351. }
  1352. }