Brak opisu

CVMakerPageView.swift 63KB

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