Geen omschrijving

DashboardView.swift 109KB

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