Нет описания

DashboardView.swift 163KB

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