Ingen beskrivning

DashboardView.swift 135KB

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