暂无描述

DashboardView.swift 189KB

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