설명 없음

DashboardView.swift 144KB

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