Нема описа

DashboardView.swift 156KB

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