No Description

DashboardView.swift 148KB

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