Sin descripción

CVMakerPageView.swift 63KB

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