Ingen beskrivning

DashboardView.swift 66KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. //
  2. // DashboardView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. import QuartzCore
  7. private enum JobListingCardContext {
  8. case homeSearchResults
  9. case savedJobsPage
  10. }
  11. final class DashboardView: NSView, NSTextFieldDelegate {
  12. /// Indeed.com-inspired neutrals and brand blue (white surfaces, `#2557a7` accent, `#2d2d2d` / `#767676` text, `#d4d2d0` borders).
  13. private enum Theme {
  14. static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  15. static let pageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  16. static let chromeBackground = NSColor(srgbRed: 247 / 255, green: 247 / 255, blue: 247 / 255, alpha: 1)
  17. static let sidebarBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  18. static let mainHostBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  19. /// Subtitle on the welcome hero: readable blue-gray aligned with brand.
  20. static let welcomeSubtitleText = NSColor(srgbRed: 52 / 255, green: 92 / 255, blue: 142 / 255, alpha: 1)
  21. static let selectionFill = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.12)
  22. static let cardBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  23. static let toggleBackground = NSColor(srgbRed: 232 / 255, green: 232 / 255, blue: 232 / 255, alpha: 1)
  24. static let primaryText = NSColor(srgbRed: 45 / 255, green: 45 / 255, blue: 45 / 255, alpha: 1)
  25. static let secondaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  26. static let tertiaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  27. static let border = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  28. /// Job search bar outer stroke (charcoal).
  29. static let searchBarBorder = NSColor(srgbRed: 58 / 255, green: 58 / 255, blue: 58 / 255, alpha: 1)
  30. /// Search bar border on hover (brand-tinted, matches focus affordance elsewhere).
  31. static let searchBarBorderHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.45)
  32. static let proCardFill = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  33. static let proCardBorder = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  34. static let proAccent = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  35. static let proCTABackground = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  36. static let proCTAText = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  37. /// Slightly lighter blue for the top of the search-bar “Find jobs” pill (reads less flat than a solid fill).
  38. static let findJobsCTAHighlight = NSColor(srgbRed: 54 / 255, green: 110 / 255, blue: 198 / 255, alpha: 1)
  39. /// Hover states: darker brand blue, deeper gradient top, stronger tints, and subtle neutral fills used across CTAs, toggles, and the sidebar.
  40. static let brandBlueHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  41. static let findJobsCTAHighlightHover = NSColor(srgbRed: 44 / 255, green: 94 / 255, blue: 178 / 255, alpha: 1)
  42. static let selectionFillHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.2)
  43. static let neutralHoverFill = NSColor(srgbRed: 240 / 255, green: 240 / 255, blue: 240 / 255, alpha: 1)
  44. static let sidebarRowHoverFill = NSColor(srgbRed: 0, green: 0, blue: 0, alpha: 0.04)
  45. }
  46. /// Multiline `NSTextField` often ignores `alignment` for wrapped runs; explicit paragraph alignment matches the title.
  47. private static func jobListingDescriptionAttributedString(_ plain: String) -> NSAttributedString {
  48. let paragraph = NSMutableParagraphStyle()
  49. paragraph.alignment = .left
  50. paragraph.lineBreakMode = .byWordWrapping
  51. paragraph.baseWritingDirection = .leftToRight
  52. let font = NSFont.systemFont(ofSize: 13, weight: .regular)
  53. return NSAttributedString(string: plain, attributes: [
  54. .font: font,
  55. .foregroundColor: Theme.secondaryText,
  56. .paragraphStyle: paragraph
  57. ])
  58. }
  59. private let contentStack = NSStackView()
  60. private let chromeContainer = NSView()
  61. private let sidebar = NSStackView()
  62. private let mainHost = NSView()
  63. private let mainOverlay = NSStackView()
  64. private let greetingLabel = NSTextField(labelWithString: "")
  65. private let subtitleLabel = NSTextField(labelWithString: "")
  66. private let searchBarShadowHost = NSView()
  67. private let searchCard = HoverableView()
  68. private let jobSearchIcon = NSImageView()
  69. private let jobKeywordsField = NSTextField()
  70. private let findJobsButton = HoverableButton()
  71. private let findJobsCTAHost = NSView()
  72. private let findJobsCTAChrome = HoverableView()
  73. private var findJobsCTAGradientLayer: CAGradientLayer?
  74. private let jobListingsScrollView = NSScrollView()
  75. /// Flipped so short result lists stay visually under the search bar instead of leaving a gap above the cards.
  76. private let jobListingsContainer = JobListingsDocumentView()
  77. private let jobListingsStack = NSStackView()
  78. /// Shown when a sidebar item other than Home is selected.
  79. private let nonHomeHost = NSView()
  80. private let nonHomeGenericContainer = NSView()
  81. private let nonHomeTitleLabel = NSTextField(labelWithString: "")
  82. private let nonHomeSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  83. private let savedJobsPageContainer = NSView()
  84. private let savedJobsPageTitleLabel = NSTextField(labelWithString: "Saved Jobs")
  85. private let savedJobsPageSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  86. private let savedJobsScrollView = NSScrollView()
  87. private let savedJobsDocumentView = JobListingsDocumentView()
  88. private let savedJobsStack = NSStackView()
  89. private var currentSidebarItems: [SidebarItem] = []
  90. private var selectedSidebarIndex: Int = 0
  91. /// Full list from `DashboardData`; results are shown after the user runs a search.
  92. private var catalogJobListings: [JobListing] = []
  93. /// Last successful search result set (used when removing a card with the dismiss control).
  94. private var lastSearchResults: [JobListing] = []
  95. private var lastNoResultsQuery: String?
  96. /// Most recently saved jobs appear first; persisted across launches.
  97. private var savedJobOrder: [JobListing] = []
  98. override init(frame frameRect: NSRect) {
  99. super.init(frame: frameRect)
  100. setupLayout()
  101. }
  102. required init?(coder: NSCoder) {
  103. super.init(coder: coder)
  104. setupLayout()
  105. }
  106. override func layout() {
  107. super.layout()
  108. updateSearchBarShadowPath()
  109. findJobsCTAGradientLayer?.frame = findJobsCTAChrome.bounds
  110. updateFindJobsCTAShadowPath()
  111. updateJobListingDescriptionWidths()
  112. }
  113. func render(_ data: DashboardData) {
  114. greetingLabel.stringValue = "Welcome"
  115. subtitleLabel.stringValue = data.subtitle
  116. currentSidebarItems = data.sidebarItems
  117. if selectedSidebarIndex >= currentSidebarItems.count {
  118. selectedSidebarIndex = max(0, currentSidebarItems.count - 1)
  119. }
  120. configureSidebar()
  121. catalogJobListings = data.jobListings
  122. savedJobOrder = Self.normalizedSavedJobs(SavedJobsStore.load())
  123. configureJobListings([], noResultsForQuery: nil)
  124. updateMainContentVisibility()
  125. }
  126. private func setupLayout() {
  127. wantsLayer = true
  128. layer?.backgroundColor = Theme.pageBackground.cgColor
  129. contentStack.orientation = .horizontal
  130. contentStack.spacing = 10
  131. contentStack.distribution = .fill
  132. contentStack.translatesAutoresizingMaskIntoConstraints = false
  133. contentStack.alignment = .height
  134. // Tighter chrome insets so panels sit closer to the window edges (especially leading / top under the title bar).
  135. contentStack.edgeInsets = NSEdgeInsets(top: 10, left: 12, bottom: 20, right: 20)
  136. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  137. chromeContainer.wantsLayer = true
  138. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  139. chromeContainer.layer?.cornerRadius = 0
  140. addSubview(chromeContainer)
  141. chromeContainer.addSubview(contentStack)
  142. sidebar.orientation = .vertical
  143. sidebar.spacing = 10
  144. sidebar.distribution = .fill
  145. sidebar.alignment = .leading
  146. sidebar.wantsLayer = true
  147. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  148. sidebar.layer?.cornerRadius = 16
  149. sidebar.edgeInsets = NSEdgeInsets(top: 16, left: 12, bottom: 16, right: 12)
  150. sidebar.translatesAutoresizingMaskIntoConstraints = false
  151. mainHost.translatesAutoresizingMaskIntoConstraints = false
  152. mainHost.wantsLayer = true
  153. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  154. mainHost.layer?.cornerRadius = 16
  155. mainHost.layer?.masksToBounds = true
  156. sidebar.setContentHuggingPriority(.required, for: .horizontal)
  157. mainHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  158. mainHost.addSubview(mainOverlay)
  159. configureNonHomePlaceholder()
  160. mainHost.addSubview(nonHomeHost)
  161. mainOverlay.orientation = .vertical
  162. mainOverlay.spacing = 0
  163. mainOverlay.alignment = .centerX
  164. mainOverlay.distribution = .fill
  165. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  166. mainOverlay.setContentHuggingPriority(.defaultLow, for: .vertical)
  167. greetingLabel.font = .systemFont(ofSize: 32, weight: .bold)
  168. greetingLabel.textColor = Theme.brandBlue
  169. greetingLabel.alignment = .center
  170. greetingLabel.maximumNumberOfLines = 1
  171. subtitleLabel.font = .systemFont(ofSize: 15, weight: .regular)
  172. subtitleLabel.textColor = Theme.welcomeSubtitleText
  173. subtitleLabel.alignment = .center
  174. subtitleLabel.maximumNumberOfLines = 2
  175. let topInset = NSView()
  176. topInset.translatesAutoresizingMaskIntoConstraints = false
  177. topInset.heightAnchor.constraint(equalToConstant: 18).isActive = true
  178. configureSearchBar()
  179. let titleBlock = NSStackView(views: [greetingLabel, subtitleLabel])
  180. titleBlock.orientation = .vertical
  181. titleBlock.spacing = 10
  182. titleBlock.alignment = .centerX
  183. let midSpacer = NSView()
  184. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  185. midSpacer.heightAnchor.constraint(equalToConstant: 20).isActive = true
  186. let listingsTopSpacer = NSView()
  187. listingsTopSpacer.translatesAutoresizingMaskIntoConstraints = false
  188. listingsTopSpacer.heightAnchor.constraint(equalToConstant: 12).isActive = true
  189. jobListingsContainer.translatesAutoresizingMaskIntoConstraints = false
  190. jobListingsStack.orientation = .vertical
  191. jobListingsStack.spacing = 14
  192. // `.leading` keeps cards left-anchored; explicit width constraints below stretch each card across the full row.
  193. jobListingsStack.alignment = .leading
  194. jobListingsStack.distribution = .fill
  195. jobListingsStack.translatesAutoresizingMaskIntoConstraints = false
  196. jobListingsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  197. jobListingsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  198. jobListingsContainer.addSubview(jobListingsStack)
  199. NSLayoutConstraint.activate([
  200. jobListingsStack.leadingAnchor.constraint(equalTo: jobListingsContainer.leadingAnchor),
  201. jobListingsStack.trailingAnchor.constraint(equalTo: jobListingsContainer.trailingAnchor),
  202. jobListingsStack.topAnchor.constraint(equalTo: jobListingsContainer.topAnchor),
  203. jobListingsStack.bottomAnchor.constraint(equalTo: jobListingsContainer.bottomAnchor)
  204. ])
  205. jobListingsScrollView.translatesAutoresizingMaskIntoConstraints = false
  206. jobListingsScrollView.hasVerticalScroller = true
  207. jobListingsScrollView.hasHorizontalScroller = false
  208. jobListingsScrollView.autohidesScrollers = true
  209. jobListingsScrollView.drawsBackground = false
  210. jobListingsScrollView.borderType = .noBorder
  211. jobListingsScrollView.documentView = jobListingsContainer
  212. jobListingsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  213. jobListingsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  214. mainOverlay.addArrangedSubview(topInset)
  215. mainOverlay.addArrangedSubview(titleBlock)
  216. mainOverlay.addArrangedSubview(midSpacer)
  217. mainOverlay.addArrangedSubview(searchBarShadowHost)
  218. mainOverlay.addArrangedSubview(listingsTopSpacer)
  219. mainOverlay.addArrangedSubview(jobListingsScrollView)
  220. contentStack.addArrangedSubview(sidebar)
  221. contentStack.addArrangedSubview(mainHost)
  222. NSLayoutConstraint.activate([
  223. chromeContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
  224. chromeContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
  225. chromeContainer.topAnchor.constraint(equalTo: topAnchor),
  226. chromeContainer.bottomAnchor.constraint(equalTo: bottomAnchor),
  227. contentStack.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor),
  228. contentStack.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor),
  229. contentStack.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
  230. contentStack.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor),
  231. sidebar.widthAnchor.constraint(equalToConstant: 218),
  232. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  233. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  234. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  235. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  236. mainOverlay.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  237. nonHomeHost.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  238. nonHomeHost.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  239. nonHomeHost.topAnchor.constraint(equalTo: mainHost.topAnchor),
  240. nonHomeHost.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  241. searchBarShadowHost.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  242. jobListingsScrollView.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  243. jobListingsContainer.topAnchor.constraint(equalTo: jobListingsScrollView.contentView.topAnchor),
  244. jobListingsContainer.leadingAnchor.constraint(equalTo: jobListingsScrollView.contentView.leadingAnchor),
  245. jobListingsContainer.widthAnchor.constraint(equalTo: jobListingsScrollView.contentView.widthAnchor),
  246. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 16),
  247. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -16),
  248. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  249. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor)
  250. ])
  251. }
  252. private func updateJobListingDescriptionWidths() {
  253. updateDescriptionColumnWidths(in: jobListingsStack, containerWidth: jobListingsContainer.bounds.width)
  254. updateDescriptionColumnWidths(in: savedJobsStack, containerWidth: savedJobsDocumentView.bounds.width)
  255. }
  256. private func updateDescriptionColumnWidths(in stack: NSStackView, containerWidth: CGFloat) {
  257. guard containerWidth > 1 else { return }
  258. // 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.
  259. let contentHorizontalInset: CGFloat = 32
  260. var didChange = false
  261. for card in stack.arrangedSubviews {
  262. guard let desc = card.viewWithTag(502) as? NSTextField else { continue }
  263. let cardWidth = card.bounds.width > 1 ? card.bounds.width : containerWidth
  264. let fallbackColumn = max(1, cardWidth - contentHorizontalInset)
  265. let columnWidth: CGFloat
  266. if desc.bounds.width > 1 {
  267. columnWidth = desc.bounds.width
  268. } else if let column = desc.superview, column.bounds.width > 1 {
  269. columnWidth = column.bounds.width
  270. } else {
  271. columnWidth = fallbackColumn
  272. }
  273. if abs(desc.preferredMaxLayoutWidth - columnWidth) > 0.5 {
  274. desc.preferredMaxLayoutWidth = columnWidth
  275. desc.invalidateIntrinsicContentSize()
  276. didChange = true
  277. }
  278. }
  279. if didChange {
  280. stack.needsLayout = true
  281. }
  282. }
  283. private func configureJobListings(_ jobs: [JobListing], noResultsForQuery: String?, updateLastResults: Bool = true) {
  284. if updateLastResults {
  285. lastSearchResults = jobs
  286. lastNoResultsQuery = noResultsForQuery
  287. }
  288. jobListingsStack.arrangedSubviews.forEach {
  289. jobListingsStack.removeArrangedSubview($0)
  290. $0.removeFromSuperview()
  291. }
  292. if jobs.isEmpty, let query = noResultsForQuery, !query.isEmpty {
  293. let empty = NSTextField(wrappingLabelWithString: "No jobs match “\(query)”. Try different keywords or browse the full list with an empty search.")
  294. empty.font = .systemFont(ofSize: 14, weight: .regular)
  295. empty.textColor = Theme.secondaryText
  296. empty.alignment = .center
  297. empty.maximumNumberOfLines = 0
  298. empty.translatesAutoresizingMaskIntoConstraints = false
  299. jobListingsStack.addArrangedSubview(empty)
  300. empty.widthAnchor.constraint(equalTo: jobListingsStack.widthAnchor).isActive = true
  301. return
  302. }
  303. for job in jobs {
  304. let card = makeJobListingCard(job, context: .homeSearchResults)
  305. jobListingsStack.addArrangedSubview(card)
  306. // Force every card to span the full row instead of hugging its intrinsic content width.
  307. card.widthAnchor.constraint(equalTo: jobListingsStack.widthAnchor).isActive = true
  308. }
  309. }
  310. private static func normalizedSavedJobs(_ jobs: [JobListing]) -> [JobListing] {
  311. var seen = Set<JobListing>()
  312. var out: [JobListing] = []
  313. for job in jobs where seen.insert(job).inserted {
  314. out.append(job)
  315. }
  316. return out
  317. }
  318. private func isJobSaved(_ job: JobListing) -> Bool {
  319. savedJobOrder.contains(job)
  320. }
  321. private func persistSavedJobs() {
  322. SavedJobsStore.save(savedJobOrder)
  323. }
  324. private func applySavedState(_ saved: Bool, for job: JobListing) {
  325. if saved {
  326. savedJobOrder.removeAll { $0 == job }
  327. savedJobOrder.insert(job, at: 0)
  328. } else {
  329. savedJobOrder.removeAll { $0 == job }
  330. }
  331. persistSavedJobs()
  332. }
  333. private func makeJobListingCard(_ job: JobListing, context: JobListingCardContext) -> NSView {
  334. let card = NSView()
  335. card.translatesAutoresizingMaskIntoConstraints = false
  336. card.wantsLayer = true
  337. card.layer?.backgroundColor = Theme.cardBackground.cgColor
  338. card.layer?.cornerRadius = 12
  339. card.layer?.borderWidth = 1
  340. card.layer?.borderColor = Theme.border.cgColor
  341. card.layer?.masksToBounds = true
  342. let titleField = NSTextField(labelWithString: job.title)
  343. titleField.font = .systemFont(ofSize: 16, weight: .semibold)
  344. titleField.textColor = Theme.primaryText
  345. titleField.maximumNumberOfLines = 2
  346. titleField.lineBreakMode = .byWordWrapping
  347. titleField.alignment = .left
  348. titleField.translatesAutoresizingMaskIntoConstraints = false
  349. let descriptionField = NSTextField(wrappingLabelWithString: job.description)
  350. descriptionField.font = .systemFont(ofSize: 13, weight: .regular)
  351. descriptionField.textColor = Theme.secondaryText
  352. descriptionField.maximumNumberOfLines = 0
  353. descriptionField.alignment = .left
  354. descriptionField.lineBreakMode = .byWordWrapping
  355. descriptionField.baseWritingDirection = .leftToRight
  356. descriptionField.attributedStringValue = Self.jobListingDescriptionAttributedString(job.description)
  357. if let cell = descriptionField.cell as? NSTextFieldCell {
  358. cell.alignment = .left
  359. cell.wraps = true
  360. }
  361. descriptionField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  362. descriptionField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  363. descriptionField.tag = 502
  364. descriptionField.translatesAutoresizingMaskIntoConstraints = false
  365. let applyButton = JobPayloadButton(title: "Apply", target: self, action: #selector(didTapJobApply(_:)))
  366. applyButton.jobPayload = job
  367. applyButton.cardContext = context
  368. applyButton.isBordered = false
  369. applyButton.bezelStyle = .rounded
  370. applyButton.font = .systemFont(ofSize: 13, weight: .semibold)
  371. applyButton.wantsLayer = true
  372. applyButton.layer?.cornerRadius = 6
  373. applyButton.layer?.backgroundColor = Theme.brandBlue.cgColor
  374. applyButton.contentTintColor = Theme.proCTAText
  375. applyButton.focusRingType = .none
  376. applyButton.pointerCursor = true
  377. applyButton.hoverHandler = { [weak applyButton] hovering in
  378. applyButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.brandBlue).cgColor
  379. }
  380. applyButton.setContentHuggingPriority(.required, for: .horizontal)
  381. applyButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  382. let savedOn = isJobSaved(job)
  383. let savedButton = JobPayloadButton(title: savedOn ? "Saved" : "Save", target: self, action: #selector(didTapJobSaved(_:)))
  384. savedButton.jobPayload = job
  385. savedButton.cardContext = context
  386. savedButton.setButtonType(.toggle)
  387. savedButton.isBordered = false
  388. savedButton.bezelStyle = .rounded
  389. savedButton.font = .systemFont(ofSize: 13, weight: .semibold)
  390. savedButton.focusRingType = .none
  391. savedButton.state = savedOn ? .on : .off
  392. savedButton.pointerCursor = true
  393. savedButton.hoverHandler = { [weak self, weak savedButton] _ in
  394. guard let savedButton = savedButton else { return }
  395. self?.styleJobSavedButton(savedButton)
  396. }
  397. styleJobSavedButton(savedButton)
  398. savedButton.setContentHuggingPriority(.required, for: .horizontal)
  399. savedButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  400. let dismissButton = JobPayloadButton()
  401. dismissButton.jobPayload = job
  402. dismissButton.cardContext = context
  403. dismissButton.image = NSImage(systemSymbolName: "xmark", accessibilityDescription: "Dismiss")
  404. dismissButton.imagePosition = .imageOnly
  405. dismissButton.imageScaling = .scaleProportionallyDown
  406. dismissButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  407. dismissButton.isBordered = false
  408. dismissButton.bezelStyle = .rounded
  409. dismissButton.contentTintColor = Theme.secondaryText
  410. dismissButton.target = self
  411. dismissButton.action = #selector(didTapJobDismiss(_:))
  412. dismissButton.toolTip = context == .savedJobsPage ? "Remove from saved" : "Dismiss"
  413. dismissButton.focusRingType = .none
  414. dismissButton.wantsLayer = true
  415. dismissButton.layer?.cornerRadius = 14
  416. dismissButton.layer?.backgroundColor = NSColor.clear.cgColor
  417. dismissButton.pointerCursor = true
  418. dismissButton.hoverHandler = { [weak dismissButton] hovering in
  419. dismissButton?.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : NSColor.clear).cgColor
  420. dismissButton?.contentTintColor = hovering ? Theme.primaryText : Theme.secondaryText
  421. }
  422. dismissButton.setContentHuggingPriority(.required, for: .horizontal)
  423. let buttonRow = NSStackView(views: [applyButton, savedButton, dismissButton])
  424. buttonRow.orientation = .horizontal
  425. buttonRow.spacing = 8
  426. buttonRow.alignment = .centerY
  427. buttonRow.translatesAutoresizingMaskIntoConstraints = false
  428. buttonRow.setContentHuggingPriority(.required, for: .horizontal)
  429. buttonRow.setContentCompressionResistancePriority(.required, for: .horizontal)
  430. // Title hugs the leading edge; a low–hugging-priority spacer absorbs remaining width so buttons stay trailing.
  431. titleField.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  432. titleField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  433. let titleRowSpacer = NSView()
  434. titleRowSpacer.translatesAutoresizingMaskIntoConstraints = false
  435. titleRowSpacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  436. titleRowSpacer.setContentCompressionResistancePriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  437. let titleAndActionsRow = NSStackView(views: [titleField, titleRowSpacer, buttonRow])
  438. titleAndActionsRow.orientation = .horizontal
  439. titleAndActionsRow.spacing = 0
  440. titleAndActionsRow.setCustomSpacing(14, after: titleRowSpacer)
  441. titleAndActionsRow.alignment = .centerY
  442. titleAndActionsRow.distribution = .fill
  443. titleAndActionsRow.userInterfaceLayoutDirection = .leftToRight
  444. titleAndActionsRow.translatesAutoresizingMaskIntoConstraints = false
  445. let contentColumn = NSStackView(views: [titleAndActionsRow, descriptionField])
  446. contentColumn.orientation = .vertical
  447. contentColumn.spacing = 6
  448. contentColumn.alignment = .width
  449. contentColumn.translatesAutoresizingMaskIntoConstraints = false
  450. card.addSubview(contentColumn)
  451. NSLayoutConstraint.activate([
  452. contentColumn.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
  453. contentColumn.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -16),
  454. contentColumn.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
  455. contentColumn.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -14),
  456. applyButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 72),
  457. applyButton.heightAnchor.constraint(equalToConstant: 28),
  458. savedButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 72),
  459. savedButton.heightAnchor.constraint(equalToConstant: 28),
  460. dismissButton.widthAnchor.constraint(equalToConstant: 28),
  461. dismissButton.heightAnchor.constraint(equalToConstant: 28),
  462. descriptionField.widthAnchor.constraint(equalTo: contentColumn.widthAnchor)
  463. ])
  464. return card
  465. }
  466. private func styleJobSavedButton(_ button: NSButton) {
  467. button.wantsLayer = true
  468. button.layer?.cornerRadius = 6
  469. let on = button.state == .on
  470. let hovering = (button as? HoverableButton)?.isHovering ?? false
  471. if on {
  472. button.layer?.backgroundColor = (hovering ? Theme.selectionFillHover : Theme.selectionFill).cgColor
  473. button.layer?.borderWidth = 1
  474. button.layer?.borderColor = Theme.brandBlue.cgColor
  475. button.contentTintColor = Theme.brandBlue
  476. } else {
  477. button.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  478. button.layer?.borderWidth = 1
  479. button.layer?.borderColor = Theme.border.cgColor
  480. button.contentTintColor = Theme.primaryText
  481. }
  482. }
  483. @objc private func didTapJobApply(_ sender: NSButton) {
  484. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  485. let allowed = CharacterSet.urlQueryAllowed
  486. let q = job.title.addingPercentEncoding(withAllowedCharacters: allowed) ?? ""
  487. guard let url = URL(string: "https://www.indeed.com/jobs?q=\(q)") else { return }
  488. NSWorkspace.shared.open(url)
  489. }
  490. @objc private func didTapJobSaved(_ sender: NSButton) {
  491. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  492. let willSave = !isJobSaved(job)
  493. applySavedState(willSave, for: job)
  494. sender.state = willSave ? .on : .off
  495. sender.title = willSave ? "Saved" : "Save"
  496. styleJobSavedButton(sender)
  497. if isSavedJobsSidebarIndex(selectedSidebarIndex) {
  498. reloadSavedJobsListings()
  499. }
  500. }
  501. @objc private func didTapJobDismiss(_ sender: NSButton) {
  502. guard let button = sender as? JobPayloadButton, let job = button.jobPayload else { return }
  503. switch button.cardContext {
  504. case .homeSearchResults:
  505. lastSearchResults.removeAll { $0 == job }
  506. configureJobListings(lastSearchResults, noResultsForQuery: lastNoResultsQuery)
  507. case .savedJobsPage:
  508. applySavedState(false, for: job)
  509. reloadSavedJobsListings()
  510. if isHomeSidebarIndex(selectedSidebarIndex), !lastSearchResults.isEmpty {
  511. configureJobListings(lastSearchResults, noResultsForQuery: lastNoResultsQuery, updateLastResults: false)
  512. }
  513. }
  514. }
  515. private func configureSearchBar() {
  516. let pillCorner: CGFloat = 27
  517. let barHeight: CGFloat = 54
  518. searchBarShadowHost.translatesAutoresizingMaskIntoConstraints = false
  519. searchBarShadowHost.wantsLayer = true
  520. searchBarShadowHost.layer?.masksToBounds = false
  521. searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(0.18).cgColor
  522. searchBarShadowHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  523. searchBarShadowHost.layer?.shadowRadius = 10
  524. searchBarShadowHost.layer?.shadowOpacity = 1
  525. searchBarShadowHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  526. searchCard.translatesAutoresizingMaskIntoConstraints = false
  527. searchCard.wantsLayer = true
  528. searchCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  529. searchCard.layer?.cornerRadius = pillCorner
  530. searchCard.layer?.borderWidth = 1
  531. searchCard.layer?.borderColor = Theme.searchBarBorder.cgColor
  532. searchCard.layer?.masksToBounds = true
  533. searchCard.hoverHandler = { [weak self] hovering in
  534. guard let self else { return }
  535. CATransaction.begin()
  536. CATransaction.setAnimationDuration(0.15)
  537. self.searchCard.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  538. self.searchCard.layer?.borderColor = (hovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  539. self.searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(hovering ? 0.24 : 0.18).cgColor
  540. self.searchBarShadowHost.layer?.shadowRadius = hovering ? 12 : 10
  541. CATransaction.commit()
  542. }
  543. searchBarShadowHost.addSubview(searchCard)
  544. func configureField(_ field: NSTextField, placeholder: String) {
  545. field.translatesAutoresizingMaskIntoConstraints = false
  546. field.isBordered = false
  547. field.drawsBackground = false
  548. field.focusRingType = .none
  549. field.font = .systemFont(ofSize: 14, weight: .regular)
  550. field.textColor = Theme.primaryText
  551. field.delegate = self
  552. field.placeholderAttributedString = NSAttributedString(
  553. string: placeholder,
  554. attributes: [
  555. .foregroundColor: Theme.secondaryText,
  556. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  557. ]
  558. )
  559. field.cell?.usesSingleLineMode = true
  560. field.cell?.wraps = false
  561. field.cell?.isScrollable = true
  562. field.target = self
  563. field.action = #selector(didSubmitSearch)
  564. }
  565. jobSearchIcon.translatesAutoresizingMaskIntoConstraints = false
  566. jobSearchIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .medium)
  567. jobSearchIcon.image = NSImage(systemSymbolName: "magnifyingglass", accessibilityDescription: "Job search")
  568. jobSearchIcon.contentTintColor = Theme.primaryText
  569. configureField(jobKeywordsField, placeholder: "Job title, keywords, or company")
  570. let ctaHeight: CGFloat = 42
  571. let ctaCorner = ctaHeight / 2
  572. findJobsCTAHost.translatesAutoresizingMaskIntoConstraints = false
  573. findJobsCTAHost.wantsLayer = true
  574. findJobsCTAHost.layer?.masksToBounds = false
  575. findJobsCTAHost.layer?.shadowColor = NSColor.black.cgColor
  576. findJobsCTAHost.layer?.shadowOpacity = 0.16
  577. findJobsCTAHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  578. findJobsCTAHost.layer?.shadowRadius = 6
  579. findJobsCTAChrome.translatesAutoresizingMaskIntoConstraints = false
  580. findJobsCTAChrome.wantsLayer = true
  581. findJobsCTAChrome.layer?.masksToBounds = true
  582. findJobsCTAChrome.layer?.cornerRadius = ctaCorner
  583. if #available(macOS 11.0, *) {
  584. findJobsCTAChrome.layer?.cornerCurve = .continuous
  585. }
  586. let gradient = CAGradientLayer()
  587. gradient.colors = [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  588. gradient.startPoint = CGPoint(x: 0.5, y: 1)
  589. gradient.endPoint = CGPoint(x: 0.5, y: 0)
  590. findJobsCTAChrome.layer?.addSublayer(gradient)
  591. findJobsCTAGradientLayer = gradient
  592. // 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.
  593. findJobsCTAChrome.pointerCursor = true
  594. findJobsCTAChrome.hoverHandler = { [weak self] hovering in
  595. guard let layer = self?.findJobsCTAGradientLayer else { return }
  596. CATransaction.begin()
  597. CATransaction.setAnimationDuration(0.15)
  598. layer.colors = hovering
  599. ? [Theme.findJobsCTAHighlightHover.cgColor, Theme.brandBlueHover.cgColor]
  600. : [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  601. CATransaction.commit()
  602. }
  603. findJobsButton.translatesAutoresizingMaskIntoConstraints = false
  604. findJobsButton.title = ""
  605. findJobsButton.attributedTitle = NSAttributedString(
  606. string: "Find jobs",
  607. attributes: [
  608. .font: NSFont.systemFont(ofSize: 14, weight: .semibold),
  609. .foregroundColor: Theme.proCTAText,
  610. .kern: 0.35
  611. ]
  612. )
  613. findJobsButton.isBordered = false
  614. findJobsButton.bezelStyle = .rounded
  615. findJobsButton.wantsLayer = true
  616. findJobsButton.layer?.backgroundColor = NSColor.clear.cgColor
  617. findJobsButton.focusRingType = .none
  618. findJobsButton.target = self
  619. findJobsButton.action = #selector(didSubmitSearch)
  620. findJobsButton.setContentHuggingPriority(.required, for: .horizontal)
  621. findJobsButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  622. findJobsCTAHost.addSubview(findJobsCTAChrome)
  623. findJobsCTAHost.addSubview(findJobsButton)
  624. NSLayoutConstraint.activate([
  625. findJobsCTAChrome.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor),
  626. findJobsCTAChrome.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor),
  627. findJobsCTAChrome.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  628. findJobsCTAChrome.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor),
  629. findJobsButton.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor, constant: 14),
  630. findJobsButton.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor, constant: -14),
  631. findJobsButton.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  632. findJobsButton.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor)
  633. ])
  634. let keywordsStack = NSStackView(views: [jobSearchIcon, jobKeywordsField])
  635. keywordsStack.orientation = .horizontal
  636. keywordsStack.spacing = 10
  637. keywordsStack.alignment = .centerY
  638. keywordsStack.translatesAutoresizingMaskIntoConstraints = false
  639. keywordsStack.edgeInsets = NSEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
  640. keywordsStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  641. let row = NSStackView(views: [keywordsStack, findJobsCTAHost])
  642. row.orientation = .horizontal
  643. row.spacing = 0
  644. row.alignment = .centerY
  645. row.distribution = .fill
  646. row.translatesAutoresizingMaskIntoConstraints = false
  647. row.edgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 7)
  648. searchCard.addSubview(row)
  649. NSLayoutConstraint.activate([
  650. searchCard.leadingAnchor.constraint(equalTo: searchBarShadowHost.leadingAnchor),
  651. searchCard.trailingAnchor.constraint(equalTo: searchBarShadowHost.trailingAnchor),
  652. searchCard.topAnchor.constraint(equalTo: searchBarShadowHost.topAnchor),
  653. searchCard.bottomAnchor.constraint(equalTo: searchBarShadowHost.bottomAnchor),
  654. searchBarShadowHost.heightAnchor.constraint(equalToConstant: barHeight),
  655. row.leadingAnchor.constraint(equalTo: searchCard.leadingAnchor),
  656. row.trailingAnchor.constraint(equalTo: searchCard.trailingAnchor),
  657. row.topAnchor.constraint(equalTo: searchCard.topAnchor),
  658. row.bottomAnchor.constraint(equalTo: searchCard.bottomAnchor),
  659. jobSearchIcon.widthAnchor.constraint(equalToConstant: 18),
  660. jobSearchIcon.heightAnchor.constraint(equalToConstant: 18),
  661. findJobsCTAHost.heightAnchor.constraint(equalToConstant: ctaHeight),
  662. findJobsCTAHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 112)
  663. ])
  664. }
  665. private func updateFindJobsCTAShadowPath() {
  666. guard findJobsCTAHost.bounds.width > 0, findJobsCTAHost.bounds.height > 0 else { return }
  667. let r = findJobsCTAHost.bounds
  668. let radius = min(r.height / 2, r.width / 2)
  669. findJobsCTAHost.layer?.shadowPath = CGPath(
  670. roundedRect: r,
  671. cornerWidth: radius,
  672. cornerHeight: radius,
  673. transform: nil
  674. )
  675. }
  676. private func configureNonHomePlaceholder() {
  677. nonHomeHost.translatesAutoresizingMaskIntoConstraints = false
  678. nonHomeHost.wantsLayer = true
  679. nonHomeHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  680. nonHomeHost.isHidden = true
  681. nonHomeGenericContainer.translatesAutoresizingMaskIntoConstraints = false
  682. savedJobsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  683. nonHomeHost.addSubview(nonHomeGenericContainer)
  684. nonHomeHost.addSubview(savedJobsPageContainer)
  685. NSLayoutConstraint.activate([
  686. nonHomeGenericContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  687. nonHomeGenericContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  688. nonHomeGenericContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  689. nonHomeGenericContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  690. savedJobsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  691. savedJobsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  692. savedJobsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  693. savedJobsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor)
  694. ])
  695. nonHomeTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  696. nonHomeTitleLabel.textColor = Theme.primaryText
  697. nonHomeTitleLabel.alignment = .center
  698. nonHomeTitleLabel.maximumNumberOfLines = 1
  699. nonHomeSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  700. nonHomeSubtitleLabel.textColor = Theme.secondaryText
  701. nonHomeSubtitleLabel.alignment = .center
  702. nonHomeSubtitleLabel.maximumNumberOfLines = 0
  703. nonHomeSubtitleLabel.stringValue = "This area is not available in the preview build. Use Home to search jobs."
  704. let genericStack = NSStackView(views: [nonHomeTitleLabel, nonHomeSubtitleLabel])
  705. genericStack.orientation = .vertical
  706. genericStack.spacing = 10
  707. genericStack.alignment = .centerX
  708. genericStack.translatesAutoresizingMaskIntoConstraints = false
  709. nonHomeGenericContainer.addSubview(genericStack)
  710. NSLayoutConstraint.activate([
  711. genericStack.centerXAnchor.constraint(equalTo: nonHomeGenericContainer.centerXAnchor),
  712. genericStack.centerYAnchor.constraint(equalTo: nonHomeGenericContainer.centerYAnchor),
  713. genericStack.leadingAnchor.constraint(greaterThanOrEqualTo: nonHomeGenericContainer.leadingAnchor, constant: 32),
  714. genericStack.trailingAnchor.constraint(lessThanOrEqualTo: nonHomeGenericContainer.trailingAnchor, constant: -32),
  715. nonHomeSubtitleLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 420)
  716. ])
  717. savedJobsPageTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  718. savedJobsPageTitleLabel.textColor = Theme.primaryText
  719. savedJobsPageTitleLabel.alignment = .left
  720. savedJobsPageTitleLabel.maximumNumberOfLines = 1
  721. savedJobsPageSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  722. savedJobsPageSubtitleLabel.textColor = Theme.secondaryText
  723. savedJobsPageSubtitleLabel.alignment = .left
  724. savedJobsPageSubtitleLabel.maximumNumberOfLines = 0
  725. savedJobsDocumentView.translatesAutoresizingMaskIntoConstraints = false
  726. savedJobsStack.orientation = .vertical
  727. savedJobsStack.spacing = 14
  728. savedJobsStack.alignment = .leading
  729. savedJobsStack.distribution = .fill
  730. savedJobsStack.translatesAutoresizingMaskIntoConstraints = false
  731. savedJobsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  732. savedJobsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  733. savedJobsDocumentView.addSubview(savedJobsStack)
  734. NSLayoutConstraint.activate([
  735. savedJobsStack.leadingAnchor.constraint(equalTo: savedJobsDocumentView.leadingAnchor),
  736. savedJobsStack.trailingAnchor.constraint(equalTo: savedJobsDocumentView.trailingAnchor),
  737. savedJobsStack.topAnchor.constraint(equalTo: savedJobsDocumentView.topAnchor),
  738. savedJobsStack.bottomAnchor.constraint(equalTo: savedJobsDocumentView.bottomAnchor)
  739. ])
  740. savedJobsScrollView.translatesAutoresizingMaskIntoConstraints = false
  741. savedJobsScrollView.hasVerticalScroller = true
  742. savedJobsScrollView.hasHorizontalScroller = false
  743. savedJobsScrollView.autohidesScrollers = true
  744. savedJobsScrollView.drawsBackground = false
  745. savedJobsScrollView.borderType = .noBorder
  746. savedJobsScrollView.documentView = savedJobsDocumentView
  747. savedJobsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  748. savedJobsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  749. let savedHeaderStack = NSStackView(views: [savedJobsPageTitleLabel, savedJobsPageSubtitleLabel])
  750. savedHeaderStack.orientation = .vertical
  751. savedHeaderStack.spacing = 6
  752. savedHeaderStack.alignment = .leading
  753. savedHeaderStack.translatesAutoresizingMaskIntoConstraints = false
  754. let savedOuterStack = NSStackView(views: [savedHeaderStack, savedJobsScrollView])
  755. savedOuterStack.orientation = .vertical
  756. savedOuterStack.spacing = 16
  757. // Leading alignment plus explicit column width keeps the title and subtitle on the same edge as the cards.
  758. savedOuterStack.alignment = .leading
  759. savedOuterStack.translatesAutoresizingMaskIntoConstraints = false
  760. savedJobsPageContainer.userInterfaceLayoutDirection = .leftToRight
  761. savedJobsPageContainer.addSubview(savedOuterStack)
  762. NSLayoutConstraint.activate([
  763. savedOuterStack.leadingAnchor.constraint(equalTo: savedJobsPageContainer.leadingAnchor, constant: 32),
  764. savedOuterStack.trailingAnchor.constraint(equalTo: savedJobsPageContainer.trailingAnchor, constant: -32),
  765. savedOuterStack.topAnchor.constraint(equalTo: savedJobsPageContainer.topAnchor, constant: 8),
  766. savedOuterStack.bottomAnchor.constraint(equalTo: savedJobsPageContainer.bottomAnchor),
  767. savedHeaderStack.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  768. savedJobsScrollView.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  769. savedJobsDocumentView.topAnchor.constraint(equalTo: savedJobsScrollView.contentView.topAnchor),
  770. savedJobsDocumentView.leadingAnchor.constraint(equalTo: savedJobsScrollView.contentView.leadingAnchor),
  771. savedJobsDocumentView.widthAnchor.constraint(equalTo: savedJobsScrollView.contentView.widthAnchor)
  772. ])
  773. }
  774. private func reloadSavedJobsListings() {
  775. savedJobsStack.arrangedSubviews.forEach {
  776. savedJobsStack.removeArrangedSubview($0)
  777. $0.removeFromSuperview()
  778. }
  779. if savedJobOrder.isEmpty {
  780. savedJobsPageSubtitleLabel.stringValue = "Save jobs from Home to see them here."
  781. let empty = NSTextField(wrappingLabelWithString: "No saved jobs yet. Search on Home, then tap Save on a listing.")
  782. empty.font = .systemFont(ofSize: 14, weight: .regular)
  783. empty.textColor = Theme.secondaryText
  784. empty.alignment = .left
  785. empty.maximumNumberOfLines = 0
  786. empty.translatesAutoresizingMaskIntoConstraints = false
  787. savedJobsStack.addArrangedSubview(empty)
  788. empty.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  789. return
  790. }
  791. savedJobsPageSubtitleLabel.stringValue = "\(savedJobOrder.count) saved \(savedJobOrder.count == 1 ? "position" : "positions")"
  792. for job in savedJobOrder {
  793. let card = makeJobListingCard(job, context: .savedJobsPage)
  794. savedJobsStack.addArrangedSubview(card)
  795. card.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  796. }
  797. }
  798. private func isSavedJobsSidebarIndex(_ index: Int) -> Bool {
  799. guard index >= 0, index < currentSidebarItems.count else { return false }
  800. return currentSidebarItems[index].title == "Saved Jobs"
  801. }
  802. private func isHomeSidebarIndex(_ index: Int) -> Bool {
  803. guard index >= 0, index < currentSidebarItems.count else { return false }
  804. return currentSidebarItems[index].title == "Home"
  805. }
  806. private func updateMainContentVisibility() {
  807. let home = isHomeSidebarIndex(selectedSidebarIndex)
  808. let savedJobs = isSavedJobsSidebarIndex(selectedSidebarIndex)
  809. mainOverlay.isHidden = !home
  810. nonHomeHost.isHidden = home
  811. nonHomeGenericContainer.isHidden = savedJobs
  812. savedJobsPageContainer.isHidden = !savedJobs
  813. if !home, selectedSidebarIndex < currentSidebarItems.count {
  814. if savedJobs {
  815. reloadSavedJobsListings()
  816. } else {
  817. nonHomeTitleLabel.stringValue = currentSidebarItems[selectedSidebarIndex].title
  818. }
  819. }
  820. }
  821. /// Removes result cards or the “no matches” message so a new search starts from a blank listing area.
  822. private func clearJobSearchResultsUI() {
  823. configureJobListings([], noResultsForQuery: nil)
  824. if let doc = jobListingsScrollView.documentView {
  825. doc.scroll(NSPoint(x: 0, y: 0))
  826. }
  827. }
  828. /// Restores the main job-search experience: cleared query and no listings until the user searches again.
  829. private func applyHomeState() {
  830. jobKeywordsField.stringValue = ""
  831. clearJobSearchResultsUI()
  832. window?.makeFirstResponder(nil)
  833. }
  834. private func updateSearchBarShadowPath() {
  835. guard searchBarShadowHost.bounds.width > 0, searchBarShadowHost.bounds.height > 0 else { return }
  836. let r = searchBarShadowHost.bounds
  837. let radius = min(r.height / 2, 27)
  838. searchBarShadowHost.layer?.shadowPath = CGPath(
  839. roundedRect: r,
  840. cornerWidth: radius,
  841. cornerHeight: radius,
  842. transform: nil
  843. )
  844. }
  845. @objc private func didSubmitSearch() {
  846. let query = jobKeywordsField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  847. let results = jobsMatchingSearch(query: query, in: catalogJobListings)
  848. let noResultsMessage: String? = (!results.isEmpty || query.isEmpty) ? nil : query
  849. configureJobListings(results, noResultsForQuery: noResultsMessage)
  850. window?.makeFirstResponder(nil)
  851. }
  852. /// Each whitespace-separated token must appear in the title or description (case-insensitive). Empty query returns the full catalog.
  853. private func jobsMatchingSearch(query: String, in jobs: [JobListing]) -> [JobListing] {
  854. let tokens = query
  855. .lowercased()
  856. .split(whereSeparator: { $0.isWhitespace })
  857. .map(String.init)
  858. .filter { !$0.isEmpty }
  859. guard !tokens.isEmpty else { return jobs }
  860. return jobs.filter { job in
  861. let haystack = "\(job.title) \(job.description)".lowercased()
  862. return tokens.allSatisfy { haystack.contains($0) }
  863. }
  864. }
  865. func controlTextDidBeginEditing(_ obj: Notification) {
  866. applySearchFieldInsertionPoint(obj.object)
  867. if (obj.object as? NSTextField) === jobKeywordsField {
  868. clearJobSearchResultsUI()
  869. }
  870. }
  871. func controlTextDidChange(_ obj: Notification) {
  872. applySearchFieldInsertionPoint(obj.object)
  873. }
  874. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  875. guard control === jobKeywordsField, commandSelector == #selector(NSResponder.insertNewline(_:)) else {
  876. return false
  877. }
  878. didSubmitSearch()
  879. return true
  880. }
  881. private func applySearchFieldInsertionPoint(_ object: Any?) {
  882. guard let field = object as? NSTextField,
  883. field === jobKeywordsField,
  884. let textView = field.window?.fieldEditor(true, for: field) as? NSTextView else { return }
  885. textView.insertionPointColor = Theme.primaryText
  886. }
  887. private func configureSidebar() {
  888. let items = currentSidebarItems
  889. sidebar.arrangedSubviews.forEach {
  890. sidebar.removeArrangedSubview($0)
  891. $0.removeFromSuperview()
  892. }
  893. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  894. brand.font = .systemFont(ofSize: 18, weight: .bold)
  895. brand.textColor = Theme.brandBlue
  896. brand.alignment = .left
  897. sidebar.addArrangedSubview(brand)
  898. let titleToMenuSpacer = NSView()
  899. titleToMenuSpacer.translatesAutoresizingMaskIntoConstraints = false
  900. titleToMenuSpacer.heightAnchor.constraint(equalToConstant: 24).isActive = true
  901. sidebar.addArrangedSubview(titleToMenuSpacer)
  902. items.enumerated().forEach { index, item in
  903. let isSelected = index == selectedSidebarIndex
  904. let rowHost = SidebarNavRowView { [weak self] in
  905. self?.selectSidebarItem(at: index)
  906. }
  907. rowHost.translatesAutoresizingMaskIntoConstraints = false
  908. rowHost.wantsLayer = true
  909. rowHost.layer?.cornerRadius = 8
  910. rowHost.restingBackgroundColor = isSelected ? Theme.selectionFill : nil
  911. rowHost.hoverBackgroundColor = isSelected ? Theme.selectionFillHover : Theme.sidebarRowHoverFill
  912. rowHost.setAccessibilityLabel(item.title)
  913. rowHost.setAccessibilityRole(.button)
  914. rowHost.setAccessibilitySelected(isSelected)
  915. let row = NSStackView()
  916. row.orientation = .horizontal
  917. row.spacing = 8
  918. row.alignment = .centerY
  919. row.translatesAutoresizingMaskIntoConstraints = false
  920. let icon = NSImageView()
  921. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  922. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  923. icon.contentTintColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  924. let text = NSTextField(labelWithString: item.title)
  925. text.font = .systemFont(ofSize: 14, weight: .medium)
  926. text.textColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  927. text.refusesFirstResponder = true
  928. row.addArrangedSubview(icon)
  929. row.addArrangedSubview(text)
  930. if let badge = item.badge {
  931. let badgeField = NSTextField(labelWithString: badge)
  932. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  933. badgeField.textColor = Theme.primaryText
  934. badgeField.wantsLayer = true
  935. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  936. badgeField.layer?.cornerRadius = 8
  937. badgeField.alignment = .center
  938. badgeField.maximumNumberOfLines = 1
  939. badgeField.lineBreakMode = .byClipping
  940. badgeField.refusesFirstResponder = true
  941. badgeField.translatesAutoresizingMaskIntoConstraints = false
  942. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  943. row.addArrangedSubview(NSView())
  944. row.addArrangedSubview(badgeField)
  945. }
  946. rowHost.addSubview(row)
  947. NSLayoutConstraint.activate([
  948. row.leadingAnchor.constraint(equalTo: rowHost.leadingAnchor, constant: 10),
  949. row.trailingAnchor.constraint(equalTo: rowHost.trailingAnchor, constant: -10),
  950. row.topAnchor.constraint(equalTo: rowHost.topAnchor, constant: 8),
  951. row.bottomAnchor.constraint(equalTo: rowHost.bottomAnchor, constant: -8)
  952. ])
  953. rowHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  954. sidebar.addArrangedSubview(rowHost)
  955. let sidebarHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  956. rowHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -sidebarHorizontalInset).isActive = true
  957. }
  958. let sidebarBottomSpacer = NSView()
  959. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  960. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  961. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  962. sidebar.addArrangedSubview(sidebarBottomSpacer)
  963. let upgradeCard = NSView()
  964. upgradeCard.translatesAutoresizingMaskIntoConstraints = false
  965. upgradeCard.wantsLayer = true
  966. upgradeCard.layer?.backgroundColor = Theme.proCardFill.cgColor
  967. upgradeCard.layer?.cornerRadius = 14
  968. upgradeCard.layer?.borderWidth = 1
  969. upgradeCard.layer?.borderColor = Theme.proCardBorder.cgColor
  970. upgradeCard.layer?.masksToBounds = true
  971. let accentBar = NSView()
  972. accentBar.translatesAutoresizingMaskIntoConstraints = false
  973. accentBar.wantsLayer = true
  974. accentBar.layer?.backgroundColor = Theme.proAccent.cgColor
  975. let inner = NSStackView()
  976. inner.translatesAutoresizingMaskIntoConstraints = false
  977. inner.orientation = .vertical
  978. inner.spacing = 10
  979. inner.alignment = .centerX
  980. let proIcon = NSImageView()
  981. proIcon.translatesAutoresizingMaskIntoConstraints = false
  982. proIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  983. proIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: nil)
  984. proIcon.contentTintColor = Theme.proAccent
  985. let proEyebrow = NSTextField(labelWithString: "Premium")
  986. proEyebrow.font = .systemFont(ofSize: 11, weight: .heavy)
  987. proEyebrow.textColor = Theme.proAccent
  988. proEyebrow.alignment = .center
  989. let eyebrowRow = NSStackView(views: [proIcon, proEyebrow])
  990. eyebrowRow.orientation = .horizontal
  991. eyebrowRow.spacing = 6
  992. eyebrowRow.alignment = .centerY
  993. let headline = NSTextField(labelWithString: "Upgrade to Pro")
  994. headline.font = .systemFont(ofSize: 16, weight: .bold)
  995. headline.textColor = Theme.primaryText
  996. headline.alignment = .center
  997. let upgradeDescription = NSTextField(wrappingLabelWithString: "Unlimited AI matches, smart alerts, and interview prep—all in one place.")
  998. upgradeDescription.font = .systemFont(ofSize: 12, weight: .regular)
  999. upgradeDescription.textColor = Theme.secondaryText
  1000. upgradeDescription.alignment = .center
  1001. // Sidebar content width is the fixed sidebar width minus horizontal edge insets; card must stay within that band.
  1002. let cardWidth: CGFloat = 186
  1003. let innerContentWidth = cardWidth - 28
  1004. upgradeDescription.preferredMaxLayoutWidth = innerContentWidth
  1005. let upgradeButton = HoverableButton(title: "Upgrade to Pro", target: self, action: #selector(didTapUpgradeToPro))
  1006. upgradeButton.isBordered = false
  1007. upgradeButton.bezelStyle = .rounded
  1008. upgradeButton.font = .systemFont(ofSize: 13, weight: .bold)
  1009. upgradeButton.contentTintColor = Theme.proCTAText
  1010. upgradeButton.alignment = .center
  1011. upgradeButton.wantsLayer = true
  1012. upgradeButton.layer?.backgroundColor = Theme.proCTABackground.cgColor
  1013. upgradeButton.layer?.cornerRadius = 20
  1014. upgradeButton.translatesAutoresizingMaskIntoConstraints = false
  1015. upgradeButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  1016. upgradeButton.pointerCursor = true
  1017. upgradeButton.hoverHandler = { [weak upgradeButton] hovering in
  1018. upgradeButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.proCTABackground).cgColor
  1019. }
  1020. inner.addArrangedSubview(eyebrowRow)
  1021. inner.addArrangedSubview(headline)
  1022. inner.addArrangedSubview(upgradeDescription)
  1023. inner.addArrangedSubview(upgradeButton)
  1024. upgradeCard.addSubview(accentBar)
  1025. upgradeCard.addSubview(inner)
  1026. NSLayoutConstraint.activate([
  1027. upgradeCard.widthAnchor.constraint(equalToConstant: cardWidth),
  1028. accentBar.topAnchor.constraint(equalTo: upgradeCard.topAnchor),
  1029. accentBar.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor),
  1030. accentBar.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor),
  1031. accentBar.heightAnchor.constraint(equalToConstant: 2),
  1032. inner.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor, constant: 14),
  1033. inner.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor, constant: -14),
  1034. inner.topAnchor.constraint(equalTo: accentBar.bottomAnchor, constant: 12),
  1035. inner.bottomAnchor.constraint(equalTo: upgradeCard.bottomAnchor, constant: -14),
  1036. upgradeButton.widthAnchor.constraint(equalTo: inner.widthAnchor)
  1037. ])
  1038. sidebar.addArrangedSubview(upgradeCard)
  1039. }
  1040. @objc private func didTapUpgradeToPro() {
  1041. guard let url = URL(string: "https://www.indeed.com") else { return }
  1042. NSWorkspace.shared.open(url)
  1043. }
  1044. private func selectSidebarItem(at index: Int) {
  1045. guard index >= 0, index < currentSidebarItems.count else { return }
  1046. let selectingHome = isHomeSidebarIndex(index)
  1047. if index == selectedSidebarIndex {
  1048. if selectingHome {
  1049. applyHomeState()
  1050. }
  1051. return
  1052. }
  1053. selectedSidebarIndex = index
  1054. configureSidebar()
  1055. updateMainContentVisibility()
  1056. if selectingHome {
  1057. applyHomeState()
  1058. }
  1059. }
  1060. }
  1061. /// `NSButton` that carries a `JobListing` for card actions (`representedObject` is unavailable on `NSButton` in this target).
  1062. private final class JobPayloadButton: HoverableButton {
  1063. var jobPayload: JobListing?
  1064. var cardContext: JobListingCardContext = .homeSearchResults
  1065. }
  1066. /// `NSButton` with a tracking area that reports hover transitions and (optionally) swaps in a pointing-hand cursor while hovered.
  1067. private class HoverableButton: NSButton {
  1068. var hoverHandler: ((Bool) -> Void)?
  1069. var pointerCursor: Bool = false
  1070. private(set) var isHovering: Bool = false
  1071. private var trackingArea: NSTrackingArea?
  1072. private var didPushCursor: Bool = false
  1073. override func updateTrackingAreas() {
  1074. super.updateTrackingAreas()
  1075. if let area = trackingArea { removeTrackingArea(area) }
  1076. let area = NSTrackingArea(
  1077. rect: bounds,
  1078. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1079. owner: self,
  1080. userInfo: nil
  1081. )
  1082. addTrackingArea(area)
  1083. trackingArea = area
  1084. }
  1085. override func mouseEntered(with event: NSEvent) {
  1086. super.mouseEntered(with: event)
  1087. isHovering = true
  1088. hoverHandler?(true)
  1089. if pointerCursor, !didPushCursor {
  1090. NSCursor.pointingHand.push()
  1091. didPushCursor = true
  1092. }
  1093. }
  1094. override func mouseExited(with event: NSEvent) {
  1095. super.mouseExited(with: event)
  1096. isHovering = false
  1097. hoverHandler?(false)
  1098. if didPushCursor {
  1099. NSCursor.pop()
  1100. didPushCursor = false
  1101. }
  1102. }
  1103. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1104. super.viewWillMove(toWindow: newWindow)
  1105. // Guard against an unbalanced cursor stack if the button is removed mid-hover (e.g. job card replaced after a search).
  1106. if newWindow == nil, didPushCursor {
  1107. NSCursor.pop()
  1108. didPushCursor = false
  1109. isHovering = false
  1110. }
  1111. }
  1112. }
  1113. /// `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.
  1114. private class HoverableView: NSView {
  1115. var hoverHandler: ((Bool) -> Void)?
  1116. var pointerCursor: Bool = false
  1117. private(set) var isHovering: Bool = false
  1118. private var trackingArea: NSTrackingArea?
  1119. private var didPushCursor: Bool = false
  1120. override func updateTrackingAreas() {
  1121. super.updateTrackingAreas()
  1122. if let area = trackingArea { removeTrackingArea(area) }
  1123. let area = NSTrackingArea(
  1124. rect: bounds,
  1125. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1126. owner: self,
  1127. userInfo: nil
  1128. )
  1129. addTrackingArea(area)
  1130. trackingArea = area
  1131. }
  1132. override func mouseEntered(with event: NSEvent) {
  1133. super.mouseEntered(with: event)
  1134. isHovering = true
  1135. hoverHandler?(true)
  1136. if pointerCursor, !didPushCursor {
  1137. NSCursor.pointingHand.push()
  1138. didPushCursor = true
  1139. }
  1140. }
  1141. override func mouseExited(with event: NSEvent) {
  1142. super.mouseExited(with: event)
  1143. isHovering = false
  1144. hoverHandler?(false)
  1145. if didPushCursor {
  1146. NSCursor.pop()
  1147. didPushCursor = false
  1148. }
  1149. }
  1150. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1151. super.viewWillMove(toWindow: newWindow)
  1152. if newWindow == nil, didPushCursor {
  1153. NSCursor.pop()
  1154. didPushCursor = false
  1155. isHovering = false
  1156. }
  1157. }
  1158. }
  1159. /// 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).
  1160. private final class JobListingsDocumentView: NSView {
  1161. override var isFlipped: Bool { true }
  1162. }
  1163. /// 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.
  1164. private final class SidebarNavRowView: NSView {
  1165. private let onSelect: () -> Void
  1166. var restingBackgroundColor: NSColor? {
  1167. didSet { applyBackground() }
  1168. }
  1169. var hoverBackgroundColor: NSColor?
  1170. private var isHovering: Bool = false
  1171. private var didPushCursor: Bool = false
  1172. init(onSelect: @escaping () -> Void) {
  1173. self.onSelect = onSelect
  1174. super.init(frame: .zero)
  1175. }
  1176. @available(*, unavailable)
  1177. required init?(coder: NSCoder) {
  1178. fatalError("init(coder:) has not been implemented")
  1179. }
  1180. override func hitTest(_ point: NSPoint) -> NSView? {
  1181. guard let superview else { return super.hitTest(point) }
  1182. let local = convert(point, from: superview)
  1183. return bounds.contains(local) ? self : nil
  1184. }
  1185. override func mouseDown(with event: NSEvent) {
  1186. onSelect()
  1187. }
  1188. override func updateTrackingAreas() {
  1189. super.updateTrackingAreas()
  1190. trackingAreas.forEach { removeTrackingArea($0) }
  1191. addTrackingArea(NSTrackingArea(
  1192. rect: bounds,
  1193. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1194. owner: self,
  1195. userInfo: nil
  1196. ))
  1197. }
  1198. override func mouseEntered(with event: NSEvent) {
  1199. super.mouseEntered(with: event)
  1200. isHovering = true
  1201. applyBackground()
  1202. if !didPushCursor {
  1203. NSCursor.pointingHand.push()
  1204. didPushCursor = true
  1205. }
  1206. }
  1207. override func mouseExited(with event: NSEvent) {
  1208. super.mouseExited(with: event)
  1209. isHovering = false
  1210. applyBackground()
  1211. if didPushCursor {
  1212. NSCursor.pop()
  1213. didPushCursor = false
  1214. }
  1215. }
  1216. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1217. super.viewWillMove(toWindow: newWindow)
  1218. if newWindow == nil, didPushCursor {
  1219. NSCursor.pop()
  1220. didPushCursor = false
  1221. isHovering = false
  1222. }
  1223. }
  1224. private func applyBackground() {
  1225. let color = isHovering ? (hoverBackgroundColor ?? restingBackgroundColor) : restingBackgroundColor
  1226. layer?.backgroundColor = color?.cgColor
  1227. }
  1228. }