暂无描述

DashboardView.swift 154KB

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