Нет описания

DashboardView.swift 146KB

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