Nessuna descrizione

DashboardView.swift 175KB

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