Sin descripción

DashboardView.swift 140KB

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