Ei kuvausta

CVMakerPageView.swift 60KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. //
  2. // CVMakerPageView.swift
  3. // App for Indeed
  4. //
  5. // Template gallery for the CV Maker sidebar destination: page header, category
  6. // toggle, style chips, thumbnail grid, and a sticky bottom CTA. Follows the
  7. // active dashboard light / dark appearance via `AppDashboardTheme`.
  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. private enum Palette {
  528. static var primaryText: NSColor { AppDashboardTheme.primaryText }
  529. static var secondaryText: NSColor { AppDashboardTheme.secondaryText }
  530. static var cardBackground: NSColor { AppDashboardTheme.cardBackground }
  531. static var cardBorder: NSColor { AppDashboardTheme.border }
  532. static var cardBorderHover: NSColor { AppDashboardTheme.cvMakerCardBorderHover }
  533. static var cardBorderSelected: NSColor { AppDashboardTheme.brandBlue }
  534. static var cardFooter: NSColor { AppDashboardTheme.cvMakerCardFooter }
  535. static var previewSurface: NSColor { AppDashboardTheme.cvMakerPreviewSurface }
  536. static var previewPaper: NSColor { NSColor.white }
  537. static var previewSidebarTint: NSColor { AppDashboardTheme.cvMakerPreviewSidebarTint }
  538. static var previewInk: NSColor {
  539. NSColor(srgbRed: 38 / 255, green: 50 / 255, blue: 71 / 255, alpha: 1)
  540. }
  541. static var previewMuted: NSColor {
  542. NSColor(srgbRed: 165 / 255, green: 175 / 255, blue: 192 / 255, alpha: 1)
  543. }
  544. static var previewAccentRed: NSColor {
  545. NSColor(srgbRed: 207 / 255, green: 67 / 255, blue: 50 / 255, alpha: 1)
  546. }
  547. static var previewAccentBlue: NSColor { AppDashboardTheme.brandBlue }
  548. static var ctaBackground: NSColor { AppDashboardTheme.brandBlue }
  549. static var ctaHover: NSColor { AppDashboardTheme.brandBlueHover }
  550. static var ctaText: NSColor { AppDashboardTheme.proCTAText }
  551. static var selectionGlow: NSColor { AppDashboardTheme.cvMakerSelectionGlow }
  552. static var gradientTop: NSColor { AppDashboardTheme.cvMakerPageGradientTop }
  553. static var gradientBottom: NSColor { AppDashboardTheme.cvMakerPageGradientBottom }
  554. static var filterChromeBorder: NSColor { AppDashboardTheme.cvMakerFilterChromeBorder }
  555. }
  556. private var appearanceObserver: NSObjectProtocol?
  557. private let pageGradientLayer = CAGradientLayer()
  558. private let filterChrome = NSVisualEffectView()
  559. private let filterStack = NSStackView()
  560. private let titleLabel = NSTextField(labelWithString: "Templates")
  561. private let subtitleLabel = NSTextField(labelWithString: "Polished layouts with live previews — pick a style that fits your story.")
  562. private let groupTabsRow = NSStackView()
  563. private let familyChipsRow = NSStackView()
  564. private let scrollView = NSScrollView()
  565. private let gridDocument = TopFlippedView()
  566. private let gridStack = NSStackView()
  567. private let ctaButton = CVHoverableButton(title: "Use Template & Select Profile →", target: nil, action: nil)
  568. private var selectedGroup: CVCategoryGroup = .professionBased
  569. private var selectedFamily: CVDesignFamily? = nil // nil == "All"
  570. private var selectedTemplateID: String? = "metro"
  571. /// Exactly one gallery card — avoids multiple highlighted cards when catalog entries share the same `template.id`.
  572. private var selectedTemplateCardToken: UUID?
  573. /// Shown immediately; replaced when `CVTemplateFetchService` returns AI-generated entries.
  574. private var activeCatalog: [CVTemplate] = CVTemplateCatalog.all
  575. private var groupTabButtons: [CVCategoryGroup: CVChipButton] = [:]
  576. private var familyChipButtons: [CVDesignFamily?: CVChipButton] = [:]
  577. /// Every visible gallery card (not keyed by id — duplicate AI ids would collapse in a dictionary and break single-selection visuals).
  578. private var templateCardsInGrid: [CVTemplateCard] = []
  579. /// 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.
  580. var onContinueToProfileSelection: ((CVTemplate) -> Void)?
  581. func templateInGallery(withID id: String) -> CVTemplate? {
  582. resolvedTemplate(withID: id)
  583. }
  584. /// 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).
  585. func resolvedTemplate(withID id: String) -> CVTemplate? {
  586. if let match = activeCatalog.first(where: { $0.id == id }) { return match }
  587. return CVTemplateCatalog.all.first { $0.id == id }
  588. }
  589. private var appliedGridColumnCount: Int = 0
  590. /// Family filter row always renders this many slots so chip widths stay stable
  591. /// when switching between Design-Based (3 labels) and Profession-Based (4).
  592. private let familyChipSlotCount = 4
  593. private enum FilterChromeLayout {
  594. static let padding: CGFloat = 12
  595. static let rowGap: CGFloat = 12
  596. static let groupRowHeight: CGFloat = 38
  597. static let familyRowHeight: CGFloat = 30
  598. static var height: CGFloat {
  599. padding * 2 + groupRowHeight + rowGap + familyRowHeight
  600. }
  601. }
  602. override init(frame frameRect: NSRect) {
  603. super.init(frame: frameRect)
  604. configureLayout()
  605. reloadFamilyChips()
  606. reloadTemplateGrid()
  607. updateSelectedChipStates()
  608. beginLoadingAICatalogIfPossible()
  609. appearanceObserver = NotificationCenter.default.addObserver(
  610. forName: AppAppearanceManager.didChangeNotification,
  611. object: nil,
  612. queue: .main
  613. ) { [weak self] _ in
  614. self?.applyCurrentAppearance()
  615. }
  616. applyCurrentAppearance()
  617. }
  618. deinit {
  619. if let appearanceObserver {
  620. NotificationCenter.default.removeObserver(appearanceObserver)
  621. }
  622. }
  623. @available(*, unavailable)
  624. required init?(coder: NSCoder) {
  625. fatalError("init(coder:) has not been implemented")
  626. }
  627. override func viewDidChangeEffectiveAppearance() {
  628. super.viewDidChangeEffectiveAppearance()
  629. applyCurrentAppearance()
  630. }
  631. override func layout() {
  632. super.layout()
  633. pageGradientLayer.frame = bounds
  634. layoutGridCardsIfNeeded()
  635. }
  636. func applyCurrentAppearance() {
  637. pageGradientLayer.colors = [Palette.gradientBottom.cgColor, Palette.gradientTop.cgColor]
  638. filterChrome.layer?.borderColor = Palette.filterChromeBorder.cgColor
  639. titleLabel.textColor = Palette.primaryText
  640. subtitleLabel.textColor = Palette.secondaryText
  641. styleCTAButton(ctaButton)
  642. for chip in groupTabButtons.values { chip.applyCurrentAppearance() }
  643. for chip in familyChipButtons.values { chip.applyCurrentAppearance() }
  644. reloadTemplateGrid()
  645. updateSelectedChipStates()
  646. }
  647. // MARK: Setup
  648. private func configureLayout() {
  649. wantsLayer = true
  650. layer?.backgroundColor = NSColor.clear.cgColor
  651. pageGradientLayer.locations = [0, 1] as [NSNumber]
  652. pageGradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
  653. pageGradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
  654. layer?.insertSublayer(pageGradientLayer, at: 0)
  655. filterChrome.translatesAutoresizingMaskIntoConstraints = false
  656. filterChrome.material = .contentBackground
  657. filterChrome.blendingMode = .withinWindow
  658. filterChrome.state = .active
  659. filterChrome.wantsLayer = true
  660. filterChrome.layer?.cornerRadius = 18
  661. filterChrome.layer?.borderWidth = 1
  662. filterStack.orientation = .vertical
  663. filterStack.spacing = 12
  664. filterStack.alignment = .leading
  665. filterStack.translatesAutoresizingMaskIntoConstraints = false
  666. filterStack.addArrangedSubview(groupTabsRow)
  667. filterStack.addArrangedSubview(familyChipsRow)
  668. filterChrome.addSubview(filterStack)
  669. NSLayoutConstraint.activate([
  670. filterStack.leadingAnchor.constraint(equalTo: filterChrome.leadingAnchor, constant: 14),
  671. filterStack.trailingAnchor.constraint(equalTo: filterChrome.trailingAnchor, constant: -14),
  672. filterStack.topAnchor.constraint(equalTo: filterChrome.topAnchor, constant: 12),
  673. filterStack.bottomAnchor.constraint(equalTo: filterChrome.bottomAnchor, constant: -12),
  674. // On this SDK `alignment` is `NSLayoutConstraint.Attribute` (no `.fill`).
  675. // Pin row widths so group tabs stay full-width / equal split instead of
  676. // shrinking to intrinsic width when selection changes.
  677. groupTabsRow.widthAnchor.constraint(equalTo: filterStack.widthAnchor),
  678. familyChipsRow.widthAnchor.constraint(equalTo: filterStack.widthAnchor)
  679. ])
  680. titleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  681. titleLabel.textColor = Palette.primaryText
  682. titleLabel.alignment = .left
  683. subtitleLabel.font = .systemFont(ofSize: 13, weight: .regular)
  684. subtitleLabel.textColor = Palette.secondaryText
  685. subtitleLabel.alignment = .left
  686. subtitleLabel.maximumNumberOfLines = 1
  687. let headerStack = NSStackView(views: [titleLabel, subtitleLabel])
  688. headerStack.orientation = .vertical
  689. headerStack.spacing = 4
  690. headerStack.alignment = .leading
  691. headerStack.translatesAutoresizingMaskIntoConstraints = false
  692. groupTabsRow.orientation = .horizontal
  693. groupTabsRow.spacing = 8
  694. groupTabsRow.alignment = .centerY
  695. groupTabsRow.distribution = .fillEqually
  696. groupTabsRow.translatesAutoresizingMaskIntoConstraints = false
  697. groupTabsRow.heightAnchor.constraint(equalToConstant: 38).isActive = true
  698. configureGroupTabs()
  699. familyChipsRow.orientation = .horizontal
  700. familyChipsRow.spacing = 8
  701. familyChipsRow.alignment = .centerY
  702. // Match the top row: equal-width segments, evenly spaced across the row.
  703. familyChipsRow.distribution = .fillEqually
  704. familyChipsRow.translatesAutoresizingMaskIntoConstraints = false
  705. familyChipsRow.heightAnchor.constraint(equalToConstant: 30).isActive = true
  706. gridStack.orientation = .vertical
  707. gridStack.spacing = 26
  708. gridStack.alignment = .leading
  709. gridStack.distribution = .fill
  710. gridStack.translatesAutoresizingMaskIntoConstraints = false
  711. gridDocument.translatesAutoresizingMaskIntoConstraints = false
  712. gridDocument.addSubview(gridStack)
  713. NSLayoutConstraint.activate([
  714. gridStack.leadingAnchor.constraint(equalTo: gridDocument.leadingAnchor),
  715. gridStack.trailingAnchor.constraint(equalTo: gridDocument.trailingAnchor),
  716. gridStack.topAnchor.constraint(equalTo: gridDocument.topAnchor),
  717. gridStack.bottomAnchor.constraint(equalTo: gridDocument.bottomAnchor)
  718. ])
  719. scrollView.translatesAutoresizingMaskIntoConstraints = false
  720. scrollView.hasVerticalScroller = true
  721. scrollView.hasHorizontalScroller = false
  722. scrollView.scrollerStyle = .legacy
  723. scrollView.autohidesScrollers = true
  724. scrollView.drawsBackground = false
  725. scrollView.borderType = .noBorder
  726. scrollView.documentView = gridDocument
  727. NSLayoutConstraint.activate([
  728. gridDocument.topAnchor.constraint(equalTo: scrollView.contentView.topAnchor),
  729. gridDocument.leadingAnchor.constraint(equalTo: scrollView.contentView.leadingAnchor),
  730. gridDocument.widthAnchor.constraint(equalTo: scrollView.contentView.widthAnchor)
  731. ])
  732. ctaButton.translatesAutoresizingMaskIntoConstraints = false
  733. styleCTAButton(ctaButton)
  734. ctaButton.target = self
  735. ctaButton.action = #selector(didTapUseTemplate)
  736. addSubview(headerStack)
  737. addSubview(filterChrome)
  738. addSubview(scrollView)
  739. addSubview(ctaButton)
  740. let horizontalInset: CGFloat = 32
  741. NSLayoutConstraint.activate([
  742. headerStack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  743. headerStack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  744. headerStack.topAnchor.constraint(equalTo: topAnchor, constant: 8),
  745. filterChrome.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  746. filterChrome.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  747. filterChrome.topAnchor.constraint(equalTo: headerStack.bottomAnchor, constant: 16),
  748. filterChrome.heightAnchor.constraint(equalToConstant: FilterChromeLayout.height),
  749. scrollView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  750. scrollView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  751. scrollView.topAnchor.constraint(equalTo: filterChrome.bottomAnchor, constant: 18),
  752. scrollView.bottomAnchor.constraint(equalTo: ctaButton.topAnchor, constant: -18),
  753. ctaButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  754. ctaButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -horizontalInset),
  755. ctaButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -24),
  756. ctaButton.heightAnchor.constraint(equalToConstant: 52)
  757. ])
  758. }
  759. private func configureGroupTabs() {
  760. groupTabsRow.arrangedSubviews.forEach {
  761. groupTabsRow.removeArrangedSubview($0)
  762. $0.removeFromSuperview()
  763. }
  764. groupTabButtons.removeAll()
  765. let groups: [(CVCategoryGroup, String)] = [
  766. (.designBased, "rectangle.3.group"),
  767. (.professionBased, "person.2")
  768. ]
  769. for (group, symbolName) in groups {
  770. let count = templates(forGroup: group, family: nil).count
  771. let chip = CVChipButton(
  772. title: group.title,
  773. badgeText: "\(count)",
  774. leadingSymbol: symbolName,
  775. style: .pillLarge
  776. )
  777. chip.translatesAutoresizingMaskIntoConstraints = false
  778. chip.onSelect = { [weak self] in self?.didSelectGroup(group) }
  779. groupTabsRow.addArrangedSubview(chip)
  780. groupTabButtons[group] = chip
  781. }
  782. }
  783. private func reloadFamilyChips() {
  784. familyChipsRow.arrangedSubviews.forEach {
  785. familyChipsRow.removeArrangedSubview($0)
  786. $0.removeFromSuperview()
  787. }
  788. familyChipButtons.removeAll()
  789. let allCount = templates(forGroup: selectedGroup, family: nil).count
  790. let allChip = CVChipButton(title: "All", badgeText: "\(allCount)", leadingSymbol: nil, style: .pillSmall)
  791. allChip.translatesAutoresizingMaskIntoConstraints = false
  792. allChip.onSelect = { [weak self] in self?.didSelectFamily(nil) }
  793. familyChipsRow.addArrangedSubview(allChip)
  794. familyChipButtons[nil] = allChip
  795. for family in CVDesignFamily.allCases {
  796. let count = templates(forGroup: selectedGroup, family: family).count
  797. guard count > 0 else { continue }
  798. let chip = CVChipButton(title: family.title, badgeText: "\(count)", leadingSymbol: nil, style: .pillSmall)
  799. chip.translatesAutoresizingMaskIntoConstraints = false
  800. chip.onSelect = { [weak self] in self?.didSelectFamily(family) }
  801. familyChipsRow.addArrangedSubview(chip)
  802. familyChipButtons[family] = chip
  803. }
  804. // Pad to a fixed slot count so `fillEqually` chip widths never change when
  805. // the visible family count differs between category groups.
  806. while familyChipsRow.arrangedSubviews.count < familyChipSlotCount {
  807. let slot = NSView()
  808. slot.translatesAutoresizingMaskIntoConstraints = false
  809. slot.setContentHuggingPriority(.defaultLow, for: .horizontal)
  810. slot.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  811. familyChipsRow.addArrangedSubview(slot)
  812. }
  813. if let f = selectedFamily, templates(forGroup: selectedGroup, family: f).isEmpty {
  814. selectedFamily = nil
  815. }
  816. }
  817. // MARK: Data filtering
  818. private func templates(forGroup group: CVCategoryGroup, family: CVDesignFamily?) -> [CVTemplate] {
  819. let base = activeCatalog.filter { $0.galleryGroup == group }
  820. guard let family else { return base }
  821. return base.filter { $0.family == family }
  822. }
  823. private var visibleTemplates: [CVTemplate] {
  824. templates(forGroup: selectedGroup, family: selectedFamily)
  825. }
  826. // MARK: Grid
  827. private func reloadTemplateGrid() {
  828. gridStack.arrangedSubviews.forEach {
  829. gridStack.removeArrangedSubview($0)
  830. $0.removeFromSuperview()
  831. }
  832. templateCardsInGrid.removeAll()
  833. let templates = visibleTemplates
  834. if templates.isEmpty {
  835. let empty = NSTextField(labelWithString: "No templates yet for this category.")
  836. empty.font = .systemFont(ofSize: 13)
  837. empty.textColor = Palette.secondaryText
  838. gridStack.addArrangedSubview(empty)
  839. selectedTemplateCardToken = nil
  840. return
  841. }
  842. let columns = resolvedGridColumnCount()
  843. var index = 0
  844. while index < templates.count {
  845. let row = NSStackView()
  846. row.orientation = .horizontal
  847. row.spacing = 22
  848. row.distribution = .fillEqually
  849. row.alignment = .top
  850. row.translatesAutoresizingMaskIntoConstraints = false
  851. row.setHuggingPriority(.defaultLow, for: .horizontal)
  852. for column in 0..<columns {
  853. let position = index + column
  854. if position < templates.count {
  855. let template = templates[position]
  856. let card = CVTemplateCard(template: template, palette: palette())
  857. card.translatesAutoresizingMaskIntoConstraints = false
  858. card.onSelect = { [weak self] in
  859. guard let self else { return }
  860. self.didSelectCard(card)
  861. }
  862. row.addArrangedSubview(card)
  863. templateCardsInGrid.append(card)
  864. } else {
  865. let filler = NSView()
  866. filler.translatesAutoresizingMaskIntoConstraints = false
  867. row.addArrangedSubview(filler)
  868. filler.heightAnchor.constraint(equalToConstant: CVTemplateCard.layoutHeight).isActive = true
  869. }
  870. }
  871. gridStack.addArrangedSubview(row)
  872. row.widthAnchor.constraint(equalTo: gridStack.widthAnchor).isActive = true
  873. row.heightAnchor.constraint(equalToConstant: CVTemplateCard.layoutHeight).isActive = true
  874. index += columns
  875. }
  876. if let sid = selectedTemplateID,
  877. let match = templateCardsInGrid.first(where: { $0.templateID == sid }) {
  878. selectedTemplateCardToken = match.selectionToken
  879. } else if let first = templateCardsInGrid.first {
  880. selectedTemplateCardToken = first.selectionToken
  881. selectedTemplateID = first.templateID
  882. } else {
  883. selectedTemplateCardToken = nil
  884. selectedTemplateID = nil
  885. }
  886. applySelectionToCards()
  887. }
  888. private func galleryLayoutWidth() -> CGFloat {
  889. if bounds.width > 8 { return bounds.width }
  890. if let s = superview, s.bounds.width > 8 { return max(s.bounds.width - 64, 400) }
  891. return 900
  892. }
  893. private func layoutGridCardsIfNeeded() {
  894. let cols = resolvedGridColumnCount()
  895. guard cols != appliedGridColumnCount else { return }
  896. appliedGridColumnCount = cols
  897. reloadTemplateGrid()
  898. updateSelectedChipStates()
  899. }
  900. private func resolvedGridColumnCount() -> Int {
  901. let w = max(galleryLayoutWidth(), 400)
  902. if w < 780 { return 2 }
  903. if w < 1080 { return 3 }
  904. return 4
  905. }
  906. private func applySelectionToCards() {
  907. let token = selectedTemplateCardToken
  908. for card in templateCardsInGrid {
  909. card.isSelected = (card.selectionToken == token)
  910. }
  911. }
  912. private func palette() -> CVTemplateCardPalette {
  913. CVTemplateCardPalette(
  914. border: Palette.cardBorder,
  915. borderHover: Palette.cardBorderHover,
  916. borderSelected: Palette.cardBorderSelected,
  917. selectionGlow: Palette.selectionGlow,
  918. cardShellBackground: Palette.cardBackground,
  919. footerBackground: Palette.cardFooter,
  920. previewSurface: Palette.previewSurface,
  921. previewPaper: Palette.previewPaper,
  922. previewSidebarTint: Palette.previewSidebarTint,
  923. previewInk: Palette.previewInk,
  924. previewMuted: Palette.previewMuted,
  925. previewAccentRed: Palette.previewAccentRed,
  926. previewAccentBlue: Palette.previewAccentBlue,
  927. primaryText: Palette.primaryText,
  928. secondaryText: Palette.secondaryText
  929. )
  930. }
  931. // MARK: Selection
  932. private func didSelectGroup(_ group: CVCategoryGroup) {
  933. guard selectedGroup != group else { return }
  934. selectedGroup = group
  935. selectedFamily = nil
  936. reloadFamilyChips()
  937. reloadTemplateGrid()
  938. updateSelectedChipStates()
  939. }
  940. private func didSelectFamily(_ family: CVDesignFamily?) {
  941. guard selectedFamily != family else { return }
  942. selectedFamily = family
  943. reloadTemplateGrid()
  944. updateSelectedChipStates()
  945. }
  946. private func didSelectCard(_ card: CVTemplateCard) {
  947. selectedTemplateCardToken = card.selectionToken
  948. selectedTemplateID = card.templateID
  949. applySelectionToCards()
  950. }
  951. @objc private func didTapUseTemplate() {
  952. let chosen: CVTemplate?
  953. if let token = selectedTemplateCardToken,
  954. let card = templateCardsInGrid.first(where: { $0.selectionToken == token }) {
  955. chosen = card.catalogTemplate
  956. } else if let id = selectedTemplateID {
  957. chosen = resolvedTemplate(withID: id)
  958. } else {
  959. chosen = nil
  960. }
  961. guard let template = chosen else {
  962. presentPlaceholderAlert(title: "Pick a template", message: "Select a template first, then choose a profile to continue.")
  963. return
  964. }
  965. onContinueToProfileSelection?(template)
  966. }
  967. private func updateSelectedChipStates() {
  968. for (group, chip) in groupTabButtons {
  969. chip.isSelected = (group == selectedGroup)
  970. }
  971. for (family, chip) in familyChipButtons {
  972. chip.isSelected = (family == selectedFamily)
  973. }
  974. }
  975. private func styleCTAButton(_ button: CVHoverableButton) {
  976. button.title = "Use Template & Select Profile →"
  977. button.font = .systemFont(ofSize: 14, weight: .semibold)
  978. button.isBordered = false
  979. button.bezelStyle = .rounded
  980. button.focusRingType = .none
  981. button.contentTintColor = Palette.ctaText
  982. button.wantsLayer = true
  983. button.layer?.cornerRadius = 14
  984. button.layer?.backgroundColor = Palette.ctaBackground.cgColor
  985. button.pointerCursor = true
  986. let attrs: [NSAttributedString.Key: Any] = [
  987. .foregroundColor: Palette.ctaText,
  988. .font: NSFont.systemFont(ofSize: 14, weight: .semibold)
  989. ]
  990. button.attributedTitle = NSAttributedString(string: button.title, attributes: attrs)
  991. button.hoverHandler = { [weak button] hovering in
  992. button?.layer?.backgroundColor = (hovering ? Palette.ctaHover : Palette.ctaBackground).cgColor
  993. }
  994. }
  995. private func beginLoadingAICatalogIfPossible() {
  996. guard OpenAIConfiguration.hasAPIKey else { return }
  997. let defaultSubtitle = subtitleLabel.stringValue
  998. subtitleLabel.stringValue = "Fetching AI-curated templates…"
  999. CVTemplateFetchService.shared.fetchTemplates { [weak self] result in
  1000. DispatchQueue.main.async {
  1001. guard let self else { return }
  1002. switch result {
  1003. case .success(let list):
  1004. self.subtitleLabel.stringValue = defaultSubtitle
  1005. if !list.isEmpty {
  1006. self.activeCatalog = list
  1007. self.configureGroupTabs()
  1008. self.reloadFamilyChips()
  1009. self.reloadTemplateGrid()
  1010. self.updateSelectedChipStates()
  1011. }
  1012. case .failure:
  1013. self.subtitleLabel.stringValue = "Couldn’t load AI templates — showing the built-in gallery."
  1014. DispatchQueue.main.asyncAfter(deadline: .now() + 5.5) { [weak self] in
  1015. self?.subtitleLabel.stringValue = defaultSubtitle
  1016. }
  1017. }
  1018. }
  1019. }
  1020. }
  1021. private func presentPlaceholderAlert(title: String, message: String) {
  1022. let alert = NSAlert()
  1023. alert.messageText = title
  1024. alert.informativeText = message
  1025. alert.alertStyle = .informational
  1026. alert.addButton(withTitle: "OK")
  1027. if let window {
  1028. alert.beginSheetModal(for: window)
  1029. } else {
  1030. alert.runModal()
  1031. }
  1032. }
  1033. }
  1034. // MARK: - Chip / pill button
  1035. /// Reusable pill button used for both the top section toggle ("Design-Based"
  1036. /// vs "Profession-Based") and the family filter chips. Switches between an
  1037. /// active brand-blue state and a soft neutral state, with an inline count badge.
  1038. private final class CVChipButton: NSView {
  1039. enum Style { case pillLarge, pillSmall }
  1040. var onSelect: (() -> Void)?
  1041. var isSelected: Bool = false { didSet { applyState() } }
  1042. private let style: Style
  1043. private let titleLabel = NSTextField(labelWithString: "")
  1044. private let badgeLabel = NSTextField(labelWithString: "")
  1045. private let badgePill = NSView()
  1046. private let symbolView = NSImageView()
  1047. private let stack = NSStackView()
  1048. private var isHovering: Bool = false
  1049. private var didPushCursor: Bool = false
  1050. private var trackingArea: NSTrackingArea?
  1051. private enum Palette {
  1052. static var restFill: NSColor { AppDashboardTheme.cvMakerChipRestFill }
  1053. static var restBorder: NSColor { AppDashboardTheme.cvMakerChipRestBorder }
  1054. static var hoverFill: NSColor { AppDashboardTheme.cvMakerChipHoverFill }
  1055. static var activeFill: NSColor { AppDashboardTheme.brandBlue }
  1056. static var activeFillHover: NSColor { AppDashboardTheme.brandBlueHover }
  1057. static var activeBorder: NSColor { AppDashboardTheme.brandBlueHover }
  1058. static var restText: NSColor { AppDashboardTheme.primaryText }
  1059. static var activeText: NSColor { AppDashboardTheme.proCTAText }
  1060. static var restBadge: NSColor { AppDashboardTheme.cvMakerChipBadgeBackground }
  1061. static var restBadgeText: NSColor { AppDashboardTheme.cvMakerChipBadgeText }
  1062. static var activeBadge: NSColor { NSColor.white.withAlphaComponent(0.22) }
  1063. static var activeBadgeText: NSColor { AppDashboardTheme.proCTAText }
  1064. }
  1065. func applyCurrentAppearance() {
  1066. applyState()
  1067. }
  1068. private static let symbolSide: CGFloat = 18
  1069. private static let badgeWidthLarge: CGFloat = 28
  1070. private static let badgeWidthSmall: CGFloat = 26
  1071. init(title: String, badgeText: String, leadingSymbol: String?, style: Style) {
  1072. self.style = style
  1073. super.init(frame: .zero)
  1074. wantsLayer = true
  1075. translatesAutoresizingMaskIntoConstraints = false
  1076. let height: CGFloat = style == .pillLarge ? 38 : 30
  1077. layer?.cornerRadius = height / 2
  1078. layer?.borderWidth = 1
  1079. heightAnchor.constraint(equalToConstant: height).isActive = true
  1080. titleLabel.stringValue = title
  1081. titleLabel.font = .systemFont(ofSize: style == .pillLarge ? 13 : 12, weight: .semibold)
  1082. titleLabel.maximumNumberOfLines = 1
  1083. titleLabel.lineBreakMode = .byTruncatingTail
  1084. titleLabel.cell?.lineBreakMode = .byTruncatingTail
  1085. titleLabel.isBordered = false
  1086. titleLabel.drawsBackground = false
  1087. titleLabel.isEditable = false
  1088. titleLabel.isSelectable = false
  1089. badgeLabel.stringValue = badgeText
  1090. badgeLabel.font = .monospacedDigitSystemFont(ofSize: 10.5, weight: .semibold)
  1091. badgeLabel.alignment = .center
  1092. badgeLabel.isBordered = false
  1093. badgeLabel.drawsBackground = false
  1094. badgeLabel.isEditable = false
  1095. badgeLabel.isSelectable = false
  1096. badgePill.translatesAutoresizingMaskIntoConstraints = false
  1097. badgePill.wantsLayer = true
  1098. badgePill.layer?.cornerRadius = 9
  1099. badgePill.addSubview(badgeLabel)
  1100. badgeLabel.translatesAutoresizingMaskIntoConstraints = false
  1101. NSLayoutConstraint.activate([
  1102. badgeLabel.leadingAnchor.constraint(equalTo: badgePill.leadingAnchor, constant: 7),
  1103. badgeLabel.trailingAnchor.constraint(equalTo: badgePill.trailingAnchor, constant: -7),
  1104. badgeLabel.centerYAnchor.constraint(equalTo: badgePill.centerYAnchor),
  1105. badgePill.heightAnchor.constraint(equalToConstant: 18),
  1106. badgePill.widthAnchor.constraint(
  1107. equalToConstant: style == .pillLarge ? Self.badgeWidthLarge : Self.badgeWidthSmall
  1108. )
  1109. ])
  1110. stack.orientation = .horizontal
  1111. stack.spacing = 8
  1112. stack.alignment = .centerY
  1113. stack.translatesAutoresizingMaskIntoConstraints = false
  1114. if let symbol = leadingSymbol, style == .pillLarge {
  1115. symbolView.translatesAutoresizingMaskIntoConstraints = false
  1116. symbolView.image = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)
  1117. symbolView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  1118. symbolView.setContentHuggingPriority(.required, for: .horizontal)
  1119. symbolView.setContentCompressionResistancePriority(.required, for: .horizontal)
  1120. stack.addArrangedSubview(symbolView)
  1121. NSLayoutConstraint.activate([
  1122. symbolView.widthAnchor.constraint(equalToConstant: Self.symbolSide),
  1123. symbolView.heightAnchor.constraint(equalToConstant: Self.symbolSide)
  1124. ])
  1125. }
  1126. stack.addArrangedSubview(titleLabel)
  1127. stack.addArrangedSubview(badgePill)
  1128. addSubview(stack)
  1129. let horizontalInset: CGFloat = style == .pillLarge ? 16 : 12
  1130. // Leading alignment for every chip so selected vs unselected pills share the
  1131. // same geometry (centered stacks shift when badge digits change).
  1132. if style == .pillLarge {
  1133. NSLayoutConstraint.activate([
  1134. stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: horizontalInset),
  1135. stack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  1136. stack.centerYAnchor.constraint(equalTo: centerYAnchor)
  1137. ])
  1138. setContentHuggingPriority(.defaultLow, for: .horizontal)
  1139. } else {
  1140. // Parent row uses `fillEqually`; center label + badge inside each segment.
  1141. NSLayoutConstraint.activate([
  1142. stack.centerXAnchor.constraint(equalTo: centerXAnchor),
  1143. stack.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: horizontalInset),
  1144. stack.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -horizontalInset),
  1145. stack.centerYAnchor.constraint(equalTo: centerYAnchor)
  1146. ])
  1147. setContentHuggingPriority(.defaultLow, for: .horizontal)
  1148. setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  1149. }
  1150. applyState()
  1151. }
  1152. @available(*, unavailable)
  1153. required init?(coder: NSCoder) {
  1154. fatalError("init(coder:) has not been implemented")
  1155. }
  1156. override func hitTest(_ point: NSPoint) -> NSView? {
  1157. guard let superview else { return super.hitTest(point) }
  1158. let local = convert(point, from: superview)
  1159. return bounds.contains(local) ? self : nil
  1160. }
  1161. override func mouseDown(with event: NSEvent) {
  1162. onSelect?()
  1163. }
  1164. override func updateTrackingAreas() {
  1165. super.updateTrackingAreas()
  1166. if let area = trackingArea { removeTrackingArea(area) }
  1167. let area = NSTrackingArea(
  1168. rect: bounds,
  1169. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1170. owner: self,
  1171. userInfo: nil
  1172. )
  1173. addTrackingArea(area)
  1174. trackingArea = area
  1175. }
  1176. override func mouseEntered(with event: NSEvent) {
  1177. super.mouseEntered(with: event)
  1178. isHovering = true
  1179. applyState()
  1180. if !didPushCursor {
  1181. NSCursor.pointingHand.push()
  1182. didPushCursor = true
  1183. }
  1184. }
  1185. override func mouseExited(with event: NSEvent) {
  1186. super.mouseExited(with: event)
  1187. isHovering = false
  1188. applyState()
  1189. if didPushCursor {
  1190. NSCursor.pop()
  1191. didPushCursor = false
  1192. }
  1193. }
  1194. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1195. super.viewWillMove(toWindow: newWindow)
  1196. if newWindow == nil, didPushCursor {
  1197. NSCursor.pop()
  1198. didPushCursor = false
  1199. isHovering = false
  1200. }
  1201. }
  1202. private func applyState() {
  1203. let fill: NSColor
  1204. let border: NSColor
  1205. let textColor: NSColor
  1206. let badgeFill: NSColor
  1207. let badgeText: NSColor
  1208. if isSelected {
  1209. fill = isHovering ? Palette.activeFillHover : Palette.activeFill
  1210. border = Palette.activeBorder
  1211. textColor = Palette.activeText
  1212. badgeFill = Palette.activeBadge
  1213. badgeText = Palette.activeBadgeText
  1214. } else {
  1215. fill = isHovering ? Palette.hoverFill : Palette.restFill
  1216. border = Palette.restBorder
  1217. textColor = Palette.restText
  1218. badgeFill = Palette.restBadge
  1219. badgeText = Palette.restBadgeText
  1220. }
  1221. layer?.backgroundColor = fill.cgColor
  1222. layer?.borderColor = border.cgColor
  1223. layer?.borderWidth = 1
  1224. titleLabel.textColor = textColor
  1225. symbolView.contentTintColor = textColor
  1226. badgePill.layer?.backgroundColor = badgeFill.cgColor
  1227. badgeLabel.textColor = badgeText
  1228. }
  1229. }
  1230. // MARK: - Template card
  1231. /// Premium gallery card: live résumé thumbnail, soft shadow, and an animated
  1232. /// brand border when selected.
  1233. private final class CVTemplateCard: NSView {
  1234. static let layoutHeight: CGFloat = 292
  1235. var onSelect: (() -> Void)?
  1236. var isSelected: Bool = false { didSet { applyChrome() } }
  1237. /// Distinguishes this card from others that may share the same catalog `template.id`.
  1238. let selectionToken = UUID()
  1239. var templateID: String { template.id }
  1240. /// Definition used for this card’s preview; pass through on “Use template” so layout cannot diverge from a later id-only lookup.
  1241. var catalogTemplate: CVTemplate { template }
  1242. private let template: CVTemplate
  1243. private let palette: CVTemplateCardPalette
  1244. private let previewSurface = NSView()
  1245. private let footerView = NSView()
  1246. private let preview: CVTemplatePreviewView
  1247. private let nameLabel = NSTextField(labelWithString: "")
  1248. private let categoryLabel = NSTextField(labelWithString: "")
  1249. private var trackingArea: NSTrackingArea?
  1250. private var isHovering: Bool = false
  1251. private var didPushCursor: Bool = false
  1252. init(template: CVTemplate, palette: CVTemplateCardPalette) {
  1253. self.template = template
  1254. self.palette = palette
  1255. self.preview = CVTemplatePreviewView(template: template, palette: palette)
  1256. super.init(frame: .zero)
  1257. wantsLayer = true
  1258. layer?.masksToBounds = false
  1259. layer?.cornerRadius = 24
  1260. layer?.backgroundColor = palette.cardShellBackground.cgColor
  1261. translatesAutoresizingMaskIntoConstraints = false
  1262. heightAnchor.constraint(equalToConstant: Self.layoutHeight).isActive = true
  1263. previewSurface.translatesAutoresizingMaskIntoConstraints = false
  1264. previewSurface.wantsLayer = true
  1265. previewSurface.layer?.backgroundColor = palette.previewSurface.cgColor
  1266. preview.translatesAutoresizingMaskIntoConstraints = false
  1267. previewSurface.addSubview(preview)
  1268. nameLabel.stringValue = template.name
  1269. nameLabel.font = .systemFont(ofSize: 14, weight: .semibold)
  1270. nameLabel.textColor = palette.primaryText
  1271. nameLabel.isBordered = false
  1272. nameLabel.drawsBackground = false
  1273. nameLabel.isEditable = false
  1274. nameLabel.isSelectable = false
  1275. categoryLabel.stringValue = "\(template.category) · \(template.layoutType.gallerySubtitle)"
  1276. categoryLabel.font = .systemFont(ofSize: 11.5, weight: .regular)
  1277. categoryLabel.textColor = palette.secondaryText
  1278. categoryLabel.isBordered = false
  1279. categoryLabel.drawsBackground = false
  1280. categoryLabel.isEditable = false
  1281. categoryLabel.isSelectable = false
  1282. let footerStack = NSStackView(views: [nameLabel, categoryLabel])
  1283. footerStack.orientation = .vertical
  1284. footerStack.spacing = 3
  1285. footerStack.alignment = .leading
  1286. footerStack.translatesAutoresizingMaskIntoConstraints = false
  1287. footerView.translatesAutoresizingMaskIntoConstraints = false
  1288. footerView.wantsLayer = true
  1289. footerView.layer?.backgroundColor = palette.footerBackground.cgColor
  1290. footerView.addSubview(footerStack)
  1291. NSLayoutConstraint.activate([
  1292. footerStack.leadingAnchor.constraint(equalTo: footerView.leadingAnchor, constant: 16),
  1293. footerStack.trailingAnchor.constraint(lessThanOrEqualTo: footerView.trailingAnchor, constant: -16),
  1294. footerStack.topAnchor.constraint(equalTo: footerView.topAnchor, constant: 13),
  1295. footerStack.bottomAnchor.constraint(equalTo: footerView.bottomAnchor, constant: -13)
  1296. ])
  1297. addSubview(previewSurface)
  1298. addSubview(footerView)
  1299. NSLayoutConstraint.activate([
  1300. previewSurface.topAnchor.constraint(equalTo: topAnchor),
  1301. previewSurface.leadingAnchor.constraint(equalTo: leadingAnchor),
  1302. previewSurface.trailingAnchor.constraint(equalTo: trailingAnchor),
  1303. previewSurface.heightAnchor.constraint(greaterThanOrEqualToConstant: 236),
  1304. preview.topAnchor.constraint(equalTo: previewSurface.topAnchor, constant: 14),
  1305. preview.leadingAnchor.constraint(equalTo: previewSurface.leadingAnchor, constant: 16),
  1306. preview.trailingAnchor.constraint(equalTo: previewSurface.trailingAnchor, constant: -16),
  1307. preview.bottomAnchor.constraint(equalTo: previewSurface.bottomAnchor, constant: -14),
  1308. footerView.topAnchor.constraint(equalTo: previewSurface.bottomAnchor),
  1309. footerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  1310. footerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  1311. footerView.bottomAnchor.constraint(equalTo: bottomAnchor)
  1312. ])
  1313. applyChrome()
  1314. }
  1315. @available(*, unavailable)
  1316. required init?(coder: NSCoder) {
  1317. fatalError("init(coder:) has not been implemented")
  1318. }
  1319. override func layout() {
  1320. super.layout()
  1321. layer?.shadowPath = CGPath(roundedRect: bounds, cornerWidth: 24, cornerHeight: 24, transform: nil)
  1322. }
  1323. override func hitTest(_ point: NSPoint) -> NSView? {
  1324. guard let superview else { return super.hitTest(point) }
  1325. let local = convert(point, from: superview)
  1326. return bounds.contains(local) ? self : nil
  1327. }
  1328. override func mouseDown(with event: NSEvent) {
  1329. playTapPulse()
  1330. onSelect?()
  1331. }
  1332. private func playTapPulse() {
  1333. guard let l = layer else { return }
  1334. let a = CABasicAnimation(keyPath: "transform")
  1335. a.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
  1336. a.toValue = NSValue(caTransform3D: CATransform3DMakeScale(0.985, 0.985, 1))
  1337. a.duration = 0.1
  1338. a.autoreverses = true
  1339. a.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  1340. l.add(a, forKey: "tapPulse")
  1341. }
  1342. override func updateTrackingAreas() {
  1343. super.updateTrackingAreas()
  1344. if let area = trackingArea { removeTrackingArea(area) }
  1345. let area = NSTrackingArea(
  1346. rect: bounds,
  1347. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1348. owner: self,
  1349. userInfo: nil
  1350. )
  1351. addTrackingArea(area)
  1352. trackingArea = area
  1353. }
  1354. override func mouseEntered(with event: NSEvent) {
  1355. super.mouseEntered(with: event)
  1356. isHovering = true
  1357. applyChrome()
  1358. if !didPushCursor {
  1359. NSCursor.pointingHand.push()
  1360. didPushCursor = true
  1361. }
  1362. }
  1363. override func mouseExited(with event: NSEvent) {
  1364. super.mouseExited(with: event)
  1365. isHovering = false
  1366. applyChrome()
  1367. if didPushCursor {
  1368. NSCursor.pop()
  1369. didPushCursor = false
  1370. }
  1371. }
  1372. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1373. super.viewWillMove(toWindow: newWindow)
  1374. if newWindow == nil, didPushCursor {
  1375. NSCursor.pop()
  1376. didPushCursor = false
  1377. isHovering = false
  1378. }
  1379. }
  1380. private func applyChrome() {
  1381. // Same border + shadow metrics in every state — only color changes on select
  1382. // so thumbnails keep identical insets (stroke is drawn inside the layer).
  1383. let uniformBorder: CGFloat = 2
  1384. let borderColor: NSColor
  1385. if isSelected {
  1386. borderColor = palette.borderSelected
  1387. } else if isHovering {
  1388. borderColor = palette.borderHover
  1389. } else {
  1390. borderColor = palette.border
  1391. }
  1392. layer?.backgroundColor = palette.cardShellBackground.cgColor
  1393. previewSurface.layer?.backgroundColor = palette.previewSurface.cgColor
  1394. footerView.layer?.backgroundColor = palette.footerBackground.cgColor
  1395. nameLabel.textColor = palette.primaryText
  1396. categoryLabel.textColor = palette.secondaryText
  1397. layer?.borderColor = borderColor.cgColor
  1398. layer?.borderWidth = uniformBorder
  1399. layer?.shadowColor = NSColor.black.cgColor
  1400. layer?.shadowOpacity = isSelected ? 0.14 : (isHovering ? 0.11 : 0.08)
  1401. layer?.shadowRadius = 14
  1402. layer?.shadowOffset = CGSize(width: 0, height: 8)
  1403. }
  1404. }
  1405. // MARK: - Helpers
  1406. /// Flipped origin so the grid stacks fill from the top.
  1407. private final class TopFlippedView: NSView {
  1408. override var isFlipped: Bool { true }
  1409. }
  1410. /// Local copy of the dashboard's hoverable button so this file stays
  1411. /// self-contained without exposing the existing private classes.
  1412. private final class CVHoverableButton: NSButton {
  1413. var hoverHandler: ((Bool) -> Void)?
  1414. var pointerCursor: Bool = false
  1415. private(set) var isHovering: Bool = false
  1416. private var trackingArea: NSTrackingArea?
  1417. private var didPushCursor: Bool = false
  1418. override func updateTrackingAreas() {
  1419. super.updateTrackingAreas()
  1420. if let area = trackingArea { removeTrackingArea(area) }
  1421. let area = NSTrackingArea(
  1422. rect: bounds,
  1423. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1424. owner: self,
  1425. userInfo: nil
  1426. )
  1427. addTrackingArea(area)
  1428. trackingArea = area
  1429. }
  1430. override func mouseEntered(with event: NSEvent) {
  1431. super.mouseEntered(with: event)
  1432. isHovering = true
  1433. hoverHandler?(true)
  1434. if pointerCursor, !didPushCursor {
  1435. NSCursor.pointingHand.push()
  1436. didPushCursor = true
  1437. }
  1438. }
  1439. override func mouseExited(with event: NSEvent) {
  1440. super.mouseExited(with: event)
  1441. isHovering = false
  1442. hoverHandler?(false)
  1443. if didPushCursor {
  1444. NSCursor.pop()
  1445. didPushCursor = false
  1446. }
  1447. }
  1448. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1449. super.viewWillMove(toWindow: newWindow)
  1450. if newWindow == nil, didPushCursor {
  1451. NSCursor.pop()
  1452. didPushCursor = false
  1453. isHovering = false
  1454. }
  1455. }
  1456. }