Nenhuma descrição

DashboardView.swift 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. //
  2. // DashboardView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. import QuartzCore
  7. final class DashboardView: NSView, NSTextFieldDelegate {
  8. /// Indeed.com-inspired neutrals and brand blue (white surfaces, `#2557a7` accent, `#2d2d2d` / `#767676` text, `#d4d2d0` borders).
  9. private enum Theme {
  10. static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  11. static let pageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  12. static let chromeBackground = NSColor(srgbRed: 247 / 255, green: 247 / 255, blue: 247 / 255, alpha: 1)
  13. static let sidebarBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  14. static let mainHostBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  15. /// Subtitle on the welcome hero: readable blue-gray aligned with brand.
  16. static let welcomeSubtitleText = NSColor(srgbRed: 52 / 255, green: 92 / 255, blue: 142 / 255, alpha: 1)
  17. static let selectionFill = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.12)
  18. static let cardBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  19. static let toggleBackground = NSColor(srgbRed: 232 / 255, green: 232 / 255, blue: 232 / 255, alpha: 1)
  20. static let primaryText = NSColor(srgbRed: 45 / 255, green: 45 / 255, blue: 45 / 255, alpha: 1)
  21. static let secondaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  22. static let tertiaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  23. static let border = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  24. /// Job search bar outer stroke (charcoal).
  25. static let searchBarBorder = NSColor(srgbRed: 58 / 255, green: 58 / 255, blue: 58 / 255, alpha: 1)
  26. static let proCardFill = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  27. static let proCardBorder = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  28. static let proAccent = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  29. static let proCTABackground = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  30. static let proCTAText = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  31. /// Slightly lighter blue for the top of the search-bar “Find jobs” pill (reads less flat than a solid fill).
  32. static let findJobsCTAHighlight = NSColor(srgbRed: 54 / 255, green: 110 / 255, blue: 198 / 255, alpha: 1)
  33. }
  34. private let contentStack = NSStackView()
  35. private let chromeContainer = NSView()
  36. private let sidebar = NSStackView()
  37. private let mainHost = NSView()
  38. private let mainOverlay = NSStackView()
  39. private let greetingLabel = NSTextField(labelWithString: "")
  40. private let subtitleLabel = NSTextField(labelWithString: "")
  41. private let searchBarShadowHost = NSView()
  42. private let searchCard = NSView()
  43. private let jobSearchIcon = NSImageView()
  44. private let jobKeywordsField = NSTextField()
  45. private let findJobsButton = NSButton()
  46. private let findJobsCTAHost = NSView()
  47. private let findJobsCTAChrome = NSView()
  48. private var findJobsCTAGradientLayer: CAGradientLayer?
  49. private let jobListingsScrollView = NSScrollView()
  50. /// Flipped so short result lists stay visually under the search bar instead of leaving a gap above the cards.
  51. private let jobListingsContainer = JobListingsDocumentView()
  52. private let jobListingsStack = NSStackView()
  53. private var currentSidebarItems: [SidebarItem] = []
  54. private var selectedSidebarIndex: Int = 0
  55. /// Full list from `DashboardData`; results are shown after the user runs a search.
  56. private var catalogJobListings: [JobListing] = []
  57. override init(frame frameRect: NSRect) {
  58. super.init(frame: frameRect)
  59. setupLayout()
  60. }
  61. required init?(coder: NSCoder) {
  62. super.init(coder: coder)
  63. setupLayout()
  64. }
  65. override func layout() {
  66. super.layout()
  67. updateSearchBarShadowPath()
  68. findJobsCTAGradientLayer?.frame = findJobsCTAChrome.bounds
  69. updateFindJobsCTAShadowPath()
  70. updateJobListingDescriptionWidths()
  71. }
  72. func render(_ data: DashboardData) {
  73. greetingLabel.stringValue = "Welcome"
  74. subtitleLabel.stringValue = data.subtitle
  75. currentSidebarItems = data.sidebarItems
  76. if selectedSidebarIndex >= currentSidebarItems.count {
  77. selectedSidebarIndex = max(0, currentSidebarItems.count - 1)
  78. }
  79. configureSidebar()
  80. catalogJobListings = data.jobListings
  81. configureJobListings([], noResultsForQuery: nil)
  82. }
  83. private func setupLayout() {
  84. wantsLayer = true
  85. layer?.backgroundColor = Theme.pageBackground.cgColor
  86. contentStack.orientation = .horizontal
  87. contentStack.spacing = 20
  88. contentStack.distribution = .fill
  89. contentStack.translatesAutoresizingMaskIntoConstraints = false
  90. contentStack.alignment = .height
  91. contentStack.edgeInsets = NSEdgeInsets(top: 24, left: 24, bottom: 24, right: 24)
  92. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  93. chromeContainer.wantsLayer = true
  94. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  95. chromeContainer.layer?.cornerRadius = 0
  96. addSubview(chromeContainer)
  97. chromeContainer.addSubview(contentStack)
  98. sidebar.orientation = .vertical
  99. sidebar.spacing = 10
  100. sidebar.distribution = .fill
  101. sidebar.alignment = .leading
  102. sidebar.wantsLayer = true
  103. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  104. sidebar.layer?.cornerRadius = 16
  105. sidebar.edgeInsets = NSEdgeInsets(top: 18, left: 14, bottom: 18, right: 14)
  106. sidebar.translatesAutoresizingMaskIntoConstraints = false
  107. mainHost.translatesAutoresizingMaskIntoConstraints = false
  108. mainHost.wantsLayer = true
  109. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  110. mainHost.layer?.cornerRadius = 16
  111. mainHost.layer?.masksToBounds = true
  112. sidebar.setContentHuggingPriority(.required, for: .horizontal)
  113. mainHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  114. mainHost.addSubview(mainOverlay)
  115. mainOverlay.orientation = .vertical
  116. mainOverlay.spacing = 0
  117. mainOverlay.alignment = .centerX
  118. mainOverlay.distribution = .fill
  119. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  120. mainOverlay.setContentHuggingPriority(.defaultLow, for: .vertical)
  121. greetingLabel.font = .systemFont(ofSize: 32, weight: .bold)
  122. greetingLabel.textColor = Theme.brandBlue
  123. greetingLabel.alignment = .center
  124. greetingLabel.maximumNumberOfLines = 1
  125. subtitleLabel.font = .systemFont(ofSize: 15, weight: .regular)
  126. subtitleLabel.textColor = Theme.welcomeSubtitleText
  127. subtitleLabel.alignment = .center
  128. subtitleLabel.maximumNumberOfLines = 2
  129. let topInset = NSView()
  130. topInset.translatesAutoresizingMaskIntoConstraints = false
  131. topInset.heightAnchor.constraint(equalToConstant: 32).isActive = true
  132. configureSearchBar()
  133. let titleBlock = NSStackView(views: [greetingLabel, subtitleLabel])
  134. titleBlock.orientation = .vertical
  135. titleBlock.spacing = 10
  136. titleBlock.alignment = .centerX
  137. let midSpacer = NSView()
  138. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  139. midSpacer.heightAnchor.constraint(equalToConstant: 20).isActive = true
  140. let listingsTopSpacer = NSView()
  141. listingsTopSpacer.translatesAutoresizingMaskIntoConstraints = false
  142. listingsTopSpacer.heightAnchor.constraint(equalToConstant: 12).isActive = true
  143. jobListingsContainer.translatesAutoresizingMaskIntoConstraints = false
  144. jobListingsStack.orientation = .vertical
  145. jobListingsStack.spacing = 14
  146. // `.leading` keeps cards left-anchored; explicit width constraints below stretch each card across the full row.
  147. jobListingsStack.alignment = .leading
  148. jobListingsStack.distribution = .fill
  149. jobListingsStack.translatesAutoresizingMaskIntoConstraints = false
  150. jobListingsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  151. jobListingsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  152. jobListingsContainer.addSubview(jobListingsStack)
  153. NSLayoutConstraint.activate([
  154. jobListingsStack.leadingAnchor.constraint(equalTo: jobListingsContainer.leadingAnchor),
  155. jobListingsStack.trailingAnchor.constraint(equalTo: jobListingsContainer.trailingAnchor),
  156. jobListingsStack.topAnchor.constraint(equalTo: jobListingsContainer.topAnchor),
  157. jobListingsStack.bottomAnchor.constraint(equalTo: jobListingsContainer.bottomAnchor)
  158. ])
  159. jobListingsScrollView.translatesAutoresizingMaskIntoConstraints = false
  160. jobListingsScrollView.hasVerticalScroller = true
  161. jobListingsScrollView.hasHorizontalScroller = false
  162. jobListingsScrollView.autohidesScrollers = true
  163. jobListingsScrollView.drawsBackground = false
  164. jobListingsScrollView.borderType = .noBorder
  165. jobListingsScrollView.documentView = jobListingsContainer
  166. jobListingsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  167. jobListingsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  168. mainOverlay.addArrangedSubview(topInset)
  169. mainOverlay.addArrangedSubview(titleBlock)
  170. mainOverlay.addArrangedSubview(midSpacer)
  171. mainOverlay.addArrangedSubview(searchBarShadowHost)
  172. mainOverlay.addArrangedSubview(listingsTopSpacer)
  173. mainOverlay.addArrangedSubview(jobListingsScrollView)
  174. contentStack.addArrangedSubview(sidebar)
  175. contentStack.addArrangedSubview(mainHost)
  176. NSLayoutConstraint.activate([
  177. chromeContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
  178. chromeContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
  179. chromeContainer.topAnchor.constraint(equalTo: topAnchor),
  180. chromeContainer.bottomAnchor.constraint(equalTo: bottomAnchor),
  181. contentStack.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor),
  182. contentStack.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor),
  183. contentStack.topAnchor.constraint(equalTo: chromeContainer.topAnchor),
  184. contentStack.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor),
  185. sidebar.widthAnchor.constraint(equalToConstant: 218),
  186. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  187. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  188. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  189. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  190. mainOverlay.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  191. searchBarShadowHost.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  192. jobListingsScrollView.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  193. jobListingsContainer.topAnchor.constraint(equalTo: jobListingsScrollView.contentView.topAnchor),
  194. jobListingsContainer.leadingAnchor.constraint(equalTo: jobListingsScrollView.contentView.leadingAnchor),
  195. jobListingsContainer.widthAnchor.constraint(equalTo: jobListingsScrollView.contentView.widthAnchor),
  196. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 24),
  197. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -24),
  198. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  199. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor)
  200. ])
  201. }
  202. private func updateJobListingDescriptionWidths() {
  203. let containerWidth = jobListingsContainer.bounds.width
  204. guard containerWidth > 1 else { return }
  205. let innerWidth = containerWidth - 32
  206. var didChange = false
  207. for card in jobListingsStack.arrangedSubviews {
  208. guard let desc = card.viewWithTag(502) as? NSTextField else { continue }
  209. if abs(desc.preferredMaxLayoutWidth - innerWidth) > 0.5 {
  210. desc.preferredMaxLayoutWidth = innerWidth
  211. desc.invalidateIntrinsicContentSize()
  212. didChange = true
  213. }
  214. }
  215. if didChange {
  216. // Wrapping width changed, so card heights need to recompute against the new intrinsic sizes.
  217. jobListingsStack.needsLayout = true
  218. }
  219. }
  220. private func configureJobListings(_ jobs: [JobListing], noResultsForQuery: String?) {
  221. jobListingsStack.arrangedSubviews.forEach {
  222. jobListingsStack.removeArrangedSubview($0)
  223. $0.removeFromSuperview()
  224. }
  225. if jobs.isEmpty, let query = noResultsForQuery, !query.isEmpty {
  226. let empty = NSTextField(wrappingLabelWithString: "No jobs match “\(query)”. Try different keywords or browse the full list with an empty search.")
  227. empty.font = .systemFont(ofSize: 14, weight: .regular)
  228. empty.textColor = Theme.secondaryText
  229. empty.alignment = .center
  230. empty.maximumNumberOfLines = 0
  231. empty.translatesAutoresizingMaskIntoConstraints = false
  232. jobListingsStack.addArrangedSubview(empty)
  233. empty.widthAnchor.constraint(equalTo: jobListingsStack.widthAnchor).isActive = true
  234. return
  235. }
  236. for job in jobs {
  237. let card = makeJobListingCard(job)
  238. jobListingsStack.addArrangedSubview(card)
  239. // Force every card to span the full row instead of hugging its intrinsic content width.
  240. card.widthAnchor.constraint(equalTo: jobListingsStack.widthAnchor).isActive = true
  241. }
  242. }
  243. private func makeJobListingCard(_ job: JobListing) -> NSView {
  244. let card = NSView()
  245. card.translatesAutoresizingMaskIntoConstraints = false
  246. card.wantsLayer = true
  247. card.layer?.backgroundColor = Theme.cardBackground.cgColor
  248. card.layer?.cornerRadius = 12
  249. card.layer?.borderWidth = 1
  250. card.layer?.borderColor = Theme.border.cgColor
  251. card.layer?.masksToBounds = true
  252. let titleField = NSTextField(labelWithString: job.title)
  253. titleField.font = .systemFont(ofSize: 16, weight: .semibold)
  254. titleField.textColor = Theme.primaryText
  255. titleField.maximumNumberOfLines = 2
  256. titleField.lineBreakMode = .byWordWrapping
  257. titleField.translatesAutoresizingMaskIntoConstraints = false
  258. let descriptionField = NSTextField(wrappingLabelWithString: job.description)
  259. descriptionField.font = .systemFont(ofSize: 13, weight: .regular)
  260. descriptionField.textColor = Theme.secondaryText
  261. descriptionField.maximumNumberOfLines = 0
  262. descriptionField.tag = 502
  263. descriptionField.translatesAutoresizingMaskIntoConstraints = false
  264. let inner = NSStackView(views: [titleField, descriptionField])
  265. inner.orientation = .vertical
  266. inner.spacing = 6
  267. inner.alignment = .leading
  268. inner.translatesAutoresizingMaskIntoConstraints = false
  269. card.addSubview(inner)
  270. NSLayoutConstraint.activate([
  271. inner.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
  272. inner.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -16),
  273. inner.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
  274. inner.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -14),
  275. titleField.leadingAnchor.constraint(equalTo: inner.leadingAnchor),
  276. titleField.trailingAnchor.constraint(equalTo: inner.trailingAnchor),
  277. descriptionField.leadingAnchor.constraint(equalTo: inner.leadingAnchor),
  278. descriptionField.trailingAnchor.constraint(equalTo: inner.trailingAnchor)
  279. ])
  280. return card
  281. }
  282. private func configureSearchBar() {
  283. let pillCorner: CGFloat = 27
  284. let barHeight: CGFloat = 54
  285. searchBarShadowHost.translatesAutoresizingMaskIntoConstraints = false
  286. searchBarShadowHost.wantsLayer = true
  287. searchBarShadowHost.layer?.masksToBounds = false
  288. searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(0.18).cgColor
  289. searchBarShadowHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  290. searchBarShadowHost.layer?.shadowRadius = 10
  291. searchBarShadowHost.layer?.shadowOpacity = 1
  292. searchBarShadowHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  293. searchCard.translatesAutoresizingMaskIntoConstraints = false
  294. searchCard.wantsLayer = true
  295. searchCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  296. searchCard.layer?.cornerRadius = pillCorner
  297. searchCard.layer?.borderWidth = 1
  298. searchCard.layer?.borderColor = Theme.searchBarBorder.cgColor
  299. searchCard.layer?.masksToBounds = true
  300. searchBarShadowHost.addSubview(searchCard)
  301. func configureField(_ field: NSTextField, placeholder: String) {
  302. field.translatesAutoresizingMaskIntoConstraints = false
  303. field.isBordered = false
  304. field.drawsBackground = false
  305. field.focusRingType = .none
  306. field.font = .systemFont(ofSize: 14, weight: .regular)
  307. field.textColor = Theme.primaryText
  308. field.delegate = self
  309. field.placeholderAttributedString = NSAttributedString(
  310. string: placeholder,
  311. attributes: [
  312. .foregroundColor: Theme.secondaryText,
  313. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  314. ]
  315. )
  316. field.cell?.usesSingleLineMode = true
  317. field.cell?.wraps = false
  318. field.cell?.isScrollable = true
  319. field.target = self
  320. field.action = #selector(didSubmitSearch)
  321. }
  322. jobSearchIcon.translatesAutoresizingMaskIntoConstraints = false
  323. jobSearchIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .medium)
  324. jobSearchIcon.image = NSImage(systemSymbolName: "magnifyingglass", accessibilityDescription: "Job search")
  325. jobSearchIcon.contentTintColor = Theme.primaryText
  326. configureField(jobKeywordsField, placeholder: "Job title, keywords, or company")
  327. let ctaHeight: CGFloat = 42
  328. let ctaCorner = ctaHeight / 2
  329. findJobsCTAHost.translatesAutoresizingMaskIntoConstraints = false
  330. findJobsCTAHost.wantsLayer = true
  331. findJobsCTAHost.layer?.masksToBounds = false
  332. findJobsCTAHost.layer?.shadowColor = NSColor.black.cgColor
  333. findJobsCTAHost.layer?.shadowOpacity = 0.16
  334. findJobsCTAHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  335. findJobsCTAHost.layer?.shadowRadius = 6
  336. findJobsCTAChrome.translatesAutoresizingMaskIntoConstraints = false
  337. findJobsCTAChrome.wantsLayer = true
  338. findJobsCTAChrome.layer?.masksToBounds = true
  339. findJobsCTAChrome.layer?.cornerRadius = ctaCorner
  340. if #available(macOS 11.0, *) {
  341. findJobsCTAChrome.layer?.cornerCurve = .continuous
  342. }
  343. let gradient = CAGradientLayer()
  344. gradient.colors = [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  345. gradient.startPoint = CGPoint(x: 0.5, y: 1)
  346. gradient.endPoint = CGPoint(x: 0.5, y: 0)
  347. findJobsCTAChrome.layer?.addSublayer(gradient)
  348. findJobsCTAGradientLayer = gradient
  349. findJobsButton.translatesAutoresizingMaskIntoConstraints = false
  350. findJobsButton.title = ""
  351. findJobsButton.attributedTitle = NSAttributedString(
  352. string: "Find jobs",
  353. attributes: [
  354. .font: NSFont.systemFont(ofSize: 14, weight: .semibold),
  355. .foregroundColor: Theme.proCTAText,
  356. .kern: 0.35
  357. ]
  358. )
  359. findJobsButton.isBordered = false
  360. findJobsButton.bezelStyle = .rounded
  361. findJobsButton.wantsLayer = true
  362. findJobsButton.layer?.backgroundColor = NSColor.clear.cgColor
  363. findJobsButton.focusRingType = .none
  364. findJobsButton.target = self
  365. findJobsButton.action = #selector(didSubmitSearch)
  366. findJobsButton.setContentHuggingPriority(.required, for: .horizontal)
  367. findJobsButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  368. findJobsCTAHost.addSubview(findJobsCTAChrome)
  369. findJobsCTAHost.addSubview(findJobsButton)
  370. NSLayoutConstraint.activate([
  371. findJobsCTAChrome.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor),
  372. findJobsCTAChrome.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor),
  373. findJobsCTAChrome.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  374. findJobsCTAChrome.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor),
  375. findJobsButton.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor, constant: 14),
  376. findJobsButton.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor, constant: -14),
  377. findJobsButton.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  378. findJobsButton.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor)
  379. ])
  380. let keywordsStack = NSStackView(views: [jobSearchIcon, jobKeywordsField])
  381. keywordsStack.orientation = .horizontal
  382. keywordsStack.spacing = 10
  383. keywordsStack.alignment = .centerY
  384. keywordsStack.translatesAutoresizingMaskIntoConstraints = false
  385. keywordsStack.edgeInsets = NSEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
  386. keywordsStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  387. let row = NSStackView(views: [keywordsStack, findJobsCTAHost])
  388. row.orientation = .horizontal
  389. row.spacing = 0
  390. row.alignment = .centerY
  391. row.distribution = .fill
  392. row.translatesAutoresizingMaskIntoConstraints = false
  393. row.edgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 7)
  394. searchCard.addSubview(row)
  395. NSLayoutConstraint.activate([
  396. searchCard.leadingAnchor.constraint(equalTo: searchBarShadowHost.leadingAnchor),
  397. searchCard.trailingAnchor.constraint(equalTo: searchBarShadowHost.trailingAnchor),
  398. searchCard.topAnchor.constraint(equalTo: searchBarShadowHost.topAnchor),
  399. searchCard.bottomAnchor.constraint(equalTo: searchBarShadowHost.bottomAnchor),
  400. searchBarShadowHost.heightAnchor.constraint(equalToConstant: barHeight),
  401. row.leadingAnchor.constraint(equalTo: searchCard.leadingAnchor),
  402. row.trailingAnchor.constraint(equalTo: searchCard.trailingAnchor),
  403. row.topAnchor.constraint(equalTo: searchCard.topAnchor),
  404. row.bottomAnchor.constraint(equalTo: searchCard.bottomAnchor),
  405. jobSearchIcon.widthAnchor.constraint(equalToConstant: 18),
  406. jobSearchIcon.heightAnchor.constraint(equalToConstant: 18),
  407. findJobsCTAHost.heightAnchor.constraint(equalToConstant: ctaHeight),
  408. findJobsCTAHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 112)
  409. ])
  410. }
  411. private func updateFindJobsCTAShadowPath() {
  412. guard findJobsCTAHost.bounds.width > 0, findJobsCTAHost.bounds.height > 0 else { return }
  413. let r = findJobsCTAHost.bounds
  414. let radius = min(r.height / 2, r.width / 2)
  415. findJobsCTAHost.layer?.shadowPath = CGPath(
  416. roundedRect: r,
  417. cornerWidth: radius,
  418. cornerHeight: radius,
  419. transform: nil
  420. )
  421. }
  422. private func updateSearchBarShadowPath() {
  423. guard searchBarShadowHost.bounds.width > 0, searchBarShadowHost.bounds.height > 0 else { return }
  424. let r = searchBarShadowHost.bounds
  425. let radius = min(r.height / 2, 27)
  426. searchBarShadowHost.layer?.shadowPath = CGPath(
  427. roundedRect: r,
  428. cornerWidth: radius,
  429. cornerHeight: radius,
  430. transform: nil
  431. )
  432. }
  433. @objc private func didSubmitSearch() {
  434. let query = jobKeywordsField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  435. let results = jobsMatchingSearch(query: query, in: catalogJobListings)
  436. let noResultsMessage: String? = (!results.isEmpty || query.isEmpty) ? nil : query
  437. configureJobListings(results, noResultsForQuery: noResultsMessage)
  438. window?.makeFirstResponder(nil)
  439. }
  440. /// Each whitespace-separated token must appear in the title or description (case-insensitive). Empty query returns the full catalog.
  441. private func jobsMatchingSearch(query: String, in jobs: [JobListing]) -> [JobListing] {
  442. let tokens = query
  443. .lowercased()
  444. .split(whereSeparator: { $0.isWhitespace })
  445. .map(String.init)
  446. .filter { !$0.isEmpty }
  447. guard !tokens.isEmpty else { return jobs }
  448. return jobs.filter { job in
  449. let haystack = "\(job.title) \(job.description)".lowercased()
  450. return tokens.allSatisfy { haystack.contains($0) }
  451. }
  452. }
  453. func controlTextDidBeginEditing(_ obj: Notification) {
  454. applySearchFieldInsertionPoint(obj.object)
  455. }
  456. func controlTextDidChange(_ obj: Notification) {
  457. applySearchFieldInsertionPoint(obj.object)
  458. }
  459. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  460. guard control === jobKeywordsField, commandSelector == #selector(NSResponder.insertNewline(_:)) else {
  461. return false
  462. }
  463. didSubmitSearch()
  464. return true
  465. }
  466. private func applySearchFieldInsertionPoint(_ object: Any?) {
  467. guard let field = object as? NSTextField,
  468. field === jobKeywordsField,
  469. let textView = field.window?.fieldEditor(true, for: field) as? NSTextView else { return }
  470. textView.insertionPointColor = Theme.primaryText
  471. }
  472. private func configureSidebar() {
  473. let items = currentSidebarItems
  474. sidebar.arrangedSubviews.forEach {
  475. sidebar.removeArrangedSubview($0)
  476. $0.removeFromSuperview()
  477. }
  478. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  479. brand.font = .systemFont(ofSize: 18, weight: .bold)
  480. brand.textColor = Theme.brandBlue
  481. brand.alignment = .left
  482. sidebar.addArrangedSubview(brand)
  483. let titleToMenuSpacer = NSView()
  484. titleToMenuSpacer.translatesAutoresizingMaskIntoConstraints = false
  485. titleToMenuSpacer.heightAnchor.constraint(equalToConstant: 24).isActive = true
  486. sidebar.addArrangedSubview(titleToMenuSpacer)
  487. items.enumerated().forEach { index, item in
  488. let isSelected = index == selectedSidebarIndex
  489. let rowHost = SidebarNavRowView { [weak self] in
  490. self?.selectSidebarItem(at: index)
  491. }
  492. rowHost.translatesAutoresizingMaskIntoConstraints = false
  493. rowHost.wantsLayer = true
  494. rowHost.layer?.cornerRadius = 8
  495. if isSelected {
  496. rowHost.layer?.backgroundColor = Theme.selectionFill.cgColor
  497. }
  498. rowHost.setAccessibilityLabel(item.title)
  499. rowHost.setAccessibilityRole(.button)
  500. rowHost.setAccessibilitySelected(isSelected)
  501. let row = NSStackView()
  502. row.orientation = .horizontal
  503. row.spacing = 8
  504. row.alignment = .centerY
  505. row.translatesAutoresizingMaskIntoConstraints = false
  506. let icon = NSImageView()
  507. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  508. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  509. icon.contentTintColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  510. let text = NSTextField(labelWithString: item.title)
  511. text.font = .systemFont(ofSize: 14, weight: .medium)
  512. text.textColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  513. text.refusesFirstResponder = true
  514. row.addArrangedSubview(icon)
  515. row.addArrangedSubview(text)
  516. if let badge = item.badge {
  517. let badgeField = NSTextField(labelWithString: badge)
  518. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  519. badgeField.textColor = Theme.primaryText
  520. badgeField.wantsLayer = true
  521. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  522. badgeField.layer?.cornerRadius = 8
  523. badgeField.alignment = .center
  524. badgeField.maximumNumberOfLines = 1
  525. badgeField.lineBreakMode = .byClipping
  526. badgeField.refusesFirstResponder = true
  527. badgeField.translatesAutoresizingMaskIntoConstraints = false
  528. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  529. row.addArrangedSubview(NSView())
  530. row.addArrangedSubview(badgeField)
  531. }
  532. rowHost.addSubview(row)
  533. NSLayoutConstraint.activate([
  534. row.leadingAnchor.constraint(equalTo: rowHost.leadingAnchor, constant: 10),
  535. row.trailingAnchor.constraint(equalTo: rowHost.trailingAnchor, constant: -10),
  536. row.topAnchor.constraint(equalTo: rowHost.topAnchor, constant: 8),
  537. row.bottomAnchor.constraint(equalTo: rowHost.bottomAnchor, constant: -8)
  538. ])
  539. rowHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  540. sidebar.addArrangedSubview(rowHost)
  541. let sidebarHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  542. rowHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -sidebarHorizontalInset).isActive = true
  543. }
  544. let sidebarBottomSpacer = NSView()
  545. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  546. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  547. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  548. sidebar.addArrangedSubview(sidebarBottomSpacer)
  549. let upgradeCard = NSView()
  550. upgradeCard.translatesAutoresizingMaskIntoConstraints = false
  551. upgradeCard.wantsLayer = true
  552. upgradeCard.layer?.backgroundColor = Theme.proCardFill.cgColor
  553. upgradeCard.layer?.cornerRadius = 14
  554. upgradeCard.layer?.borderWidth = 1
  555. upgradeCard.layer?.borderColor = Theme.proCardBorder.cgColor
  556. upgradeCard.layer?.masksToBounds = true
  557. let accentBar = NSView()
  558. accentBar.translatesAutoresizingMaskIntoConstraints = false
  559. accentBar.wantsLayer = true
  560. accentBar.layer?.backgroundColor = Theme.proAccent.cgColor
  561. let inner = NSStackView()
  562. inner.translatesAutoresizingMaskIntoConstraints = false
  563. inner.orientation = .vertical
  564. inner.spacing = 10
  565. inner.alignment = .centerX
  566. let proIcon = NSImageView()
  567. proIcon.translatesAutoresizingMaskIntoConstraints = false
  568. proIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  569. proIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: nil)
  570. proIcon.contentTintColor = Theme.proAccent
  571. let proEyebrow = NSTextField(labelWithString: "Premium")
  572. proEyebrow.font = .systemFont(ofSize: 11, weight: .heavy)
  573. proEyebrow.textColor = Theme.proAccent
  574. proEyebrow.alignment = .center
  575. let eyebrowRow = NSStackView(views: [proIcon, proEyebrow])
  576. eyebrowRow.orientation = .horizontal
  577. eyebrowRow.spacing = 6
  578. eyebrowRow.alignment = .centerY
  579. let headline = NSTextField(labelWithString: "Upgrade to Pro")
  580. headline.font = .systemFont(ofSize: 16, weight: .bold)
  581. headline.textColor = Theme.primaryText
  582. headline.alignment = .center
  583. let upgradeDescription = NSTextField(wrappingLabelWithString: "Unlimited AI matches, smart alerts, and interview prep—all in one place.")
  584. upgradeDescription.font = .systemFont(ofSize: 12, weight: .regular)
  585. upgradeDescription.textColor = Theme.secondaryText
  586. upgradeDescription.alignment = .center
  587. // Sidebar content width is 190pt (218 − edge insets); card must stay within that band.
  588. let cardWidth: CGFloat = 186
  589. let innerContentWidth = cardWidth - 28
  590. upgradeDescription.preferredMaxLayoutWidth = innerContentWidth
  591. let upgradeButton = NSButton(title: "Upgrade to Pro", target: self, action: #selector(didTapUpgradeToPro))
  592. upgradeButton.isBordered = false
  593. upgradeButton.bezelStyle = .rounded
  594. upgradeButton.font = .systemFont(ofSize: 13, weight: .bold)
  595. upgradeButton.contentTintColor = Theme.proCTAText
  596. upgradeButton.alignment = .center
  597. upgradeButton.wantsLayer = true
  598. upgradeButton.layer?.backgroundColor = Theme.proCTABackground.cgColor
  599. upgradeButton.layer?.cornerRadius = 20
  600. upgradeButton.translatesAutoresizingMaskIntoConstraints = false
  601. upgradeButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  602. inner.addArrangedSubview(eyebrowRow)
  603. inner.addArrangedSubview(headline)
  604. inner.addArrangedSubview(upgradeDescription)
  605. inner.addArrangedSubview(upgradeButton)
  606. upgradeCard.addSubview(accentBar)
  607. upgradeCard.addSubview(inner)
  608. NSLayoutConstraint.activate([
  609. upgradeCard.widthAnchor.constraint(equalToConstant: cardWidth),
  610. accentBar.topAnchor.constraint(equalTo: upgradeCard.topAnchor),
  611. accentBar.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor),
  612. accentBar.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor),
  613. accentBar.heightAnchor.constraint(equalToConstant: 2),
  614. inner.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor, constant: 14),
  615. inner.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor, constant: -14),
  616. inner.topAnchor.constraint(equalTo: accentBar.bottomAnchor, constant: 12),
  617. inner.bottomAnchor.constraint(equalTo: upgradeCard.bottomAnchor, constant: -14),
  618. upgradeButton.widthAnchor.constraint(equalTo: inner.widthAnchor)
  619. ])
  620. sidebar.addArrangedSubview(upgradeCard)
  621. }
  622. @objc private func didTapUpgradeToPro() {
  623. guard let url = URL(string: "https://www.indeed.com") else { return }
  624. NSWorkspace.shared.open(url)
  625. }
  626. private func selectSidebarItem(at index: Int) {
  627. guard index >= 0, index < currentSidebarItems.count, index != selectedSidebarIndex else { return }
  628. selectedSidebarIndex = index
  629. configureSidebar()
  630. }
  631. }
  632. /// 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).
  633. private final class JobListingsDocumentView: NSView {
  634. override var isFlipped: Bool { true }
  635. }
  636. /// Captures clicks for the full sidebar pill so icon, label, and padding behave as one tab.
  637. private final class SidebarNavRowView: NSView {
  638. private let onSelect: () -> Void
  639. init(onSelect: @escaping () -> Void) {
  640. self.onSelect = onSelect
  641. super.init(frame: .zero)
  642. }
  643. @available(*, unavailable)
  644. required init?(coder: NSCoder) {
  645. fatalError("init(coder:) has not been implemented")
  646. }
  647. override func hitTest(_ point: NSPoint) -> NSView? {
  648. guard let superview else { return super.hitTest(point) }
  649. let local = convert(point, from: superview)
  650. return bounds.contains(local) ? self : nil
  651. }
  652. override func mouseDown(with event: NSEvent) {
  653. onSelect()
  654. }
  655. override func updateTrackingAreas() {
  656. super.updateTrackingAreas()
  657. trackingAreas.forEach { removeTrackingArea($0) }
  658. addTrackingArea(NSTrackingArea(
  659. rect: bounds,
  660. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  661. owner: self,
  662. userInfo: nil
  663. ))
  664. }
  665. override func mouseEntered(with event: NSEvent) {
  666. super.mouseEntered(with: event)
  667. NSCursor.pointingHand.push()
  668. }
  669. override func mouseExited(with event: NSEvent) {
  670. super.mouseExited(with: event)
  671. NSCursor.pop()
  672. }
  673. }