暂无描述

DashboardView.swift 180KB

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