Ei kuvausta

DashboardView.swift 66KB

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