설명 없음

DashboardView.swift 164KB

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