Aucune description

DashboardView.swift 111KB

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