Ingen beskrivning

CVMakerPageView.swift 57KB

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