Nenhuma descrição

CVMakerPageView.swift 60KB

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