説明なし

CVMakerPageView.swift 56KB

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