Нет описания

DashboardView.swift 159KB

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