Sin descripción

DashboardView.swift 166KB

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