暂无描述

CVMakerPageView.swift 56KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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.distribution = .fillEqually
  567. groupTabsRow.translatesAutoresizingMaskIntoConstraints = false
  568. configureGroupTabs()
  569. familyChipsRow.orientation = .horizontal
  570. familyChipsRow.spacing = 8
  571. familyChipsRow.alignment = .centerY
  572. familyChipsRow.translatesAutoresizingMaskIntoConstraints = false
  573. gridStack.orientation = .vertical
  574. gridStack.spacing = 26
  575. gridStack.alignment = .leading
  576. gridStack.distribution = .fill
  577. gridStack.translatesAutoresizingMaskIntoConstraints = false
  578. gridDocument.translatesAutoresizingMaskIntoConstraints = false
  579. gridDocument.addSubview(gridStack)
  580. NSLayoutConstraint.activate([
  581. gridStack.leadingAnchor.constraint(equalTo: gridDocument.leadingAnchor),
  582. gridStack.trailingAnchor.constraint(equalTo: gridDocument.trailingAnchor),
  583. gridStack.topAnchor.constraint(equalTo: gridDocument.topAnchor),
  584. gridStack.bottomAnchor.constraint(equalTo: gridDocument.bottomAnchor)
  585. ])
  586. scrollView.translatesAutoresizingMaskIntoConstraints = false
  587. scrollView.hasVerticalScroller = true
  588. scrollView.hasHorizontalScroller = false
  589. scrollView.scrollerStyle = .legacy
  590. scrollView.autohidesScrollers = true
  591. scrollView.drawsBackground = false
  592. scrollView.borderType = .noBorder
  593. scrollView.documentView = gridDocument
  594. NSLayoutConstraint.activate([
  595. gridDocument.topAnchor.constraint(equalTo: scrollView.contentView.topAnchor),
  596. gridDocument.leadingAnchor.constraint(equalTo: scrollView.contentView.leadingAnchor),
  597. gridDocument.widthAnchor.constraint(equalTo: scrollView.contentView.widthAnchor)
  598. ])
  599. ctaButton.translatesAutoresizingMaskIntoConstraints = false
  600. styleCTAButton(ctaButton)
  601. ctaButton.target = self
  602. ctaButton.action = #selector(didTapUseTemplate)
  603. addSubview(headerStack)
  604. addSubview(filterChrome)
  605. addSubview(scrollView)
  606. addSubview(ctaButton)
  607. let horizontalInset: CGFloat = 32
  608. NSLayoutConstraint.activate([
  609. headerStack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  610. headerStack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  611. headerStack.topAnchor.constraint(equalTo: topAnchor, constant: 8),
  612. filterChrome.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  613. filterChrome.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  614. filterChrome.topAnchor.constraint(equalTo: headerStack.bottomAnchor, constant: 16),
  615. scrollView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  616. scrollView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  617. scrollView.topAnchor.constraint(equalTo: filterChrome.bottomAnchor, constant: 18),
  618. scrollView.bottomAnchor.constraint(equalTo: ctaButton.topAnchor, constant: -18),
  619. ctaButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  620. ctaButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  621. ctaButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -24),
  622. ctaButton.heightAnchor.constraint(equalToConstant: 52)
  623. ])
  624. }
  625. private func configureGroupTabs() {
  626. groupTabsRow.arrangedSubviews.forEach {
  627. groupTabsRow.removeArrangedSubview($0)
  628. $0.removeFromSuperview()
  629. }
  630. groupTabButtons.removeAll()
  631. let groups: [(CVCategoryGroup, String)] = [
  632. (.designBased, "rectangle.3.group"),
  633. (.professionBased, "person.2")
  634. ]
  635. for (group, symbolName) in groups {
  636. let count = templates(forGroup: group, family: nil).count
  637. let chip = CVChipButton(
  638. title: group.title,
  639. badgeText: "\(count)",
  640. leadingSymbol: symbolName,
  641. style: .pillLarge
  642. )
  643. chip.translatesAutoresizingMaskIntoConstraints = false
  644. chip.onSelect = { [weak self] in self?.didSelectGroup(group) }
  645. groupTabsRow.addArrangedSubview(chip)
  646. groupTabButtons[group] = chip
  647. }
  648. }
  649. private func reloadFamilyChips() {
  650. familyChipsRow.arrangedSubviews.forEach {
  651. familyChipsRow.removeArrangedSubview($0)
  652. $0.removeFromSuperview()
  653. }
  654. familyChipButtons.removeAll()
  655. let allCount = templates(forGroup: selectedGroup, family: nil).count
  656. let allChip = CVChipButton(title: "All", badgeText: "\(allCount)", leadingSymbol: nil, style: .pillSmall)
  657. allChip.onSelect = { [weak self] in self?.didSelectFamily(nil) }
  658. familyChipsRow.addArrangedSubview(allChip)
  659. familyChipButtons[nil] = allChip
  660. for family in CVDesignFamily.allCases {
  661. let count = templates(forGroup: selectedGroup, family: family).count
  662. let chip = CVChipButton(title: family.title, badgeText: "\(count)", leadingSymbol: nil, style: .pillSmall)
  663. chip.onSelect = { [weak self] in self?.didSelectFamily(family) }
  664. familyChipsRow.addArrangedSubview(chip)
  665. familyChipButtons[family] = chip
  666. }
  667. }
  668. // MARK: Data filtering
  669. private func templates(forGroup group: CVCategoryGroup, family: CVDesignFamily?) -> [CVTemplate] {
  670. // The catalog is design-driven; profession-based reuses the same templates
  671. // so the gallery is fully populated for both groups in this preview build.
  672. let base = activeCatalog
  673. guard let family else { return base }
  674. _ = group
  675. return base.filter { $0.family == family }
  676. }
  677. private var visibleTemplates: [CVTemplate] {
  678. templates(forGroup: selectedGroup, family: selectedFamily)
  679. }
  680. // MARK: Grid
  681. private func reloadTemplateGrid() {
  682. gridStack.arrangedSubviews.forEach {
  683. gridStack.removeArrangedSubview($0)
  684. $0.removeFromSuperview()
  685. }
  686. templateCardsByID.removeAll()
  687. let templates = visibleTemplates
  688. if templates.isEmpty {
  689. let empty = NSTextField(labelWithString: "No templates yet for this category.")
  690. empty.font = .systemFont(ofSize: 13)
  691. empty.textColor = Palette.secondaryText
  692. gridStack.addArrangedSubview(empty)
  693. return
  694. }
  695. let columns = resolvedGridColumnCount()
  696. var index = 0
  697. while index < templates.count {
  698. let row = NSStackView()
  699. row.orientation = .horizontal
  700. row.spacing = 22
  701. row.distribution = .fillEqually
  702. row.alignment = .top
  703. row.translatesAutoresizingMaskIntoConstraints = false
  704. row.setHuggingPriority(.defaultLow, for: .horizontal)
  705. for column in 0..<columns {
  706. let position = index + column
  707. if position < templates.count {
  708. let template = templates[position]
  709. let card = CVTemplateCard(template: template, palette: palette())
  710. card.translatesAutoresizingMaskIntoConstraints = false
  711. card.onSelect = { [weak self] in self?.didSelectTemplate(template.id) }
  712. card.onPreview = { [weak self] in self?.didPreviewTemplate(template.id) }
  713. row.addArrangedSubview(card)
  714. templateCardsByID[template.id] = card
  715. } else {
  716. let filler = NSView()
  717. filler.translatesAutoresizingMaskIntoConstraints = false
  718. row.addArrangedSubview(filler)
  719. }
  720. }
  721. gridStack.addArrangedSubview(row)
  722. row.widthAnchor.constraint(equalTo: gridStack.widthAnchor).isActive = true
  723. index += columns
  724. }
  725. if selectedTemplateID == nil || templateCardsByID[selectedTemplateID ?? ""] == nil {
  726. selectedTemplateID = templates.first?.id
  727. }
  728. applySelectionToCards()
  729. }
  730. private func galleryLayoutWidth() -> CGFloat {
  731. if bounds.width > 8 { return bounds.width }
  732. if let s = superview, s.bounds.width > 8 { return max(s.bounds.width - 64, 400) }
  733. return 900
  734. }
  735. private func layoutGridCardsIfNeeded() {
  736. let w = max(galleryLayoutWidth(), 400)
  737. let cols: Int
  738. if w < 780 { cols = 2 }
  739. else if w < 1080 { cols = 3 }
  740. else { cols = 4 }
  741. guard cols != appliedGridColumnCount else { return }
  742. appliedGridColumnCount = cols
  743. reloadTemplateGrid()
  744. updateSelectedChipStates()
  745. }
  746. private func resolvedGridColumnCount() -> Int {
  747. let w = max(galleryLayoutWidth(), 400)
  748. if w < 780 { return 2 }
  749. if w < 1080 { return 3 }
  750. return 4
  751. }
  752. private func applySelectionToCards() {
  753. for (id, card) in templateCardsByID {
  754. card.isSelected = (id == selectedTemplateID)
  755. }
  756. }
  757. private func palette() -> CVTemplateCardPalette {
  758. CVTemplateCardPalette(
  759. border: Palette.cardBorder,
  760. borderHover: Palette.cardBorderHover,
  761. borderSelected: Palette.cardBorderSelected,
  762. selectionGlow: Palette.selectionGlow,
  763. footerBackground: Palette.cardFooter,
  764. previewSurface: Palette.previewSurface,
  765. previewPaper: Palette.previewPaper,
  766. previewSidebarTint: Palette.previewSidebarTint,
  767. previewInk: Palette.previewInk,
  768. previewMuted: Palette.previewMuted,
  769. previewAccentRed: Palette.previewAccentRed,
  770. previewAccentBlue: Palette.previewAccentBlue,
  771. primaryText: Palette.primaryText,
  772. secondaryText: Palette.secondaryText,
  773. overlayTint: Palette.overlayTint
  774. )
  775. }
  776. // MARK: Selection
  777. private func didSelectGroup(_ group: CVCategoryGroup) {
  778. guard selectedGroup != group else { return }
  779. selectedGroup = group
  780. selectedFamily = nil
  781. reloadFamilyChips()
  782. reloadTemplateGrid()
  783. updateSelectedChipStates()
  784. }
  785. private func didSelectFamily(_ family: CVDesignFamily?) {
  786. guard selectedFamily != family else { return }
  787. selectedFamily = family
  788. reloadTemplateGrid()
  789. updateSelectedChipStates()
  790. }
  791. private func didSelectTemplate(_ id: String) {
  792. selectedTemplateID = id
  793. applySelectionToCards()
  794. }
  795. private func didPreviewTemplate(_ id: String) {
  796. selectedTemplateID = id
  797. applySelectionToCards()
  798. guard let template = activeCatalog.first(where: { $0.id == id }) else { return }
  799. presentPlaceholderAlert(
  800. title: "Preview \"\(template.name)\"",
  801. message: "Full-page previews and PDF export are coming soon."
  802. )
  803. }
  804. @objc private func didTapUseTemplate() {
  805. guard let id = selectedTemplateID,
  806. let template = activeCatalog.first(where: { $0.id == id }) else {
  807. presentPlaceholderAlert(title: "Pick a template", message: "Select a template first, then choose a profile to continue.")
  808. return
  809. }
  810. presentPlaceholderAlert(
  811. title: "Use \"\(template.name)\"",
  812. message: "Profile selection and CV editing are not available in this preview build yet."
  813. )
  814. }
  815. private func updateSelectedChipStates() {
  816. for (group, chip) in groupTabButtons {
  817. chip.isSelected = (group == selectedGroup)
  818. }
  819. for (family, chip) in familyChipButtons {
  820. chip.isSelected = (family == selectedFamily)
  821. }
  822. }
  823. private func styleCTAButton(_ button: CVHoverableButton) {
  824. button.title = "Use Template & Select Profile →"
  825. button.font = .systemFont(ofSize: 14, weight: .semibold)
  826. button.isBordered = false
  827. button.bezelStyle = .rounded
  828. button.focusRingType = .none
  829. button.contentTintColor = Palette.ctaText
  830. button.wantsLayer = true
  831. button.layer?.cornerRadius = 14
  832. button.layer?.backgroundColor = Palette.ctaBackground.cgColor
  833. button.pointerCursor = true
  834. let attrs: [NSAttributedString.Key: Any] = [
  835. .foregroundColor: Palette.ctaText,
  836. .font: NSFont.systemFont(ofSize: 14, weight: .semibold)
  837. ]
  838. button.attributedTitle = NSAttributedString(string: button.title, attributes: attrs)
  839. button.hoverHandler = { [weak button] hovering in
  840. button?.layer?.backgroundColor = (hovering ? Palette.ctaHover : Palette.ctaBackground).cgColor
  841. }
  842. }
  843. private func beginLoadingAICatalogIfPossible() {
  844. guard OpenAIConfiguration.hasAPIKey else { return }
  845. let defaultSubtitle = subtitleLabel.stringValue
  846. subtitleLabel.stringValue = "Fetching AI-curated templates…"
  847. CVTemplateFetchService.shared.fetchTemplates { [weak self] result in
  848. DispatchQueue.main.async {
  849. guard let self else { return }
  850. switch result {
  851. case .success(let list):
  852. self.subtitleLabel.stringValue = defaultSubtitle
  853. if !list.isEmpty {
  854. self.activeCatalog = list
  855. self.configureGroupTabs()
  856. self.reloadFamilyChips()
  857. self.reloadTemplateGrid()
  858. self.updateSelectedChipStates()
  859. }
  860. case .failure:
  861. self.subtitleLabel.stringValue = "Couldn’t load AI templates — showing the built-in gallery."
  862. DispatchQueue.main.asyncAfter(deadline: .now() + 5.5) { [weak self] in
  863. self?.subtitleLabel.stringValue = defaultSubtitle
  864. }
  865. }
  866. }
  867. }
  868. }
  869. private func presentPlaceholderAlert(title: String, message: String) {
  870. let alert = NSAlert()
  871. alert.messageText = title
  872. alert.informativeText = message
  873. alert.alertStyle = .informational
  874. alert.addButton(withTitle: "OK")
  875. if let window {
  876. alert.beginSheetModal(for: window)
  877. } else {
  878. alert.runModal()
  879. }
  880. }
  881. }
  882. // MARK: - Chip / pill button
  883. /// Reusable pill button used for both the top section toggle ("Design-Based"
  884. /// vs "Profession-Based") and the family filter chips. Switches between an
  885. /// active brand-blue state and a soft neutral state, with an inline count badge.
  886. private final class CVChipButton: NSView {
  887. enum Style { case pillLarge, pillSmall }
  888. var onSelect: (() -> Void)?
  889. var isSelected: Bool = false { didSet { applyState() } }
  890. private let style: Style
  891. private let titleLabel = NSTextField(labelWithString: "")
  892. private let badgeLabel = NSTextField(labelWithString: "")
  893. private let badgePill = NSView()
  894. private let symbolView = NSImageView()
  895. private let stack = NSStackView()
  896. private var isHovering: Bool = false
  897. private var didPushCursor: Bool = false
  898. private var trackingArea: NSTrackingArea?
  899. private enum Palette {
  900. static let restFill = NSColor(srgbRed: 247 / 255, green: 249 / 255, blue: 252 / 255, alpha: 1)
  901. static let restBorder = NSColor(srgbRed: 222 / 255, green: 226 / 255, blue: 233 / 255, alpha: 1)
  902. static let hoverFill = NSColor(srgbRed: 238 / 255, green: 241 / 255, blue: 247 / 255, alpha: 1)
  903. static let activeFill = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  904. static let activeFillHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  905. static let restText = NSColor(srgbRed: 71 / 255, green: 85 / 255, blue: 105 / 255, alpha: 1)
  906. static let activeText = NSColor.white
  907. static let restBadge = NSColor(srgbRed: 230 / 255, green: 234 / 255, blue: 240 / 255, alpha: 1)
  908. static let restBadgeText = NSColor(srgbRed: 100 / 255, green: 116 / 255, blue: 139 / 255, alpha: 1)
  909. static let activeBadge = NSColor.white.withAlphaComponent(0.22)
  910. static let activeBadgeText = NSColor.white
  911. }
  912. init(title: String, badgeText: String, leadingSymbol: String?, style: Style) {
  913. self.style = style
  914. super.init(frame: .zero)
  915. wantsLayer = true
  916. translatesAutoresizingMaskIntoConstraints = false
  917. let height: CGFloat = style == .pillLarge ? 38 : 30
  918. layer?.cornerRadius = height / 2
  919. layer?.borderWidth = 1
  920. heightAnchor.constraint(equalToConstant: height).isActive = true
  921. titleLabel.stringValue = title
  922. titleLabel.font = .systemFont(ofSize: style == .pillLarge ? 13 : 12, weight: .semibold)
  923. titleLabel.isBordered = false
  924. titleLabel.drawsBackground = false
  925. titleLabel.isEditable = false
  926. titleLabel.isSelectable = false
  927. badgeLabel.stringValue = badgeText
  928. badgeLabel.font = .systemFont(ofSize: 10.5, weight: .semibold)
  929. badgeLabel.alignment = .center
  930. badgeLabel.isBordered = false
  931. badgeLabel.drawsBackground = false
  932. badgeLabel.isEditable = false
  933. badgeLabel.isSelectable = false
  934. badgePill.translatesAutoresizingMaskIntoConstraints = false
  935. badgePill.wantsLayer = true
  936. badgePill.layer?.cornerRadius = 9
  937. badgePill.addSubview(badgeLabel)
  938. badgeLabel.translatesAutoresizingMaskIntoConstraints = false
  939. NSLayoutConstraint.activate([
  940. badgeLabel.leadingAnchor.constraint(equalTo: badgePill.leadingAnchor, constant: 7),
  941. badgeLabel.trailingAnchor.constraint(equalTo: badgePill.trailingAnchor, constant: -7),
  942. badgeLabel.centerYAnchor.constraint(equalTo: badgePill.centerYAnchor),
  943. badgePill.heightAnchor.constraint(equalToConstant: 18),
  944. badgePill.widthAnchor.constraint(greaterThanOrEqualToConstant: 22)
  945. ])
  946. stack.orientation = .horizontal
  947. stack.spacing = 8
  948. stack.alignment = .centerY
  949. stack.translatesAutoresizingMaskIntoConstraints = false
  950. if let symbol = leadingSymbol, style == .pillLarge {
  951. symbolView.translatesAutoresizingMaskIntoConstraints = false
  952. symbolView.image = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)
  953. symbolView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  954. stack.addArrangedSubview(symbolView)
  955. }
  956. stack.addArrangedSubview(titleLabel)
  957. stack.addArrangedSubview(badgePill)
  958. addSubview(stack)
  959. let horizontalInset: CGFloat = style == .pillLarge ? 16 : 12
  960. if style == .pillLarge {
  961. // Group toggle shares a row: equal widths from the parent stack; keep
  962. // icon + label + badge centered so selection state does not reshuffle width.
  963. NSLayoutConstraint.activate([
  964. stack.centerXAnchor.constraint(equalTo: centerXAnchor),
  965. stack.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: horizontalInset),
  966. stack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  967. stack.centerYAnchor.constraint(equalTo: centerYAnchor)
  968. ])
  969. setContentHuggingPriority(.defaultLow, for: .horizontal)
  970. } else {
  971. NSLayoutConstraint.activate([
  972. stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  973. stack.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  974. stack.centerYAnchor.constraint(equalTo: centerYAnchor)
  975. ])
  976. setContentHuggingPriority(.required, for: .horizontal)
  977. }
  978. applyState()
  979. }
  980. @available(*, unavailable)
  981. required init?(coder: NSCoder) {
  982. fatalError("init(coder:) has not been implemented")
  983. }
  984. override func hitTest(_ point: NSPoint) -> NSView? {
  985. guard let superview else { return super.hitTest(point) }
  986. let local = convert(point, from: superview)
  987. return bounds.contains(local) ? self : nil
  988. }
  989. override func mouseDown(with event: NSEvent) {
  990. onSelect?()
  991. }
  992. override func updateTrackingAreas() {
  993. super.updateTrackingAreas()
  994. if let area = trackingArea { removeTrackingArea(area) }
  995. let area = NSTrackingArea(
  996. rect: bounds,
  997. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  998. owner: self,
  999. userInfo: nil
  1000. )
  1001. addTrackingArea(area)
  1002. trackingArea = area
  1003. }
  1004. override func mouseEntered(with event: NSEvent) {
  1005. super.mouseEntered(with: event)
  1006. isHovering = true
  1007. applyState()
  1008. if !didPushCursor {
  1009. NSCursor.pointingHand.push()
  1010. didPushCursor = true
  1011. }
  1012. }
  1013. override func mouseExited(with event: NSEvent) {
  1014. super.mouseExited(with: event)
  1015. isHovering = false
  1016. applyState()
  1017. if didPushCursor {
  1018. NSCursor.pop()
  1019. didPushCursor = false
  1020. }
  1021. }
  1022. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1023. super.viewWillMove(toWindow: newWindow)
  1024. if newWindow == nil, didPushCursor {
  1025. NSCursor.pop()
  1026. didPushCursor = false
  1027. isHovering = false
  1028. }
  1029. }
  1030. private func applyState() {
  1031. let fill: NSColor
  1032. let border: NSColor
  1033. let textColor: NSColor
  1034. let badgeFill: NSColor
  1035. let badgeText: NSColor
  1036. if isSelected {
  1037. fill = isHovering ? Palette.activeFillHover : Palette.activeFill
  1038. border = fill
  1039. textColor = Palette.activeText
  1040. badgeFill = Palette.activeBadge
  1041. badgeText = Palette.activeBadgeText
  1042. } else {
  1043. fill = isHovering ? Palette.hoverFill : Palette.restFill
  1044. border = Palette.restBorder
  1045. textColor = Palette.restText
  1046. badgeFill = Palette.restBadge
  1047. badgeText = Palette.restBadgeText
  1048. }
  1049. layer?.backgroundColor = fill.cgColor
  1050. layer?.borderColor = border.cgColor
  1051. titleLabel.textColor = textColor
  1052. symbolView.contentTintColor = textColor
  1053. badgePill.layer?.backgroundColor = badgeFill.cgColor
  1054. badgeLabel.textColor = badgeText
  1055. }
  1056. }
  1057. // MARK: - Template card
  1058. /// Premium gallery card: live résumé thumbnail, glass-style preview overlay, soft
  1059. /// shadow, and an animated brand border when selected.
  1060. private final class CVTemplateCard: NSView {
  1061. var onSelect: (() -> Void)?
  1062. var onPreview: (() -> Void)?
  1063. var isSelected: Bool = false { didSet { applyChrome() } }
  1064. private let template: CVTemplate
  1065. private let palette: CVTemplateCardPalette
  1066. private let previewSurface = NSView()
  1067. private let preview: CVTemplatePreviewView
  1068. private let nameLabel = NSTextField(labelWithString: "")
  1069. private let categoryLabel = NSTextField(labelWithString: "")
  1070. private let overlay = NSView()
  1071. private let overlayBadge = NSView()
  1072. private let overlayBadgeLabel = NSTextField(labelWithString: "Preview")
  1073. private let overlayBadgeIcon = NSImageView()
  1074. private var trackingArea: NSTrackingArea?
  1075. private var isHovering: Bool = false
  1076. private var didPushCursor: Bool = false
  1077. init(template: CVTemplate, palette: CVTemplateCardPalette) {
  1078. self.template = template
  1079. self.palette = palette
  1080. self.preview = CVTemplatePreviewView(template: template, palette: palette)
  1081. super.init(frame: .zero)
  1082. wantsLayer = true
  1083. layer?.masksToBounds = false
  1084. layer?.cornerRadius = 24
  1085. layer?.backgroundColor = NSColor.white.cgColor
  1086. translatesAutoresizingMaskIntoConstraints = false
  1087. heightAnchor.constraint(greaterThanOrEqualToConstant: 292).isActive = true
  1088. previewSurface.translatesAutoresizingMaskIntoConstraints = false
  1089. previewSurface.wantsLayer = true
  1090. previewSurface.layer?.backgroundColor = palette.previewSurface.cgColor
  1091. preview.translatesAutoresizingMaskIntoConstraints = false
  1092. previewSurface.addSubview(preview)
  1093. nameLabel.stringValue = template.name
  1094. nameLabel.font = .systemFont(ofSize: 14, weight: .semibold)
  1095. nameLabel.textColor = palette.primaryText
  1096. nameLabel.isBordered = false
  1097. nameLabel.drawsBackground = false
  1098. nameLabel.isEditable = false
  1099. nameLabel.isSelectable = false
  1100. categoryLabel.stringValue = "\(template.category) · \(template.layoutType.gallerySubtitle)"
  1101. categoryLabel.font = .systemFont(ofSize: 11.5, weight: .regular)
  1102. categoryLabel.textColor = palette.secondaryText
  1103. categoryLabel.isBordered = false
  1104. categoryLabel.drawsBackground = false
  1105. categoryLabel.isEditable = false
  1106. categoryLabel.isSelectable = false
  1107. let footerStack = NSStackView(views: [nameLabel, categoryLabel])
  1108. footerStack.orientation = .vertical
  1109. footerStack.spacing = 3
  1110. footerStack.alignment = .leading
  1111. footerStack.translatesAutoresizingMaskIntoConstraints = false
  1112. let footer = NSView()
  1113. footer.translatesAutoresizingMaskIntoConstraints = false
  1114. footer.wantsLayer = true
  1115. footer.layer?.backgroundColor = palette.footerBackground.cgColor
  1116. footer.addSubview(footerStack)
  1117. NSLayoutConstraint.activate([
  1118. footerStack.leadingAnchor.constraint(equalTo: footer.leadingAnchor, constant: 16),
  1119. footerStack.trailingAnchor.constraint(lessThanOrEqualTo: footer.trailingAnchor, constant: -16),
  1120. footerStack.topAnchor.constraint(equalTo: footer.topAnchor, constant: 13),
  1121. footerStack.bottomAnchor.constraint(equalTo: footer.bottomAnchor, constant: -13)
  1122. ])
  1123. addSubview(previewSurface)
  1124. addSubview(footer)
  1125. addSubview(overlay)
  1126. configureOverlay()
  1127. NSLayoutConstraint.activate([
  1128. previewSurface.topAnchor.constraint(equalTo: topAnchor),
  1129. previewSurface.leadingAnchor.constraint(equalTo: leadingAnchor),
  1130. previewSurface.trailingAnchor.constraint(equalTo: trailingAnchor),
  1131. previewSurface.heightAnchor.constraint(greaterThanOrEqualToConstant: 236),
  1132. preview.topAnchor.constraint(equalTo: previewSurface.topAnchor, constant: 14),
  1133. preview.leadingAnchor.constraint(equalTo: previewSurface.leadingAnchor, constant: 16),
  1134. preview.trailingAnchor.constraint(equalTo: previewSurface.trailingAnchor, constant: -16),
  1135. preview.bottomAnchor.constraint(equalTo: previewSurface.bottomAnchor, constant: -14),
  1136. footer.topAnchor.constraint(equalTo: previewSurface.bottomAnchor),
  1137. footer.leadingAnchor.constraint(equalTo: leadingAnchor),
  1138. footer.trailingAnchor.constraint(equalTo: trailingAnchor),
  1139. footer.bottomAnchor.constraint(equalTo: bottomAnchor),
  1140. overlay.topAnchor.constraint(equalTo: previewSurface.topAnchor),
  1141. overlay.leadingAnchor.constraint(equalTo: previewSurface.leadingAnchor),
  1142. overlay.trailingAnchor.constraint(equalTo: previewSurface.trailingAnchor),
  1143. overlay.bottomAnchor.constraint(equalTo: previewSurface.bottomAnchor)
  1144. ])
  1145. applyChrome()
  1146. }
  1147. @available(*, unavailable)
  1148. required init?(coder: NSCoder) {
  1149. fatalError("init(coder:) has not been implemented")
  1150. }
  1151. override func layout() {
  1152. super.layout()
  1153. layer?.shadowPath = CGPath(roundedRect: bounds, cornerWidth: 24, cornerHeight: 24, transform: nil)
  1154. }
  1155. private func configureOverlay() {
  1156. overlay.translatesAutoresizingMaskIntoConstraints = false
  1157. overlay.wantsLayer = true
  1158. overlay.layer?.backgroundColor = palette.overlayTint.cgColor
  1159. overlay.alphaValue = 0
  1160. overlay.isHidden = false
  1161. overlayBadge.translatesAutoresizingMaskIntoConstraints = false
  1162. overlayBadge.wantsLayer = true
  1163. overlayBadge.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.22).cgColor
  1164. overlayBadge.layer?.cornerRadius = 20
  1165. overlayBadge.layer?.borderWidth = 1
  1166. overlayBadge.layer?.borderColor = NSColor.white.withAlphaComponent(0.45).cgColor
  1167. overlayBadgeIcon.translatesAutoresizingMaskIntoConstraints = false
  1168. overlayBadgeIcon.image = NSImage(systemSymbolName: "eye", accessibilityDescription: nil)
  1169. overlayBadgeIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
  1170. overlayBadgeIcon.contentTintColor = .white
  1171. overlayBadgeLabel.font = .systemFont(ofSize: 12.5, weight: .semibold)
  1172. overlayBadgeLabel.textColor = .white
  1173. overlayBadgeLabel.isBordered = false
  1174. overlayBadgeLabel.drawsBackground = false
  1175. overlayBadgeLabel.isEditable = false
  1176. overlayBadgeLabel.isSelectable = false
  1177. let badgeStack = NSStackView(views: [overlayBadgeIcon, overlayBadgeLabel])
  1178. badgeStack.orientation = .horizontal
  1179. badgeStack.spacing = 7
  1180. badgeStack.alignment = .centerY
  1181. badgeStack.translatesAutoresizingMaskIntoConstraints = false
  1182. overlayBadge.addSubview(badgeStack)
  1183. overlay.addSubview(overlayBadge)
  1184. NSLayoutConstraint.activate([
  1185. badgeStack.leadingAnchor.constraint(equalTo: overlayBadge.leadingAnchor, constant: 16),
  1186. badgeStack.trailingAnchor.constraint(equalTo: overlayBadge.trailingAnchor, constant: -16),
  1187. badgeStack.topAnchor.constraint(equalTo: overlayBadge.topAnchor, constant: 9),
  1188. badgeStack.bottomAnchor.constraint(equalTo: overlayBadge.bottomAnchor, constant: -9),
  1189. overlayBadge.centerXAnchor.constraint(equalTo: overlay.centerXAnchor),
  1190. overlayBadge.centerYAnchor.constraint(equalTo: overlay.centerYAnchor)
  1191. ])
  1192. }
  1193. override func hitTest(_ point: NSPoint) -> NSView? {
  1194. guard let superview else { return super.hitTest(point) }
  1195. let local = convert(point, from: superview)
  1196. return bounds.contains(local) ? self : nil
  1197. }
  1198. override func mouseDown(with event: NSEvent) {
  1199. let local = convert(event.locationInWindow, from: nil)
  1200. if overlay.frame.contains(local) {
  1201. onPreview?()
  1202. } else {
  1203. playTapPulse()
  1204. onSelect?()
  1205. }
  1206. }
  1207. private func playTapPulse() {
  1208. guard let l = layer else { return }
  1209. let a = CABasicAnimation(keyPath: "transform")
  1210. a.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
  1211. a.toValue = NSValue(caTransform3D: CATransform3DMakeScale(0.985, 0.985, 1))
  1212. a.duration = 0.1
  1213. a.autoreverses = true
  1214. a.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  1215. l.add(a, forKey: "tapPulse")
  1216. }
  1217. override func updateTrackingAreas() {
  1218. super.updateTrackingAreas()
  1219. if let area = trackingArea { removeTrackingArea(area) }
  1220. let area = NSTrackingArea(
  1221. rect: bounds,
  1222. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1223. owner: self,
  1224. userInfo: nil
  1225. )
  1226. addTrackingArea(area)
  1227. trackingArea = area
  1228. }
  1229. override func mouseEntered(with event: NSEvent) {
  1230. super.mouseEntered(with: event)
  1231. isHovering = true
  1232. animateOverlay(visible: true)
  1233. applyChrome()
  1234. if !didPushCursor {
  1235. NSCursor.pointingHand.push()
  1236. didPushCursor = true
  1237. }
  1238. }
  1239. override func mouseExited(with event: NSEvent) {
  1240. super.mouseExited(with: event)
  1241. isHovering = false
  1242. animateOverlay(visible: false)
  1243. applyChrome()
  1244. if didPushCursor {
  1245. NSCursor.pop()
  1246. didPushCursor = false
  1247. }
  1248. }
  1249. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1250. super.viewWillMove(toWindow: newWindow)
  1251. if newWindow == nil, didPushCursor {
  1252. NSCursor.pop()
  1253. didPushCursor = false
  1254. isHovering = false
  1255. }
  1256. }
  1257. private func animateOverlay(visible: Bool) {
  1258. let target: CGFloat = visible ? 1 : 0
  1259. NSAnimationContext.runAnimationGroup { context in
  1260. context.duration = 0.18
  1261. context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  1262. context.allowsImplicitAnimation = true
  1263. overlay.animator().alphaValue = target
  1264. }
  1265. }
  1266. private func applyChrome() {
  1267. if isSelected {
  1268. layer?.borderColor = palette.borderSelected.cgColor
  1269. layer?.borderWidth = 2.5
  1270. layer?.shadowColor = palette.borderSelected.cgColor
  1271. layer?.shadowOpacity = 0.42
  1272. layer?.shadowRadius = 22
  1273. layer?.shadowOffset = CGSize(width: 0, height: 10)
  1274. } else if isHovering {
  1275. layer?.borderColor = palette.borderHover.cgColor
  1276. layer?.borderWidth = 1.5
  1277. layer?.shadowColor = NSColor.black.cgColor
  1278. layer?.shadowOpacity = 0.14
  1279. layer?.shadowRadius = 18
  1280. layer?.shadowOffset = CGSize(width: 0, height: 10)
  1281. } else {
  1282. layer?.borderColor = palette.border.cgColor
  1283. layer?.borderWidth = 1
  1284. layer?.shadowColor = NSColor.black.cgColor
  1285. layer?.shadowOpacity = 0.08
  1286. layer?.shadowRadius = 12
  1287. layer?.shadowOffset = CGSize(width: 0, height: 6)
  1288. }
  1289. }
  1290. }
  1291. // MARK: - Helpers
  1292. /// Flipped origin so the grid stacks fill from the top.
  1293. private final class TopFlippedView: NSView {
  1294. override var isFlipped: Bool { true }
  1295. }
  1296. /// Local copy of the dashboard's hoverable button so this file stays
  1297. /// self-contained without exposing the existing private classes.
  1298. private final class CVHoverableButton: NSButton {
  1299. var hoverHandler: ((Bool) -> Void)?
  1300. var pointerCursor: Bool = false
  1301. private(set) var isHovering: Bool = false
  1302. private var trackingArea: NSTrackingArea?
  1303. private var didPushCursor: Bool = false
  1304. override func updateTrackingAreas() {
  1305. super.updateTrackingAreas()
  1306. if let area = trackingArea { removeTrackingArea(area) }
  1307. let area = NSTrackingArea(
  1308. rect: bounds,
  1309. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1310. owner: self,
  1311. userInfo: nil
  1312. )
  1313. addTrackingArea(area)
  1314. trackingArea = area
  1315. }
  1316. override func mouseEntered(with event: NSEvent) {
  1317. super.mouseEntered(with: event)
  1318. isHovering = true
  1319. hoverHandler?(true)
  1320. if pointerCursor, !didPushCursor {
  1321. NSCursor.pointingHand.push()
  1322. didPushCursor = true
  1323. }
  1324. }
  1325. override func mouseExited(with event: NSEvent) {
  1326. super.mouseExited(with: event)
  1327. isHovering = false
  1328. hoverHandler?(false)
  1329. if didPushCursor {
  1330. NSCursor.pop()
  1331. didPushCursor = false
  1332. }
  1333. }
  1334. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1335. super.viewWillMove(toWindow: newWindow)
  1336. if newWindow == nil, didPushCursor {
  1337. NSCursor.pop()
  1338. didPushCursor = false
  1339. isHovering = false
  1340. }
  1341. }
  1342. }