Keine Beschreibung

CVMakerPageView.swift 56KB

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