Geen omschrijving

CVMakerPageView.swift 57KB

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