Aucune description

DashboardView.swift 58KB

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