Sin descripción

DashboardView.swift 134KB

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