Nessuna descrizione

DashboardView.swift 163KB

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