Nenhuma descrição

CVMakerPageView.swift 64KB

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