Nessuna descrizione

DashboardView.swift 47KB

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