暫無描述

DashboardView.swift 167KB

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