Keine Beschreibung

DashboardView.swift 167KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682
  1. //
  2. // DashboardView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. import QuartzCore
  7. import Security
  8. private enum JobListingCardContext {
  9. case homeSearchResults
  10. case savedJobsPage
  11. }
  12. private enum PremiumSheetLayout {
  13. /// Grow the sheet past the host content rect on each side to hide compositing hairlines.
  14. static let overscanPerEdge: CGFloat = 2
  15. /// Additional growth on the top edge only (pt).
  16. static let overscanExtraTop: CGFloat = 0.5
  17. }
  18. final class DashboardView: NSView, NSTextFieldDelegate, NSSharingServicePickerDelegate, NSSharingServiceDelegate {
  19. /// Indeed.com-inspired neutrals and brand blue (white surfaces, `#2557a7` accent, `#2d2d2d` / `#767676` text, `#d4d2d0` borders).
  20. private enum Theme {
  21. static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  22. static let pageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  23. static let chromeBackground = NSColor(srgbRed: 247 / 255, green: 247 / 255, blue: 247 / 255, alpha: 1)
  24. static let sidebarBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  25. static let mainHostBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  26. /// Welcome hero (matches reference: `#0052CC` heading, `#334155` subline, `#EFF6FF` icon well).
  27. static let welcomeHeroHeadingBlue = NSColor(srgbRed: 0, green: 82 / 255, blue: 204 / 255, alpha: 1)
  28. static let welcomeHeroSubtitleText = NSColor(srgbRed: 51 / 255, green: 65 / 255, blue: 85 / 255, alpha: 1)
  29. static let welcomeHeroIconWell = NSColor(srgbRed: 239 / 255, green: 246 / 255, blue: 255 / 255, alpha: 1)
  30. /// Light decorative strokes / sparkles behind the welcome hero.
  31. static let welcomeHeroWaveTint = NSColor(srgbRed: 186 / 255, green: 210 / 255, blue: 253 / 255, alpha: 1)
  32. /// Subtitle on the welcome hero: dark neutral gray to match the reference layout.
  33. static let welcomeSubtitleText = NSColor(srgbRed: 64 / 255, green: 64 / 255, blue: 64 / 255, alpha: 1)
  34. static let selectionFill = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.12)
  35. static let cardBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  36. static let toggleBackground = NSColor(srgbRed: 232 / 255, green: 232 / 255, blue: 232 / 255, alpha: 1)
  37. static let primaryText = NSColor(srgbRed: 45 / 255, green: 45 / 255, blue: 45 / 255, alpha: 1)
  38. static let secondaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  39. static let tertiaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  40. static let border = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  41. static let featureIconWell = NSColor(srgbRed: 220 / 255, green: 235 / 255, blue: 252 / 255, alpha: 1)
  42. /// Job search bar outer stroke (soft blue-gray, pill field in the reference UI).
  43. static let searchBarBorder = NSColor(srgbRed: 180 / 255, green: 200 / 255, blue: 228 / 255, alpha: 1)
  44. /// Search bar border on hover (brand-tinted, matches focus affordance elsewhere).
  45. static let searchBarBorderHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.55)
  46. static let proCardFill = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  47. static let proCardBorder = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  48. static let proAccent = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  49. static let proCTABackground = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  50. static let proCTAText = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  51. /// Slightly lighter blue for the top of the search-bar “Find jobs” pill (reads less flat than a solid fill).
  52. static let findJobsCTAHighlight = NSColor(srgbRed: 54 / 255, green: 110 / 255, blue: 198 / 255, alpha: 1)
  53. /// Hover states: darker brand blue, deeper gradient top, stronger tints, and subtle neutral fills used across CTAs, toggles, and the sidebar.
  54. static let brandBlueHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  55. static let findJobsCTAHighlightHover = NSColor(srgbRed: 44 / 255, green: 94 / 255, blue: 178 / 255, alpha: 1)
  56. static let selectionFillHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.2)
  57. static let neutralHoverFill = NSColor(srgbRed: 240 / 255, green: 240 / 255, blue: 240 / 255, alpha: 1)
  58. static let sidebarRowHoverFill = NSColor(srgbRed: 0, green: 0, blue: 0, alpha: 0.04)
  59. static let settingsPageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  60. static let settingsGroupBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  61. static let settingsIconBackground = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  62. static let settingsDivider = NSColor(srgbRed: 228 / 255, green: 228 / 255, blue: 228 / 255, alpha: 1)
  63. }
  64. /// Multiline `NSTextField` often ignores `alignment` for wrapped runs; explicit paragraph alignment matches the title.
  65. private static func jobListingDescriptionAttributedString(_ plain: String) -> NSAttributedString {
  66. let paragraph = NSMutableParagraphStyle()
  67. paragraph.alignment = .left
  68. paragraph.lineBreakMode = .byWordWrapping
  69. paragraph.baseWritingDirection = .leftToRight
  70. let font = NSFont.systemFont(ofSize: 13, weight: .regular)
  71. return NSAttributedString(string: plain, attributes: [
  72. .font: font,
  73. .foregroundColor: Theme.secondaryText,
  74. .paragraphStyle: paragraph
  75. ])
  76. }
  77. /// Horizontal row for sidebar + main; plain view + constraints keep both panels top/bottom aligned (stack view height alignment was inconsistent).
  78. private let panelsRow = NSView()
  79. private let chromeContainer = NSView()
  80. private let sidebar = NSStackView()
  81. private let mainHost = NSView()
  82. private let mainOverlay = NSStackView()
  83. private let greetingLabel = NSTextField(labelWithString: "")
  84. private let subtitleLabel = NSTextField(labelWithString: "")
  85. private let searchBarShadowHost = NSView()
  86. private let searchCard = HoverableView()
  87. private let jobSearchIcon = NSImageView()
  88. private let jobKeywordsField = NSTextField()
  89. private let findJobsButton = HoverableButton()
  90. private let findJobsCTAHost = NSView()
  91. private let findJobsCTAChrome = HoverableView()
  92. private var findJobsCTAGradientLayer: CAGradientLayer?
  93. private let welcomeHeroHost = NSView()
  94. private let welcomeHeroBackgroundView = WelcomeHeroBackgroundView()
  95. private lazy var welcomeSparkleCluster: WelcomeSparkleClusterView = {
  96. WelcomeSparkleClusterView(iconWell: Theme.welcomeHeroIconWell, tint: Theme.welcomeHeroHeadingBlue)
  97. }()
  98. private let featureCardsRow = NSStackView()
  99. private let clearChatButton = NSButton(title: "Clear chat", target: nil, action: nil)
  100. private let chatScrollView = NSScrollView()
  101. private let chatDocumentView = JobListingsDocumentView()
  102. private let chatStack = NSStackView()
  103. /// Shown when a sidebar item other than Home is selected.
  104. private let nonHomeHost = NSView()
  105. /// Full-bleed Indeed apply / listing web view inside the main panel (same window as the dashboard).
  106. private let indeedJobBrowserHost = NSView()
  107. private let nonHomeGenericContainer = NSView()
  108. private let nonHomeTitleLabel = NSTextField(labelWithString: "")
  109. private let nonHomeSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  110. private let savedJobsPageContainer = NSView()
  111. private let savedJobsPageTitleLabel = NSTextField(labelWithString: "Saved Jobs")
  112. private let savedJobsPageSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  113. private let savedJobsScrollView = NSScrollView()
  114. private let savedJobsDocumentView = JobListingsDocumentView()
  115. private let savedJobsStack = NSStackView()
  116. private let settingsPageContainer = NSView()
  117. private let cvMakerPageContainer = NSView()
  118. private lazy var cvMakerPageView: CVMakerPageView = {
  119. CVMakerPageView()
  120. }()
  121. private let profilePageContainer = NSView()
  122. private lazy var profilesListPageView: ProfilesListPageView = {
  123. ProfilesListPageView()
  124. }()
  125. private lazy var myProfilePageView: MyProfilePageView = {
  126. MyProfilePageView()
  127. }()
  128. /// When true, `myProfilePageView` is visible instead of the profiles list.
  129. private var isProfileEditorPresented = false
  130. /// When true, the merged CV preview is visible instead of the profiles list or editor.
  131. private var isCVDocumentPreviewPresented = false
  132. /// Exact template chosen in CV Maker until the user leaves Profile or starts a new CV Maker hand-off (avoids re-resolving by id and picking a different row).
  133. private var pendingCVTemplate: CVTemplate?
  134. private let cvFilledPreviewPageView = CVFilledPreviewPageView()
  135. private var currentSidebarItems: [SidebarItem] = []
  136. private var selectedSidebarIndex: Int = 0
  137. /// All jobs that have been shown in the current chat session, oldest first. Used to deduplicate continuation searches so "show more" doesn't re-display the same listings.
  138. private var lastSearchResults: [JobListing] = []
  139. /// "Show more jobs" row under the latest assistant message that listed jobs; removed when a newer listing block replaces it.
  140. private var trailingLoadMoreJobsRow: NSView?
  141. private weak var trailingLoadMoreJobsButton: HoverableButton?
  142. /// Most recently saved jobs appear first; persisted across launches.
  143. private var savedJobOrder: [JobListing] = []
  144. private var chatMessages: [ChatMessage] = []
  145. private var isAwaitingResponse = false
  146. /// Shown under the latest user message while a job search request is in flight.
  147. private var chatThinkingRowHost: NSView?
  148. private let jobSearchService = OpenAIJobSearchService()
  149. private var premiumPlansWindowController: PremiumPlansWindowController?
  150. private var indeedJobBrowserViewController: IndeedJobBrowserViewController?
  151. private var isIndeedJobBrowserPresented = false
  152. private weak var sidebarUpgradeCard: NSView?
  153. private weak var sidebarUpgradeHeadline: NSTextField?
  154. private weak var sidebarUpgradeDescription: NSTextField?
  155. private weak var sidebarUpgradeButton: HoverableButton?
  156. private var subscriptionObserver: NSObjectProtocol?
  157. /// Retains the system share picker until the user picks a destination or dismisses the menu.
  158. private var appSharePicker: NSSharingServicePicker?
  159. /// Upper bound sent to the model per request (was fixed at 8 in the prompt). Clamped when calling the API.
  160. private static let jobsPerSearchDefault = 15
  161. private static let jobsPerSearchMin = 1
  162. private static let jobsPerSearchMaxCap = 25
  163. private static func clampedJobsPerRequest(_ requested: Int = jobsPerSearchDefault) -> Int {
  164. min(jobsPerSearchMaxCap, max(jobsPerSearchMin, requested))
  165. }
  166. override init(frame frameRect: NSRect) {
  167. super.init(frame: frameRect)
  168. setupLayout()
  169. }
  170. required init?(coder: NSCoder) {
  171. super.init(coder: coder)
  172. setupLayout()
  173. }
  174. deinit {
  175. if let subscriptionObserver {
  176. NotificationCenter.default.removeObserver(subscriptionObserver)
  177. }
  178. }
  179. override func viewDidMoveToWindow() {
  180. super.viewDidMoveToWindow()
  181. guard window != nil else { return }
  182. Task { @MainActor in
  183. await SubscriptionStore.shared.refreshEntitlements(deep: true)
  184. self.applyProSubscriptionToSidebar()
  185. }
  186. }
  187. override func layout() {
  188. super.layout()
  189. updateSearchBarShadowPath()
  190. findJobsCTAGradientLayer?.frame = findJobsCTAChrome.bounds
  191. updateFindJobsCTAShadowPath()
  192. updateJobListingDescriptionWidths()
  193. updateChatBubbleWidths()
  194. }
  195. func render(_ data: DashboardData) {
  196. dismissIndeedJobBrowserEmbedded()
  197. greetingLabel.stringValue = "Welcome"
  198. subtitleLabel.stringValue = data.subtitle
  199. currentSidebarItems = data.sidebarItems
  200. if selectedSidebarIndex >= currentSidebarItems.count {
  201. selectedSidebarIndex = max(0, currentSidebarItems.count - 1)
  202. }
  203. configureSidebar()
  204. savedJobOrder = Self.normalizedSavedJobs(SavedJobsStore.load())
  205. resetChatState()
  206. updateMainContentVisibility()
  207. }
  208. private func setupLayout() {
  209. wantsLayer = true
  210. layer?.backgroundColor = Theme.pageBackground.cgColor
  211. panelsRow.translatesAutoresizingMaskIntoConstraints = false
  212. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  213. chromeContainer.wantsLayer = true
  214. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  215. chromeContainer.layer?.cornerRadius = 18
  216. chromeContainer.layer?.masksToBounds = true
  217. addSubview(chromeContainer)
  218. chromeContainer.addSubview(panelsRow)
  219. sidebar.orientation = .vertical
  220. sidebar.spacing = 10
  221. sidebar.distribution = .fill
  222. sidebar.alignment = .leading
  223. sidebar.wantsLayer = true
  224. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  225. sidebar.layer?.cornerRadius = 16
  226. sidebar.edgeInsets = NSEdgeInsets(top: 16, left: 12, bottom: 16, right: 12)
  227. sidebar.translatesAutoresizingMaskIntoConstraints = false
  228. mainHost.translatesAutoresizingMaskIntoConstraints = false
  229. mainHost.wantsLayer = true
  230. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  231. mainHost.layer?.cornerRadius = 16
  232. mainHost.layer?.masksToBounds = true
  233. sidebar.setContentHuggingPriority(.required, for: .horizontal)
  234. mainHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  235. mainHost.addSubview(mainOverlay)
  236. configureNonHomePlaceholder()
  237. mainHost.addSubview(nonHomeHost)
  238. indeedJobBrowserHost.translatesAutoresizingMaskIntoConstraints = false
  239. indeedJobBrowserHost.wantsLayer = true
  240. indeedJobBrowserHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  241. indeedJobBrowserHost.isHidden = true
  242. mainHost.addSubview(indeedJobBrowserHost)
  243. mainOverlay.orientation = .vertical
  244. mainOverlay.spacing = 0
  245. mainOverlay.alignment = .centerX
  246. mainOverlay.distribution = .fill
  247. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  248. mainOverlay.setContentHuggingPriority(.defaultLow, for: .vertical)
  249. greetingLabel.font = .systemFont(ofSize: 28, weight: .bold)
  250. greetingLabel.textColor = Theme.welcomeHeroHeadingBlue
  251. greetingLabel.alignment = .center
  252. greetingLabel.maximumNumberOfLines = 1
  253. subtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  254. subtitleLabel.textColor = Theme.welcomeHeroSubtitleText
  255. subtitleLabel.alignment = .center
  256. subtitleLabel.maximumNumberOfLines = 2
  257. let topInset = NSView()
  258. topInset.translatesAutoresizingMaskIntoConstraints = false
  259. topInset.heightAnchor.constraint(equalToConstant: 12).isActive = true
  260. configureSearchBar()
  261. configureChatViews()
  262. welcomeHeroHost.translatesAutoresizingMaskIntoConstraints = false
  263. welcomeHeroBackgroundView.translatesAutoresizingMaskIntoConstraints = false
  264. welcomeHeroBackgroundView.waveTint = Theme.welcomeHeroWaveTint
  265. let welcomeHeroContent = NSStackView(views: [welcomeSparkleCluster, greetingLabel, subtitleLabel])
  266. welcomeHeroContent.orientation = .vertical
  267. welcomeHeroContent.spacing = 8
  268. welcomeHeroContent.alignment = .centerX
  269. welcomeHeroContent.translatesAutoresizingMaskIntoConstraints = false
  270. welcomeHeroHost.addSubview(welcomeHeroBackgroundView)
  271. welcomeHeroHost.addSubview(welcomeHeroContent)
  272. welcomeHeroHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  273. NSLayoutConstraint.activate([
  274. welcomeHeroBackgroundView.leadingAnchor.constraint(equalTo: welcomeHeroHost.leadingAnchor),
  275. welcomeHeroBackgroundView.trailingAnchor.constraint(equalTo: welcomeHeroHost.trailingAnchor),
  276. welcomeHeroBackgroundView.topAnchor.constraint(equalTo: welcomeHeroHost.topAnchor),
  277. welcomeHeroBackgroundView.bottomAnchor.constraint(equalTo: welcomeHeroHost.bottomAnchor),
  278. welcomeHeroContent.centerXAnchor.constraint(equalTo: welcomeHeroHost.centerXAnchor),
  279. welcomeHeroContent.leadingAnchor.constraint(greaterThanOrEqualTo: welcomeHeroHost.leadingAnchor, constant: 16),
  280. welcomeHeroContent.trailingAnchor.constraint(lessThanOrEqualTo: welcomeHeroHost.trailingAnchor, constant: -16),
  281. welcomeHeroContent.topAnchor.constraint(equalTo: welcomeHeroHost.topAnchor, constant: 4),
  282. welcomeHeroContent.bottomAnchor.constraint(equalTo: welcomeHeroHost.bottomAnchor, constant: -2)
  283. ])
  284. configureFeatureShortcutCards()
  285. featureCardsRow.setContentHuggingPriority(.defaultHigh, for: .vertical)
  286. let heroCardsSpacer = NSView()
  287. heroCardsSpacer.translatesAutoresizingMaskIntoConstraints = false
  288. heroCardsSpacer.heightAnchor.constraint(equalToConstant: 14).isActive = true
  289. let midSpacer = NSView()
  290. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  291. midSpacer.heightAnchor.constraint(equalToConstant: 6).isActive = true
  292. let chatTopSpacer = NSView()
  293. chatTopSpacer.translatesAutoresizingMaskIntoConstraints = false
  294. chatTopSpacer.heightAnchor.constraint(equalToConstant: 8).isActive = true
  295. let chatBottomSpacer = NSView()
  296. chatBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  297. chatBottomSpacer.heightAnchor.constraint(equalToConstant: 8).isActive = true
  298. mainOverlay.addArrangedSubview(topInset)
  299. mainOverlay.addArrangedSubview(welcomeHeroHost)
  300. mainOverlay.addArrangedSubview(heroCardsSpacer)
  301. mainOverlay.addArrangedSubview(featureCardsRow)
  302. mainOverlay.addArrangedSubview(midSpacer)
  303. mainOverlay.addArrangedSubview(chatTopSpacer)
  304. let chatHeaderRow = NSStackView()
  305. chatHeaderRow.orientation = .horizontal
  306. chatHeaderRow.spacing = 8
  307. chatHeaderRow.alignment = .centerY
  308. chatHeaderRow.distribution = .fill
  309. chatHeaderRow.translatesAutoresizingMaskIntoConstraints = false
  310. let chatHeaderLeadingSpacer = NSView()
  311. chatHeaderLeadingSpacer.translatesAutoresizingMaskIntoConstraints = false
  312. chatHeaderLeadingSpacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  313. clearChatButton.translatesAutoresizingMaskIntoConstraints = false
  314. clearChatButton.bezelStyle = .rounded
  315. clearChatButton.font = .systemFont(ofSize: 12, weight: .medium)
  316. clearChatButton.target = self
  317. clearChatButton.action = #selector(didTapClearChat)
  318. clearChatButton.toolTip = "Remove all messages and start a new conversation"
  319. clearChatButton.setContentHuggingPriority(.required, for: .horizontal)
  320. chatHeaderRow.addArrangedSubview(chatHeaderLeadingSpacer)
  321. chatHeaderRow.addArrangedSubview(clearChatButton)
  322. mainOverlay.addArrangedSubview(chatHeaderRow)
  323. mainOverlay.addArrangedSubview(chatScrollView)
  324. mainOverlay.addArrangedSubview(chatBottomSpacer)
  325. mainOverlay.addArrangedSubview(searchBarShadowHost)
  326. panelsRow.addSubview(sidebar)
  327. panelsRow.addSubview(mainHost)
  328. NSLayoutConstraint.activate([
  329. chromeContainer.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12),
  330. chromeContainer.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12),
  331. chromeContainer.topAnchor.constraint(equalTo: topAnchor, constant: 10),
  332. chromeContainer.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12),
  333. panelsRow.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor, constant: 12),
  334. panelsRow.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor, constant: -20),
  335. panelsRow.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 10),
  336. panelsRow.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor, constant: -20),
  337. sidebar.leadingAnchor.constraint(equalTo: panelsRow.leadingAnchor),
  338. sidebar.topAnchor.constraint(equalTo: panelsRow.topAnchor),
  339. sidebar.bottomAnchor.constraint(equalTo: panelsRow.bottomAnchor),
  340. sidebar.widthAnchor.constraint(equalToConstant: 218),
  341. mainHost.leadingAnchor.constraint(equalTo: sidebar.trailingAnchor, constant: 10),
  342. mainHost.trailingAnchor.constraint(equalTo: panelsRow.trailingAnchor),
  343. mainHost.topAnchor.constraint(equalTo: panelsRow.topAnchor),
  344. mainHost.bottomAnchor.constraint(equalTo: panelsRow.bottomAnchor),
  345. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  346. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  347. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  348. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  349. mainOverlay.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  350. nonHomeHost.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  351. nonHomeHost.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  352. nonHomeHost.topAnchor.constraint(equalTo: mainHost.topAnchor),
  353. nonHomeHost.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  354. indeedJobBrowserHost.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  355. indeedJobBrowserHost.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  356. indeedJobBrowserHost.topAnchor.constraint(equalTo: mainHost.topAnchor),
  357. indeedJobBrowserHost.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  358. searchBarShadowHost.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  359. featureCardsRow.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  360. chatHeaderRow.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  361. chatScrollView.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  362. welcomeHeroHost.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor),
  363. welcomeHeroHost.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor),
  364. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 16),
  365. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -16),
  366. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  367. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor),
  368. welcomeHeroContent.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, constant: -32)
  369. ])
  370. registerSubscriptionObserverOnce()
  371. }
  372. private func registerSubscriptionObserverOnce() {
  373. guard subscriptionObserver == nil else { return }
  374. subscriptionObserver = NotificationCenter.default.addObserver(
  375. forName: .subscriptionStatusDidChange,
  376. object: nil,
  377. queue: .main
  378. ) { [weak self] _ in
  379. self?.applyProSubscriptionToSidebar()
  380. }
  381. }
  382. private func applyProSubscriptionToSidebar() {
  383. let active = SubscriptionStore.shared.isProActive
  384. sidebarUpgradeCard?.isHidden = false
  385. guard let headline = sidebarUpgradeHeadline,
  386. let upgradeDescription = sidebarUpgradeDescription,
  387. let upgradeButton = sidebarUpgradeButton else { return }
  388. let descriptionWidth: CGFloat = 158
  389. if active {
  390. headline.stringValue = "You're on Pro"
  391. upgradeDescription.stringValue = "Manage billing, renewals, and plans in Premium."
  392. upgradeDescription.preferredMaxLayoutWidth = descriptionWidth
  393. upgradeButton.title = "Manage Subscription"
  394. } else {
  395. headline.stringValue = "Upgrade to Pro"
  396. upgradeDescription.stringValue = "Unlimited AI matches, smart alerts, and interview prep—all in one place."
  397. upgradeDescription.preferredMaxLayoutWidth = descriptionWidth
  398. upgradeButton.title = "Try Pro"
  399. }
  400. }
  401. private func presentPremiumPlansSheet() {
  402. guard let hostWindow = window else { return }
  403. if premiumPlansWindowController == nil {
  404. premiumPlansWindowController = PremiumPlansWindowController()
  405. }
  406. guard let paywallWindow = premiumPlansWindowController?.window else { return }
  407. if hostWindow.attachedSheet === paywallWindow {
  408. return
  409. }
  410. paywallWindow.styleMask = [.borderless, .closable, .resizable]
  411. paywallWindow.isOpaque = true
  412. paywallWindow.backgroundColor = PremiumPlansWindowController.paywallSheetBackground
  413. let hostContentRect = hostWindow.contentRect(forFrameRect: hostWindow.frame)
  414. let overscan = PremiumSheetLayout.overscanPerEdge
  415. var expandedContentRect = hostContentRect.insetBy(dx: -overscan, dy: -overscan)
  416. expandedContentRect.size.height += PremiumSheetLayout.overscanExtraTop
  417. let paywallFrame = paywallWindow.frameRect(forContentRect: expandedContentRect)
  418. paywallWindow.setFrame(paywallFrame, display: false)
  419. let lockedSize = paywallWindow.frame.size
  420. paywallWindow.minSize = lockedSize
  421. paywallWindow.maxSize = lockedSize
  422. hostWindow.beginSheet(paywallWindow)
  423. }
  424. private func configureFeatureShortcutCards() {
  425. featureCardsRow.orientation = .horizontal
  426. featureCardsRow.spacing = 16
  427. featureCardsRow.distribution = .fillEqually
  428. featureCardsRow.alignment = .top
  429. featureCardsRow.translatesAutoresizingMaskIntoConstraints = false
  430. let specs: [(symbol: String, title: String, subtitle: String, action: Selector)] = [
  431. ("briefcase", "Role", "Explore similar or better job roles", #selector(didTapFeatureRole)),
  432. ("building.2", "Company", "Find opportunities at other companies", #selector(didTapFeatureCompany)),
  433. ("chevron.left.forwardslash.chevron.right", "Skill", "Match jobs that fit your skills", #selector(didTapFeatureSkill))
  434. ]
  435. for spec in specs {
  436. let card = FeatureShortcutCardView(
  437. symbolName: spec.symbol,
  438. title: spec.title,
  439. subtitle: spec.subtitle,
  440. target: self,
  441. action: spec.action
  442. )
  443. featureCardsRow.addArrangedSubview(card)
  444. }
  445. }
  446. private func configureChatViews() {
  447. chatDocumentView.translatesAutoresizingMaskIntoConstraints = false
  448. chatStack.orientation = .vertical
  449. chatStack.spacing = 18
  450. chatStack.alignment = .width
  451. chatStack.distribution = .fill
  452. chatStack.translatesAutoresizingMaskIntoConstraints = false
  453. chatDocumentView.addSubview(chatStack)
  454. NSLayoutConstraint.activate([
  455. chatStack.leadingAnchor.constraint(equalTo: chatDocumentView.leadingAnchor, constant: 4),
  456. chatStack.trailingAnchor.constraint(equalTo: chatDocumentView.trailingAnchor, constant: -4),
  457. chatStack.topAnchor.constraint(equalTo: chatDocumentView.topAnchor, constant: 8),
  458. chatStack.bottomAnchor.constraint(equalTo: chatDocumentView.bottomAnchor, constant: -8)
  459. ])
  460. chatScrollView.translatesAutoresizingMaskIntoConstraints = false
  461. chatScrollView.hasVerticalScroller = true
  462. chatScrollView.hasHorizontalScroller = false
  463. // Legacy reserves a dedicated track to the right of the clip view so the thumb never sits on top of cards/buttons.
  464. chatScrollView.scrollerStyle = .legacy
  465. chatScrollView.autohidesScrollers = true
  466. chatScrollView.drawsBackground = false
  467. chatScrollView.borderType = .noBorder
  468. chatScrollView.documentView = chatDocumentView
  469. chatScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  470. chatScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  471. // Match Saved Jobs: pin document width to the clip view so cards and bubbles track window width instead of sticking to a narrow intrinsic width.
  472. NSLayoutConstraint.activate([
  473. chatDocumentView.topAnchor.constraint(equalTo: chatScrollView.contentView.topAnchor),
  474. chatDocumentView.leadingAnchor.constraint(equalTo: chatScrollView.contentView.leadingAnchor),
  475. chatDocumentView.widthAnchor.constraint(equalTo: chatScrollView.contentView.widthAnchor)
  476. ])
  477. }
  478. private var prefersReducedMotion: Bool {
  479. NSWorkspace.shared.accessibilityDisplayShouldReduceMotion
  480. }
  481. private func updateJobListingDescriptionWidths() {
  482. updateDescriptionColumnWidths(in: savedJobsStack, containerWidth: savedJobsDocumentView.bounds.width)
  483. walkChatJobStacks { stack in
  484. updateDescriptionColumnWidths(in: stack, containerWidth: stack.bounds.width)
  485. }
  486. }
  487. private func walkChatJobStacks(_ visitor: (ChatJobsStackView) -> Void) {
  488. func walk(_ view: NSView) {
  489. if let stack = view as? ChatJobsStackView {
  490. visitor(stack)
  491. }
  492. for sub in view.subviews { walk(sub) }
  493. }
  494. walk(chatStack)
  495. }
  496. /// Chat bubble text fields are wrapping labels whose `preferredMaxLayoutWidth` needs to track the available row width so long strings reflow correctly when the window resizes.
  497. private func updateChatBubbleWidths() {
  498. func walk(_ view: NSView) {
  499. if let label = view as? ChatBubbleLabel,
  500. let bubble = label.superview, bubble.bounds.width > 1 {
  501. let target = max(40, bubble.bounds.width - 28)
  502. if abs(label.preferredMaxLayoutWidth - target) > 0.5 {
  503. label.preferredMaxLayoutWidth = target
  504. label.invalidateIntrinsicContentSize()
  505. }
  506. }
  507. for sub in view.subviews { walk(sub) }
  508. }
  509. walk(chatStack)
  510. }
  511. private func updateDescriptionColumnWidths(in stack: NSStackView, containerWidth: CGFloat) {
  512. guard containerWidth > 1 else { return }
  513. // Matches `contentColumn` insets on the card (16 leading + 16 trailing). The description spans the full row below the buttons, so never subtract a “button strip” here — a too-narrow `preferredMaxLayoutWidth` inside a wider field makes wrapped `NSTextField` text lay out like a trailing-aligned band after resizes.
  514. let contentHorizontalInset: CGFloat = 32
  515. var didChange = false
  516. for card in stack.arrangedSubviews {
  517. guard let desc = card.viewWithTag(502) as? NSTextField else { continue }
  518. let cardWidth = card.bounds.width > 1 ? card.bounds.width : containerWidth
  519. let fallbackColumn = max(1, cardWidth - contentHorizontalInset)
  520. let columnWidth: CGFloat
  521. if desc.bounds.width > 1 {
  522. columnWidth = desc.bounds.width
  523. } else if let column = desc.superview, column.bounds.width > 1 {
  524. columnWidth = column.bounds.width
  525. } else {
  526. columnWidth = fallbackColumn
  527. }
  528. if abs(desc.preferredMaxLayoutWidth - columnWidth) > 0.5 {
  529. desc.preferredMaxLayoutWidth = columnWidth
  530. desc.invalidateIntrinsicContentSize()
  531. didChange = true
  532. }
  533. }
  534. if didChange {
  535. stack.needsLayout = true
  536. }
  537. }
  538. private static func normalizedSavedJobs(_ jobs: [JobListing]) -> [JobListing] {
  539. var seen = Set<JobListing>()
  540. var out: [JobListing] = []
  541. for job in jobs where seen.insert(job).inserted {
  542. out.append(job)
  543. }
  544. return out
  545. }
  546. private func isJobSaved(_ job: JobListing) -> Bool {
  547. savedJobOrder.contains(job)
  548. }
  549. private func persistSavedJobs() {
  550. SavedJobsStore.save(savedJobOrder)
  551. }
  552. private func applySavedState(_ saved: Bool, for job: JobListing) {
  553. if saved {
  554. savedJobOrder.removeAll { $0 == job }
  555. savedJobOrder.insert(job, at: 0)
  556. } else {
  557. savedJobOrder.removeAll { $0 == job }
  558. }
  559. persistSavedJobs()
  560. }
  561. private func jobListingHostSubtitle(_ job: JobListing) -> String {
  562. guard let raw = job.url, let url = URL(string: raw), let host = url.host?.lowercased() else {
  563. return "Indeed"
  564. }
  565. if host.hasPrefix("www.") {
  566. return String(host.dropFirst(4))
  567. }
  568. return host
  569. }
  570. private func jobListingCategorySymbol(for job: JobListing) -> String {
  571. let blob = (job.title + " " + job.description).lowercased()
  572. if blob.contains("machine learning") || blob.contains("deep learning") || blob.contains(" ml ") {
  573. return "brain.head.profile"
  574. }
  575. if blob.contains("audio") || blob.contains(" sound ") || blob.contains("dsp") {
  576. return "waveform"
  577. }
  578. if blob.contains("ios") || blob.contains("swift") || blob.contains("mobile") {
  579. return "iphone"
  580. }
  581. if blob.contains("design") || blob.contains(" ux") || blob.contains("figma") {
  582. return "paintpalette.fill"
  583. }
  584. if blob.contains("data ") || blob.contains("analytics") {
  585. return "chart.bar.fill"
  586. }
  587. if blob.contains("ai") || blob.contains("llm") || blob.contains("nlp") {
  588. return "cpu"
  589. }
  590. return "briefcase.fill"
  591. }
  592. private func makeJobListingCard(_ job: JobListing, context: JobListingCardContext) -> NSView {
  593. let card = NSView()
  594. card.translatesAutoresizingMaskIntoConstraints = false
  595. card.wantsLayer = true
  596. card.layer?.backgroundColor = Theme.cardBackground.cgColor
  597. card.layer?.cornerRadius = 14
  598. card.layer?.borderWidth = 1
  599. card.layer?.borderColor = Theme.border.cgColor
  600. card.layer?.masksToBounds = true
  601. let iconBox = NSView()
  602. iconBox.translatesAutoresizingMaskIntoConstraints = false
  603. iconBox.wantsLayer = true
  604. iconBox.layer?.backgroundColor = Theme.brandBlue.cgColor
  605. iconBox.layer?.cornerRadius = 12
  606. if #available(macOS 11.0, *) {
  607. iconBox.layer?.cornerCurve = .continuous
  608. }
  609. let categoryIcon = NSImageView()
  610. categoryIcon.translatesAutoresizingMaskIntoConstraints = false
  611. categoryIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 22, weight: .medium)
  612. categoryIcon.image = NSImage(systemSymbolName: jobListingCategorySymbol(for: job), accessibilityDescription: nil)
  613. categoryIcon.contentTintColor = .white
  614. iconBox.addSubview(categoryIcon)
  615. let titleField = NSTextField(labelWithString: job.title)
  616. titleField.font = .systemFont(ofSize: 16, weight: .semibold)
  617. titleField.textColor = Theme.brandBlue
  618. titleField.maximumNumberOfLines = 2
  619. titleField.lineBreakMode = .byWordWrapping
  620. titleField.alignment = .left
  621. titleField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  622. titleField.translatesAutoresizingMaskIntoConstraints = false
  623. let buildingIcon = NSImageView()
  624. buildingIcon.translatesAutoresizingMaskIntoConstraints = false
  625. buildingIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .medium)
  626. buildingIcon.image = NSImage(systemSymbolName: "building.2.fill", accessibilityDescription: nil)
  627. buildingIcon.contentTintColor = Theme.welcomeSubtitleText
  628. let companyLabel = NSTextField(labelWithString: jobListingHostSubtitle(job))
  629. companyLabel.font = .systemFont(ofSize: 12, weight: .medium)
  630. companyLabel.textColor = Theme.welcomeSubtitleText
  631. companyLabel.maximumNumberOfLines = 1
  632. companyLabel.lineBreakMode = .byTruncatingTail
  633. companyLabel.translatesAutoresizingMaskIntoConstraints = false
  634. let companyRow = NSStackView(views: [buildingIcon, companyLabel])
  635. companyRow.orientation = .horizontal
  636. companyRow.spacing = 5
  637. companyRow.alignment = .centerY
  638. companyRow.translatesAutoresizingMaskIntoConstraints = false
  639. let descriptionField = NSTextField(wrappingLabelWithString: job.description)
  640. descriptionField.font = .systemFont(ofSize: 13, weight: .regular)
  641. descriptionField.textColor = Theme.secondaryText
  642. descriptionField.maximumNumberOfLines = 2
  643. descriptionField.lineBreakMode = .byWordWrapping
  644. descriptionField.alignment = .left
  645. descriptionField.baseWritingDirection = .leftToRight
  646. descriptionField.attributedStringValue = Self.jobListingDescriptionAttributedString(job.description)
  647. if let cell = descriptionField.cell as? NSTextFieldCell {
  648. cell.alignment = .left
  649. cell.wraps = true
  650. }
  651. descriptionField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  652. descriptionField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  653. descriptionField.tag = 502
  654. descriptionField.translatesAutoresizingMaskIntoConstraints = false
  655. let applyButton = JobPayloadButton(title: "Apply", target: self, action: #selector(didTapJobApply(_:)))
  656. applyButton.jobPayload = job
  657. applyButton.cardContext = context
  658. applyButton.isBordered = false
  659. applyButton.bezelStyle = .rounded
  660. applyButton.font = .systemFont(ofSize: 13, weight: .semibold)
  661. applyButton.wantsLayer = true
  662. applyButton.layer?.cornerRadius = 8
  663. applyButton.layer?.backgroundColor = Theme.brandBlue.cgColor
  664. applyButton.contentTintColor = Theme.proCTAText
  665. applyButton.focusRingType = .none
  666. applyButton.pointerCursor = true
  667. applyButton.hoverHandler = { [weak applyButton] hovering in
  668. applyButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.brandBlue).cgColor
  669. }
  670. applyButton.setContentHuggingPriority(.required, for: .horizontal)
  671. applyButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  672. let savedOn = isJobSaved(job)
  673. let savedButton = SaveJobPayloadButton(title: savedOn ? "Saved" : "Save", target: self, action: #selector(didTapJobSaved(_:)))
  674. savedButton.jobPayload = job
  675. savedButton.cardContext = context
  676. savedButton.setButtonType(.toggle)
  677. savedButton.isBordered = false
  678. savedButton.bezelStyle = .rounded
  679. savedButton.font = .systemFont(ofSize: 13, weight: .semibold)
  680. savedButton.image = NSImage(systemSymbolName: savedOn ? "heart.fill" : "heart", accessibilityDescription: nil)
  681. savedButton.imagePosition = .imageLeading
  682. savedButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  683. savedButton.focusRingType = .none
  684. savedButton.state = savedOn ? .on : .off
  685. savedButton.pointerCursor = true
  686. savedButton.hoverHandler = { [weak self, weak savedButton] _ in
  687. guard let savedButton = savedButton else { return }
  688. self?.styleJobSavedButton(savedButton)
  689. }
  690. styleJobSavedButton(savedButton)
  691. savedButton.setContentHuggingPriority(.required, for: .horizontal)
  692. savedButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  693. let dismissButton = JobPayloadButton()
  694. dismissButton.jobPayload = job
  695. dismissButton.cardContext = context
  696. dismissButton.image = NSImage(systemSymbolName: "xmark", accessibilityDescription: "Dismiss")
  697. dismissButton.imagePosition = .imageOnly
  698. dismissButton.imageScaling = .scaleProportionallyDown
  699. dismissButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
  700. dismissButton.isBordered = false
  701. dismissButton.bezelStyle = .rounded
  702. dismissButton.contentTintColor = Theme.secondaryText
  703. dismissButton.target = self
  704. dismissButton.action = #selector(didTapJobDismiss(_:))
  705. dismissButton.toolTip = context == .savedJobsPage ? "Remove from saved" : "Dismiss"
  706. dismissButton.focusRingType = .none
  707. dismissButton.wantsLayer = true
  708. dismissButton.layer?.cornerRadius = 8
  709. dismissButton.layer?.backgroundColor = NSColor.clear.cgColor
  710. dismissButton.pointerCursor = true
  711. dismissButton.hoverHandler = { [weak dismissButton] hovering in
  712. dismissButton?.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : NSColor.clear).cgColor
  713. dismissButton?.contentTintColor = hovering ? Theme.primaryText : Theme.secondaryText
  714. }
  715. dismissButton.setContentHuggingPriority(.required, for: .horizontal)
  716. let buttonRow = NSStackView(views: [applyButton, savedButton, dismissButton])
  717. buttonRow.orientation = .horizontal
  718. buttonRow.spacing = 8
  719. buttonRow.alignment = .top
  720. buttonRow.translatesAutoresizingMaskIntoConstraints = false
  721. buttonRow.setContentHuggingPriority(.required, for: .horizontal)
  722. buttonRow.setContentCompressionResistancePriority(.required, for: .horizontal)
  723. buttonRow.setContentHuggingPriority(.required, for: .vertical)
  724. buttonRow.setContentCompressionResistancePriority(.required, for: .vertical)
  725. applyButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  726. savedButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  727. dismissButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  728. let middleColumn = NSStackView(views: [titleField, companyRow, descriptionField])
  729. middleColumn.orientation = .vertical
  730. middleColumn.spacing = 5
  731. middleColumn.alignment = .leading
  732. middleColumn.translatesAutoresizingMaskIntoConstraints = false
  733. middleColumn.setContentHuggingPriority(.defaultLow, for: .horizontal)
  734. middleColumn.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  735. let contentRow = NSStackView(views: [iconBox, middleColumn])
  736. contentRow.orientation = .horizontal
  737. contentRow.spacing = 14
  738. contentRow.alignment = .top
  739. contentRow.distribution = .fill
  740. contentRow.translatesAutoresizingMaskIntoConstraints = false
  741. card.addSubview(contentRow)
  742. card.addSubview(buttonRow)
  743. let actionCornerInset: CGFloat = 8
  744. let contentToActionsGap: CGFloat = 12
  745. let bodyTrailingInset: CGFloat = 16
  746. NSLayoutConstraint.activate([
  747. contentRow.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
  748. contentRow.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -bodyTrailingInset),
  749. contentRow.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
  750. contentRow.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -14),
  751. buttonRow.topAnchor.constraint(equalTo: card.topAnchor, constant: actionCornerInset),
  752. buttonRow.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -actionCornerInset),
  753. middleColumn.trailingAnchor.constraint(lessThanOrEqualTo: buttonRow.leadingAnchor, constant: -contentToActionsGap),
  754. iconBox.widthAnchor.constraint(equalToConstant: 58),
  755. iconBox.heightAnchor.constraint(equalToConstant: 58),
  756. categoryIcon.centerXAnchor.constraint(equalTo: iconBox.centerXAnchor),
  757. categoryIcon.centerYAnchor.constraint(equalTo: iconBox.centerYAnchor),
  758. buildingIcon.widthAnchor.constraint(equalToConstant: 14),
  759. buildingIcon.heightAnchor.constraint(equalToConstant: 14),
  760. applyButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 76),
  761. applyButton.heightAnchor.constraint(equalToConstant: 32),
  762. savedButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 84),
  763. savedButton.heightAnchor.constraint(equalToConstant: 32),
  764. dismissButton.widthAnchor.constraint(equalToConstant: 32),
  765. dismissButton.heightAnchor.constraint(equalToConstant: 32),
  766. descriptionField.widthAnchor.constraint(equalTo: middleColumn.widthAnchor)
  767. ])
  768. return card
  769. }
  770. private func styleJobSavedButton(_ button: NSButton) {
  771. button.wantsLayer = true
  772. button.layer?.cornerRadius = 10
  773. let hovering = (button as? HoverableButton)?.isHovering ?? false
  774. // Reference: white surface, soft blue outline, brand blue icon + label (no tinted fill on hover).
  775. button.layer?.backgroundColor = Theme.cardBackground.cgColor
  776. button.layer?.borderWidth = 1
  777. button.layer?.borderColor = (hovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  778. button.contentTintColor = Theme.brandBlue
  779. }
  780. @objc private func didTapJobApply(_ sender: NSButton) {
  781. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  782. presentIndeedJobBrowser(url: Self.resolvedIndeedApplyURL(for: job))
  783. }
  784. /// Apply always loads a live Indeed page. Single-job URLs (`/viewjob`, `/pagead`, `/rc/clk`, …) are often expired or incorrectly synthesized by the model and show Indeed’s 404; we only trust **search** URLs whose path is `/jobs`. Otherwise we open a fresh `/jobs?q=<title>` (same regional host when the model provided one).
  785. private static func resolvedIndeedApplyURL(for job: JobListing) -> URL {
  786. let title = job.title.trimmingCharacters(in: .whitespacesAndNewlines)
  787. let raw = job.url?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
  788. var preferredHost: String?
  789. if !raw.isEmpty, let components = URLComponents(string: raw), let host = components.host {
  790. let lower = host.lowercased()
  791. if isIndeedApplyHost(lower) {
  792. preferredHost = host
  793. let pathLower = components.path.lowercased()
  794. let isJobsSearchPath = pathLower == "/jobs" || pathLower.hasSuffix("/jobs")
  795. if isJobsSearchPath, let url = components.url {
  796. return url
  797. }
  798. }
  799. }
  800. return indeedJobsSearchURL(title: title, preferredHost: preferredHost)
  801. }
  802. private static func indeedJobsSearchURL(title: String, preferredHost: String?) -> URL {
  803. let allowed = CharacterSet.urlQueryAllowed
  804. let q = title.addingPercentEncoding(withAllowedCharacters: allowed) ?? ""
  805. let host: String
  806. if let preferredHost, isIndeedApplyHost(preferredHost.lowercased()) {
  807. host = preferredHost
  808. } else {
  809. host = "www.indeed.com"
  810. }
  811. if let url = URL(string: "https://\(host)/jobs?q=\(q)") {
  812. return url
  813. }
  814. return URL(string: "https://www.indeed.com/jobs?q=\(q)")!
  815. }
  816. private static func isIndeedApplyHost(_ host: String) -> Bool {
  817. if host == "indeed.com" { return true }
  818. if host.hasPrefix("indeed.") { return true }
  819. return host.contains(".indeed.")
  820. }
  821. private func presentIndeedJobBrowser(url: URL) {
  822. guard let parentVC = hostingViewController else { return }
  823. if indeedJobBrowserViewController == nil {
  824. let vc = IndeedJobBrowserViewController()
  825. vc.onDismissEmbedded = { [weak self] in
  826. self?.dismissIndeedJobBrowserEmbedded()
  827. }
  828. vc.embed(in: indeedJobBrowserHost, parent: parentVC)
  829. indeedJobBrowserViewController = vc
  830. }
  831. indeedJobBrowserViewController?.loadPage(url)
  832. isIndeedJobBrowserPresented = true
  833. updateMainContentVisibility()
  834. }
  835. private func dismissIndeedJobBrowserEmbedded() {
  836. guard isIndeedJobBrowserPresented else { return }
  837. isIndeedJobBrowserPresented = false
  838. updateMainContentVisibility()
  839. }
  840. private var hostingViewController: NSViewController? {
  841. var responder: NSResponder? = self
  842. while let current = responder {
  843. if let viewController = current as? NSViewController {
  844. return viewController
  845. }
  846. responder = current.nextResponder
  847. }
  848. return nil
  849. }
  850. @objc private func didTapJobSaved(_ sender: NSButton) {
  851. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  852. let willSave = !isJobSaved(job)
  853. applySavedState(willSave, for: job)
  854. sender.state = willSave ? .on : .off
  855. sender.title = willSave ? "Saved" : "Save"
  856. sender.image = NSImage(systemSymbolName: willSave ? "heart.fill" : "heart", accessibilityDescription: nil)
  857. styleJobSavedButton(sender)
  858. if isSavedJobsSidebarIndex(selectedSidebarIndex) {
  859. reloadSavedJobsListings()
  860. }
  861. }
  862. @objc private func didTapJobDismiss(_ sender: NSButton) {
  863. guard let button = sender as? JobPayloadButton, let job = button.jobPayload else { return }
  864. switch button.cardContext {
  865. case .homeSearchResults:
  866. removeJobCardFromChat(originating: button, job: job)
  867. case .savedJobsPage:
  868. applySavedState(false, for: job)
  869. reloadSavedJobsListings()
  870. }
  871. }
  872. /// Walks up from a dismiss button until it finds the enclosing chat job stack, then removes only the card that owns the button. Other chat history (older searches, the assistant summary text) is untouched.
  873. private func removeJobCardFromChat(originating button: NSView, job: JobListing) {
  874. var node: NSView? = button
  875. var card: NSView?
  876. var stack: ChatJobsStackView?
  877. while let v = node {
  878. if let parent = v.superview as? ChatJobsStackView {
  879. card = v
  880. stack = parent
  881. break
  882. }
  883. node = v.superview
  884. }
  885. guard let card, let stack else { return }
  886. stack.removeArrangedSubview(card)
  887. card.removeFromSuperview()
  888. lastSearchResults.removeAll { $0 == job }
  889. }
  890. private func configureSearchBar() {
  891. let pillCorner: CGFloat = 27
  892. let barHeight: CGFloat = 54
  893. searchBarShadowHost.translatesAutoresizingMaskIntoConstraints = false
  894. searchBarShadowHost.wantsLayer = true
  895. searchBarShadowHost.layer?.masksToBounds = false
  896. searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(0.18).cgColor
  897. searchBarShadowHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  898. searchBarShadowHost.layer?.shadowRadius = 10
  899. searchBarShadowHost.layer?.shadowOpacity = 1
  900. searchBarShadowHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  901. searchCard.translatesAutoresizingMaskIntoConstraints = false
  902. searchCard.wantsLayer = true
  903. searchCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  904. searchCard.layer?.cornerRadius = pillCorner
  905. searchCard.layer?.borderWidth = 1
  906. searchCard.layer?.borderColor = Theme.searchBarBorder.cgColor
  907. searchCard.layer?.masksToBounds = true
  908. searchCard.hoverHandler = { [weak self] hovering in
  909. guard let self else { return }
  910. CATransaction.begin()
  911. CATransaction.setAnimationDuration(0.15)
  912. self.searchCard.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  913. self.searchCard.layer?.borderColor = (hovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  914. self.searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(hovering ? 0.24 : 0.18).cgColor
  915. self.searchBarShadowHost.layer?.shadowRadius = hovering ? 12 : 10
  916. CATransaction.commit()
  917. }
  918. searchBarShadowHost.addSubview(searchCard)
  919. func configureField(_ field: NSTextField, placeholder: String) {
  920. field.translatesAutoresizingMaskIntoConstraints = false
  921. field.isBordered = false
  922. field.drawsBackground = false
  923. field.focusRingType = .none
  924. field.font = .systemFont(ofSize: 14, weight: .regular)
  925. field.textColor = Theme.primaryText
  926. field.delegate = self
  927. field.placeholderAttributedString = NSAttributedString(
  928. string: placeholder,
  929. attributes: [
  930. .foregroundColor: Theme.secondaryText,
  931. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  932. ]
  933. )
  934. field.cell?.usesSingleLineMode = true
  935. field.cell?.wraps = false
  936. field.cell?.isScrollable = true
  937. field.target = self
  938. field.action = #selector(didSubmitSearch)
  939. }
  940. jobSearchIcon.translatesAutoresizingMaskIntoConstraints = false
  941. jobSearchIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 16, weight: .semibold)
  942. jobSearchIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: "Ask AI")
  943. jobSearchIcon.contentTintColor = Theme.brandBlue
  944. configureField(jobKeywordsField, placeholder: "Ask for roles, skills, salary, or job descriptions...")
  945. let ctaHeight: CGFloat = 42
  946. let ctaCorner = ctaHeight / 2
  947. findJobsCTAHost.translatesAutoresizingMaskIntoConstraints = false
  948. findJobsCTAHost.wantsLayer = true
  949. findJobsCTAHost.layer?.masksToBounds = false
  950. findJobsCTAHost.layer?.shadowColor = NSColor.black.cgColor
  951. findJobsCTAHost.layer?.shadowOpacity = 0.16
  952. findJobsCTAHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  953. findJobsCTAHost.layer?.shadowRadius = 6
  954. findJobsCTAChrome.translatesAutoresizingMaskIntoConstraints = false
  955. findJobsCTAChrome.wantsLayer = true
  956. findJobsCTAChrome.layer?.masksToBounds = true
  957. findJobsCTAChrome.layer?.cornerRadius = ctaCorner
  958. if #available(macOS 11.0, *) {
  959. findJobsCTAChrome.layer?.cornerCurve = .continuous
  960. }
  961. let gradient = CAGradientLayer()
  962. gradient.colors = [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  963. gradient.startPoint = CGPoint(x: 0.5, y: 1)
  964. gradient.endPoint = CGPoint(x: 0.5, y: 0)
  965. findJobsCTAChrome.layer?.addSublayer(gradient)
  966. findJobsCTAGradientLayer = gradient
  967. // Tracks hover over the full pill (the button only covers an inset area), so the gradient darkens whenever the mouse is anywhere over the CTA.
  968. findJobsCTAChrome.pointerCursor = true
  969. findJobsCTAChrome.hoverHandler = { [weak self] hovering in
  970. guard let layer = self?.findJobsCTAGradientLayer else { return }
  971. CATransaction.begin()
  972. CATransaction.setAnimationDuration(0.15)
  973. layer.colors = hovering
  974. ? [Theme.findJobsCTAHighlightHover.cgColor, Theme.brandBlueHover.cgColor]
  975. : [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  976. CATransaction.commit()
  977. }
  978. findJobsButton.translatesAutoresizingMaskIntoConstraints = false
  979. findJobsButton.title = ""
  980. findJobsButton.image = NSImage(systemSymbolName: "paperplane.fill", accessibilityDescription: nil)
  981. findJobsButton.imagePosition = .imageLeading
  982. findJobsButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  983. findJobsButton.attributedTitle = NSAttributedString(
  984. string: " Send",
  985. attributes: [
  986. .font: NSFont.systemFont(ofSize: 14, weight: .semibold),
  987. .foregroundColor: Theme.proCTAText,
  988. .kern: 0.35
  989. ]
  990. )
  991. findJobsButton.contentTintColor = Theme.proCTAText
  992. findJobsButton.isBordered = false
  993. findJobsButton.bezelStyle = .rounded
  994. findJobsButton.wantsLayer = true
  995. findJobsButton.layer?.backgroundColor = NSColor.clear.cgColor
  996. findJobsButton.focusRingType = .none
  997. findJobsButton.target = self
  998. findJobsButton.action = #selector(didSubmitSearch)
  999. findJobsButton.setContentHuggingPriority(.required, for: .horizontal)
  1000. findJobsButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  1001. findJobsCTAHost.addSubview(findJobsCTAChrome)
  1002. findJobsCTAHost.addSubview(findJobsButton)
  1003. NSLayoutConstraint.activate([
  1004. findJobsCTAChrome.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor),
  1005. findJobsCTAChrome.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor),
  1006. findJobsCTAChrome.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  1007. findJobsCTAChrome.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor),
  1008. findJobsButton.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor, constant: 14),
  1009. findJobsButton.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor, constant: -14),
  1010. findJobsButton.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  1011. findJobsButton.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor)
  1012. ])
  1013. let keywordsStack = NSStackView(views: [jobSearchIcon, jobKeywordsField])
  1014. keywordsStack.orientation = .horizontal
  1015. keywordsStack.spacing = 10
  1016. keywordsStack.alignment = .centerY
  1017. keywordsStack.translatesAutoresizingMaskIntoConstraints = false
  1018. keywordsStack.edgeInsets = NSEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
  1019. keywordsStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1020. let row = NSStackView(views: [keywordsStack, findJobsCTAHost])
  1021. row.orientation = .horizontal
  1022. row.spacing = 0
  1023. row.alignment = .centerY
  1024. row.distribution = .fill
  1025. row.translatesAutoresizingMaskIntoConstraints = false
  1026. row.edgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 7)
  1027. searchCard.addSubview(row)
  1028. NSLayoutConstraint.activate([
  1029. searchCard.leadingAnchor.constraint(equalTo: searchBarShadowHost.leadingAnchor),
  1030. searchCard.trailingAnchor.constraint(equalTo: searchBarShadowHost.trailingAnchor),
  1031. searchCard.topAnchor.constraint(equalTo: searchBarShadowHost.topAnchor),
  1032. searchCard.bottomAnchor.constraint(equalTo: searchBarShadowHost.bottomAnchor),
  1033. searchBarShadowHost.heightAnchor.constraint(equalToConstant: barHeight),
  1034. row.leadingAnchor.constraint(equalTo: searchCard.leadingAnchor),
  1035. row.trailingAnchor.constraint(equalTo: searchCard.trailingAnchor),
  1036. row.topAnchor.constraint(equalTo: searchCard.topAnchor),
  1037. row.bottomAnchor.constraint(equalTo: searchCard.bottomAnchor),
  1038. jobSearchIcon.widthAnchor.constraint(equalToConstant: 18),
  1039. jobSearchIcon.heightAnchor.constraint(equalToConstant: 18),
  1040. findJobsCTAHost.heightAnchor.constraint(equalToConstant: ctaHeight),
  1041. findJobsCTAHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 112)
  1042. ])
  1043. searchCard.hoverHandler = nil
  1044. }
  1045. private func updateFindJobsCTAShadowPath() {
  1046. guard findJobsCTAHost.bounds.width > 0, findJobsCTAHost.bounds.height > 0 else { return }
  1047. let r = findJobsCTAHost.bounds
  1048. let radius = min(r.height / 2, r.width / 2)
  1049. findJobsCTAHost.layer?.shadowPath = CGPath(
  1050. roundedRect: r,
  1051. cornerWidth: radius,
  1052. cornerHeight: radius,
  1053. transform: nil
  1054. )
  1055. }
  1056. private func configureNonHomePlaceholder() {
  1057. nonHomeHost.translatesAutoresizingMaskIntoConstraints = false
  1058. nonHomeHost.wantsLayer = true
  1059. nonHomeHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  1060. nonHomeHost.isHidden = true
  1061. nonHomeHost.userInterfaceLayoutDirection = .leftToRight
  1062. nonHomeGenericContainer.translatesAutoresizingMaskIntoConstraints = false
  1063. savedJobsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  1064. settingsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  1065. cvMakerPageContainer.translatesAutoresizingMaskIntoConstraints = false
  1066. profilePageContainer.translatesAutoresizingMaskIntoConstraints = false
  1067. nonHomeHost.addSubview(nonHomeGenericContainer)
  1068. nonHomeHost.addSubview(savedJobsPageContainer)
  1069. nonHomeHost.addSubview(settingsPageContainer)
  1070. nonHomeHost.addSubview(cvMakerPageContainer)
  1071. nonHomeHost.addSubview(profilePageContainer)
  1072. NSLayoutConstraint.activate([
  1073. nonHomeGenericContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  1074. nonHomeGenericContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  1075. nonHomeGenericContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1076. nonHomeGenericContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  1077. savedJobsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  1078. savedJobsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  1079. savedJobsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1080. savedJobsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  1081. settingsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  1082. settingsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  1083. settingsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1084. settingsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  1085. cvMakerPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  1086. cvMakerPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  1087. cvMakerPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1088. cvMakerPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  1089. profilePageContainer.leftAnchor.constraint(equalTo: nonHomeHost.leftAnchor),
  1090. profilePageContainer.rightAnchor.constraint(equalTo: nonHomeHost.rightAnchor),
  1091. profilePageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  1092. profilePageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor)
  1093. ])
  1094. nonHomeTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  1095. nonHomeTitleLabel.textColor = Theme.primaryText
  1096. nonHomeTitleLabel.alignment = .center
  1097. nonHomeTitleLabel.maximumNumberOfLines = 1
  1098. nonHomeSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  1099. nonHomeSubtitleLabel.textColor = Theme.secondaryText
  1100. nonHomeSubtitleLabel.alignment = .center
  1101. nonHomeSubtitleLabel.maximumNumberOfLines = 0
  1102. nonHomeSubtitleLabel.stringValue = "This area is not available in the preview build. Use Home to search jobs."
  1103. let genericStack = NSStackView(views: [nonHomeTitleLabel, nonHomeSubtitleLabel])
  1104. genericStack.orientation = .vertical
  1105. genericStack.spacing = 10
  1106. genericStack.alignment = .centerX
  1107. genericStack.translatesAutoresizingMaskIntoConstraints = false
  1108. nonHomeGenericContainer.addSubview(genericStack)
  1109. NSLayoutConstraint.activate([
  1110. genericStack.centerXAnchor.constraint(equalTo: nonHomeGenericContainer.centerXAnchor),
  1111. genericStack.centerYAnchor.constraint(equalTo: nonHomeGenericContainer.centerYAnchor),
  1112. genericStack.leadingAnchor.constraint(greaterThanOrEqualTo: nonHomeGenericContainer.leadingAnchor, constant: 32),
  1113. genericStack.trailingAnchor.constraint(lessThanOrEqualTo: nonHomeGenericContainer.trailingAnchor, constant: -32),
  1114. nonHomeSubtitleLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 420)
  1115. ])
  1116. savedJobsPageTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  1117. savedJobsPageTitleLabel.textColor = Theme.primaryText
  1118. savedJobsPageTitleLabel.alignment = .left
  1119. savedJobsPageTitleLabel.maximumNumberOfLines = 1
  1120. savedJobsPageSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  1121. savedJobsPageSubtitleLabel.textColor = Theme.secondaryText
  1122. savedJobsPageSubtitleLabel.alignment = .left
  1123. savedJobsPageSubtitleLabel.maximumNumberOfLines = 0
  1124. savedJobsDocumentView.translatesAutoresizingMaskIntoConstraints = false
  1125. savedJobsStack.orientation = .vertical
  1126. savedJobsStack.spacing = 14
  1127. savedJobsStack.alignment = .leading
  1128. savedJobsStack.distribution = .fill
  1129. savedJobsStack.translatesAutoresizingMaskIntoConstraints = false
  1130. savedJobsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  1131. savedJobsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  1132. savedJobsDocumentView.addSubview(savedJobsStack)
  1133. NSLayoutConstraint.activate([
  1134. savedJobsStack.leadingAnchor.constraint(equalTo: savedJobsDocumentView.leadingAnchor),
  1135. savedJobsStack.trailingAnchor.constraint(equalTo: savedJobsDocumentView.trailingAnchor),
  1136. savedJobsStack.topAnchor.constraint(equalTo: savedJobsDocumentView.topAnchor),
  1137. savedJobsStack.bottomAnchor.constraint(equalTo: savedJobsDocumentView.bottomAnchor)
  1138. ])
  1139. savedJobsScrollView.translatesAutoresizingMaskIntoConstraints = false
  1140. savedJobsScrollView.hasVerticalScroller = true
  1141. savedJobsScrollView.hasHorizontalScroller = false
  1142. savedJobsScrollView.scrollerStyle = .legacy
  1143. savedJobsScrollView.autohidesScrollers = true
  1144. savedJobsScrollView.drawsBackground = false
  1145. savedJobsScrollView.borderType = .noBorder
  1146. savedJobsScrollView.documentView = savedJobsDocumentView
  1147. savedJobsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  1148. savedJobsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  1149. let savedHeaderStack = NSStackView(views: [savedJobsPageTitleLabel, savedJobsPageSubtitleLabel])
  1150. savedHeaderStack.orientation = .vertical
  1151. savedHeaderStack.spacing = 6
  1152. savedHeaderStack.alignment = .leading
  1153. savedHeaderStack.translatesAutoresizingMaskIntoConstraints = false
  1154. let savedOuterStack = NSStackView(views: [savedHeaderStack, savedJobsScrollView])
  1155. savedOuterStack.orientation = .vertical
  1156. savedOuterStack.spacing = 16
  1157. // Leading alignment plus explicit column width keeps the title and subtitle on the same edge as the cards.
  1158. savedOuterStack.alignment = .leading
  1159. savedOuterStack.translatesAutoresizingMaskIntoConstraints = false
  1160. savedJobsPageContainer.userInterfaceLayoutDirection = .leftToRight
  1161. savedJobsPageContainer.addSubview(savedOuterStack)
  1162. NSLayoutConstraint.activate([
  1163. savedOuterStack.leadingAnchor.constraint(equalTo: savedJobsPageContainer.leadingAnchor, constant: 32),
  1164. savedOuterStack.trailingAnchor.constraint(equalTo: savedJobsPageContainer.trailingAnchor, constant: -32),
  1165. savedOuterStack.topAnchor.constraint(equalTo: savedJobsPageContainer.topAnchor, constant: 8),
  1166. savedOuterStack.bottomAnchor.constraint(equalTo: savedJobsPageContainer.bottomAnchor),
  1167. savedHeaderStack.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  1168. savedJobsScrollView.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  1169. savedJobsDocumentView.topAnchor.constraint(equalTo: savedJobsScrollView.contentView.topAnchor),
  1170. savedJobsDocumentView.leadingAnchor.constraint(equalTo: savedJobsScrollView.contentView.leadingAnchor),
  1171. savedJobsDocumentView.widthAnchor.constraint(equalTo: savedJobsScrollView.contentView.widthAnchor)
  1172. ])
  1173. configureSettingsPage()
  1174. configureCVMakerPage()
  1175. configureProfilePage()
  1176. }
  1177. private func configureCVMakerPage() {
  1178. cvMakerPageContainer.wantsLayer = true
  1179. cvMakerPageContainer.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  1180. cvMakerPageContainer.isHidden = true
  1181. cvMakerPageView.translatesAutoresizingMaskIntoConstraints = false
  1182. cvMakerPageContainer.addSubview(cvMakerPageView)
  1183. NSLayoutConstraint.activate([
  1184. cvMakerPageView.leadingAnchor.constraint(equalTo: cvMakerPageContainer.leadingAnchor),
  1185. cvMakerPageView.trailingAnchor.constraint(equalTo: cvMakerPageContainer.trailingAnchor),
  1186. cvMakerPageView.topAnchor.constraint(equalTo: cvMakerPageContainer.topAnchor),
  1187. cvMakerPageView.bottomAnchor.constraint(equalTo: cvMakerPageContainer.bottomAnchor)
  1188. ])
  1189. cvMakerPageView.onContinueToProfileSelection = { [weak self] template in
  1190. guard let self else { return }
  1191. self.pendingCVTemplate = template
  1192. self.profilesListPageView.setPendingCVTemplateDisplayName(template.name)
  1193. self.selectProfileSidebarForCVMakerFlow()
  1194. }
  1195. }
  1196. /// Switches the main panel to **Profile** so the user can pick a saved CV profile after choosing a template in CV Maker.
  1197. private func selectProfileSidebarForCVMakerFlow() {
  1198. guard let index = currentSidebarItems.firstIndex(where: { $0.title == "Profile" }) else { return }
  1199. selectSidebarItem(at: index)
  1200. }
  1201. private func configureProfilePage() {
  1202. profilePageContainer.wantsLayer = true
  1203. profilePageContainer.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  1204. profilePageContainer.isHidden = true
  1205. profilePageContainer.userInterfaceLayoutDirection = .leftToRight
  1206. profilesListPageView.translatesAutoresizingMaskIntoConstraints = false
  1207. myProfilePageView.translatesAutoresizingMaskIntoConstraints = false
  1208. cvFilledPreviewPageView.translatesAutoresizingMaskIntoConstraints = false
  1209. profilePageContainer.addSubview(profilesListPageView)
  1210. profilePageContainer.addSubview(myProfilePageView)
  1211. profilePageContainer.addSubview(cvFilledPreviewPageView)
  1212. NSLayoutConstraint.activate([
  1213. profilesListPageView.leftAnchor.constraint(equalTo: profilePageContainer.leftAnchor),
  1214. profilesListPageView.rightAnchor.constraint(equalTo: profilePageContainer.rightAnchor),
  1215. profilesListPageView.topAnchor.constraint(equalTo: profilePageContainer.topAnchor),
  1216. profilesListPageView.bottomAnchor.constraint(equalTo: profilePageContainer.bottomAnchor),
  1217. myProfilePageView.leftAnchor.constraint(equalTo: profilePageContainer.leftAnchor),
  1218. myProfilePageView.rightAnchor.constraint(equalTo: profilePageContainer.rightAnchor),
  1219. myProfilePageView.topAnchor.constraint(equalTo: profilePageContainer.topAnchor),
  1220. myProfilePageView.bottomAnchor.constraint(equalTo: profilePageContainer.bottomAnchor),
  1221. cvFilledPreviewPageView.leftAnchor.constraint(equalTo: profilePageContainer.leftAnchor),
  1222. cvFilledPreviewPageView.rightAnchor.constraint(equalTo: profilePageContainer.rightAnchor),
  1223. cvFilledPreviewPageView.topAnchor.constraint(equalTo: profilePageContainer.topAnchor),
  1224. cvFilledPreviewPageView.bottomAnchor.constraint(equalTo: profilePageContainer.bottomAnchor)
  1225. ])
  1226. profilesListPageView.onAddProfile = { [weak self] in
  1227. self?.presentProfileEditor(existingID: nil)
  1228. }
  1229. profilesListPageView.onEditProfile = { [weak self] id in
  1230. self?.presentProfileEditor(existingID: id)
  1231. }
  1232. profilesListPageView.onDeleteProfile = { [weak self] id in
  1233. self?.confirmDeleteProfile(id: id)
  1234. }
  1235. profilesListPageView.onBuildCVWithProfile = { [weak self] profileID in
  1236. guard let self,
  1237. let template = self.pendingCVTemplate,
  1238. let profile = SavedProfilesStore.profile(id: profileID) else { return }
  1239. self.presentCVDocumentPreview(profile: profile, template: template)
  1240. }
  1241. cvFilledPreviewPageView.onDismiss = { [weak self] in
  1242. self?.dismissCVDocumentPreview()
  1243. }
  1244. myProfilePageView.onDismiss = { [weak self] in
  1245. self?.dismissProfileEditor()
  1246. }
  1247. isProfileEditorPresented = false
  1248. isCVDocumentPreviewPresented = false
  1249. profilesListPageView.isHidden = false
  1250. myProfilePageView.isHidden = true
  1251. cvFilledPreviewPageView.isHidden = true
  1252. profilesListPageView.reloadFromStore()
  1253. }
  1254. private func presentCVDocumentPreview(profile: SavedProfile, template: CVTemplate) {
  1255. isCVDocumentPreviewPresented = true
  1256. cvFilledPreviewPageView.configure(profile: profile, template: template)
  1257. cvFilledPreviewPageView.isHidden = false
  1258. profilesListPageView.isHidden = true
  1259. myProfilePageView.isHidden = true
  1260. }
  1261. private func dismissCVDocumentPreview() {
  1262. isCVDocumentPreviewPresented = false
  1263. cvFilledPreviewPageView.isHidden = true
  1264. profilesListPageView.reloadFromStore()
  1265. profilesListPageView.isHidden = false
  1266. myProfilePageView.isHidden = true
  1267. }
  1268. private func presentProfileEditor(existingID: UUID?) {
  1269. if isCVDocumentPreviewPresented {
  1270. dismissCVDocumentPreview()
  1271. }
  1272. isProfileEditorPresented = true
  1273. if let id = existingID, let profile = SavedProfilesStore.profile(id: id) {
  1274. myProfilePageView.loadSavedProfile(profile)
  1275. } else {
  1276. myProfilePageView.prepareNewProfile()
  1277. }
  1278. profilesListPageView.isHidden = true
  1279. myProfilePageView.isHidden = false
  1280. }
  1281. private func dismissProfileEditor() {
  1282. isProfileEditorPresented = false
  1283. profilesListPageView.reloadFromStore()
  1284. profilesListPageView.isHidden = false
  1285. myProfilePageView.isHidden = true
  1286. }
  1287. private func confirmDeleteProfile(id: UUID) {
  1288. let displayName = SavedProfilesStore.profile(id: id)?.profileDisplayName ?? ""
  1289. let alert = NSAlert()
  1290. alert.messageText = "Delete this profile?"
  1291. alert.informativeText = displayName.isEmpty
  1292. ? "This profile will be removed from this Mac."
  1293. : "“\(displayName)” will be removed from this Mac."
  1294. alert.alertStyle = .warning
  1295. alert.addButton(withTitle: "Cancel")
  1296. alert.addButton(withTitle: "Delete")
  1297. guard let window = window else {
  1298. let response = alert.runModal()
  1299. if response == .alertSecondButtonReturn {
  1300. SavedProfilesStore.delete(id: id)
  1301. profilesListPageView.reloadFromStore()
  1302. }
  1303. return
  1304. }
  1305. alert.beginSheetModal(for: window) { [weak self] response in
  1306. guard let self else { return }
  1307. if response == .alertSecondButtonReturn {
  1308. SavedProfilesStore.delete(id: id)
  1309. if self.isProfileEditorPresented {
  1310. self.dismissProfileEditor()
  1311. } else {
  1312. self.profilesListPageView.reloadFromStore()
  1313. }
  1314. }
  1315. }
  1316. }
  1317. private func configureSettingsPage() {
  1318. settingsPageContainer.wantsLayer = true
  1319. settingsPageContainer.layer?.backgroundColor = Theme.settingsPageBackground.cgColor
  1320. settingsPageContainer.isHidden = true
  1321. let contentStack = NSStackView()
  1322. contentStack.orientation = .vertical
  1323. contentStack.spacing = 26
  1324. contentStack.alignment = .leading
  1325. contentStack.translatesAutoresizingMaskIntoConstraints = false
  1326. let settingsSection = makeSettingsSection(rows: [
  1327. makeSettingsRow(title: "Share App", systemImage: "square.and.arrow.up", accessory: nil, tapAction: #selector(didTapShareApp)),
  1328. makeSettingsRow(title: "More Apps", systemImage: "square.grid.2x2", accessory: nil, tapAction: #selector(didTapMoreApps))
  1329. ])
  1330. let aboutTitle = NSTextField(labelWithString: "About")
  1331. aboutTitle.font = .systemFont(ofSize: 12, weight: .semibold)
  1332. aboutTitle.textColor = Theme.secondaryText
  1333. aboutTitle.alignment = .left
  1334. let aboutSection = makeSettingsSection(rows: [
  1335. makeSettingsRow(title: "Support", systemImage: "questionmark.circle", accessory: nil, tapAction: #selector(didTapSupport)),
  1336. makeSettingsRow(title: "Terms of Use", systemImage: "doc.text", accessory: nil, tapAction: #selector(didTapTermsOfUse)),
  1337. makeSettingsRow(title: "Privacy Policy", systemImage: "shield", accessory: nil, tapAction: #selector(didTapPrivacyPolicy))
  1338. ])
  1339. let aboutStack = NSStackView(views: [aboutTitle, aboutSection])
  1340. aboutStack.orientation = .vertical
  1341. aboutStack.spacing = 14
  1342. aboutStack.alignment = .leading
  1343. aboutStack.translatesAutoresizingMaskIntoConstraints = false
  1344. contentStack.addArrangedSubview(settingsSection)
  1345. contentStack.addArrangedSubview(aboutStack)
  1346. settingsPageContainer.addSubview(contentStack)
  1347. NSLayoutConstraint.activate([
  1348. contentStack.leadingAnchor.constraint(equalTo: settingsPageContainer.leadingAnchor, constant: 42),
  1349. contentStack.trailingAnchor.constraint(lessThanOrEqualTo: settingsPageContainer.trailingAnchor, constant: -42),
  1350. contentStack.topAnchor.constraint(equalTo: settingsPageContainer.topAnchor, constant: 48),
  1351. settingsSection.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  1352. aboutStack.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  1353. aboutSection.widthAnchor.constraint(equalTo: aboutStack.widthAnchor),
  1354. contentStack.widthAnchor.constraint(equalTo: settingsPageContainer.widthAnchor, constant: -84)
  1355. ])
  1356. }
  1357. private func makeSettingsSection(rows: [NSView]) -> NSView {
  1358. let section = NSStackView()
  1359. section.orientation = .vertical
  1360. section.spacing = 0
  1361. section.alignment = .leading
  1362. section.translatesAutoresizingMaskIntoConstraints = false
  1363. section.wantsLayer = true
  1364. section.layer?.backgroundColor = Theme.settingsGroupBackground.cgColor
  1365. section.layer?.cornerRadius = 14
  1366. section.layer?.borderWidth = 1
  1367. section.layer?.borderColor = Theme.border.cgColor
  1368. section.layer?.masksToBounds = true
  1369. for (index, row) in rows.enumerated() {
  1370. section.addArrangedSubview(row)
  1371. row.widthAnchor.constraint(equalTo: section.widthAnchor).isActive = true
  1372. if index < rows.count - 1 {
  1373. let divider = NSView()
  1374. divider.translatesAutoresizingMaskIntoConstraints = false
  1375. divider.wantsLayer = true
  1376. divider.layer?.backgroundColor = Theme.settingsDivider.cgColor
  1377. section.addArrangedSubview(divider)
  1378. NSLayoutConstraint.activate([
  1379. divider.heightAnchor.constraint(equalToConstant: 1),
  1380. divider.leadingAnchor.constraint(equalTo: section.leadingAnchor),
  1381. divider.trailingAnchor.constraint(equalTo: section.trailingAnchor)
  1382. ])
  1383. }
  1384. }
  1385. return section
  1386. }
  1387. private func makeSettingsRow(title: String, systemImage: String, accessory: NSView?, tapAction: Selector? = nil) -> NSView {
  1388. let row = NSView()
  1389. row.translatesAutoresizingMaskIntoConstraints = false
  1390. row.wantsLayer = true
  1391. let iconTile = NSView()
  1392. iconTile.translatesAutoresizingMaskIntoConstraints = false
  1393. iconTile.wantsLayer = true
  1394. iconTile.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  1395. iconTile.layer?.cornerRadius = 9
  1396. let icon = NSImageView()
  1397. icon.translatesAutoresizingMaskIntoConstraints = false
  1398. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  1399. icon.image = NSImage(systemSymbolName: systemImage, accessibilityDescription: title)
  1400. icon.contentTintColor = Theme.brandBlue
  1401. let titleLabel = NSTextField(labelWithString: title)
  1402. titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
  1403. titleLabel.textColor = Theme.secondaryText
  1404. titleLabel.alignment = .left
  1405. let rowStack = NSStackView()
  1406. rowStack.orientation = .horizontal
  1407. rowStack.spacing = 16
  1408. rowStack.alignment = .centerY
  1409. rowStack.translatesAutoresizingMaskIntoConstraints = false
  1410. let spacer = NSView()
  1411. spacer.translatesAutoresizingMaskIntoConstraints = false
  1412. spacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  1413. iconTile.addSubview(icon)
  1414. rowStack.addArrangedSubview(iconTile)
  1415. rowStack.addArrangedSubview(titleLabel)
  1416. rowStack.addArrangedSubview(spacer)
  1417. if let accessory {
  1418. rowStack.addArrangedSubview(accessory)
  1419. }
  1420. row.addSubview(rowStack)
  1421. NSLayoutConstraint.activate([
  1422. row.heightAnchor.constraint(equalToConstant: 68),
  1423. iconTile.widthAnchor.constraint(equalToConstant: 38),
  1424. iconTile.heightAnchor.constraint(equalToConstant: 38),
  1425. icon.centerXAnchor.constraint(equalTo: iconTile.centerXAnchor),
  1426. icon.centerYAnchor.constraint(equalTo: iconTile.centerYAnchor),
  1427. icon.widthAnchor.constraint(equalToConstant: 20),
  1428. icon.heightAnchor.constraint(equalToConstant: 20),
  1429. rowStack.leadingAnchor.constraint(equalTo: row.leadingAnchor, constant: 16),
  1430. rowStack.trailingAnchor.constraint(equalTo: row.trailingAnchor, constant: -16),
  1431. rowStack.topAnchor.constraint(equalTo: row.topAnchor),
  1432. rowStack.bottomAnchor.constraint(equalTo: row.bottomAnchor)
  1433. ])
  1434. if let tapAction {
  1435. let rowButton = NSButton(title: "", target: self, action: tapAction)
  1436. rowButton.translatesAutoresizingMaskIntoConstraints = false
  1437. rowButton.isBordered = false
  1438. rowButton.bezelStyle = .regularSquare
  1439. rowButton.setButtonType(.momentaryChange)
  1440. rowButton.focusRingType = .none
  1441. rowButton.wantsLayer = true
  1442. rowButton.layer?.backgroundColor = .clear
  1443. row.addSubview(rowButton)
  1444. NSLayoutConstraint.activate([
  1445. rowButton.leadingAnchor.constraint(equalTo: row.leadingAnchor),
  1446. rowButton.trailingAnchor.constraint(equalTo: row.trailingAnchor),
  1447. rowButton.topAnchor.constraint(equalTo: row.topAnchor),
  1448. rowButton.bottomAnchor.constraint(equalTo: row.bottomAnchor)
  1449. ])
  1450. }
  1451. return row
  1452. }
  1453. private func reloadSavedJobsListings() {
  1454. savedJobsStack.arrangedSubviews.forEach {
  1455. savedJobsStack.removeArrangedSubview($0)
  1456. $0.removeFromSuperview()
  1457. }
  1458. if savedJobOrder.isEmpty {
  1459. savedJobsPageSubtitleLabel.stringValue = "Save jobs from Home to see them here."
  1460. let empty = NSTextField(wrappingLabelWithString: "No saved jobs yet. Search on Home, then tap Save on a listing.")
  1461. empty.font = .systemFont(ofSize: 14, weight: .regular)
  1462. empty.textColor = Theme.secondaryText
  1463. empty.alignment = .left
  1464. empty.maximumNumberOfLines = 0
  1465. empty.translatesAutoresizingMaskIntoConstraints = false
  1466. savedJobsStack.addArrangedSubview(empty)
  1467. empty.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  1468. return
  1469. }
  1470. savedJobsPageSubtitleLabel.stringValue = "\(savedJobOrder.count) saved \(savedJobOrder.count == 1 ? "position" : "positions")"
  1471. for job in savedJobOrder {
  1472. let card = makeJobListingCard(job, context: .savedJobsPage)
  1473. savedJobsStack.addArrangedSubview(card)
  1474. card.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  1475. }
  1476. }
  1477. private func isSavedJobsSidebarIndex(_ index: Int) -> Bool {
  1478. guard index >= 0, index < currentSidebarItems.count else { return false }
  1479. return currentSidebarItems[index].title == "Saved Jobs"
  1480. }
  1481. private func isHomeSidebarIndex(_ index: Int) -> Bool {
  1482. guard index >= 0, index < currentSidebarItems.count else { return false }
  1483. return currentSidebarItems[index].title == "Home"
  1484. }
  1485. private func isSettingsSidebarIndex(_ index: Int) -> Bool {
  1486. guard index >= 0, index < currentSidebarItems.count else { return false }
  1487. return currentSidebarItems[index].title == "Settings"
  1488. }
  1489. private func isCVMakerSidebarIndex(_ index: Int) -> Bool {
  1490. guard index >= 0, index < currentSidebarItems.count else { return false }
  1491. return currentSidebarItems[index].title == "CV Maker"
  1492. }
  1493. private func isProfileSidebarIndex(_ index: Int) -> Bool {
  1494. guard index >= 0, index < currentSidebarItems.count else { return false }
  1495. return currentSidebarItems[index].title == "Profile"
  1496. }
  1497. private func updateMainContentVisibility() {
  1498. if isIndeedJobBrowserPresented {
  1499. mainOverlay.isHidden = true
  1500. nonHomeHost.isHidden = true
  1501. indeedJobBrowserHost.isHidden = false
  1502. return
  1503. }
  1504. indeedJobBrowserHost.isHidden = true
  1505. let home = isHomeSidebarIndex(selectedSidebarIndex)
  1506. let savedJobs = isSavedJobsSidebarIndex(selectedSidebarIndex)
  1507. let settings = isSettingsSidebarIndex(selectedSidebarIndex)
  1508. let cvMaker = isCVMakerSidebarIndex(selectedSidebarIndex)
  1509. let profile = isProfileSidebarIndex(selectedSidebarIndex)
  1510. mainOverlay.isHidden = !home
  1511. nonHomeHost.isHidden = home
  1512. nonHomeGenericContainer.isHidden = savedJobs || settings || cvMaker || profile
  1513. savedJobsPageContainer.isHidden = !savedJobs
  1514. settingsPageContainer.isHidden = !settings
  1515. cvMakerPageContainer.isHidden = !cvMaker
  1516. profilePageContainer.isHidden = !profile
  1517. if !profile {
  1518. isProfileEditorPresented = false
  1519. isCVDocumentPreviewPresented = false
  1520. pendingCVTemplate = nil
  1521. profilesListPageView.setPendingCVTemplateDisplayName(nil)
  1522. cvFilledPreviewPageView.isHidden = true
  1523. profilesListPageView.isHidden = false
  1524. myProfilePageView.isHidden = true
  1525. }
  1526. if profile, !isProfileEditorPresented {
  1527. if isCVDocumentPreviewPresented {
  1528. profilesListPageView.isHidden = true
  1529. myProfilePageView.isHidden = true
  1530. cvFilledPreviewPageView.isHidden = false
  1531. } else {
  1532. profilesListPageView.reloadFromStore()
  1533. profilesListPageView.isHidden = false
  1534. myProfilePageView.isHidden = true
  1535. cvFilledPreviewPageView.isHidden = true
  1536. }
  1537. }
  1538. if !home, selectedSidebarIndex < currentSidebarItems.count {
  1539. if savedJobs {
  1540. reloadSavedJobsListings()
  1541. } else if settings || cvMaker || profile {
  1542. window?.makeFirstResponder(nil)
  1543. } else {
  1544. nonHomeTitleLabel.stringValue = currentSidebarItems[selectedSidebarIndex].title
  1545. }
  1546. }
  1547. }
  1548. /// Restores the main job-search field when returning to Home; chat history is kept until the user chooses **Clear chat**.
  1549. private func applyHomeState() {
  1550. jobKeywordsField.stringValue = ""
  1551. window?.makeFirstResponder(nil)
  1552. }
  1553. @objc private func didTapClearChat() {
  1554. guard !isAwaitingResponse else { return }
  1555. resetChatState()
  1556. window?.makeFirstResponder(nil)
  1557. }
  1558. private func updateSearchBarShadowPath() {
  1559. guard searchBarShadowHost.bounds.width > 0, searchBarShadowHost.bounds.height > 0 else { return }
  1560. let r = searchBarShadowHost.bounds
  1561. let radius = min(r.height / 2, 27)
  1562. searchBarShadowHost.layer?.shadowPath = CGPath(
  1563. roundedRect: r,
  1564. cornerWidth: radius,
  1565. cornerHeight: radius,
  1566. transform: nil
  1567. )
  1568. }
  1569. @objc private func didSubmitSearch() {
  1570. guard SubscriptionStore.shared.isProActive else {
  1571. presentPremiumPlansSheet()
  1572. return
  1573. }
  1574. let prompt = jobKeywordsField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  1575. guard !prompt.isEmpty, !isAwaitingResponse else { return }
  1576. let isContinuation = isContinuationPrompt(prompt)
  1577. let effectiveQuery = resolvedSearchQuery(for: prompt)
  1578. appendChatBubble(text: prompt, isUser: true)
  1579. chatMessages.append(ChatMessage(role: "user", content: prompt))
  1580. jobKeywordsField.stringValue = ""
  1581. startJobSearchRequest(effectiveQuery: effectiveQuery, isContinuation: isContinuation)
  1582. window?.makeFirstResponder(nil)
  1583. }
  1584. @objc private func didTapFeatureRole() {
  1585. focusSearchField(seed: "Find roles similar to: ")
  1586. }
  1587. @objc private func didTapFeatureCompany() {
  1588. focusSearchField(seed: "Find jobs at company: ")
  1589. }
  1590. @objc private func didTapFeatureSkill() {
  1591. focusSearchField(seed: "Find jobs that require skill: ")
  1592. }
  1593. @objc private func didTapShareApp(_ sender: NSButton) {
  1594. presentAppShareMenu(anchoredTo: sender)
  1595. }
  1596. /// Shows the macOS share menu (Mail, Messages, AirDrop, Copy Link, etc.) with the app link.
  1597. private func presentAppShareMenu(anchoredTo sender: NSButton) {
  1598. guard let row = sender.superview else { return }
  1599. let items = AppMarketingLinks.shareItems
  1600. guard !items.isEmpty else { return }
  1601. let picker = NSSharingServicePicker(items: items)
  1602. picker.delegate = self
  1603. appSharePicker = picker
  1604. // Match `makeSettingsRow` layout: 16pt leading inset + 38pt icon tile — anchor the
  1605. // popover beside the share icon, not the horizontal center of the full-width row.
  1606. let iconTileInset: CGFloat = 16
  1607. let iconTileSize: CGFloat = 38
  1608. let anchorRect = NSRect(
  1609. x: iconTileInset,
  1610. y: row.bounds.minY + 6,
  1611. width: iconTileSize,
  1612. height: max(row.bounds.height - 12, 1)
  1613. )
  1614. picker.show(relativeTo: anchorRect, of: row, preferredEdge: .minY)
  1615. }
  1616. func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, delegateFor sharingService: NSSharingService) -> Any? {
  1617. self
  1618. }
  1619. func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, didChoose sharingService: NSSharingService?) {
  1620. appSharePicker = nil
  1621. }
  1622. func sharingService(_ sharingService: NSSharingService, willShareItems items: [Any]) -> [Any] {
  1623. if sharingService == NSSharingService(named: .composeEmail) {
  1624. sharingService.subject = AppMarketingLinks.shareEmailSubject
  1625. }
  1626. return items
  1627. }
  1628. @objc private func didTapMoreApps() {
  1629. guard let url = AppMarketingLinks.developerAppsURL else {
  1630. presentAppMarketingConfigurationAlert(feature: "More Apps")
  1631. return
  1632. }
  1633. NSWorkspace.shared.open(url)
  1634. }
  1635. private func presentAppMarketingConfigurationAlert(feature: String) {
  1636. let alert = NSAlert()
  1637. alert.messageText = "\(feature) isn’t available yet"
  1638. alert.informativeText = """
  1639. Add your Mac App Store IDs in the target’s build settings:
  1640. • AppStoreAppID — numeric app ID from App Store Connect
  1641. • AppStoreDeveloperID — numeric developer ID (for your other apps page)
  1642. """
  1643. alert.alertStyle = .informational
  1644. alert.addButton(withTitle: "OK")
  1645. if let window {
  1646. alert.beginSheetModal(for: window)
  1647. } else {
  1648. alert.runModal()
  1649. }
  1650. }
  1651. @objc private func didTapSupport() {
  1652. presentSettingsPlaceholderAlert(for: "Support")
  1653. }
  1654. @objc private func didTapTermsOfUse() {
  1655. presentSettingsPlaceholderAlert(for: "Terms of Use")
  1656. }
  1657. @objc private func didTapPrivacyPolicy() {
  1658. presentSettingsPlaceholderAlert(for: "Privacy Policy")
  1659. }
  1660. private func presentSettingsPlaceholderAlert(for featureName: String) {
  1661. let alert = NSAlert()
  1662. alert.messageText = "\(featureName) coming soon"
  1663. alert.informativeText = "This section is a placeholder for now."
  1664. alert.alertStyle = .informational
  1665. alert.addButton(withTitle: "OK")
  1666. if let window {
  1667. alert.beginSheetModal(for: window)
  1668. } else {
  1669. alert.runModal()
  1670. }
  1671. }
  1672. private func focusSearchField(seed: String) {
  1673. jobKeywordsField.stringValue = seed
  1674. window?.makeFirstResponder(jobKeywordsField)
  1675. if let editor = jobKeywordsField.window?.fieldEditor(true, for: jobKeywordsField) as? NSTextView {
  1676. editor.moveToEndOfDocument(nil)
  1677. }
  1678. }
  1679. @objc private func didTapLoadMoreJobs() {
  1680. guard SubscriptionStore.shared.isProActive else {
  1681. presentPremiumPlansSheet()
  1682. return
  1683. }
  1684. let prompt = "Show more jobs"
  1685. guard !isAwaitingResponse, isContinuationPrompt(prompt) else { return }
  1686. if anchorUserJobQuery(excludingLatestUserMessage: prompt) == nil { return }
  1687. appendChatBubble(text: prompt, isUser: true)
  1688. chatMessages.append(ChatMessage(role: "user", content: prompt))
  1689. let effectiveQuery = resolvedSearchQuery(for: prompt)
  1690. startJobSearchRequest(effectiveQuery: effectiveQuery, isContinuation: true)
  1691. }
  1692. private func startJobSearchRequest(effectiveQuery: String, isContinuation: Bool) {
  1693. isAwaitingResponse = true
  1694. addInlineChatThinkingRow()
  1695. setInputEnabled(false)
  1696. let contextMessages = chatMessages
  1697. let maxJobs = Self.clampedJobsPerRequest()
  1698. jobSearchService.searchJobs(query: effectiveQuery, conversation: contextMessages, maxJobs: maxJobs) { [weak self] result in
  1699. DispatchQueue.main.async {
  1700. guard let self else { return }
  1701. self.removeInlineChatThinkingRow()
  1702. self.isAwaitingResponse = false
  1703. self.setInputEnabled(true)
  1704. switch result {
  1705. case .success(let output):
  1706. let normalizedJobs = self.normalizedJobs(output.jobs)
  1707. let freshJobs: [JobListing]
  1708. if isContinuation {
  1709. // Continuations append only the *new* matches; previous cards already live in their own assistant message above.
  1710. let alreadySeen = Set(self.lastSearchResults)
  1711. freshJobs = normalizedJobs.filter { !alreadySeen.contains($0) }
  1712. } else {
  1713. freshJobs = normalizedJobs
  1714. }
  1715. self.lastSearchResults.append(contentsOf: freshJobs)
  1716. let reply = self.makeAssistantSearchReply(
  1717. query: effectiveQuery,
  1718. newJobsCount: freshJobs.count,
  1719. isContinuation: isContinuation
  1720. )
  1721. self.chatMessages.append(ChatMessage(role: "assistant", content: reply))
  1722. self.appendChatBubble(text: reply, isUser: false, jobs: freshJobs)
  1723. case .failure(let error):
  1724. self.appendChatBubble(text: error.localizedDescription, isUser: false)
  1725. }
  1726. }
  1727. }
  1728. }
  1729. private func normalizedJobs(_ jobs: [JobListing]) -> [JobListing] {
  1730. let trimmed = jobs.map {
  1731. JobListing(
  1732. title: $0.title.trimmingCharacters(in: .whitespacesAndNewlines),
  1733. description: $0.description.trimmingCharacters(in: .whitespacesAndNewlines),
  1734. url: $0.url?.trimmingCharacters(in: .whitespacesAndNewlines)
  1735. )
  1736. }
  1737. return trimmed.filter { !$0.title.isEmpty && !$0.description.isEmpty }
  1738. }
  1739. private func makeAssistantSearchReply(query: String, newJobsCount: Int, isContinuation: Bool) -> String {
  1740. if newJobsCount == 0 {
  1741. if isContinuation {
  1742. return "I couldn't find new matches for \u{201C}\(query)\u{201D}. Try a different angle or a more specific keyword."
  1743. }
  1744. return "No jobs found for \u{201C}\(query)\u{201D}. Try another title, skill, company, or location."
  1745. }
  1746. let plural = newJobsCount == 1 ? "match" : "matches"
  1747. if isContinuation {
  1748. return "Here are \(newJobsCount) more \(plural) for \u{201C}\(query)\u{201D}."
  1749. }
  1750. return "Found \(newJobsCount) \(plural) for \u{201C}\(query)\u{201D}. Tap Apply to open the listing or Save to revisit later."
  1751. }
  1752. private func resolvedSearchQuery(for prompt: String) -> String {
  1753. let anchor = anchorUserJobQuery(excludingLatestUserMessage: prompt)
  1754. if isContinuationPrompt(prompt), !isRefinementPrompt(prompt) {
  1755. if let anchor { return anchor }
  1756. return prompt
  1757. }
  1758. if isRefinementPrompt(prompt), let anchor {
  1759. return "\(anchor). User follow-up (apply on top of the same search topic): \(prompt)"
  1760. }
  1761. return prompt
  1762. }
  1763. /// First prior user message that looks like an original job query (skips short continuations and refinements so follow-ups keep a stable topic anchor).
  1764. private func anchorUserJobQuery(excludingLatestUserMessage latest: String) -> String? {
  1765. let prior = Array(chatMessages.dropLast())
  1766. for message in prior.reversed() where message.role == "user" {
  1767. let candidate = message.content.trimmingCharacters(in: .whitespacesAndNewlines)
  1768. guard !candidate.isEmpty, candidate != latest else { continue }
  1769. if isContinuationPrompt(candidate) { continue }
  1770. if isRefinementPrompt(candidate) { continue }
  1771. return candidate
  1772. }
  1773. return nil
  1774. }
  1775. private func isContinuationPrompt(_ prompt: String) -> Bool {
  1776. let normalized = prompt.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
  1777. let continuationPhrases: Set<String> = [
  1778. "more",
  1779. "show more",
  1780. "more jobs",
  1781. "more results",
  1782. "do more searches",
  1783. "more searches",
  1784. "search more",
  1785. "continue",
  1786. "next"
  1787. ]
  1788. if continuationPhrases.contains(normalized) {
  1789. return true
  1790. }
  1791. return normalized.contains("more search") || normalized.contains("more job")
  1792. }
  1793. /// Follow-ups that narrow, re-rank, or re-frame results rather than starting a brand-new role search.
  1794. /// Strong phrases always count. Single-word cues (e.g. "remote") only count after we already showed results, so first searches like "Senior iOS remote" stay anchored as primary queries.
  1795. private func isRefinementPrompt(_ prompt: String) -> Bool {
  1796. let n = prompt.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
  1797. if n.isEmpty { return false }
  1798. let strongPhrases = [
  1799. "higher pay", "high pay", "better pay", "more pay", "top pay", "best pay",
  1800. "higher salary", "better salary", "more salary", "pay rate", "hourly rate",
  1801. "paid more", "paying more", "earn more", "better paid", "paying better",
  1802. "work from home", "in office", "in-office", "on-site only", "remote only",
  1803. "hybrid only", "onsite only", "visa sponsorship", "h1b",
  1804. "entry level", "entry-level", "mid level", "mid-level", "full time", "full-time",
  1805. "part time", "part-time",
  1806. "closer to", "nearer", "different city", "different state", "relocate",
  1807. "filter", "only show", "just show", "exclude", "without", "sort by", "rank by",
  1808. "cheaper", "lower pay", "less travel",
  1809. "better benefits", "equity", "bonus", "overtime",
  1810. "get me the jobs", "show me the jobs", "give me the jobs", "narrow", "refine"
  1811. ]
  1812. if strongPhrases.contains(where: { n.contains($0) }) { return true }
  1813. if n.hasPrefix("only ") || n.hasPrefix("just ") { return true }
  1814. guard !lastSearchResults.isEmpty, n.count <= 52 else { return false }
  1815. let softAfterResults = [
  1816. "remote", "hybrid", "onsite", "on-site", "senior", "junior", "staff", "lead",
  1817. "principal", "intern", "contract", "location"
  1818. ]
  1819. return softAfterResults.contains(where: { n.contains($0) })
  1820. }
  1821. func controlTextDidBeginEditing(_ obj: Notification) {
  1822. applySearchFieldInsertionPoint(obj.object)
  1823. }
  1824. func controlTextDidChange(_ obj: Notification) {
  1825. applySearchFieldInsertionPoint(obj.object)
  1826. }
  1827. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  1828. guard control === jobKeywordsField, commandSelector == #selector(NSResponder.insertNewline(_:)) else {
  1829. return false
  1830. }
  1831. didSubmitSearch()
  1832. return true
  1833. }
  1834. private func applySearchFieldInsertionPoint(_ object: Any?) {
  1835. guard let field = object as? NSTextField,
  1836. field === jobKeywordsField,
  1837. let textView = field.window?.fieldEditor(true, for: field) as? NSTextView else { return }
  1838. textView.insertionPointColor = Theme.primaryText
  1839. }
  1840. private func resetChatState() {
  1841. removeInlineChatThinkingRow()
  1842. trailingLoadMoreJobsRow = nil
  1843. trailingLoadMoreJobsButton = nil
  1844. chatMessages.removeAll()
  1845. lastSearchResults.removeAll()
  1846. chatStack.arrangedSubviews.forEach {
  1847. chatStack.removeArrangedSubview($0)
  1848. $0.removeFromSuperview()
  1849. }
  1850. let welcome = "Tell me what role you want and I will return job descriptions, key skills, and a quick fit summary."
  1851. chatMessages.append(ChatMessage(role: "assistant", content: welcome))
  1852. appendChatBubble(text: welcome, isUser: false)
  1853. }
  1854. private func appendChatBubble(text: String, isUser: Bool, jobs: [JobListing]? = nil) {
  1855. let host = NSView()
  1856. host.translatesAutoresizingMaskIntoConstraints = false
  1857. if isUser {
  1858. installUserBubble(text: text, into: host)
  1859. } else {
  1860. installAssistantBubble(text: text, jobs: jobs, into: host)
  1861. }
  1862. chatStack.addArrangedSubview(host)
  1863. host.widthAnchor.constraint(equalTo: chatStack.widthAnchor).isActive = true
  1864. if prefersReducedMotion {
  1865. host.alphaValue = 1
  1866. } else {
  1867. host.alphaValue = 0
  1868. }
  1869. DispatchQueue.main.async { [weak self] in
  1870. guard let self else { return }
  1871. let scroll: () -> Void = {
  1872. if isUser {
  1873. self.scrollChatToBottom()
  1874. } else {
  1875. self.scrollChatToShowTopOfView(host)
  1876. }
  1877. }
  1878. if self.prefersReducedMotion {
  1879. self.updateChatBubbleWidths()
  1880. scroll()
  1881. return
  1882. }
  1883. NSAnimationContext.runAnimationGroup { ctx in
  1884. ctx.duration = 0.3
  1885. ctx.timingFunction = CAMediaTimingFunction(name: .easeOut)
  1886. host.animator().alphaValue = 1
  1887. }
  1888. self.updateChatBubbleWidths()
  1889. scroll()
  1890. }
  1891. }
  1892. private func installUserBubble(text: String, into host: NSView) {
  1893. let bubble = makeChatBubbleContainer(text: text, isUser: true)
  1894. host.addSubview(bubble)
  1895. NSLayoutConstraint.activate([
  1896. bubble.topAnchor.constraint(equalTo: host.topAnchor),
  1897. bubble.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  1898. bubble.trailingAnchor.constraint(equalTo: host.trailingAnchor),
  1899. bubble.leadingAnchor.constraint(greaterThanOrEqualTo: host.leadingAnchor, constant: 64),
  1900. bubble.widthAnchor.constraint(lessThanOrEqualTo: host.widthAnchor, multiplier: 0.78)
  1901. ])
  1902. }
  1903. private func installAssistantBubble(text: String, jobs: [JobListing]?, into host: NSView) {
  1904. let avatar = makeAssistantAvatarView()
  1905. let nameLabel = NSTextField(labelWithString: "AI Job Finder")
  1906. nameLabel.font = .systemFont(ofSize: 11, weight: .semibold)
  1907. nameLabel.textColor = Theme.secondaryText
  1908. nameLabel.alignment = .left
  1909. nameLabel.translatesAutoresizingMaskIntoConstraints = false
  1910. let bubble = makeChatBubbleContainer(text: text, isUser: false)
  1911. let column = NSStackView(views: [nameLabel, bubble])
  1912. column.orientation = .vertical
  1913. column.spacing = 6
  1914. // Leading keeps the assistant label, text bubble, and job cards hugging the left (after the avatar); `.width` was letting narrow intrinsic widths sit on the trailing side so AI read like a second “user” column.
  1915. column.alignment = .leading
  1916. column.translatesAutoresizingMaskIntoConstraints = false
  1917. if let jobs, !jobs.isEmpty {
  1918. trailingLoadMoreJobsRow?.removeFromSuperview()
  1919. trailingLoadMoreJobsRow = nil
  1920. trailingLoadMoreJobsButton = nil
  1921. let jobsStack = makeChatJobsStackView(jobs: jobs)
  1922. column.addArrangedSubview(jobsStack)
  1923. jobsStack.widthAnchor.constraint(equalTo: column.widthAnchor).isActive = true
  1924. let moreRow = makeLoadMoreJobsRowView()
  1925. column.addArrangedSubview(moreRow)
  1926. moreRow.widthAnchor.constraint(equalTo: column.widthAnchor).isActive = true
  1927. trailingLoadMoreJobsRow = moreRow
  1928. }
  1929. host.addSubview(avatar)
  1930. host.addSubview(column)
  1931. host.userInterfaceLayoutDirection = .leftToRight
  1932. NSLayoutConstraint.activate([
  1933. avatar.leadingAnchor.constraint(equalTo: host.leadingAnchor),
  1934. avatar.topAnchor.constraint(equalTo: host.topAnchor),
  1935. avatar.widthAnchor.constraint(equalToConstant: 36),
  1936. avatar.heightAnchor.constraint(equalToConstant: 36),
  1937. column.leadingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: 12),
  1938. column.trailingAnchor.constraint(equalTo: host.trailingAnchor),
  1939. column.topAnchor.constraint(equalTo: host.topAnchor),
  1940. column.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  1941. bubble.widthAnchor.constraint(lessThanOrEqualTo: host.widthAnchor, multiplier: 0.78)
  1942. ])
  1943. }
  1944. private func makeChatBubbleContainer(text: String, isUser: Bool) -> NSView {
  1945. let container = NSView()
  1946. container.translatesAutoresizingMaskIntoConstraints = false
  1947. container.wantsLayer = true
  1948. container.layer?.cornerRadius = 14
  1949. if #available(macOS 11.0, *) {
  1950. container.layer?.cornerCurve = .continuous
  1951. }
  1952. container.layer?.masksToBounds = true
  1953. if isUser {
  1954. container.layer?.backgroundColor = Theme.brandBlue.cgColor
  1955. } else {
  1956. container.layer?.backgroundColor = Theme.chromeBackground.cgColor
  1957. container.layer?.borderWidth = 1
  1958. container.layer?.borderColor = Theme.border.cgColor
  1959. }
  1960. let label = ChatBubbleLabel(wrappingLabelWithString: text)
  1961. label.font = .systemFont(ofSize: 13.5, weight: .regular)
  1962. label.textColor = isUser ? .white : Theme.primaryText
  1963. label.maximumNumberOfLines = 0
  1964. label.lineBreakMode = .byWordWrapping
  1965. label.alignment = .left
  1966. label.translatesAutoresizingMaskIntoConstraints = false
  1967. label.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1968. label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  1969. container.addSubview(label)
  1970. NSLayoutConstraint.activate([
  1971. label.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 14),
  1972. label.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -14),
  1973. label.topAnchor.constraint(equalTo: container.topAnchor, constant: 10),
  1974. label.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -10)
  1975. ])
  1976. return container
  1977. }
  1978. private func makeAssistantAvatarView() -> NSView {
  1979. let view = NSView()
  1980. view.translatesAutoresizingMaskIntoConstraints = false
  1981. view.wantsLayer = true
  1982. view.layer?.cornerRadius = 18
  1983. if #available(macOS 11.0, *) {
  1984. view.layer?.cornerCurve = .continuous
  1985. }
  1986. view.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  1987. view.layer?.borderWidth = 1
  1988. view.layer?.borderColor = Theme.proCardBorder.cgColor
  1989. let icon = NSImageView()
  1990. icon.translatesAutoresizingMaskIntoConstraints = false
  1991. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .semibold)
  1992. icon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: "AI Job Finder")
  1993. icon.contentTintColor = Theme.brandBlue
  1994. view.addSubview(icon)
  1995. NSLayoutConstraint.activate([
  1996. icon.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  1997. icon.centerYAnchor.constraint(equalTo: view.centerYAnchor)
  1998. ])
  1999. return view
  2000. }
  2001. private func makeLoadMoreJobsRowView() -> NSView {
  2002. let row = NSView()
  2003. row.translatesAutoresizingMaskIntoConstraints = false
  2004. let button = HoverableButton()
  2005. button.pointerCursor = true
  2006. button.title = "Show more jobs"
  2007. button.font = .systemFont(ofSize: 12, weight: .semibold)
  2008. button.bezelStyle = .rounded
  2009. button.controlSize = .regular
  2010. button.contentTintColor = Theme.brandBlue
  2011. button.target = self
  2012. button.action = #selector(didTapLoadMoreJobs)
  2013. button.translatesAutoresizingMaskIntoConstraints = false
  2014. trailingLoadMoreJobsButton = button
  2015. row.addSubview(button)
  2016. NSLayoutConstraint.activate([
  2017. button.leadingAnchor.constraint(equalTo: row.leadingAnchor),
  2018. button.topAnchor.constraint(equalTo: row.topAnchor, constant: 2),
  2019. button.bottomAnchor.constraint(equalTo: row.bottomAnchor, constant: -2)
  2020. ])
  2021. return row
  2022. }
  2023. private func makeChatJobsStackView(jobs: [JobListing]) -> ChatJobsStackView {
  2024. let stack = ChatJobsStackView()
  2025. stack.orientation = .vertical
  2026. stack.spacing = 10
  2027. stack.alignment = .width
  2028. stack.distribution = .fill
  2029. stack.translatesAutoresizingMaskIntoConstraints = false
  2030. for job in jobs {
  2031. let card = makeJobListingCard(job, context: .homeSearchResults)
  2032. stack.addArrangedSubview(card)
  2033. card.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
  2034. }
  2035. return stack
  2036. }
  2037. private func scrollChatToBottom() {
  2038. let maxY = max(0, chatDocumentView.bounds.height - chatScrollView.contentView.bounds.height)
  2039. chatScrollView.contentView.scroll(to: NSPoint(x: 0, y: maxY))
  2040. chatScrollView.reflectScrolledClipView(chatScrollView.contentView)
  2041. }
  2042. /// Scrolls so the top of `view` sits at the top of the chat clip (flipped document coords). Long assistant replies stay readable from the first line instead of jumping to the end.
  2043. private func scrollChatToShowTopOfView(_ view: NSView) {
  2044. chatDocumentView.layoutSubtreeIfNeeded()
  2045. view.layoutSubtreeIfNeeded()
  2046. let doc = chatDocumentView
  2047. let visibleHeight = chatScrollView.contentView.bounds.height
  2048. let docHeight = doc.bounds.height
  2049. guard docHeight > 0, visibleHeight > 0 else {
  2050. scrollChatToBottom()
  2051. return
  2052. }
  2053. let rectInDoc = view.convert(view.bounds, to: doc)
  2054. let maxY = max(0, docHeight - visibleHeight)
  2055. let targetY = min(max(0, rectInDoc.minY), maxY)
  2056. chatScrollView.contentView.scroll(to: NSPoint(x: 0, y: targetY))
  2057. chatScrollView.reflectScrolledClipView(chatScrollView.contentView)
  2058. }
  2059. private func addInlineChatThinkingRow() {
  2060. removeInlineChatThinkingRow()
  2061. let host = NSView()
  2062. host.translatesAutoresizingMaskIntoConstraints = false
  2063. let indicator = ChatThinkingIndicatorView(compact: false)
  2064. host.addSubview(indicator)
  2065. NSLayoutConstraint.activate([
  2066. indicator.leadingAnchor.constraint(equalTo: host.leadingAnchor, constant: 8),
  2067. indicator.topAnchor.constraint(equalTo: host.topAnchor),
  2068. indicator.bottomAnchor.constraint(equalTo: host.bottomAnchor, constant: -2)
  2069. ])
  2070. chatThinkingRowHost = host
  2071. chatStack.addArrangedSubview(host)
  2072. host.widthAnchor.constraint(equalTo: chatStack.widthAnchor).isActive = true
  2073. indicator.startAnimatingIfNeeded()
  2074. DispatchQueue.main.async { [weak self] in
  2075. self?.updateChatBubbleWidths()
  2076. self?.scrollChatToBottom()
  2077. }
  2078. }
  2079. private func removeInlineChatThinkingRow() {
  2080. guard let host = chatThinkingRowHost else { return }
  2081. for sub in host.subviews {
  2082. (sub as? ChatThinkingIndicatorView)?.stopAnimating()
  2083. }
  2084. chatStack.removeArrangedSubview(host)
  2085. host.removeFromSuperview()
  2086. chatThinkingRowHost = nil
  2087. }
  2088. private func setInputEnabled(_ enabled: Bool) {
  2089. jobKeywordsField.isEnabled = enabled
  2090. findJobsButton.isEnabled = enabled
  2091. findJobsButton.alphaValue = enabled ? 1 : 0.65
  2092. clearChatButton.isEnabled = enabled
  2093. clearChatButton.alphaValue = enabled ? 1 : 0.65
  2094. trailingLoadMoreJobsButton?.isEnabled = enabled
  2095. trailingLoadMoreJobsButton?.alphaValue = enabled ? 1 : 0.65
  2096. }
  2097. private func configureSidebar() {
  2098. let items = currentSidebarItems
  2099. sidebar.arrangedSubviews.forEach {
  2100. sidebar.removeArrangedSubview($0)
  2101. $0.removeFromSuperview()
  2102. }
  2103. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  2104. brand.font = .systemFont(ofSize: 18, weight: .bold)
  2105. brand.textColor = Theme.brandBlue
  2106. brand.alignment = .left
  2107. brand.maximumNumberOfLines = 2
  2108. // Tight multiline height in the sidebar stack (zero width makes intrinsic height unreliable).
  2109. brand.preferredMaxLayoutWidth = 194
  2110. sidebar.addArrangedSubview(brand)
  2111. sidebar.setCustomSpacing(10, after: brand)
  2112. items.enumerated().forEach { index, item in
  2113. let isSelected = index == selectedSidebarIndex
  2114. let rowHost = SidebarNavRowView { [weak self] in
  2115. self?.selectSidebarItem(at: index)
  2116. }
  2117. rowHost.translatesAutoresizingMaskIntoConstraints = false
  2118. rowHost.wantsLayer = true
  2119. rowHost.layer?.cornerRadius = 8
  2120. rowHost.restingBackgroundColor = isSelected ? Theme.selectionFill : nil
  2121. rowHost.hoverBackgroundColor = isSelected ? Theme.selectionFillHover : Theme.sidebarRowHoverFill
  2122. rowHost.setAccessibilityLabel(item.title)
  2123. rowHost.setAccessibilityRole(.button)
  2124. rowHost.setAccessibilitySelected(isSelected)
  2125. let row = NSStackView()
  2126. row.orientation = .horizontal
  2127. row.spacing = 8
  2128. row.alignment = .centerY
  2129. row.translatesAutoresizingMaskIntoConstraints = false
  2130. let icon = NSImageView()
  2131. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  2132. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  2133. icon.contentTintColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  2134. let text = NSTextField(labelWithString: item.title)
  2135. text.font = .systemFont(ofSize: 14, weight: .medium)
  2136. text.textColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  2137. text.refusesFirstResponder = true
  2138. row.addArrangedSubview(icon)
  2139. row.addArrangedSubview(text)
  2140. if let badge = item.badge {
  2141. let badgeField = NSTextField(labelWithString: badge)
  2142. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  2143. badgeField.textColor = Theme.primaryText
  2144. badgeField.wantsLayer = true
  2145. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  2146. badgeField.layer?.cornerRadius = 8
  2147. badgeField.alignment = .center
  2148. badgeField.maximumNumberOfLines = 1
  2149. badgeField.lineBreakMode = .byClipping
  2150. badgeField.refusesFirstResponder = true
  2151. badgeField.translatesAutoresizingMaskIntoConstraints = false
  2152. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  2153. row.addArrangedSubview(NSView())
  2154. row.addArrangedSubview(badgeField)
  2155. }
  2156. rowHost.addSubview(row)
  2157. NSLayoutConstraint.activate([
  2158. row.leadingAnchor.constraint(equalTo: rowHost.leadingAnchor, constant: 10),
  2159. row.trailingAnchor.constraint(equalTo: rowHost.trailingAnchor, constant: -10),
  2160. row.topAnchor.constraint(equalTo: rowHost.topAnchor, constant: 8),
  2161. row.bottomAnchor.constraint(equalTo: rowHost.bottomAnchor, constant: -8)
  2162. ])
  2163. rowHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  2164. sidebar.addArrangedSubview(rowHost)
  2165. let sidebarHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  2166. rowHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -sidebarHorizontalInset).isActive = true
  2167. }
  2168. let sidebarBottomSpacer = NSView()
  2169. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  2170. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  2171. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  2172. sidebar.addArrangedSubview(sidebarBottomSpacer)
  2173. let upgradeCard = NSView()
  2174. upgradeCard.translatesAutoresizingMaskIntoConstraints = false
  2175. upgradeCard.wantsLayer = true
  2176. upgradeCard.layer?.backgroundColor = Theme.proCardFill.cgColor
  2177. upgradeCard.layer?.cornerRadius = 14
  2178. upgradeCard.layer?.borderWidth = 1
  2179. upgradeCard.layer?.borderColor = Theme.proCardBorder.cgColor
  2180. upgradeCard.layer?.masksToBounds = true
  2181. let accentBar = NSView()
  2182. accentBar.translatesAutoresizingMaskIntoConstraints = false
  2183. accentBar.wantsLayer = true
  2184. accentBar.layer?.backgroundColor = Theme.proAccent.cgColor
  2185. let inner = NSStackView()
  2186. inner.translatesAutoresizingMaskIntoConstraints = false
  2187. inner.orientation = .vertical
  2188. inner.spacing = 10
  2189. inner.alignment = .centerX
  2190. let proIcon = NSImageView()
  2191. proIcon.translatesAutoresizingMaskIntoConstraints = false
  2192. proIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  2193. proIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: nil)
  2194. proIcon.contentTintColor = Theme.proAccent
  2195. let proEyebrow = NSTextField(labelWithString: "Premium")
  2196. proEyebrow.font = .systemFont(ofSize: 11, weight: .heavy)
  2197. proEyebrow.textColor = Theme.proAccent
  2198. proEyebrow.alignment = .center
  2199. let eyebrowRow = NSStackView(views: [proIcon, proEyebrow])
  2200. eyebrowRow.orientation = .horizontal
  2201. eyebrowRow.spacing = 6
  2202. eyebrowRow.alignment = .centerY
  2203. let headline = NSTextField(labelWithString: "Upgrade to Pro")
  2204. headline.font = .systemFont(ofSize: 16, weight: .bold)
  2205. headline.textColor = Theme.primaryText
  2206. headline.alignment = .center
  2207. let upgradeDescription = NSTextField(wrappingLabelWithString: "Unlimited AI matches, smart alerts, and interview prep—all in one place.")
  2208. upgradeDescription.font = .systemFont(ofSize: 12, weight: .regular)
  2209. upgradeDescription.textColor = Theme.secondaryText
  2210. upgradeDescription.alignment = .center
  2211. // Sidebar content width is the fixed sidebar width minus horizontal edge insets; card must stay within that band.
  2212. let cardWidth: CGFloat = 186
  2213. let innerContentWidth = cardWidth - 28
  2214. upgradeDescription.preferredMaxLayoutWidth = innerContentWidth
  2215. let upgradeButton = HoverableButton(title: "Try Pro", target: self, action: #selector(didTapUpgradeToPro))
  2216. upgradeButton.isBordered = false
  2217. upgradeButton.bezelStyle = .rounded
  2218. upgradeButton.font = .systemFont(ofSize: 13, weight: .bold)
  2219. upgradeButton.contentTintColor = Theme.proCTAText
  2220. upgradeButton.alignment = .center
  2221. upgradeButton.wantsLayer = true
  2222. upgradeButton.layer?.backgroundColor = Theme.proCTABackground.cgColor
  2223. upgradeButton.layer?.cornerRadius = 20
  2224. upgradeButton.translatesAutoresizingMaskIntoConstraints = false
  2225. upgradeButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  2226. upgradeButton.pointerCursor = true
  2227. upgradeButton.hoverHandler = { [weak upgradeButton] hovering in
  2228. upgradeButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.proCTABackground).cgColor
  2229. }
  2230. inner.addArrangedSubview(eyebrowRow)
  2231. inner.addArrangedSubview(headline)
  2232. inner.addArrangedSubview(upgradeDescription)
  2233. inner.addArrangedSubview(upgradeButton)
  2234. upgradeCard.addSubview(accentBar)
  2235. upgradeCard.addSubview(inner)
  2236. NSLayoutConstraint.activate([
  2237. upgradeCard.widthAnchor.constraint(equalToConstant: cardWidth),
  2238. accentBar.topAnchor.constraint(equalTo: upgradeCard.topAnchor),
  2239. accentBar.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor),
  2240. accentBar.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor),
  2241. accentBar.heightAnchor.constraint(equalToConstant: 2),
  2242. inner.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor, constant: 14),
  2243. inner.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor, constant: -14),
  2244. inner.topAnchor.constraint(equalTo: accentBar.bottomAnchor, constant: 12),
  2245. inner.bottomAnchor.constraint(equalTo: upgradeCard.bottomAnchor, constant: -14),
  2246. upgradeButton.widthAnchor.constraint(equalTo: inner.widthAnchor)
  2247. ])
  2248. sidebar.addArrangedSubview(upgradeCard)
  2249. sidebarUpgradeCard = upgradeCard
  2250. sidebarUpgradeHeadline = headline
  2251. sidebarUpgradeDescription = upgradeDescription
  2252. sidebarUpgradeButton = upgradeButton
  2253. applyProSubscriptionToSidebar()
  2254. }
  2255. @objc private func didTapUpgradeToPro() {
  2256. Task { @MainActor in
  2257. await SubscriptionStore.shared.refreshEntitlements(deep: true)
  2258. applyProSubscriptionToSidebar()
  2259. presentPremiumPlansSheet()
  2260. }
  2261. }
  2262. private func selectSidebarItem(at index: Int) {
  2263. dismissIndeedJobBrowserEmbedded()
  2264. guard index >= 0, index < currentSidebarItems.count else { return }
  2265. let selectingHome = isHomeSidebarIndex(index)
  2266. if index == selectedSidebarIndex {
  2267. if selectingHome {
  2268. applyHomeState()
  2269. }
  2270. return
  2271. }
  2272. selectedSidebarIndex = index
  2273. configureSidebar()
  2274. updateMainContentVisibility()
  2275. if selectingHome {
  2276. applyHomeState()
  2277. }
  2278. }
  2279. }
  2280. private struct ChatMessage: Codable {
  2281. let role: String
  2282. let content: String
  2283. }
  2284. private final class OpenAIJobSearchService {
  2285. private let endpoint = URL(string: "https://api.openai.com/v1/responses")!
  2286. private let session = URLSession(configuration: .ephemeral)
  2287. func searchJobs(query: String, conversation: [ChatMessage], maxJobs: Int, completion: @escaping (Result<JobSearchOutput, Error>) -> Void) {
  2288. let jobLimit = max(1, min(maxJobs, 25))
  2289. let apiKey = OpenAIConfiguration.apiKey
  2290. guard OpenAIConfiguration.hasAPIKey else {
  2291. completion(.failure(NSError(
  2292. domain: "OpenAIJobSearchService",
  2293. code: 1,
  2294. userInfo: [NSLocalizedDescriptionKey: "Missing API key. Set OPENAI_API_KEY in Xcode Build Settings."]
  2295. )))
  2296. return
  2297. }
  2298. var request = URLRequest(url: endpoint)
  2299. request.httpMethod = "POST"
  2300. request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  2301. request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
  2302. request.timeoutInterval = 45
  2303. let recentContext = conversation.suffix(8)
  2304. .map { "\($0.role.uppercased()): \($0.content)" }
  2305. .joined(separator: "\n")
  2306. let contextBlock: String
  2307. if recentContext.isEmpty {
  2308. contextBlock = "No prior conversation context."
  2309. } else {
  2310. contextBlock = recentContext
  2311. }
  2312. let developerInstructions = """
  2313. You are the job-search backend for an Indeed-focused desktop app. Always use web search, but only to discover roles that are listed on Indeed (indeed.com and regional Indeed sites such as indeed.co.uk, ca.indeed.com, etc.).
  2314. Do not include jobs sourced only from LinkedIn, Glassdoor, company career pages (unless the same opening is clearly on Indeed with an Indeed URL), Google Jobs aggregates, or other job boards.
  2315. Your final assistant message must be JSON that strictly matches the configured response schema (one object with a "jobs" array). Do not add markdown, code fences, or conversational prose outside that JSON.
  2316. Each job entry needs a title, a single-sentence description, and a "url" string. Prefer a stable Indeed **search results** URL on the correct regional domain (path `/jobs` with a `q=` query built from the listing title, company, and location—e.g. `https://www.indeed.com/jobs?q=…&l=…`). Never invent or guess job keys (`jk=`), click IDs, or viewjob URLs you did not copy exactly from live search results—wrong permalinks show 404 inside the app. Use an empty string for "url" only when you cannot construct any Indeed URL (omit the listing if it is not on Indeed).
  2317. Return at most \(jobLimit) distinct listings. If the conversation context already lists jobs, do not repeat the same titles or URLs when the user asks for more—it is fine to return fewer than \(jobLimit) new results.
  2318. Full sentences such as "looking for an AI developer job" are still job queries: always populate "jobs" from Indeed-oriented web search rather than answering with chatty text alone.
  2319. """
  2320. let userInput = """
  2321. Continue this same job-search conversation. Use prior context when useful. The line "Latest user query" is the primary task; earlier USER/ASSISTANT lines are context (previous role, location, or results).
  2322. Conversation context:
  2323. \(contextBlock)
  2324. Latest user query: "\(query)"
  2325. If the user refines pay, seniority, work location, or similar, run a new Indeed-focused search that applies those constraints to the same career topic as in the context.
  2326. """
  2327. let payload = OpenAIJobSearchAPIRequest.jobSearchPayload(
  2328. model: "gpt-4o-mini",
  2329. instructions: developerInstructions,
  2330. input: userInput,
  2331. tools: [OpenAIResponsesTool(type: "web_search_preview")],
  2332. jobLimit: jobLimit
  2333. )
  2334. do {
  2335. request.httpBody = try JSONEncoder().encode(payload)
  2336. } catch {
  2337. completion(.failure(error))
  2338. return
  2339. }
  2340. session.dataTask(with: request) { data, response, error in
  2341. if let error {
  2342. completion(.failure(error))
  2343. return
  2344. }
  2345. guard let data else {
  2346. completion(.failure(NSError(
  2347. domain: "OpenAIJobSearchService",
  2348. code: 2,
  2349. userInfo: [NSLocalizedDescriptionKey: "API returned an empty response."]
  2350. )))
  2351. return
  2352. }
  2353. if let http = response as? HTTPURLResponse, !(200...299).contains(http.statusCode) {
  2354. if let apiError = try? JSONDecoder().decode(OpenAIAPIErrorResponse.self, from: data) {
  2355. completion(.failure(NSError(
  2356. domain: "OpenAIJobSearchService",
  2357. code: http.statusCode,
  2358. userInfo: [NSLocalizedDescriptionKey: apiError.error.message]
  2359. )))
  2360. } else {
  2361. completion(.failure(NSError(
  2362. domain: "OpenAIJobSearchService",
  2363. code: http.statusCode,
  2364. userInfo: [NSLocalizedDescriptionKey: "Job search request failed with status \(http.statusCode)."]
  2365. )))
  2366. }
  2367. return
  2368. }
  2369. do {
  2370. let modelText = try Self.extractModelTextFromResponsesBody(data)
  2371. let trimmed = modelText.trimmingCharacters(in: .whitespacesAndNewlines)
  2372. guard !trimmed.isEmpty else {
  2373. throw NSError(
  2374. domain: "OpenAIJobSearchService",
  2375. code: 4,
  2376. userInfo: [NSLocalizedDescriptionKey: "The API returned an empty text payload."]
  2377. )
  2378. }
  2379. let jobs = try Self.parseJobListings(fromModelText: trimmed)
  2380. .filter(Self.jobListingUsesIndeedOrEmptyURL)
  2381. .map(Self.normalizedJobListing)
  2382. completion(.success(JobSearchOutput(jobs: jobs)))
  2383. } catch {
  2384. completion(.failure(error))
  2385. }
  2386. }.resume()
  2387. }
  2388. /// Walks the `/v1/responses` JSON without strict Codable so tool calls, refusals, and future output item types do not break decoding. Collects only `output_text` segments from assistant `message` items (and any other output items that expose a `content` array).
  2389. private static func extractModelTextFromResponsesBody(_ data: Data) throws -> String {
  2390. let rootObject: Any
  2391. do {
  2392. rootObject = try JSONSerialization.jsonObject(with: data, options: [])
  2393. } catch {
  2394. throw NSError(
  2395. domain: "OpenAIJobSearchService",
  2396. code: 5,
  2397. userInfo: [NSLocalizedDescriptionKey: "The job search service returned data that was not valid JSON."]
  2398. )
  2399. }
  2400. guard let root = rootObject as? [String: Any] else {
  2401. throw NSError(
  2402. domain: "OpenAIJobSearchService",
  2403. code: 5,
  2404. userInfo: [NSLocalizedDescriptionKey: "Unexpected response shape from the job search service."]
  2405. )
  2406. }
  2407. if let status = root["status"] as? String {
  2408. if status == "failed" {
  2409. let message = (root["error"] as? [String: Any])?["message"] as? String ?? "The search request failed."
  2410. throw NSError(domain: "OpenAIJobSearchService", code: 7, userInfo: [NSLocalizedDescriptionKey: message])
  2411. }
  2412. if status == "incomplete",
  2413. let details = root["incomplete_details"] as? [String: Any],
  2414. let reason = details["reason"] as? String {
  2415. throw NSError(
  2416. domain: "OpenAIJobSearchService",
  2417. code: 8,
  2418. userInfo: [NSLocalizedDescriptionKey: "Search stopped early (\(reason)). Try a simpler query or try again."]
  2419. )
  2420. }
  2421. }
  2422. if let direct = root["output_text"] as? String {
  2423. let trimmed = direct.trimmingCharacters(in: .whitespacesAndNewlines)
  2424. if !trimmed.isEmpty { return trimmed }
  2425. }
  2426. guard let output = root["output"] as? [Any] else {
  2427. throw NSError(
  2428. domain: "OpenAIJobSearchService",
  2429. code: 9,
  2430. userInfo: [NSLocalizedDescriptionKey: "The search service returned no assistant text. Try again in a moment."]
  2431. )
  2432. }
  2433. var segments: [String] = []
  2434. for case let item as [String: Any] in output where (item["type"] as? String) == "message" {
  2435. collectOutputTextSegments(fromOutputItem: item, into: &segments)
  2436. }
  2437. if segments.isEmpty {
  2438. for case let item as [String: Any] in output {
  2439. collectOutputTextSegments(fromOutputItem: item, into: &segments)
  2440. }
  2441. }
  2442. let combined = segments.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
  2443. if !combined.isEmpty {
  2444. return combined
  2445. }
  2446. throw NSError(
  2447. domain: "OpenAIJobSearchService",
  2448. code: 9,
  2449. userInfo: [NSLocalizedDescriptionKey: "The model did not return readable job-search text. Try again."]
  2450. )
  2451. }
  2452. private static func collectOutputTextSegments(fromOutputItem item: [String: Any], into segments: inout [String]) {
  2453. guard let content = item["content"] as? [Any] else { return }
  2454. for case let part as [String: Any] in content {
  2455. guard (part["type"] as? String) == "output_text" else { continue }
  2456. if let s = part["text"] as? String {
  2457. segments.append(s)
  2458. } else if let nested = part["text"] as? [String: Any], let value = nested["value"] as? String {
  2459. segments.append(value)
  2460. }
  2461. }
  2462. }
  2463. private static func normalizedJobListing(_ job: JobListing) -> JobListing {
  2464. let trimmedURL = job.url?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
  2465. if trimmedURL.isEmpty {
  2466. return JobListing(title: job.title, description: job.description, url: nil)
  2467. }
  2468. return JobListing(title: job.title, description: job.description, url: trimmedURL)
  2469. }
  2470. /// Drops listings whose `url` points at non-Indeed sites (e.g. LinkedIn) when the model ignores instructions.
  2471. private static func jobListingUsesIndeedOrEmptyURL(_ job: JobListing) -> Bool {
  2472. let trimmed = job.url?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
  2473. if trimmed.isEmpty { return true }
  2474. return isIndeedJobURL(trimmed)
  2475. }
  2476. /// Host looks like an official Indeed property (`indeed.com`, `www.indeed.co.uk`, `ca.indeed.com`, …), not `notindeed.com`.
  2477. private static func isIndeedJobURL(_ string: String) -> Bool {
  2478. guard let host = URL(string: string)?.host?.lowercased() else { return false }
  2479. if host == "indeed.com" { return true }
  2480. if host.hasPrefix("indeed.") { return true }
  2481. return host.contains(".indeed.")
  2482. }
  2483. private static func parseJobListings(fromModelText text: String) throws -> [JobListing] {
  2484. let stripped = text.trimmingCharacters(in: .whitespacesAndNewlines)
  2485. if stripped.hasPrefix("{"), let directData = stripped.data(using: .utf8),
  2486. let payload = try? JSONDecoder().decode(JobSearchResultsPayload.self, from: directData) {
  2487. return payload.jobs
  2488. }
  2489. let jsonString = extractJobJSONObjectString(from: text) ?? extractJSONObject(from: text)
  2490. let jsonData = Data(jsonString.utf8)
  2491. if let payload = try? JSONDecoder().decode(JobSearchResultsPayload.self, from: jsonData) {
  2492. return payload.jobs
  2493. }
  2494. if let listings = try? JSONDecoder().decode([JobListing].self, from: jsonData) {
  2495. return listings
  2496. }
  2497. if let obj = try? JSONSerialization.jsonObject(with: jsonData, options: []) {
  2498. if let dict = obj as? [String: Any], let jobs = jobListings(fromFlexibleJSONObject: dict) {
  2499. return jobs
  2500. }
  2501. if let arr = obj as? [[String: Any]], let jobs = jobListings(fromFlexibleJobArray: arr) {
  2502. return jobs
  2503. }
  2504. }
  2505. throw NSError(
  2506. domain: "OpenAIJobSearchService",
  2507. code: 10,
  2508. userInfo: [NSLocalizedDescriptionKey: "The assistant reply did not include job listings in the expected JSON format. Try your search again."]
  2509. )
  2510. }
  2511. private static func jobListings(fromFlexibleJSONObject dict: [String: Any]) -> [JobListing]? {
  2512. for (key, value) in dict {
  2513. guard key.caseInsensitiveCompare("jobs") == .orderedSame, let arr = value as? [[String: Any]] else { continue }
  2514. if let jobs = jobListings(fromFlexibleJobArray: arr) { return jobs }
  2515. }
  2516. for wrapKey in ["data", "result", "results", "payload"] {
  2517. if let inner = dict[wrapKey] as? [String: Any], let nested = jobListings(fromFlexibleJSONObject: inner) {
  2518. return nested
  2519. }
  2520. }
  2521. return nil
  2522. }
  2523. private static func jobListings(fromFlexibleJobArray jobs: [[String: Any]]) -> [JobListing]? {
  2524. var out: [JobListing] = []
  2525. for item in jobs {
  2526. guard let title = firstString(valuesForKeys: ["title", "job_title", "name", "position"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines),
  2527. !title.isEmpty,
  2528. let desc = firstString(valuesForKeys: ["description", "snippet", "summary", "desc"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines),
  2529. !desc.isEmpty else { continue }
  2530. let urlRaw = firstString(valuesForKeys: ["url", "link", "apply_url", "job_url"], in: item)?.trimmingCharacters(in: .whitespacesAndNewlines)
  2531. let url: String? = (urlRaw?.isEmpty == true) ? nil : urlRaw
  2532. out.append(JobListing(title: title, description: desc, url: url))
  2533. }
  2534. return out.isEmpty ? nil : out
  2535. }
  2536. private static func firstString(valuesForKeys keys: [String], in dict: [String: Any]) -> String? {
  2537. for wanted in keys {
  2538. for (dk, dv) in dict {
  2539. guard dk.caseInsensitiveCompare(wanted) == .orderedSame, let s = dv as? String else { continue }
  2540. return s
  2541. }
  2542. }
  2543. return nil
  2544. }
  2545. private static func stripMarkdownCodeFence(_ text: String) -> String {
  2546. var s = text.trimmingCharacters(in: .whitespacesAndNewlines)
  2547. guard s.hasPrefix("```") else { return s }
  2548. s.removeFirst(3)
  2549. if s.lowercased().hasPrefix("json") {
  2550. s.removeFirst(4)
  2551. }
  2552. s = s.trimmingCharacters(in: .whitespacesAndNewlines)
  2553. if let fence = s.range(of: "```", options: .backwards) {
  2554. s = String(s[..<fence.lowerBound])
  2555. }
  2556. return s.trimmingCharacters(in: .whitespacesAndNewlines)
  2557. }
  2558. private static func balancedJSONObject(from openBrace: String.Index, in s: String) -> String? {
  2559. var depth = 0
  2560. var inString = false
  2561. var escaped = false
  2562. var i = openBrace
  2563. while i < s.endIndex {
  2564. let ch = s[i]
  2565. if inString {
  2566. if escaped {
  2567. escaped = false
  2568. } else if ch == "\\" {
  2569. escaped = true
  2570. } else if ch == "\"" {
  2571. inString = false
  2572. }
  2573. } else {
  2574. switch ch {
  2575. case "\"":
  2576. inString = true
  2577. case "{":
  2578. depth += 1
  2579. case "}":
  2580. depth -= 1
  2581. if depth == 0 {
  2582. return String(s[openBrace...i])
  2583. }
  2584. default:
  2585. break
  2586. }
  2587. }
  2588. i = s.index(after: i)
  2589. }
  2590. return nil
  2591. }
  2592. /// Prefers the JSON object that contains a `"jobs"` key so prose before/after the payload does not confuse the decoder.
  2593. private static func extractJobJSONObjectString(from text: String) -> String? {
  2594. let s = stripMarkdownCodeFence(text)
  2595. guard let jobsRange = s.range(of: "\"jobs\"", options: .caseInsensitive) else { return nil }
  2596. let head = s[..<jobsRange.lowerBound]
  2597. guard let open = head.lastIndex(of: "{") else { return nil }
  2598. return balancedJSONObject(from: open, in: s)
  2599. }
  2600. private static func extractJSONObject(from text: String) -> String {
  2601. if let extracted = extractJobJSONObjectString(from: text) {
  2602. return extracted
  2603. }
  2604. let stripped = stripMarkdownCodeFence(text)
  2605. if let first = stripped.firstIndex(of: "{"), let balanced = balancedJSONObject(from: first, in: stripped) {
  2606. return balanced
  2607. }
  2608. if let range = text.range(of: "\\{[\\s\\S]*\\}", options: .regularExpression) {
  2609. return String(text[range])
  2610. }
  2611. return text
  2612. }
  2613. }
  2614. /// Responses API request with structured JSON output so web-search replies cannot omit the `jobs` schema.
  2615. private struct OpenAIJobSearchAPIRequest: Encodable {
  2616. let model: String
  2617. let instructions: String
  2618. let input: String
  2619. let tools: [OpenAIResponsesTool]
  2620. let text: OpenAITextOutputConfig
  2621. static func jobSearchPayload(
  2622. model: String,
  2623. instructions: String,
  2624. input: String,
  2625. tools: [OpenAIResponsesTool],
  2626. jobLimit: Int
  2627. ) -> OpenAIJobSearchAPIRequest {
  2628. let itemProperties = OpenAIJobSearchJobItemProperties(
  2629. title: OpenAIJSONSchemaStringField(type: "string", description: "Job title as shown on the Indeed listing."),
  2630. description: OpenAIJSONSchemaStringField(type: "string", description: "One concise sentence summarizing the role from the Indeed posting."),
  2631. url: OpenAIJSONSchemaStringField(type: "string", description: "Indeed search URL (https, path /jobs, query q=…) on indeed.com or the correct regional Indeed host; never fabricate viewjob/jk links. Empty string only if no Indeed URL exists—never use LinkedIn, Glassdoor, or other boards.")
  2632. )
  2633. let itemSchema = OpenAIJobSearchJobItemSchema(
  2634. type: "object",
  2635. properties: itemProperties,
  2636. required: ["title", "description", "url"],
  2637. additionalProperties: false
  2638. )
  2639. let jobsProperty = OpenAIJobSearchJobsArrayProperty(
  2640. type: "array",
  2641. description: "Up to \(jobLimit) jobs found on Indeed via web search; use an empty array if none are found. Do not include listings that only exist off Indeed.",
  2642. items: itemSchema
  2643. )
  2644. let rootProperties = OpenAIJobSearchRootProperties(jobs: jobsProperty)
  2645. let rootSchema = OpenAIJobSearchRootSchema(
  2646. type: "object",
  2647. properties: rootProperties,
  2648. required: ["jobs"],
  2649. additionalProperties: false
  2650. )
  2651. let format = OpenAIJobSearchResponseJSONSchemaFormat(
  2652. type: "json_schema",
  2653. name: "job_search_results",
  2654. strict: true,
  2655. schema: rootSchema
  2656. )
  2657. return OpenAIJobSearchAPIRequest(
  2658. model: model,
  2659. instructions: instructions,
  2660. input: input,
  2661. tools: tools,
  2662. text: OpenAITextOutputConfig(format: format)
  2663. )
  2664. }
  2665. }
  2666. private struct OpenAITextOutputConfig: Encodable {
  2667. let format: OpenAIJobSearchResponseJSONSchemaFormat
  2668. }
  2669. private struct OpenAIJobSearchResponseJSONSchemaFormat: Encodable {
  2670. let type: String
  2671. let name: String
  2672. let strict: Bool
  2673. let schema: OpenAIJobSearchRootSchema
  2674. }
  2675. private struct OpenAIJobSearchRootSchema: Encodable {
  2676. let type: String
  2677. let properties: OpenAIJobSearchRootProperties
  2678. let required: [String]
  2679. let additionalProperties: Bool
  2680. }
  2681. private struct OpenAIJobSearchRootProperties: Encodable {
  2682. let jobs: OpenAIJobSearchJobsArrayProperty
  2683. }
  2684. private struct OpenAIJobSearchJobsArrayProperty: Encodable {
  2685. let type: String
  2686. let description: String
  2687. let items: OpenAIJobSearchJobItemSchema
  2688. }
  2689. private struct OpenAIJobSearchJobItemSchema: Encodable {
  2690. let type: String
  2691. let properties: OpenAIJobSearchJobItemProperties
  2692. let required: [String]
  2693. let additionalProperties: Bool
  2694. }
  2695. private struct OpenAIJobSearchJobItemProperties: Encodable {
  2696. let title: OpenAIJSONSchemaStringField
  2697. let description: OpenAIJSONSchemaStringField
  2698. let url: OpenAIJSONSchemaStringField
  2699. }
  2700. private struct OpenAIJSONSchemaStringField: Encodable {
  2701. let type: String
  2702. let description: String
  2703. }
  2704. private struct OpenAIResponsesTool: Codable {
  2705. let type: String
  2706. }
  2707. private struct JobSearchResultsPayload: Codable {
  2708. let jobs: [JobListing]
  2709. }
  2710. private struct JobSearchOutput {
  2711. let jobs: [JobListing]
  2712. }
  2713. private struct OpenAIAPIErrorResponse: Codable {
  2714. let error: APIError
  2715. struct APIError: Codable {
  2716. let message: String
  2717. }
  2718. }
  2719. /// Decorative waves and faint sparkles behind the welcome hero (reference layout).
  2720. private final class WelcomeHeroBackgroundView: NSView {
  2721. /// Stroke color for side waves (pastel blue).
  2722. var waveTint = NSColor(srgbRed: 186 / 255, green: 210 / 255, blue: 253 / 255, alpha: 1)
  2723. override var isFlipped: Bool { true }
  2724. override func draw(_ dirtyRect: NSRect) {
  2725. NSColor.clear.setFill()
  2726. bounds.fill()
  2727. guard bounds.width > 24, bounds.height > 24 else { return }
  2728. drawSideWaves(in: bounds, isLeft: true)
  2729. drawSideWaves(in: bounds, isLeft: false)
  2730. drawAmbientSparkles(in: bounds)
  2731. }
  2732. private func drawSideWaves(in bounds: NSRect, isLeft: Bool) {
  2733. for i in 0..<9 {
  2734. let path = NSBezierPath()
  2735. path.lineWidth = 1
  2736. path.lineCapStyle = .round
  2737. let phase = CGFloat(i) * 0.88
  2738. let base = CGFloat(i + 1) * 11 + 4
  2739. var first = true
  2740. for y in stride(from: CGFloat(0), through: bounds.height, by: 2.8) {
  2741. let wobble = sin(y * 0.048 + phase) * (4 + CGFloat(i % 5))
  2742. let x = isLeft ? (base + wobble) : (bounds.width - base - wobble)
  2743. let point = NSPoint(x: x, y: y)
  2744. if first {
  2745. path.move(to: point)
  2746. first = false
  2747. } else {
  2748. path.line(to: point)
  2749. }
  2750. }
  2751. let fade = 1 - CGFloat(i) / 10
  2752. waveTint.withAlphaComponent((0.09 + CGFloat(i % 3) * 0.022) * fade).setStroke()
  2753. path.stroke()
  2754. }
  2755. }
  2756. private func drawAmbientSparkles(in bounds: NSRect) {
  2757. let accent = NSColor(srgbRed: 0, green: 82 / 255, blue: 204 / 255, alpha: 1)
  2758. let specs: [(CGFloat, CGFloat, CGFloat, CGFloat)] = [
  2759. (0.06, 0.14, 9, 0.28),
  2760. (0.94, 0.12, 12, 0.34),
  2761. (0.18, 0.42, 5, 0.18),
  2762. (0.86, 0.44, 6, 0.2),
  2763. (0.5, 0.06, 7, 0.15)
  2764. ]
  2765. for (nx, ny, size, a) in specs {
  2766. let center = NSPoint(x: bounds.width * nx, y: bounds.height * ny)
  2767. fillFourPointStar(center: center, radius: size, color: accent.withAlphaComponent(a))
  2768. }
  2769. }
  2770. private func fillFourPointStar(center: NSPoint, radius: CGFloat, color: NSColor) {
  2771. let path = NSBezierPath()
  2772. for i in 0..<4 {
  2773. let angle = CGFloat(i) * .pi / 2 - .pi / 2
  2774. let x = center.x + cos(angle) * radius
  2775. let y = center.y + sin(angle) * radius
  2776. let point = NSPoint(x: x, y: y)
  2777. if i == 0 {
  2778. path.move(to: point)
  2779. } else {
  2780. path.line(to: point)
  2781. }
  2782. }
  2783. path.close()
  2784. color.setFill()
  2785. path.fill()
  2786. }
  2787. }
  2788. /// Circular pastel well with three sparkle symbols (reference: layered stars of different sizes).
  2789. private final class WelcomeSparkleClusterView: NSView {
  2790. private let diameter: CGFloat = 56
  2791. override var intrinsicContentSize: NSSize {
  2792. NSSize(width: diameter, height: diameter)
  2793. }
  2794. init(iconWell: NSColor, tint: NSColor) {
  2795. super.init(frame: .zero)
  2796. translatesAutoresizingMaskIntoConstraints = false
  2797. wantsLayer = true
  2798. layer?.cornerRadius = diameter / 2
  2799. if #available(macOS 11.0, *) {
  2800. layer?.cornerCurve = .continuous
  2801. }
  2802. layer?.backgroundColor = iconWell.cgColor
  2803. let configs: [(CGFloat, NSFont.Weight, CGFloat, CGFloat)] = [
  2804. (17, .medium, 0, 0),
  2805. (11, .regular, -11, -9),
  2806. (9, .regular, 12, 10)
  2807. ]
  2808. let symbolName = Self.sparkleSymbolName()
  2809. for (size, weight, ox, oy) in configs {
  2810. let iv = NSImageView()
  2811. iv.translatesAutoresizingMaskIntoConstraints = false
  2812. iv.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: size, weight: weight)
  2813. iv.image = NSImage(systemSymbolName: symbolName, accessibilityDescription: nil)
  2814. iv.contentTintColor = tint
  2815. addSubview(iv)
  2816. NSLayoutConstraint.activate([
  2817. iv.centerXAnchor.constraint(equalTo: centerXAnchor, constant: ox),
  2818. iv.centerYAnchor.constraint(equalTo: centerYAnchor, constant: oy)
  2819. ])
  2820. }
  2821. }
  2822. private static func sparkleSymbolName() -> String {
  2823. if NSImage(systemSymbolName: "sparkle", accessibilityDescription: nil) != nil {
  2824. return "sparkle"
  2825. }
  2826. return "sparkles"
  2827. }
  2828. @available(*, unavailable)
  2829. required init?(coder: NSCoder) {
  2830. fatalError("init(coder:) has not been implemented")
  2831. }
  2832. }
  2833. /// Home welcome row: three tappable shortcuts that seed the main search field (reference: white cards, pastel icon well, arrow at bottom trailing).
  2834. private final class FeatureShortcutCardView: NSView {
  2835. private static let cardCornerRadius: CGFloat = 14
  2836. private weak var actionTarget: AnyObject?
  2837. private var actionSelector: Selector
  2838. init(symbolName: String, title: String, subtitle: String, target: AnyObject?, action: Selector) {
  2839. self.actionTarget = target
  2840. self.actionSelector = action
  2841. super.init(frame: .zero)
  2842. translatesAutoresizingMaskIntoConstraints = false
  2843. wantsLayer = true
  2844. layer?.cornerRadius = Self.cardCornerRadius
  2845. if #available(macOS 11.0, *) {
  2846. layer?.cornerCurve = .continuous
  2847. }
  2848. layer?.backgroundColor = NSColor.white.cgColor
  2849. layer?.masksToBounds = false
  2850. layer?.borderWidth = 1
  2851. // `#EDF2F7` — light card stroke.
  2852. layer?.borderColor = NSColor(srgbRed: 237 / 255, green: 242 / 255, blue: 247 / 255, alpha: 1).cgColor
  2853. layer?.shadowColor = NSColor.black.withAlphaComponent(0.06).cgColor
  2854. layer?.shadowOffset = CGSize(width: 0, height: 2)
  2855. layer?.shadowRadius = 12
  2856. layer?.shadowOpacity = 1
  2857. // `#0052CC` — primary title / icons / arrow (matches welcome hero reference).
  2858. let primaryBlue = NSColor(srgbRed: 0, green: 82 / 255, blue: 204 / 255, alpha: 1)
  2859. // `#EBF2FF` — circular icon well.
  2860. let iconWellColor = NSColor(srgbRed: 235 / 255, green: 242 / 255, blue: 255 / 255, alpha: 1)
  2861. // `#5D6D7E` — muted description.
  2862. let secondary = NSColor(srgbRed: 93 / 255, green: 109 / 255, blue: 126 / 255, alpha: 1)
  2863. let iconSize: CGFloat = 40
  2864. let iconHost = NSView()
  2865. iconHost.translatesAutoresizingMaskIntoConstraints = false
  2866. iconHost.wantsLayer = true
  2867. iconHost.layer?.backgroundColor = iconWellColor.cgColor
  2868. iconHost.layer?.cornerRadius = iconSize / 2
  2869. let icon = NSImageView()
  2870. icon.translatesAutoresizingMaskIntoConstraints = false
  2871. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 16, weight: .regular)
  2872. icon.image = NSImage(systemSymbolName: symbolName, accessibilityDescription: nil)
  2873. icon.contentTintColor = primaryBlue
  2874. iconHost.addSubview(icon)
  2875. let titleField = NSTextField(wrappingLabelWithString: title)
  2876. titleField.font = .systemFont(ofSize: 15, weight: .bold)
  2877. titleField.textColor = primaryBlue
  2878. titleField.maximumNumberOfLines = 1
  2879. titleField.isEditable = false
  2880. titleField.isBordered = false
  2881. titleField.drawsBackground = false
  2882. titleField.alignment = .left
  2883. let subtitleField = NSTextField(wrappingLabelWithString: subtitle)
  2884. subtitleField.font = .systemFont(ofSize: 12, weight: .regular)
  2885. subtitleField.textColor = secondary
  2886. subtitleField.maximumNumberOfLines = 2
  2887. subtitleField.isEditable = false
  2888. subtitleField.isBordered = false
  2889. subtitleField.drawsBackground = false
  2890. subtitleField.alignment = .left
  2891. subtitleField.lineBreakMode = .byWordWrapping
  2892. subtitleField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  2893. subtitleField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  2894. let chevron = NSImageView()
  2895. chevron.translatesAutoresizingMaskIntoConstraints = false
  2896. chevron.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
  2897. chevron.image = NSImage(systemSymbolName: "arrow.right", accessibilityDescription: nil)
  2898. chevron.contentTintColor = primaryBlue
  2899. chevron.setContentHuggingPriority(.required, for: .horizontal)
  2900. chevron.setContentCompressionResistancePriority(.required, for: .horizontal)
  2901. let subtitleRow = NSStackView(views: [subtitleField, chevron])
  2902. subtitleRow.orientation = .horizontal
  2903. subtitleRow.spacing = 10
  2904. subtitleRow.alignment = .bottom
  2905. subtitleRow.distribution = .fill
  2906. subtitleRow.translatesAutoresizingMaskIntoConstraints = false
  2907. let textColumn = NSStackView(views: [titleField, subtitleRow])
  2908. textColumn.orientation = .vertical
  2909. textColumn.spacing = 6
  2910. textColumn.alignment = .leading
  2911. textColumn.translatesAutoresizingMaskIntoConstraints = false
  2912. textColumn.setContentHuggingPriority(.defaultLow, for: .horizontal)
  2913. textColumn.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  2914. iconHost.setContentHuggingPriority(.required, for: .horizontal)
  2915. iconHost.setContentCompressionResistancePriority(.required, for: .horizontal)
  2916. let row = NSStackView(views: [iconHost, textColumn])
  2917. row.orientation = .horizontal
  2918. row.spacing = 16
  2919. row.alignment = .centerY
  2920. row.distribution = .fill
  2921. row.translatesAutoresizingMaskIntoConstraints = false
  2922. addSubview(row)
  2923. let inset: CGFloat = 16
  2924. NSLayoutConstraint.activate([
  2925. row.leadingAnchor.constraint(equalTo: leadingAnchor, constant: inset),
  2926. row.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -inset),
  2927. row.topAnchor.constraint(equalTo: topAnchor, constant: inset),
  2928. row.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -inset),
  2929. iconHost.widthAnchor.constraint(equalToConstant: iconSize),
  2930. iconHost.heightAnchor.constraint(equalToConstant: iconSize),
  2931. icon.centerXAnchor.constraint(equalTo: iconHost.centerXAnchor),
  2932. icon.centerYAnchor.constraint(equalTo: iconHost.centerYAnchor)
  2933. ])
  2934. setAccessibilityElement(true)
  2935. setAccessibilityRole(.button)
  2936. setAccessibilityLabel("\(title). \(subtitle)")
  2937. }
  2938. @available(*, unavailable)
  2939. required init?(coder: NSCoder) {
  2940. fatalError("init(coder:) has not been implemented")
  2941. }
  2942. override func layout() {
  2943. super.layout()
  2944. guard let layer = layer, bounds.width > 0, bounds.height > 0 else { return }
  2945. let r = bounds
  2946. let cr = Self.cardCornerRadius
  2947. layer.shadowPath = CGPath(roundedRect: r, cornerWidth: cr, cornerHeight: cr, transform: nil)
  2948. }
  2949. override func mouseDown(with event: NSEvent) {
  2950. if let target = actionTarget {
  2951. _ = target.perform(actionSelector, with: nil)
  2952. }
  2953. }
  2954. override func resetCursorRects() {
  2955. super.resetCursorRects()
  2956. addCursorRect(bounds, cursor: .pointingHand)
  2957. }
  2958. }
  2959. /// `NSButton` that carries a `JobListing` for card actions (`representedObject` is unavailable on `NSButton` in this target).
  2960. private class JobPayloadButton: HoverableButton {
  2961. var jobPayload: JobListing?
  2962. var cardContext: JobListingCardContext = .homeSearchResults
  2963. }
  2964. /// Insets image + title so the Save pill matches the reference (balanced padding, not flush to the stroke).
  2965. private final class SaveJobButtonCell: NSButtonCell {
  2966. private let horizontalInset: CGFloat = 10
  2967. private let verticalInset: CGFloat = 3
  2968. private let imageTitleGap: CGFloat = 5
  2969. override func imageRect(forBounds rect: NSRect) -> NSRect {
  2970. super.imageRect(forBounds: rect.insetBy(dx: horizontalInset, dy: verticalInset))
  2971. }
  2972. override func titleRect(forBounds rect: NSRect) -> NSRect {
  2973. let padded = rect.insetBy(dx: horizontalInset, dy: verticalInset)
  2974. var t = super.titleRect(forBounds: padded)
  2975. t.origin.x += imageTitleGap
  2976. t.size.width = max(0, t.size.width - imageTitleGap)
  2977. return t
  2978. }
  2979. }
  2980. private final class SaveJobPayloadButton: JobPayloadButton {
  2981. override class var cellClass: AnyClass? {
  2982. get { SaveJobButtonCell.self }
  2983. set { }
  2984. }
  2985. }
  2986. /// `NSButton` with a tracking area that reports hover transitions and (optionally) swaps in a pointing-hand cursor while hovered.
  2987. private class HoverableButton: NSButton {
  2988. var hoverHandler: ((Bool) -> Void)?
  2989. var pointerCursor: Bool = false
  2990. private(set) var isHovering: Bool = false
  2991. private var trackingArea: NSTrackingArea?
  2992. private var didPushCursor: Bool = false
  2993. override func updateTrackingAreas() {
  2994. super.updateTrackingAreas()
  2995. if let area = trackingArea { removeTrackingArea(area) }
  2996. let area = NSTrackingArea(
  2997. rect: bounds,
  2998. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  2999. owner: self,
  3000. userInfo: nil
  3001. )
  3002. addTrackingArea(area)
  3003. trackingArea = area
  3004. }
  3005. override func mouseEntered(with event: NSEvent) {
  3006. super.mouseEntered(with: event)
  3007. isHovering = true
  3008. hoverHandler?(true)
  3009. if pointerCursor, !didPushCursor {
  3010. NSCursor.pointingHand.push()
  3011. didPushCursor = true
  3012. }
  3013. }
  3014. override func mouseExited(with event: NSEvent) {
  3015. super.mouseExited(with: event)
  3016. isHovering = false
  3017. hoverHandler?(false)
  3018. if didPushCursor {
  3019. NSCursor.pop()
  3020. didPushCursor = false
  3021. }
  3022. }
  3023. override func viewWillMove(toWindow newWindow: NSWindow?) {
  3024. super.viewWillMove(toWindow: newWindow)
  3025. // Guard against an unbalanced cursor stack if the button is removed mid-hover (e.g. job card replaced after a search).
  3026. if newWindow == nil, didPushCursor {
  3027. NSCursor.pop()
  3028. didPushCursor = false
  3029. isHovering = false
  3030. }
  3031. }
  3032. }
  3033. /// `NSView` companion to `HoverableButton`: emits hover transitions and can manage a pointing-hand cursor. Used to track hover over composite controls like the gradient "Find jobs" pill.
  3034. private class HoverableView: NSView {
  3035. var hoverHandler: ((Bool) -> Void)?
  3036. var pointerCursor: Bool = false
  3037. private(set) var isHovering: Bool = false
  3038. private var trackingArea: NSTrackingArea?
  3039. private var didPushCursor: Bool = false
  3040. override func updateTrackingAreas() {
  3041. super.updateTrackingAreas()
  3042. if let area = trackingArea { removeTrackingArea(area) }
  3043. let area = NSTrackingArea(
  3044. rect: bounds,
  3045. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  3046. owner: self,
  3047. userInfo: nil
  3048. )
  3049. addTrackingArea(area)
  3050. trackingArea = area
  3051. }
  3052. override func mouseEntered(with event: NSEvent) {
  3053. super.mouseEntered(with: event)
  3054. isHovering = true
  3055. hoverHandler?(true)
  3056. if pointerCursor, !didPushCursor {
  3057. NSCursor.pointingHand.push()
  3058. didPushCursor = true
  3059. }
  3060. }
  3061. override func mouseExited(with event: NSEvent) {
  3062. super.mouseExited(with: event)
  3063. isHovering = false
  3064. hoverHandler?(false)
  3065. if didPushCursor {
  3066. NSCursor.pop()
  3067. didPushCursor = false
  3068. }
  3069. }
  3070. override func viewWillMove(toWindow newWindow: NSWindow?) {
  3071. super.viewWillMove(toWindow: newWindow)
  3072. if newWindow == nil, didPushCursor {
  3073. NSCursor.pop()
  3074. didPushCursor = false
  3075. isHovering = false
  3076. }
  3077. }
  3078. }
  3079. /// Single sparkle with a soft brand-blue glow and three dots whose opacity animates in a staggered wave (typing-style “thinking” affordance).
  3080. private final class ChatThinkingIndicatorView: NSView {
  3081. private enum AnimationKey {
  3082. static let dotOpacity = "thinkingDotOpacity"
  3083. static let sparklePulse = "thinkingSparklePulse"
  3084. }
  3085. /// Matches `DashboardView.Theme.brandBlue` — Indeed-style `#2557a7`.
  3086. private static let accentBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  3087. /// Slightly lighter blue for the sparkle glow (reads clearly on light banner/chat surfaces).
  3088. private static let accentBlueGlow = NSColor(srgbRed: 54 / 255, green: 130 / 255, blue: 220 / 255, alpha: 1)
  3089. private let sparkleView = NSImageView()
  3090. private let dotStack = NSStackView()
  3091. private var dotViews: [NSView] = []
  3092. init(compact: Bool) {
  3093. super.init(frame: .zero)
  3094. translatesAutoresizingMaskIntoConstraints = false
  3095. let sparklePoint: CGFloat = compact ? 11 : 14
  3096. let dotSize: CGFloat = compact ? 4 : 5
  3097. let sparkleDotGap: CGFloat = compact ? 7 : 10
  3098. let dotSpacing: CGFloat = compact ? 4 : 5
  3099. sparkleView.translatesAutoresizingMaskIntoConstraints = false
  3100. sparkleView.image = NSImage(systemSymbolName: "sparkle", accessibilityDescription: nil)
  3101. sparkleView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: sparklePoint, weight: .medium)
  3102. sparkleView.contentTintColor = Self.accentBlue
  3103. sparkleView.wantsLayer = true
  3104. sparkleView.layer?.masksToBounds = false
  3105. sparkleView.layer?.shadowColor = Self.accentBlueGlow.cgColor
  3106. sparkleView.layer?.shadowRadius = compact ? 5 : 9
  3107. sparkleView.layer?.shadowOpacity = 0.55
  3108. sparkleView.layer?.shadowOffset = .zero
  3109. NSLayoutConstraint.activate([
  3110. sparkleView.widthAnchor.constraint(equalToConstant: sparklePoint + 6),
  3111. sparkleView.heightAnchor.constraint(equalToConstant: sparklePoint + 6)
  3112. ])
  3113. dotStack.orientation = .horizontal
  3114. dotStack.spacing = dotSpacing
  3115. dotStack.alignment = .centerY
  3116. dotStack.translatesAutoresizingMaskIntoConstraints = false
  3117. let dotFill = Self.accentBlue
  3118. for _ in 0..<3 {
  3119. let dot = NSView()
  3120. dot.translatesAutoresizingMaskIntoConstraints = false
  3121. dot.wantsLayer = true
  3122. dot.layer?.cornerRadius = dotSize / 2
  3123. dot.layer?.backgroundColor = dotFill.cgColor
  3124. NSLayoutConstraint.activate([
  3125. dot.widthAnchor.constraint(equalToConstant: dotSize),
  3126. dot.heightAnchor.constraint(equalToConstant: dotSize)
  3127. ])
  3128. dotStack.addArrangedSubview(dot)
  3129. dotViews.append(dot)
  3130. }
  3131. let row = NSStackView(views: [sparkleView, dotStack])
  3132. row.orientation = .horizontal
  3133. row.spacing = sparkleDotGap
  3134. row.alignment = .centerY
  3135. row.translatesAutoresizingMaskIntoConstraints = false
  3136. addSubview(row)
  3137. NSLayoutConstraint.activate([
  3138. row.leadingAnchor.constraint(equalTo: leadingAnchor),
  3139. row.trailingAnchor.constraint(equalTo: trailingAnchor),
  3140. row.topAnchor.constraint(equalTo: topAnchor),
  3141. row.bottomAnchor.constraint(equalTo: bottomAnchor)
  3142. ])
  3143. setAccessibilityElement(true)
  3144. setAccessibilityRole(.group)
  3145. setAccessibilityLabel("Assistant is searching")
  3146. }
  3147. @available(*, unavailable)
  3148. required init?(coder: NSCoder) {
  3149. fatalError("init(coder:) has not been implemented")
  3150. }
  3151. private static var reducedMotion: Bool {
  3152. NSWorkspace.shared.accessibilityDisplayShouldReduceMotion
  3153. }
  3154. func startAnimatingIfNeeded() {
  3155. stopAnimating()
  3156. guard !Self.reducedMotion else {
  3157. dotViews.forEach { $0.layer?.opacity = 0.78 }
  3158. return
  3159. }
  3160. guard let sparkleLayer = sparkleView.layer else { return }
  3161. let waveDuration: CFTimeInterval = 0.52
  3162. let n = max(1, dotViews.count)
  3163. for (i, dot) in dotViews.enumerated() {
  3164. guard let layer = dot.layer else { continue }
  3165. layer.opacity = 1
  3166. let pulse = CABasicAnimation(keyPath: "opacity")
  3167. pulse.fromValue = 0.2
  3168. pulse.toValue = 1.0
  3169. pulse.duration = waveDuration
  3170. pulse.autoreverses = true
  3171. pulse.repeatCount = .greatestFiniteMagnitude
  3172. pulse.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  3173. pulse.timeOffset = waveDuration * Double(i) / Double(n)
  3174. layer.add(pulse, forKey: AnimationKey.dotOpacity)
  3175. }
  3176. let scale = CABasicAnimation(keyPath: "transform.scale")
  3177. scale.fromValue = 1.0
  3178. scale.toValue = 1.07
  3179. scale.duration = 1.15
  3180. scale.autoreverses = true
  3181. scale.repeatCount = .greatestFiniteMagnitude
  3182. scale.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  3183. sparkleLayer.add(scale, forKey: AnimationKey.sparklePulse)
  3184. }
  3185. func stopAnimating() {
  3186. sparkleView.layer?.removeAnimation(forKey: AnimationKey.sparklePulse)
  3187. sparkleView.layer?.transform = CATransform3DIdentity
  3188. for dot in dotViews {
  3189. dot.layer?.removeAnimation(forKey: AnimationKey.dotOpacity)
  3190. dot.layer?.opacity = 1
  3191. }
  3192. }
  3193. }
  3194. /// Document view for the job list `NSScrollView`; flipped coordinates keep short result sets aligned to the top of the clip (avoids a large empty band above the cards on macOS).
  3195. private final class JobListingsDocumentView: NSView {
  3196. override var isFlipped: Bool { true }
  3197. }
  3198. /// Marker subclass for the per-assistant-message job stack embedded inside a chat bubble. `NSView.tag` is read-only on macOS, so a typed subclass is the cleanest way to identify these stacks during dismiss and layout passes.
  3199. private final class ChatJobsStackView: NSStackView {}
  3200. /// Marker subclass for the wrapping label inside a chat bubble. Lets the layout pass find each bubble label to update its `preferredMaxLayoutWidth` when the chat width changes.
  3201. private final class ChatBubbleLabel: NSTextField {}
  3202. /// Captures clicks for the full sidebar pill so icon, label, and padding behave as one tab. Manages its own hover background so non-selected rows highlight subtly on hover without disturbing the selected-row fill.
  3203. private final class SidebarNavRowView: NSView {
  3204. private let onSelect: () -> Void
  3205. var restingBackgroundColor: NSColor? {
  3206. didSet { applyBackground() }
  3207. }
  3208. var hoverBackgroundColor: NSColor?
  3209. private var isHovering: Bool = false
  3210. private var didPushCursor: Bool = false
  3211. init(onSelect: @escaping () -> Void) {
  3212. self.onSelect = onSelect
  3213. super.init(frame: .zero)
  3214. }
  3215. @available(*, unavailable)
  3216. required init?(coder: NSCoder) {
  3217. fatalError("init(coder:) has not been implemented")
  3218. }
  3219. override func hitTest(_ point: NSPoint) -> NSView? {
  3220. guard let superview else { return super.hitTest(point) }
  3221. let local = convert(point, from: superview)
  3222. return bounds.contains(local) ? self : nil
  3223. }
  3224. override func mouseDown(with event: NSEvent) {
  3225. onSelect()
  3226. }
  3227. override func updateTrackingAreas() {
  3228. super.updateTrackingAreas()
  3229. trackingAreas.forEach { removeTrackingArea($0) }
  3230. addTrackingArea(NSTrackingArea(
  3231. rect: bounds,
  3232. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  3233. owner: self,
  3234. userInfo: nil
  3235. ))
  3236. }
  3237. override func mouseEntered(with event: NSEvent) {
  3238. super.mouseEntered(with: event)
  3239. isHovering = true
  3240. applyBackground()
  3241. if !didPushCursor {
  3242. NSCursor.pointingHand.push()
  3243. didPushCursor = true
  3244. }
  3245. }
  3246. override func mouseExited(with event: NSEvent) {
  3247. super.mouseExited(with: event)
  3248. isHovering = false
  3249. applyBackground()
  3250. if didPushCursor {
  3251. NSCursor.pop()
  3252. didPushCursor = false
  3253. }
  3254. }
  3255. override func viewWillMove(toWindow newWindow: NSWindow?) {
  3256. super.viewWillMove(toWindow: newWindow)
  3257. if newWindow == nil, didPushCursor {
  3258. NSCursor.pop()
  3259. didPushCursor = false
  3260. isHovering = false
  3261. }
  3262. }
  3263. private func applyBackground() {
  3264. let color = isHovering ? (hoverBackgroundColor ?? restingBackgroundColor) : restingBackgroundColor
  3265. layer?.backgroundColor = color?.cgColor
  3266. }
  3267. }