Geen omschrijving

DashboardView.swift 75KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. //
  2. // DashboardView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. import QuartzCore
  7. private enum JobListingCardContext {
  8. case homeSearchResults
  9. case savedJobsPage
  10. }
  11. final class DashboardView: NSView, NSTextFieldDelegate {
  12. /// Indeed.com-inspired neutrals and brand blue (white surfaces, `#2557a7` accent, `#2d2d2d` / `#767676` text, `#d4d2d0` borders).
  13. private enum Theme {
  14. static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  15. static let pageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  16. static let chromeBackground = NSColor(srgbRed: 247 / 255, green: 247 / 255, blue: 247 / 255, alpha: 1)
  17. static let sidebarBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  18. static let mainHostBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  19. /// Subtitle on the welcome hero: readable blue-gray aligned with brand.
  20. static let welcomeSubtitleText = NSColor(srgbRed: 52 / 255, green: 92 / 255, blue: 142 / 255, alpha: 1)
  21. static let selectionFill = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.12)
  22. static let cardBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  23. static let toggleBackground = NSColor(srgbRed: 232 / 255, green: 232 / 255, blue: 232 / 255, alpha: 1)
  24. static let primaryText = NSColor(srgbRed: 45 / 255, green: 45 / 255, blue: 45 / 255, alpha: 1)
  25. static let secondaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  26. static let tertiaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  27. static let border = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  28. /// Job search bar outer stroke (charcoal).
  29. static let searchBarBorder = NSColor(srgbRed: 58 / 255, green: 58 / 255, blue: 58 / 255, alpha: 1)
  30. /// Search bar border on hover (brand-tinted, matches focus affordance elsewhere).
  31. static let searchBarBorderHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.45)
  32. static let proCardFill = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  33. static let proCardBorder = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  34. static let proAccent = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  35. static let proCTABackground = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  36. static let proCTAText = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  37. /// Slightly lighter blue for the top of the search-bar “Find jobs” pill (reads less flat than a solid fill).
  38. static let findJobsCTAHighlight = NSColor(srgbRed: 54 / 255, green: 110 / 255, blue: 198 / 255, alpha: 1)
  39. /// Hover states: darker brand blue, deeper gradient top, stronger tints, and subtle neutral fills used across CTAs, toggles, and the sidebar.
  40. static let brandBlueHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  41. static let findJobsCTAHighlightHover = NSColor(srgbRed: 44 / 255, green: 94 / 255, blue: 178 / 255, alpha: 1)
  42. static let selectionFillHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.2)
  43. static let neutralHoverFill = NSColor(srgbRed: 240 / 255, green: 240 / 255, blue: 240 / 255, alpha: 1)
  44. static let sidebarRowHoverFill = NSColor(srgbRed: 0, green: 0, blue: 0, alpha: 0.04)
  45. static let settingsPageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  46. static let settingsGroupBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  47. static let settingsIconBackground = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  48. static let settingsDivider = NSColor(srgbRed: 228 / 255, green: 228 / 255, blue: 228 / 255, alpha: 1)
  49. }
  50. /// Multiline `NSTextField` often ignores `alignment` for wrapped runs; explicit paragraph alignment matches the title.
  51. private static func jobListingDescriptionAttributedString(_ plain: String) -> NSAttributedString {
  52. let paragraph = NSMutableParagraphStyle()
  53. paragraph.alignment = .left
  54. paragraph.lineBreakMode = .byWordWrapping
  55. paragraph.baseWritingDirection = .leftToRight
  56. let font = NSFont.systemFont(ofSize: 13, weight: .regular)
  57. return NSAttributedString(string: plain, attributes: [
  58. .font: font,
  59. .foregroundColor: Theme.secondaryText,
  60. .paragraphStyle: paragraph
  61. ])
  62. }
  63. private let contentStack = NSStackView()
  64. private let chromeContainer = NSView()
  65. private let sidebar = NSStackView()
  66. private let mainHost = NSView()
  67. private let mainOverlay = NSStackView()
  68. private let greetingLabel = NSTextField(labelWithString: "")
  69. private let subtitleLabel = NSTextField(labelWithString: "")
  70. private let searchBarShadowHost = NSView()
  71. private let searchCard = HoverableView()
  72. private let jobSearchIcon = NSImageView()
  73. private let jobKeywordsField = NSTextField()
  74. private let findJobsButton = HoverableButton()
  75. private let findJobsCTAHost = NSView()
  76. private let findJobsCTAChrome = HoverableView()
  77. private var findJobsCTAGradientLayer: CAGradientLayer?
  78. private let jobListingsScrollView = NSScrollView()
  79. /// Flipped so short result lists stay visually under the search bar instead of leaving a gap above the cards.
  80. private let jobListingsContainer = JobListingsDocumentView()
  81. private let jobListingsStack = NSStackView()
  82. /// Shown when a sidebar item other than Home is selected.
  83. private let nonHomeHost = NSView()
  84. private let nonHomeGenericContainer = NSView()
  85. private let nonHomeTitleLabel = NSTextField(labelWithString: "")
  86. private let nonHomeSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  87. private let savedJobsPageContainer = NSView()
  88. private let savedJobsPageTitleLabel = NSTextField(labelWithString: "Saved Jobs")
  89. private let savedJobsPageSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  90. private let savedJobsScrollView = NSScrollView()
  91. private let savedJobsDocumentView = JobListingsDocumentView()
  92. private let savedJobsStack = NSStackView()
  93. private let settingsPageContainer = NSView()
  94. private let themeControl = NSSegmentedControl(labels: ["System", "Light", "Dark"], trackingMode: .selectOne, target: nil, action: nil)
  95. private var currentSidebarItems: [SidebarItem] = []
  96. private var selectedSidebarIndex: Int = 0
  97. /// Full list from `DashboardData`; results are shown after the user runs a search.
  98. private var catalogJobListings: [JobListing] = []
  99. /// Last successful search result set (used when removing a card with the dismiss control).
  100. private var lastSearchResults: [JobListing] = []
  101. private var lastNoResultsQuery: String?
  102. /// Most recently saved jobs appear first; persisted across launches.
  103. private var savedJobOrder: [JobListing] = []
  104. override init(frame frameRect: NSRect) {
  105. super.init(frame: frameRect)
  106. setupLayout()
  107. }
  108. required init?(coder: NSCoder) {
  109. super.init(coder: coder)
  110. setupLayout()
  111. }
  112. override func layout() {
  113. super.layout()
  114. updateSearchBarShadowPath()
  115. findJobsCTAGradientLayer?.frame = findJobsCTAChrome.bounds
  116. updateFindJobsCTAShadowPath()
  117. updateJobListingDescriptionWidths()
  118. }
  119. func render(_ data: DashboardData) {
  120. greetingLabel.stringValue = "Welcome"
  121. subtitleLabel.stringValue = data.subtitle
  122. currentSidebarItems = data.sidebarItems
  123. if selectedSidebarIndex >= currentSidebarItems.count {
  124. selectedSidebarIndex = max(0, currentSidebarItems.count - 1)
  125. }
  126. configureSidebar()
  127. catalogJobListings = data.jobListings
  128. savedJobOrder = Self.normalizedSavedJobs(SavedJobsStore.load())
  129. configureJobListings([], noResultsForQuery: nil)
  130. updateMainContentVisibility()
  131. }
  132. private func setupLayout() {
  133. wantsLayer = true
  134. layer?.backgroundColor = Theme.pageBackground.cgColor
  135. contentStack.orientation = .horizontal
  136. contentStack.spacing = 10
  137. contentStack.distribution = .fill
  138. contentStack.translatesAutoresizingMaskIntoConstraints = false
  139. contentStack.alignment = .height
  140. // Tighter chrome insets so panels sit closer to the window edges (especially leading / top under the title bar).
  141. contentStack.edgeInsets = NSEdgeInsets(top: 10, left: 12, bottom: 20, right: 20)
  142. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  143. chromeContainer.wantsLayer = true
  144. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  145. chromeContainer.layer?.cornerRadius = 0
  146. addSubview(chromeContainer)
  147. chromeContainer.addSubview(contentStack)
  148. sidebar.orientation = .vertical
  149. sidebar.spacing = 10
  150. sidebar.distribution = .fill
  151. sidebar.alignment = .leading
  152. sidebar.wantsLayer = true
  153. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  154. sidebar.layer?.cornerRadius = 16
  155. sidebar.edgeInsets = NSEdgeInsets(top: 16, left: 12, bottom: 16, right: 12)
  156. sidebar.translatesAutoresizingMaskIntoConstraints = false
  157. mainHost.translatesAutoresizingMaskIntoConstraints = false
  158. mainHost.wantsLayer = true
  159. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  160. mainHost.layer?.cornerRadius = 16
  161. mainHost.layer?.masksToBounds = true
  162. sidebar.setContentHuggingPriority(.required, for: .horizontal)
  163. mainHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  164. mainHost.addSubview(mainOverlay)
  165. configureNonHomePlaceholder()
  166. mainHost.addSubview(nonHomeHost)
  167. mainOverlay.orientation = .vertical
  168. mainOverlay.spacing = 0
  169. mainOverlay.alignment = .centerX
  170. mainOverlay.distribution = .fill
  171. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  172. mainOverlay.setContentHuggingPriority(.defaultLow, for: .vertical)
  173. greetingLabel.font = .systemFont(ofSize: 32, weight: .bold)
  174. greetingLabel.textColor = Theme.brandBlue
  175. greetingLabel.alignment = .center
  176. greetingLabel.maximumNumberOfLines = 1
  177. subtitleLabel.font = .systemFont(ofSize: 15, weight: .regular)
  178. subtitleLabel.textColor = Theme.welcomeSubtitleText
  179. subtitleLabel.alignment = .center
  180. subtitleLabel.maximumNumberOfLines = 2
  181. let topInset = NSView()
  182. topInset.translatesAutoresizingMaskIntoConstraints = false
  183. topInset.heightAnchor.constraint(equalToConstant: 18).isActive = true
  184. configureSearchBar()
  185. let titleBlock = NSStackView(views: [greetingLabel, subtitleLabel])
  186. titleBlock.orientation = .vertical
  187. titleBlock.spacing = 10
  188. titleBlock.alignment = .centerX
  189. let midSpacer = NSView()
  190. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  191. midSpacer.heightAnchor.constraint(equalToConstant: 20).isActive = true
  192. let listingsTopSpacer = NSView()
  193. listingsTopSpacer.translatesAutoresizingMaskIntoConstraints = false
  194. listingsTopSpacer.heightAnchor.constraint(equalToConstant: 12).isActive = true
  195. jobListingsContainer.translatesAutoresizingMaskIntoConstraints = false
  196. jobListingsStack.orientation = .vertical
  197. jobListingsStack.spacing = 14
  198. // `.leading` keeps cards left-anchored; explicit width constraints below stretch each card across the full row.
  199. jobListingsStack.alignment = .leading
  200. jobListingsStack.distribution = .fill
  201. jobListingsStack.translatesAutoresizingMaskIntoConstraints = false
  202. jobListingsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  203. jobListingsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  204. jobListingsContainer.addSubview(jobListingsStack)
  205. NSLayoutConstraint.activate([
  206. jobListingsStack.leadingAnchor.constraint(equalTo: jobListingsContainer.leadingAnchor),
  207. jobListingsStack.trailingAnchor.constraint(equalTo: jobListingsContainer.trailingAnchor),
  208. jobListingsStack.topAnchor.constraint(equalTo: jobListingsContainer.topAnchor),
  209. jobListingsStack.bottomAnchor.constraint(equalTo: jobListingsContainer.bottomAnchor)
  210. ])
  211. jobListingsScrollView.translatesAutoresizingMaskIntoConstraints = false
  212. jobListingsScrollView.hasVerticalScroller = true
  213. jobListingsScrollView.hasHorizontalScroller = false
  214. jobListingsScrollView.autohidesScrollers = true
  215. jobListingsScrollView.drawsBackground = false
  216. jobListingsScrollView.borderType = .noBorder
  217. jobListingsScrollView.documentView = jobListingsContainer
  218. jobListingsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  219. jobListingsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  220. mainOverlay.addArrangedSubview(topInset)
  221. mainOverlay.addArrangedSubview(titleBlock)
  222. mainOverlay.addArrangedSubview(midSpacer)
  223. mainOverlay.addArrangedSubview(searchBarShadowHost)
  224. mainOverlay.addArrangedSubview(listingsTopSpacer)
  225. mainOverlay.addArrangedSubview(jobListingsScrollView)
  226. contentStack.addArrangedSubview(sidebar)
  227. contentStack.addArrangedSubview(mainHost)
  228. NSLayoutConstraint.activate([
  229. chromeContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
  230. chromeContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
  231. chromeContainer.topAnchor.constraint(equalTo: topAnchor),
  232. chromeContainer.bottomAnchor.constraint(equalTo: bottomAnchor),
  233. contentStack.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor),
  234. contentStack.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor),
  235. contentStack.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
  236. contentStack.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor),
  237. sidebar.widthAnchor.constraint(equalToConstant: 218),
  238. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  239. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  240. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  241. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  242. mainOverlay.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  243. nonHomeHost.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  244. nonHomeHost.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  245. nonHomeHost.topAnchor.constraint(equalTo: mainHost.topAnchor),
  246. nonHomeHost.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  247. searchBarShadowHost.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  248. jobListingsScrollView.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  249. jobListingsContainer.topAnchor.constraint(equalTo: jobListingsScrollView.contentView.topAnchor),
  250. jobListingsContainer.leadingAnchor.constraint(equalTo: jobListingsScrollView.contentView.leadingAnchor),
  251. jobListingsContainer.widthAnchor.constraint(equalTo: jobListingsScrollView.contentView.widthAnchor),
  252. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 16),
  253. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -16),
  254. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  255. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor)
  256. ])
  257. }
  258. private func updateJobListingDescriptionWidths() {
  259. updateDescriptionColumnWidths(in: jobListingsStack, containerWidth: jobListingsContainer.bounds.width)
  260. updateDescriptionColumnWidths(in: savedJobsStack, containerWidth: savedJobsDocumentView.bounds.width)
  261. }
  262. private func updateDescriptionColumnWidths(in stack: NSStackView, containerWidth: CGFloat) {
  263. guard containerWidth > 1 else { return }
  264. // 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.
  265. let contentHorizontalInset: CGFloat = 32
  266. var didChange = false
  267. for card in stack.arrangedSubviews {
  268. guard let desc = card.viewWithTag(502) as? NSTextField else { continue }
  269. let cardWidth = card.bounds.width > 1 ? card.bounds.width : containerWidth
  270. let fallbackColumn = max(1, cardWidth - contentHorizontalInset)
  271. let columnWidth: CGFloat
  272. if desc.bounds.width > 1 {
  273. columnWidth = desc.bounds.width
  274. } else if let column = desc.superview, column.bounds.width > 1 {
  275. columnWidth = column.bounds.width
  276. } else {
  277. columnWidth = fallbackColumn
  278. }
  279. if abs(desc.preferredMaxLayoutWidth - columnWidth) > 0.5 {
  280. desc.preferredMaxLayoutWidth = columnWidth
  281. desc.invalidateIntrinsicContentSize()
  282. didChange = true
  283. }
  284. }
  285. if didChange {
  286. stack.needsLayout = true
  287. }
  288. }
  289. private func configureJobListings(_ jobs: [JobListing], noResultsForQuery: String?, updateLastResults: Bool = true) {
  290. if updateLastResults {
  291. lastSearchResults = jobs
  292. lastNoResultsQuery = noResultsForQuery
  293. }
  294. jobListingsStack.arrangedSubviews.forEach {
  295. jobListingsStack.removeArrangedSubview($0)
  296. $0.removeFromSuperview()
  297. }
  298. if jobs.isEmpty, let query = noResultsForQuery, !query.isEmpty {
  299. let empty = NSTextField(wrappingLabelWithString: "No jobs match “\(query)”. Try different keywords or browse the full list with an empty search.")
  300. empty.font = .systemFont(ofSize: 14, weight: .regular)
  301. empty.textColor = Theme.secondaryText
  302. empty.alignment = .center
  303. empty.maximumNumberOfLines = 0
  304. empty.translatesAutoresizingMaskIntoConstraints = false
  305. jobListingsStack.addArrangedSubview(empty)
  306. empty.widthAnchor.constraint(equalTo: jobListingsStack.widthAnchor).isActive = true
  307. return
  308. }
  309. for job in jobs {
  310. let card = makeJobListingCard(job, context: .homeSearchResults)
  311. jobListingsStack.addArrangedSubview(card)
  312. // Force every card to span the full row instead of hugging its intrinsic content width.
  313. card.widthAnchor.constraint(equalTo: jobListingsStack.widthAnchor).isActive = true
  314. }
  315. }
  316. private static func normalizedSavedJobs(_ jobs: [JobListing]) -> [JobListing] {
  317. var seen = Set<JobListing>()
  318. var out: [JobListing] = []
  319. for job in jobs where seen.insert(job).inserted {
  320. out.append(job)
  321. }
  322. return out
  323. }
  324. private func isJobSaved(_ job: JobListing) -> Bool {
  325. savedJobOrder.contains(job)
  326. }
  327. private func persistSavedJobs() {
  328. SavedJobsStore.save(savedJobOrder)
  329. }
  330. private func applySavedState(_ saved: Bool, for job: JobListing) {
  331. if saved {
  332. savedJobOrder.removeAll { $0 == job }
  333. savedJobOrder.insert(job, at: 0)
  334. } else {
  335. savedJobOrder.removeAll { $0 == job }
  336. }
  337. persistSavedJobs()
  338. }
  339. private func makeJobListingCard(_ job: JobListing, context: JobListingCardContext) -> NSView {
  340. let card = NSView()
  341. card.translatesAutoresizingMaskIntoConstraints = false
  342. card.wantsLayer = true
  343. card.layer?.backgroundColor = Theme.cardBackground.cgColor
  344. card.layer?.cornerRadius = 12
  345. card.layer?.borderWidth = 1
  346. card.layer?.borderColor = Theme.border.cgColor
  347. card.layer?.masksToBounds = true
  348. let titleField = NSTextField(labelWithString: job.title)
  349. titleField.font = .systemFont(ofSize: 16, weight: .semibold)
  350. titleField.textColor = Theme.primaryText
  351. titleField.maximumNumberOfLines = 2
  352. titleField.lineBreakMode = .byWordWrapping
  353. titleField.alignment = .left
  354. titleField.translatesAutoresizingMaskIntoConstraints = false
  355. let descriptionField = NSTextField(wrappingLabelWithString: job.description)
  356. descriptionField.font = .systemFont(ofSize: 13, weight: .regular)
  357. descriptionField.textColor = Theme.secondaryText
  358. descriptionField.maximumNumberOfLines = 0
  359. descriptionField.alignment = .left
  360. descriptionField.lineBreakMode = .byWordWrapping
  361. descriptionField.baseWritingDirection = .leftToRight
  362. descriptionField.attributedStringValue = Self.jobListingDescriptionAttributedString(job.description)
  363. if let cell = descriptionField.cell as? NSTextFieldCell {
  364. cell.alignment = .left
  365. cell.wraps = true
  366. }
  367. descriptionField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  368. descriptionField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  369. descriptionField.tag = 502
  370. descriptionField.translatesAutoresizingMaskIntoConstraints = false
  371. let applyButton = JobPayloadButton(title: "Apply", target: self, action: #selector(didTapJobApply(_:)))
  372. applyButton.jobPayload = job
  373. applyButton.cardContext = context
  374. applyButton.isBordered = false
  375. applyButton.bezelStyle = .rounded
  376. applyButton.font = .systemFont(ofSize: 13, weight: .semibold)
  377. applyButton.wantsLayer = true
  378. applyButton.layer?.cornerRadius = 6
  379. applyButton.layer?.backgroundColor = Theme.brandBlue.cgColor
  380. applyButton.contentTintColor = Theme.proCTAText
  381. applyButton.focusRingType = .none
  382. applyButton.pointerCursor = true
  383. applyButton.hoverHandler = { [weak applyButton] hovering in
  384. applyButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.brandBlue).cgColor
  385. }
  386. applyButton.setContentHuggingPriority(.required, for: .horizontal)
  387. applyButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  388. let savedOn = isJobSaved(job)
  389. let savedButton = JobPayloadButton(title: savedOn ? "Saved" : "Save", target: self, action: #selector(didTapJobSaved(_:)))
  390. savedButton.jobPayload = job
  391. savedButton.cardContext = context
  392. savedButton.setButtonType(.toggle)
  393. savedButton.isBordered = false
  394. savedButton.bezelStyle = .rounded
  395. savedButton.font = .systemFont(ofSize: 13, weight: .semibold)
  396. savedButton.focusRingType = .none
  397. savedButton.state = savedOn ? .on : .off
  398. savedButton.pointerCursor = true
  399. savedButton.hoverHandler = { [weak self, weak savedButton] _ in
  400. guard let savedButton = savedButton else { return }
  401. self?.styleJobSavedButton(savedButton)
  402. }
  403. styleJobSavedButton(savedButton)
  404. savedButton.setContentHuggingPriority(.required, for: .horizontal)
  405. savedButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  406. let dismissButton = JobPayloadButton()
  407. dismissButton.jobPayload = job
  408. dismissButton.cardContext = context
  409. dismissButton.image = NSImage(systemSymbolName: "xmark", accessibilityDescription: "Dismiss")
  410. dismissButton.imagePosition = .imageOnly
  411. dismissButton.imageScaling = .scaleProportionallyDown
  412. dismissButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  413. dismissButton.isBordered = false
  414. dismissButton.bezelStyle = .rounded
  415. dismissButton.contentTintColor = Theme.secondaryText
  416. dismissButton.target = self
  417. dismissButton.action = #selector(didTapJobDismiss(_:))
  418. dismissButton.toolTip = context == .savedJobsPage ? "Remove from saved" : "Dismiss"
  419. dismissButton.focusRingType = .none
  420. dismissButton.wantsLayer = true
  421. dismissButton.layer?.cornerRadius = 14
  422. dismissButton.layer?.backgroundColor = NSColor.clear.cgColor
  423. dismissButton.pointerCursor = true
  424. dismissButton.hoverHandler = { [weak dismissButton] hovering in
  425. dismissButton?.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : NSColor.clear).cgColor
  426. dismissButton?.contentTintColor = hovering ? Theme.primaryText : Theme.secondaryText
  427. }
  428. dismissButton.setContentHuggingPriority(.required, for: .horizontal)
  429. let buttonRow = NSStackView(views: [applyButton, savedButton, dismissButton])
  430. buttonRow.orientation = .horizontal
  431. buttonRow.spacing = 8
  432. buttonRow.alignment = .centerY
  433. buttonRow.translatesAutoresizingMaskIntoConstraints = false
  434. buttonRow.setContentHuggingPriority(.required, for: .horizontal)
  435. buttonRow.setContentCompressionResistancePriority(.required, for: .horizontal)
  436. // Title hugs the leading edge; a low–hugging-priority spacer absorbs remaining width so buttons stay trailing.
  437. titleField.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  438. titleField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  439. let titleRowSpacer = NSView()
  440. titleRowSpacer.translatesAutoresizingMaskIntoConstraints = false
  441. titleRowSpacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  442. titleRowSpacer.setContentCompressionResistancePriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  443. let titleAndActionsRow = NSStackView(views: [titleField, titleRowSpacer, buttonRow])
  444. titleAndActionsRow.orientation = .horizontal
  445. titleAndActionsRow.spacing = 0
  446. titleAndActionsRow.setCustomSpacing(14, after: titleRowSpacer)
  447. titleAndActionsRow.alignment = .centerY
  448. titleAndActionsRow.distribution = .fill
  449. titleAndActionsRow.userInterfaceLayoutDirection = .leftToRight
  450. titleAndActionsRow.translatesAutoresizingMaskIntoConstraints = false
  451. let contentColumn = NSStackView(views: [titleAndActionsRow, descriptionField])
  452. contentColumn.orientation = .vertical
  453. contentColumn.spacing = 6
  454. contentColumn.alignment = .width
  455. contentColumn.translatesAutoresizingMaskIntoConstraints = false
  456. card.addSubview(contentColumn)
  457. NSLayoutConstraint.activate([
  458. contentColumn.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
  459. contentColumn.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -16),
  460. contentColumn.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
  461. contentColumn.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -14),
  462. applyButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 72),
  463. applyButton.heightAnchor.constraint(equalToConstant: 28),
  464. savedButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 72),
  465. savedButton.heightAnchor.constraint(equalToConstant: 28),
  466. dismissButton.widthAnchor.constraint(equalToConstant: 28),
  467. dismissButton.heightAnchor.constraint(equalToConstant: 28),
  468. descriptionField.widthAnchor.constraint(equalTo: contentColumn.widthAnchor)
  469. ])
  470. return card
  471. }
  472. private func styleJobSavedButton(_ button: NSButton) {
  473. button.wantsLayer = true
  474. button.layer?.cornerRadius = 6
  475. let on = button.state == .on
  476. let hovering = (button as? HoverableButton)?.isHovering ?? false
  477. if on {
  478. button.layer?.backgroundColor = (hovering ? Theme.selectionFillHover : Theme.selectionFill).cgColor
  479. button.layer?.borderWidth = 1
  480. button.layer?.borderColor = Theme.brandBlue.cgColor
  481. button.contentTintColor = Theme.brandBlue
  482. } else {
  483. button.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  484. button.layer?.borderWidth = 1
  485. button.layer?.borderColor = Theme.border.cgColor
  486. button.contentTintColor = Theme.primaryText
  487. }
  488. }
  489. @objc private func didTapJobApply(_ sender: NSButton) {
  490. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  491. let allowed = CharacterSet.urlQueryAllowed
  492. let q = job.title.addingPercentEncoding(withAllowedCharacters: allowed) ?? ""
  493. guard let url = URL(string: "https://www.indeed.com/jobs?q=\(q)") else { return }
  494. NSWorkspace.shared.open(url)
  495. }
  496. @objc private func didTapJobSaved(_ sender: NSButton) {
  497. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  498. let willSave = !isJobSaved(job)
  499. applySavedState(willSave, for: job)
  500. sender.state = willSave ? .on : .off
  501. sender.title = willSave ? "Saved" : "Save"
  502. styleJobSavedButton(sender)
  503. if isSavedJobsSidebarIndex(selectedSidebarIndex) {
  504. reloadSavedJobsListings()
  505. }
  506. }
  507. @objc private func didTapJobDismiss(_ sender: NSButton) {
  508. guard let button = sender as? JobPayloadButton, let job = button.jobPayload else { return }
  509. switch button.cardContext {
  510. case .homeSearchResults:
  511. lastSearchResults.removeAll { $0 == job }
  512. configureJobListings(lastSearchResults, noResultsForQuery: lastNoResultsQuery)
  513. case .savedJobsPage:
  514. applySavedState(false, for: job)
  515. reloadSavedJobsListings()
  516. if isHomeSidebarIndex(selectedSidebarIndex), !lastSearchResults.isEmpty {
  517. configureJobListings(lastSearchResults, noResultsForQuery: lastNoResultsQuery, updateLastResults: false)
  518. }
  519. }
  520. }
  521. private func configureSearchBar() {
  522. let pillCorner: CGFloat = 27
  523. let barHeight: CGFloat = 54
  524. searchBarShadowHost.translatesAutoresizingMaskIntoConstraints = false
  525. searchBarShadowHost.wantsLayer = true
  526. searchBarShadowHost.layer?.masksToBounds = false
  527. searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(0.18).cgColor
  528. searchBarShadowHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  529. searchBarShadowHost.layer?.shadowRadius = 10
  530. searchBarShadowHost.layer?.shadowOpacity = 1
  531. searchBarShadowHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  532. searchCard.translatesAutoresizingMaskIntoConstraints = false
  533. searchCard.wantsLayer = true
  534. searchCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  535. searchCard.layer?.cornerRadius = pillCorner
  536. searchCard.layer?.borderWidth = 1
  537. searchCard.layer?.borderColor = Theme.searchBarBorder.cgColor
  538. searchCard.layer?.masksToBounds = true
  539. searchCard.hoverHandler = { [weak self] hovering in
  540. guard let self else { return }
  541. CATransaction.begin()
  542. CATransaction.setAnimationDuration(0.15)
  543. self.searchCard.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  544. self.searchCard.layer?.borderColor = (hovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  545. self.searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(hovering ? 0.24 : 0.18).cgColor
  546. self.searchBarShadowHost.layer?.shadowRadius = hovering ? 12 : 10
  547. CATransaction.commit()
  548. }
  549. searchBarShadowHost.addSubview(searchCard)
  550. func configureField(_ field: NSTextField, placeholder: String) {
  551. field.translatesAutoresizingMaskIntoConstraints = false
  552. field.isBordered = false
  553. field.drawsBackground = false
  554. field.focusRingType = .none
  555. field.font = .systemFont(ofSize: 14, weight: .regular)
  556. field.textColor = Theme.primaryText
  557. field.delegate = self
  558. field.placeholderAttributedString = NSAttributedString(
  559. string: placeholder,
  560. attributes: [
  561. .foregroundColor: Theme.secondaryText,
  562. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  563. ]
  564. )
  565. field.cell?.usesSingleLineMode = true
  566. field.cell?.wraps = false
  567. field.cell?.isScrollable = true
  568. field.target = self
  569. field.action = #selector(didSubmitSearch)
  570. }
  571. jobSearchIcon.translatesAutoresizingMaskIntoConstraints = false
  572. jobSearchIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .medium)
  573. jobSearchIcon.image = NSImage(systemSymbolName: "magnifyingglass", accessibilityDescription: "Job search")
  574. jobSearchIcon.contentTintColor = Theme.primaryText
  575. configureField(jobKeywordsField, placeholder: "Job title, keywords, or company")
  576. let ctaHeight: CGFloat = 42
  577. let ctaCorner = ctaHeight / 2
  578. findJobsCTAHost.translatesAutoresizingMaskIntoConstraints = false
  579. findJobsCTAHost.wantsLayer = true
  580. findJobsCTAHost.layer?.masksToBounds = false
  581. findJobsCTAHost.layer?.shadowColor = NSColor.black.cgColor
  582. findJobsCTAHost.layer?.shadowOpacity = 0.16
  583. findJobsCTAHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  584. findJobsCTAHost.layer?.shadowRadius = 6
  585. findJobsCTAChrome.translatesAutoresizingMaskIntoConstraints = false
  586. findJobsCTAChrome.wantsLayer = true
  587. findJobsCTAChrome.layer?.masksToBounds = true
  588. findJobsCTAChrome.layer?.cornerRadius = ctaCorner
  589. if #available(macOS 11.0, *) {
  590. findJobsCTAChrome.layer?.cornerCurve = .continuous
  591. }
  592. let gradient = CAGradientLayer()
  593. gradient.colors = [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  594. gradient.startPoint = CGPoint(x: 0.5, y: 1)
  595. gradient.endPoint = CGPoint(x: 0.5, y: 0)
  596. findJobsCTAChrome.layer?.addSublayer(gradient)
  597. findJobsCTAGradientLayer = gradient
  598. // Tracks hover over the full pill (the button only covers an inset area), so the gradient darkens whenever the mouse is anywhere over the CTA.
  599. findJobsCTAChrome.pointerCursor = true
  600. findJobsCTAChrome.hoverHandler = { [weak self] hovering in
  601. guard let layer = self?.findJobsCTAGradientLayer else { return }
  602. CATransaction.begin()
  603. CATransaction.setAnimationDuration(0.15)
  604. layer.colors = hovering
  605. ? [Theme.findJobsCTAHighlightHover.cgColor, Theme.brandBlueHover.cgColor]
  606. : [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  607. CATransaction.commit()
  608. }
  609. findJobsButton.translatesAutoresizingMaskIntoConstraints = false
  610. findJobsButton.title = ""
  611. findJobsButton.attributedTitle = NSAttributedString(
  612. string: "Find jobs",
  613. attributes: [
  614. .font: NSFont.systemFont(ofSize: 14, weight: .semibold),
  615. .foregroundColor: Theme.proCTAText,
  616. .kern: 0.35
  617. ]
  618. )
  619. findJobsButton.isBordered = false
  620. findJobsButton.bezelStyle = .rounded
  621. findJobsButton.wantsLayer = true
  622. findJobsButton.layer?.backgroundColor = NSColor.clear.cgColor
  623. findJobsButton.focusRingType = .none
  624. findJobsButton.target = self
  625. findJobsButton.action = #selector(didSubmitSearch)
  626. findJobsButton.setContentHuggingPriority(.required, for: .horizontal)
  627. findJobsButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  628. findJobsCTAHost.addSubview(findJobsCTAChrome)
  629. findJobsCTAHost.addSubview(findJobsButton)
  630. NSLayoutConstraint.activate([
  631. findJobsCTAChrome.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor),
  632. findJobsCTAChrome.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor),
  633. findJobsCTAChrome.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  634. findJobsCTAChrome.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor),
  635. findJobsButton.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor, constant: 14),
  636. findJobsButton.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor, constant: -14),
  637. findJobsButton.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  638. findJobsButton.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor)
  639. ])
  640. let keywordsStack = NSStackView(views: [jobSearchIcon, jobKeywordsField])
  641. keywordsStack.orientation = .horizontal
  642. keywordsStack.spacing = 10
  643. keywordsStack.alignment = .centerY
  644. keywordsStack.translatesAutoresizingMaskIntoConstraints = false
  645. keywordsStack.edgeInsets = NSEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
  646. keywordsStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  647. let row = NSStackView(views: [keywordsStack, findJobsCTAHost])
  648. row.orientation = .horizontal
  649. row.spacing = 0
  650. row.alignment = .centerY
  651. row.distribution = .fill
  652. row.translatesAutoresizingMaskIntoConstraints = false
  653. row.edgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 7)
  654. searchCard.addSubview(row)
  655. NSLayoutConstraint.activate([
  656. searchCard.leadingAnchor.constraint(equalTo: searchBarShadowHost.leadingAnchor),
  657. searchCard.trailingAnchor.constraint(equalTo: searchBarShadowHost.trailingAnchor),
  658. searchCard.topAnchor.constraint(equalTo: searchBarShadowHost.topAnchor),
  659. searchCard.bottomAnchor.constraint(equalTo: searchBarShadowHost.bottomAnchor),
  660. searchBarShadowHost.heightAnchor.constraint(equalToConstant: barHeight),
  661. row.leadingAnchor.constraint(equalTo: searchCard.leadingAnchor),
  662. row.trailingAnchor.constraint(equalTo: searchCard.trailingAnchor),
  663. row.topAnchor.constraint(equalTo: searchCard.topAnchor),
  664. row.bottomAnchor.constraint(equalTo: searchCard.bottomAnchor),
  665. jobSearchIcon.widthAnchor.constraint(equalToConstant: 18),
  666. jobSearchIcon.heightAnchor.constraint(equalToConstant: 18),
  667. findJobsCTAHost.heightAnchor.constraint(equalToConstant: ctaHeight),
  668. findJobsCTAHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 112)
  669. ])
  670. }
  671. private func updateFindJobsCTAShadowPath() {
  672. guard findJobsCTAHost.bounds.width > 0, findJobsCTAHost.bounds.height > 0 else { return }
  673. let r = findJobsCTAHost.bounds
  674. let radius = min(r.height / 2, r.width / 2)
  675. findJobsCTAHost.layer?.shadowPath = CGPath(
  676. roundedRect: r,
  677. cornerWidth: radius,
  678. cornerHeight: radius,
  679. transform: nil
  680. )
  681. }
  682. private func configureNonHomePlaceholder() {
  683. nonHomeHost.translatesAutoresizingMaskIntoConstraints = false
  684. nonHomeHost.wantsLayer = true
  685. nonHomeHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  686. nonHomeHost.isHidden = true
  687. nonHomeGenericContainer.translatesAutoresizingMaskIntoConstraints = false
  688. savedJobsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  689. settingsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  690. nonHomeHost.addSubview(nonHomeGenericContainer)
  691. nonHomeHost.addSubview(savedJobsPageContainer)
  692. nonHomeHost.addSubview(settingsPageContainer)
  693. NSLayoutConstraint.activate([
  694. nonHomeGenericContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  695. nonHomeGenericContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  696. nonHomeGenericContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  697. nonHomeGenericContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  698. savedJobsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  699. savedJobsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  700. savedJobsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  701. savedJobsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  702. settingsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  703. settingsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  704. settingsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  705. settingsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor)
  706. ])
  707. nonHomeTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  708. nonHomeTitleLabel.textColor = Theme.primaryText
  709. nonHomeTitleLabel.alignment = .center
  710. nonHomeTitleLabel.maximumNumberOfLines = 1
  711. nonHomeSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  712. nonHomeSubtitleLabel.textColor = Theme.secondaryText
  713. nonHomeSubtitleLabel.alignment = .center
  714. nonHomeSubtitleLabel.maximumNumberOfLines = 0
  715. nonHomeSubtitleLabel.stringValue = "This area is not available in the preview build. Use Home to search jobs."
  716. let genericStack = NSStackView(views: [nonHomeTitleLabel, nonHomeSubtitleLabel])
  717. genericStack.orientation = .vertical
  718. genericStack.spacing = 10
  719. genericStack.alignment = .centerX
  720. genericStack.translatesAutoresizingMaskIntoConstraints = false
  721. nonHomeGenericContainer.addSubview(genericStack)
  722. NSLayoutConstraint.activate([
  723. genericStack.centerXAnchor.constraint(equalTo: nonHomeGenericContainer.centerXAnchor),
  724. genericStack.centerYAnchor.constraint(equalTo: nonHomeGenericContainer.centerYAnchor),
  725. genericStack.leadingAnchor.constraint(greaterThanOrEqualTo: nonHomeGenericContainer.leadingAnchor, constant: 32),
  726. genericStack.trailingAnchor.constraint(lessThanOrEqualTo: nonHomeGenericContainer.trailingAnchor, constant: -32),
  727. nonHomeSubtitleLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 420)
  728. ])
  729. savedJobsPageTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  730. savedJobsPageTitleLabel.textColor = Theme.primaryText
  731. savedJobsPageTitleLabel.alignment = .left
  732. savedJobsPageTitleLabel.maximumNumberOfLines = 1
  733. savedJobsPageSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  734. savedJobsPageSubtitleLabel.textColor = Theme.secondaryText
  735. savedJobsPageSubtitleLabel.alignment = .left
  736. savedJobsPageSubtitleLabel.maximumNumberOfLines = 0
  737. savedJobsDocumentView.translatesAutoresizingMaskIntoConstraints = false
  738. savedJobsStack.orientation = .vertical
  739. savedJobsStack.spacing = 14
  740. savedJobsStack.alignment = .leading
  741. savedJobsStack.distribution = .fill
  742. savedJobsStack.translatesAutoresizingMaskIntoConstraints = false
  743. savedJobsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  744. savedJobsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  745. savedJobsDocumentView.addSubview(savedJobsStack)
  746. NSLayoutConstraint.activate([
  747. savedJobsStack.leadingAnchor.constraint(equalTo: savedJobsDocumentView.leadingAnchor),
  748. savedJobsStack.trailingAnchor.constraint(equalTo: savedJobsDocumentView.trailingAnchor),
  749. savedJobsStack.topAnchor.constraint(equalTo: savedJobsDocumentView.topAnchor),
  750. savedJobsStack.bottomAnchor.constraint(equalTo: savedJobsDocumentView.bottomAnchor)
  751. ])
  752. savedJobsScrollView.translatesAutoresizingMaskIntoConstraints = false
  753. savedJobsScrollView.hasVerticalScroller = true
  754. savedJobsScrollView.hasHorizontalScroller = false
  755. savedJobsScrollView.autohidesScrollers = true
  756. savedJobsScrollView.drawsBackground = false
  757. savedJobsScrollView.borderType = .noBorder
  758. savedJobsScrollView.documentView = savedJobsDocumentView
  759. savedJobsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  760. savedJobsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  761. let savedHeaderStack = NSStackView(views: [savedJobsPageTitleLabel, savedJobsPageSubtitleLabel])
  762. savedHeaderStack.orientation = .vertical
  763. savedHeaderStack.spacing = 6
  764. savedHeaderStack.alignment = .leading
  765. savedHeaderStack.translatesAutoresizingMaskIntoConstraints = false
  766. let savedOuterStack = NSStackView(views: [savedHeaderStack, savedJobsScrollView])
  767. savedOuterStack.orientation = .vertical
  768. savedOuterStack.spacing = 16
  769. // Leading alignment plus explicit column width keeps the title and subtitle on the same edge as the cards.
  770. savedOuterStack.alignment = .leading
  771. savedOuterStack.translatesAutoresizingMaskIntoConstraints = false
  772. savedJobsPageContainer.userInterfaceLayoutDirection = .leftToRight
  773. savedJobsPageContainer.addSubview(savedOuterStack)
  774. NSLayoutConstraint.activate([
  775. savedOuterStack.leadingAnchor.constraint(equalTo: savedJobsPageContainer.leadingAnchor, constant: 32),
  776. savedOuterStack.trailingAnchor.constraint(equalTo: savedJobsPageContainer.trailingAnchor, constant: -32),
  777. savedOuterStack.topAnchor.constraint(equalTo: savedJobsPageContainer.topAnchor, constant: 8),
  778. savedOuterStack.bottomAnchor.constraint(equalTo: savedJobsPageContainer.bottomAnchor),
  779. savedHeaderStack.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  780. savedJobsScrollView.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  781. savedJobsDocumentView.topAnchor.constraint(equalTo: savedJobsScrollView.contentView.topAnchor),
  782. savedJobsDocumentView.leadingAnchor.constraint(equalTo: savedJobsScrollView.contentView.leadingAnchor),
  783. savedJobsDocumentView.widthAnchor.constraint(equalTo: savedJobsScrollView.contentView.widthAnchor)
  784. ])
  785. configureSettingsPage()
  786. }
  787. private func configureSettingsPage() {
  788. settingsPageContainer.wantsLayer = true
  789. settingsPageContainer.layer?.backgroundColor = Theme.settingsPageBackground.cgColor
  790. settingsPageContainer.isHidden = true
  791. let contentStack = NSStackView()
  792. contentStack.orientation = .vertical
  793. contentStack.spacing = 26
  794. contentStack.alignment = .leading
  795. contentStack.translatesAutoresizingMaskIntoConstraints = false
  796. let settingsSection = makeSettingsSection(rows: [
  797. makeSettingsRow(title: "Share App", systemImage: "square.and.arrow.up", accessory: nil),
  798. makeSettingsRow(title: "Theme", systemImage: "circle.lefthalf.filled", accessory: makeThemeControl()),
  799. makeSettingsRow(title: "More Apps", systemImage: "square.grid.2x2", accessory: nil)
  800. ])
  801. let aboutTitle = NSTextField(labelWithString: "About")
  802. aboutTitle.font = .systemFont(ofSize: 12, weight: .semibold)
  803. aboutTitle.textColor = Theme.secondaryText
  804. aboutTitle.alignment = .left
  805. let aboutSection = makeSettingsSection(rows: [
  806. makeSettingsRow(title: "Support", systemImage: "questionmark.circle", accessory: nil),
  807. makeSettingsRow(title: "Terms of Use", systemImage: "doc.text", accessory: nil),
  808. makeSettingsRow(title: "Privacy Policy", systemImage: "shield", accessory: nil)
  809. ])
  810. let aboutStack = NSStackView(views: [aboutTitle, aboutSection])
  811. aboutStack.orientation = .vertical
  812. aboutStack.spacing = 14
  813. aboutStack.alignment = .leading
  814. aboutStack.translatesAutoresizingMaskIntoConstraints = false
  815. contentStack.addArrangedSubview(settingsSection)
  816. contentStack.addArrangedSubview(aboutStack)
  817. settingsPageContainer.addSubview(contentStack)
  818. NSLayoutConstraint.activate([
  819. contentStack.leadingAnchor.constraint(equalTo: settingsPageContainer.leadingAnchor, constant: 42),
  820. contentStack.trailingAnchor.constraint(lessThanOrEqualTo: settingsPageContainer.trailingAnchor, constant: -42),
  821. contentStack.topAnchor.constraint(equalTo: settingsPageContainer.topAnchor, constant: 48),
  822. settingsSection.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  823. aboutStack.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  824. aboutSection.widthAnchor.constraint(equalTo: aboutStack.widthAnchor),
  825. contentStack.widthAnchor.constraint(equalTo: settingsPageContainer.widthAnchor, constant: -84)
  826. ])
  827. }
  828. private func makeThemeControl() -> NSSegmentedControl {
  829. themeControl.target = self
  830. themeControl.action = #selector(didChangeThemeSelection(_:))
  831. themeControl.selectedSegment = 0
  832. themeControl.segmentStyle = .rounded
  833. themeControl.controlSize = .large
  834. themeControl.font = .systemFont(ofSize: 13, weight: .semibold)
  835. themeControl.translatesAutoresizingMaskIntoConstraints = false
  836. themeControl.widthAnchor.constraint(equalToConstant: 204).isActive = true
  837. themeControl.heightAnchor.constraint(equalToConstant: 30).isActive = true
  838. return themeControl
  839. }
  840. private func makeSettingsSection(rows: [NSView]) -> NSView {
  841. let section = NSStackView()
  842. section.orientation = .vertical
  843. section.spacing = 0
  844. section.alignment = .leading
  845. section.translatesAutoresizingMaskIntoConstraints = false
  846. section.wantsLayer = true
  847. section.layer?.backgroundColor = Theme.settingsGroupBackground.cgColor
  848. section.layer?.cornerRadius = 14
  849. section.layer?.borderWidth = 1
  850. section.layer?.borderColor = Theme.border.cgColor
  851. section.layer?.masksToBounds = true
  852. for (index, row) in rows.enumerated() {
  853. section.addArrangedSubview(row)
  854. row.widthAnchor.constraint(equalTo: section.widthAnchor).isActive = true
  855. if index < rows.count - 1 {
  856. let divider = NSView()
  857. divider.translatesAutoresizingMaskIntoConstraints = false
  858. divider.wantsLayer = true
  859. divider.layer?.backgroundColor = Theme.settingsDivider.cgColor
  860. section.addArrangedSubview(divider)
  861. NSLayoutConstraint.activate([
  862. divider.heightAnchor.constraint(equalToConstant: 1),
  863. divider.leadingAnchor.constraint(equalTo: section.leadingAnchor),
  864. divider.trailingAnchor.constraint(equalTo: section.trailingAnchor)
  865. ])
  866. }
  867. }
  868. return section
  869. }
  870. private func makeSettingsRow(title: String, systemImage: String, accessory: NSView?) -> NSView {
  871. let row = NSView()
  872. row.translatesAutoresizingMaskIntoConstraints = false
  873. row.wantsLayer = true
  874. let iconTile = NSView()
  875. iconTile.translatesAutoresizingMaskIntoConstraints = false
  876. iconTile.wantsLayer = true
  877. iconTile.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  878. iconTile.layer?.cornerRadius = 9
  879. let icon = NSImageView()
  880. icon.translatesAutoresizingMaskIntoConstraints = false
  881. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  882. icon.image = NSImage(systemSymbolName: systemImage, accessibilityDescription: title)
  883. icon.contentTintColor = Theme.brandBlue
  884. let titleLabel = NSTextField(labelWithString: title)
  885. titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
  886. titleLabel.textColor = Theme.secondaryText
  887. titleLabel.alignment = .left
  888. let rowStack = NSStackView()
  889. rowStack.orientation = .horizontal
  890. rowStack.spacing = 16
  891. rowStack.alignment = .centerY
  892. rowStack.translatesAutoresizingMaskIntoConstraints = false
  893. let spacer = NSView()
  894. spacer.translatesAutoresizingMaskIntoConstraints = false
  895. spacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  896. iconTile.addSubview(icon)
  897. rowStack.addArrangedSubview(iconTile)
  898. rowStack.addArrangedSubview(titleLabel)
  899. rowStack.addArrangedSubview(spacer)
  900. if let accessory {
  901. rowStack.addArrangedSubview(accessory)
  902. }
  903. row.addSubview(rowStack)
  904. NSLayoutConstraint.activate([
  905. row.heightAnchor.constraint(equalToConstant: 68),
  906. iconTile.widthAnchor.constraint(equalToConstant: 38),
  907. iconTile.heightAnchor.constraint(equalToConstant: 38),
  908. icon.centerXAnchor.constraint(equalTo: iconTile.centerXAnchor),
  909. icon.centerYAnchor.constraint(equalTo: iconTile.centerYAnchor),
  910. icon.widthAnchor.constraint(equalToConstant: 20),
  911. icon.heightAnchor.constraint(equalToConstant: 20),
  912. rowStack.leadingAnchor.constraint(equalTo: row.leadingAnchor, constant: 16),
  913. rowStack.trailingAnchor.constraint(equalTo: row.trailingAnchor, constant: -16),
  914. rowStack.topAnchor.constraint(equalTo: row.topAnchor),
  915. rowStack.bottomAnchor.constraint(equalTo: row.bottomAnchor)
  916. ])
  917. return row
  918. }
  919. private func reloadSavedJobsListings() {
  920. savedJobsStack.arrangedSubviews.forEach {
  921. savedJobsStack.removeArrangedSubview($0)
  922. $0.removeFromSuperview()
  923. }
  924. if savedJobOrder.isEmpty {
  925. savedJobsPageSubtitleLabel.stringValue = "Save jobs from Home to see them here."
  926. let empty = NSTextField(wrappingLabelWithString: "No saved jobs yet. Search on Home, then tap Save on a listing.")
  927. empty.font = .systemFont(ofSize: 14, weight: .regular)
  928. empty.textColor = Theme.secondaryText
  929. empty.alignment = .left
  930. empty.maximumNumberOfLines = 0
  931. empty.translatesAutoresizingMaskIntoConstraints = false
  932. savedJobsStack.addArrangedSubview(empty)
  933. empty.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  934. return
  935. }
  936. savedJobsPageSubtitleLabel.stringValue = "\(savedJobOrder.count) saved \(savedJobOrder.count == 1 ? "position" : "positions")"
  937. for job in savedJobOrder {
  938. let card = makeJobListingCard(job, context: .savedJobsPage)
  939. savedJobsStack.addArrangedSubview(card)
  940. card.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  941. }
  942. }
  943. private func isSavedJobsSidebarIndex(_ index: Int) -> Bool {
  944. guard index >= 0, index < currentSidebarItems.count else { return false }
  945. return currentSidebarItems[index].title == "Saved Jobs"
  946. }
  947. private func isHomeSidebarIndex(_ index: Int) -> Bool {
  948. guard index >= 0, index < currentSidebarItems.count else { return false }
  949. return currentSidebarItems[index].title == "Home"
  950. }
  951. private func isSettingsSidebarIndex(_ index: Int) -> Bool {
  952. guard index >= 0, index < currentSidebarItems.count else { return false }
  953. return currentSidebarItems[index].title == "Settings"
  954. }
  955. private func updateMainContentVisibility() {
  956. let home = isHomeSidebarIndex(selectedSidebarIndex)
  957. let savedJobs = isSavedJobsSidebarIndex(selectedSidebarIndex)
  958. let settings = isSettingsSidebarIndex(selectedSidebarIndex)
  959. mainOverlay.isHidden = !home
  960. nonHomeHost.isHidden = home
  961. nonHomeGenericContainer.isHidden = savedJobs || settings
  962. savedJobsPageContainer.isHidden = !savedJobs
  963. settingsPageContainer.isHidden = !settings
  964. if !home, selectedSidebarIndex < currentSidebarItems.count {
  965. if savedJobs {
  966. reloadSavedJobsListings()
  967. } else if settings {
  968. window?.makeFirstResponder(nil)
  969. } else {
  970. nonHomeTitleLabel.stringValue = currentSidebarItems[selectedSidebarIndex].title
  971. }
  972. }
  973. }
  974. /// Removes result cards or the “no matches” message so a new search starts from a blank listing area.
  975. private func clearJobSearchResultsUI() {
  976. configureJobListings([], noResultsForQuery: nil)
  977. if let doc = jobListingsScrollView.documentView {
  978. doc.scroll(NSPoint(x: 0, y: 0))
  979. }
  980. }
  981. /// Restores the main job-search experience: cleared query and no listings until the user searches again.
  982. private func applyHomeState() {
  983. jobKeywordsField.stringValue = ""
  984. clearJobSearchResultsUI()
  985. window?.makeFirstResponder(nil)
  986. }
  987. private func updateSearchBarShadowPath() {
  988. guard searchBarShadowHost.bounds.width > 0, searchBarShadowHost.bounds.height > 0 else { return }
  989. let r = searchBarShadowHost.bounds
  990. let radius = min(r.height / 2, 27)
  991. searchBarShadowHost.layer?.shadowPath = CGPath(
  992. roundedRect: r,
  993. cornerWidth: radius,
  994. cornerHeight: radius,
  995. transform: nil
  996. )
  997. }
  998. @objc private func didSubmitSearch() {
  999. let query = jobKeywordsField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  1000. let results = jobsMatchingSearch(query: query, in: catalogJobListings)
  1001. let noResultsMessage: String? = (!results.isEmpty || query.isEmpty) ? nil : query
  1002. configureJobListings(results, noResultsForQuery: noResultsMessage)
  1003. window?.makeFirstResponder(nil)
  1004. }
  1005. /// Each whitespace-separated token must appear in the title or description (case-insensitive). Empty query returns the full catalog.
  1006. private func jobsMatchingSearch(query: String, in jobs: [JobListing]) -> [JobListing] {
  1007. let tokens = query
  1008. .lowercased()
  1009. .split(whereSeparator: { $0.isWhitespace })
  1010. .map(String.init)
  1011. .filter { !$0.isEmpty }
  1012. guard !tokens.isEmpty else { return jobs }
  1013. return jobs.filter { job in
  1014. let haystack = "\(job.title) \(job.description)".lowercased()
  1015. return tokens.allSatisfy { haystack.contains($0) }
  1016. }
  1017. }
  1018. func controlTextDidBeginEditing(_ obj: Notification) {
  1019. applySearchFieldInsertionPoint(obj.object)
  1020. if (obj.object as? NSTextField) === jobKeywordsField {
  1021. clearJobSearchResultsUI()
  1022. }
  1023. }
  1024. func controlTextDidChange(_ obj: Notification) {
  1025. applySearchFieldInsertionPoint(obj.object)
  1026. }
  1027. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  1028. guard control === jobKeywordsField, commandSelector == #selector(NSResponder.insertNewline(_:)) else {
  1029. return false
  1030. }
  1031. didSubmitSearch()
  1032. return true
  1033. }
  1034. private func applySearchFieldInsertionPoint(_ object: Any?) {
  1035. guard let field = object as? NSTextField,
  1036. field === jobKeywordsField,
  1037. let textView = field.window?.fieldEditor(true, for: field) as? NSTextView else { return }
  1038. textView.insertionPointColor = Theme.primaryText
  1039. }
  1040. private func configureSidebar() {
  1041. let items = currentSidebarItems
  1042. sidebar.arrangedSubviews.forEach {
  1043. sidebar.removeArrangedSubview($0)
  1044. $0.removeFromSuperview()
  1045. }
  1046. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  1047. brand.font = .systemFont(ofSize: 18, weight: .bold)
  1048. brand.textColor = Theme.brandBlue
  1049. brand.alignment = .left
  1050. brand.maximumNumberOfLines = 2
  1051. // Tight multiline height in the sidebar stack (zero width makes intrinsic height unreliable).
  1052. brand.preferredMaxLayoutWidth = 194
  1053. sidebar.addArrangedSubview(brand)
  1054. sidebar.setCustomSpacing(10, after: brand)
  1055. items.enumerated().forEach { index, item in
  1056. let isSelected = index == selectedSidebarIndex
  1057. let rowHost = SidebarNavRowView { [weak self] in
  1058. self?.selectSidebarItem(at: index)
  1059. }
  1060. rowHost.translatesAutoresizingMaskIntoConstraints = false
  1061. rowHost.wantsLayer = true
  1062. rowHost.layer?.cornerRadius = 8
  1063. rowHost.restingBackgroundColor = isSelected ? Theme.selectionFill : nil
  1064. rowHost.hoverBackgroundColor = isSelected ? Theme.selectionFillHover : Theme.sidebarRowHoverFill
  1065. rowHost.setAccessibilityLabel(item.title)
  1066. rowHost.setAccessibilityRole(.button)
  1067. rowHost.setAccessibilitySelected(isSelected)
  1068. let row = NSStackView()
  1069. row.orientation = .horizontal
  1070. row.spacing = 8
  1071. row.alignment = .centerY
  1072. row.translatesAutoresizingMaskIntoConstraints = false
  1073. let icon = NSImageView()
  1074. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  1075. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  1076. icon.contentTintColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  1077. let text = NSTextField(labelWithString: item.title)
  1078. text.font = .systemFont(ofSize: 14, weight: .medium)
  1079. text.textColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  1080. text.refusesFirstResponder = true
  1081. row.addArrangedSubview(icon)
  1082. row.addArrangedSubview(text)
  1083. if let badge = item.badge {
  1084. let badgeField = NSTextField(labelWithString: badge)
  1085. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  1086. badgeField.textColor = Theme.primaryText
  1087. badgeField.wantsLayer = true
  1088. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  1089. badgeField.layer?.cornerRadius = 8
  1090. badgeField.alignment = .center
  1091. badgeField.maximumNumberOfLines = 1
  1092. badgeField.lineBreakMode = .byClipping
  1093. badgeField.refusesFirstResponder = true
  1094. badgeField.translatesAutoresizingMaskIntoConstraints = false
  1095. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  1096. row.addArrangedSubview(NSView())
  1097. row.addArrangedSubview(badgeField)
  1098. }
  1099. rowHost.addSubview(row)
  1100. NSLayoutConstraint.activate([
  1101. row.leadingAnchor.constraint(equalTo: rowHost.leadingAnchor, constant: 10),
  1102. row.trailingAnchor.constraint(equalTo: rowHost.trailingAnchor, constant: -10),
  1103. row.topAnchor.constraint(equalTo: rowHost.topAnchor, constant: 8),
  1104. row.bottomAnchor.constraint(equalTo: rowHost.bottomAnchor, constant: -8)
  1105. ])
  1106. rowHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1107. sidebar.addArrangedSubview(rowHost)
  1108. let sidebarHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  1109. rowHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -sidebarHorizontalInset).isActive = true
  1110. }
  1111. let sidebarBottomSpacer = NSView()
  1112. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  1113. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  1114. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  1115. sidebar.addArrangedSubview(sidebarBottomSpacer)
  1116. let upgradeCard = NSView()
  1117. upgradeCard.translatesAutoresizingMaskIntoConstraints = false
  1118. upgradeCard.wantsLayer = true
  1119. upgradeCard.layer?.backgroundColor = Theme.proCardFill.cgColor
  1120. upgradeCard.layer?.cornerRadius = 14
  1121. upgradeCard.layer?.borderWidth = 1
  1122. upgradeCard.layer?.borderColor = Theme.proCardBorder.cgColor
  1123. upgradeCard.layer?.masksToBounds = true
  1124. let accentBar = NSView()
  1125. accentBar.translatesAutoresizingMaskIntoConstraints = false
  1126. accentBar.wantsLayer = true
  1127. accentBar.layer?.backgroundColor = Theme.proAccent.cgColor
  1128. let inner = NSStackView()
  1129. inner.translatesAutoresizingMaskIntoConstraints = false
  1130. inner.orientation = .vertical
  1131. inner.spacing = 10
  1132. inner.alignment = .centerX
  1133. let proIcon = NSImageView()
  1134. proIcon.translatesAutoresizingMaskIntoConstraints = false
  1135. proIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  1136. proIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: nil)
  1137. proIcon.contentTintColor = Theme.proAccent
  1138. let proEyebrow = NSTextField(labelWithString: "Premium")
  1139. proEyebrow.font = .systemFont(ofSize: 11, weight: .heavy)
  1140. proEyebrow.textColor = Theme.proAccent
  1141. proEyebrow.alignment = .center
  1142. let eyebrowRow = NSStackView(views: [proIcon, proEyebrow])
  1143. eyebrowRow.orientation = .horizontal
  1144. eyebrowRow.spacing = 6
  1145. eyebrowRow.alignment = .centerY
  1146. let headline = NSTextField(labelWithString: "Upgrade to Pro")
  1147. headline.font = .systemFont(ofSize: 16, weight: .bold)
  1148. headline.textColor = Theme.primaryText
  1149. headline.alignment = .center
  1150. let upgradeDescription = NSTextField(wrappingLabelWithString: "Unlimited AI matches, smart alerts, and interview prep—all in one place.")
  1151. upgradeDescription.font = .systemFont(ofSize: 12, weight: .regular)
  1152. upgradeDescription.textColor = Theme.secondaryText
  1153. upgradeDescription.alignment = .center
  1154. // Sidebar content width is the fixed sidebar width minus horizontal edge insets; card must stay within that band.
  1155. let cardWidth: CGFloat = 186
  1156. let innerContentWidth = cardWidth - 28
  1157. upgradeDescription.preferredMaxLayoutWidth = innerContentWidth
  1158. let upgradeButton = HoverableButton(title: "Upgrade to Pro", target: self, action: #selector(didTapUpgradeToPro))
  1159. upgradeButton.isBordered = false
  1160. upgradeButton.bezelStyle = .rounded
  1161. upgradeButton.font = .systemFont(ofSize: 13, weight: .bold)
  1162. upgradeButton.contentTintColor = Theme.proCTAText
  1163. upgradeButton.alignment = .center
  1164. upgradeButton.wantsLayer = true
  1165. upgradeButton.layer?.backgroundColor = Theme.proCTABackground.cgColor
  1166. upgradeButton.layer?.cornerRadius = 20
  1167. upgradeButton.translatesAutoresizingMaskIntoConstraints = false
  1168. upgradeButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  1169. upgradeButton.pointerCursor = true
  1170. upgradeButton.hoverHandler = { [weak upgradeButton] hovering in
  1171. upgradeButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.proCTABackground).cgColor
  1172. }
  1173. inner.addArrangedSubview(eyebrowRow)
  1174. inner.addArrangedSubview(headline)
  1175. inner.addArrangedSubview(upgradeDescription)
  1176. inner.addArrangedSubview(upgradeButton)
  1177. upgradeCard.addSubview(accentBar)
  1178. upgradeCard.addSubview(inner)
  1179. NSLayoutConstraint.activate([
  1180. upgradeCard.widthAnchor.constraint(equalToConstant: cardWidth),
  1181. accentBar.topAnchor.constraint(equalTo: upgradeCard.topAnchor),
  1182. accentBar.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor),
  1183. accentBar.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor),
  1184. accentBar.heightAnchor.constraint(equalToConstant: 2),
  1185. inner.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor, constant: 14),
  1186. inner.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor, constant: -14),
  1187. inner.topAnchor.constraint(equalTo: accentBar.bottomAnchor, constant: 12),
  1188. inner.bottomAnchor.constraint(equalTo: upgradeCard.bottomAnchor, constant: -14),
  1189. upgradeButton.widthAnchor.constraint(equalTo: inner.widthAnchor)
  1190. ])
  1191. sidebar.addArrangedSubview(upgradeCard)
  1192. }
  1193. @objc private func didTapUpgradeToPro() {
  1194. guard let url = URL(string: "https://www.indeed.com") else { return }
  1195. NSWorkspace.shared.open(url)
  1196. }
  1197. @objc private func didChangeThemeSelection(_ sender: NSSegmentedControl) {
  1198. switch sender.selectedSegment {
  1199. case 1:
  1200. NSApp.appearance = NSAppearance(named: .aqua)
  1201. case 2:
  1202. NSApp.appearance = NSAppearance(named: .darkAqua)
  1203. default:
  1204. NSApp.appearance = nil
  1205. }
  1206. }
  1207. private func selectSidebarItem(at index: Int) {
  1208. guard index >= 0, index < currentSidebarItems.count else { return }
  1209. let selectingHome = isHomeSidebarIndex(index)
  1210. if index == selectedSidebarIndex {
  1211. if selectingHome {
  1212. applyHomeState()
  1213. }
  1214. return
  1215. }
  1216. selectedSidebarIndex = index
  1217. configureSidebar()
  1218. updateMainContentVisibility()
  1219. if selectingHome {
  1220. applyHomeState()
  1221. }
  1222. }
  1223. }
  1224. /// `NSButton` that carries a `JobListing` for card actions (`representedObject` is unavailable on `NSButton` in this target).
  1225. private final class JobPayloadButton: HoverableButton {
  1226. var jobPayload: JobListing?
  1227. var cardContext: JobListingCardContext = .homeSearchResults
  1228. }
  1229. /// `NSButton` with a tracking area that reports hover transitions and (optionally) swaps in a pointing-hand cursor while hovered.
  1230. private class HoverableButton: NSButton {
  1231. var hoverHandler: ((Bool) -> Void)?
  1232. var pointerCursor: Bool = false
  1233. private(set) var isHovering: Bool = false
  1234. private var trackingArea: NSTrackingArea?
  1235. private var didPushCursor: Bool = false
  1236. override func updateTrackingAreas() {
  1237. super.updateTrackingAreas()
  1238. if let area = trackingArea { removeTrackingArea(area) }
  1239. let area = NSTrackingArea(
  1240. rect: bounds,
  1241. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1242. owner: self,
  1243. userInfo: nil
  1244. )
  1245. addTrackingArea(area)
  1246. trackingArea = area
  1247. }
  1248. override func mouseEntered(with event: NSEvent) {
  1249. super.mouseEntered(with: event)
  1250. isHovering = true
  1251. hoverHandler?(true)
  1252. if pointerCursor, !didPushCursor {
  1253. NSCursor.pointingHand.push()
  1254. didPushCursor = true
  1255. }
  1256. }
  1257. override func mouseExited(with event: NSEvent) {
  1258. super.mouseExited(with: event)
  1259. isHovering = false
  1260. hoverHandler?(false)
  1261. if didPushCursor {
  1262. NSCursor.pop()
  1263. didPushCursor = false
  1264. }
  1265. }
  1266. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1267. super.viewWillMove(toWindow: newWindow)
  1268. // Guard against an unbalanced cursor stack if the button is removed mid-hover (e.g. job card replaced after a search).
  1269. if newWindow == nil, didPushCursor {
  1270. NSCursor.pop()
  1271. didPushCursor = false
  1272. isHovering = false
  1273. }
  1274. }
  1275. }
  1276. /// `NSView` companion to `HoverableButton`: emits hover transitions and can manage a pointing-hand cursor. Used to track hover over composite controls like the gradient "Find jobs" pill.
  1277. private class HoverableView: NSView {
  1278. var hoverHandler: ((Bool) -> Void)?
  1279. var pointerCursor: Bool = false
  1280. private(set) var isHovering: Bool = false
  1281. private var trackingArea: NSTrackingArea?
  1282. private var didPushCursor: Bool = false
  1283. override func updateTrackingAreas() {
  1284. super.updateTrackingAreas()
  1285. if let area = trackingArea { removeTrackingArea(area) }
  1286. let area = NSTrackingArea(
  1287. rect: bounds,
  1288. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1289. owner: self,
  1290. userInfo: nil
  1291. )
  1292. addTrackingArea(area)
  1293. trackingArea = area
  1294. }
  1295. override func mouseEntered(with event: NSEvent) {
  1296. super.mouseEntered(with: event)
  1297. isHovering = true
  1298. hoverHandler?(true)
  1299. if pointerCursor, !didPushCursor {
  1300. NSCursor.pointingHand.push()
  1301. didPushCursor = true
  1302. }
  1303. }
  1304. override func mouseExited(with event: NSEvent) {
  1305. super.mouseExited(with: event)
  1306. isHovering = false
  1307. hoverHandler?(false)
  1308. if didPushCursor {
  1309. NSCursor.pop()
  1310. didPushCursor = false
  1311. }
  1312. }
  1313. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1314. super.viewWillMove(toWindow: newWindow)
  1315. if newWindow == nil, didPushCursor {
  1316. NSCursor.pop()
  1317. didPushCursor = false
  1318. isHovering = false
  1319. }
  1320. }
  1321. }
  1322. /// 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).
  1323. private final class JobListingsDocumentView: NSView {
  1324. override var isFlipped: Bool { true }
  1325. }
  1326. /// Captures clicks for the full sidebar pill so icon, label, and padding behave as one tab. Manages its own hover background so non-selected rows highlight subtly on hover without disturbing the selected-row fill.
  1327. private final class SidebarNavRowView: NSView {
  1328. private let onSelect: () -> Void
  1329. var restingBackgroundColor: NSColor? {
  1330. didSet { applyBackground() }
  1331. }
  1332. var hoverBackgroundColor: NSColor?
  1333. private var isHovering: Bool = false
  1334. private var didPushCursor: Bool = false
  1335. init(onSelect: @escaping () -> Void) {
  1336. self.onSelect = onSelect
  1337. super.init(frame: .zero)
  1338. }
  1339. @available(*, unavailable)
  1340. required init?(coder: NSCoder) {
  1341. fatalError("init(coder:) has not been implemented")
  1342. }
  1343. override func hitTest(_ point: NSPoint) -> NSView? {
  1344. guard let superview else { return super.hitTest(point) }
  1345. let local = convert(point, from: superview)
  1346. return bounds.contains(local) ? self : nil
  1347. }
  1348. override func mouseDown(with event: NSEvent) {
  1349. onSelect()
  1350. }
  1351. override func updateTrackingAreas() {
  1352. super.updateTrackingAreas()
  1353. trackingAreas.forEach { removeTrackingArea($0) }
  1354. addTrackingArea(NSTrackingArea(
  1355. rect: bounds,
  1356. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1357. owner: self,
  1358. userInfo: nil
  1359. ))
  1360. }
  1361. override func mouseEntered(with event: NSEvent) {
  1362. super.mouseEntered(with: event)
  1363. isHovering = true
  1364. applyBackground()
  1365. if !didPushCursor {
  1366. NSCursor.pointingHand.push()
  1367. didPushCursor = true
  1368. }
  1369. }
  1370. override func mouseExited(with event: NSEvent) {
  1371. super.mouseExited(with: event)
  1372. isHovering = false
  1373. applyBackground()
  1374. if didPushCursor {
  1375. NSCursor.pop()
  1376. didPushCursor = false
  1377. }
  1378. }
  1379. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1380. super.viewWillMove(toWindow: newWindow)
  1381. if newWindow == nil, didPushCursor {
  1382. NSCursor.pop()
  1383. didPushCursor = false
  1384. isHovering = false
  1385. }
  1386. }
  1387. private func applyBackground() {
  1388. let color = isHovering ? (hoverBackgroundColor ?? restingBackgroundColor) : restingBackgroundColor
  1389. layer?.backgroundColor = color?.cgColor
  1390. }
  1391. }