Няма описание

DashboardView.swift 40KB

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