Ingen beskrivning

DashboardView.swift 163KB

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