Brak opisu

DashboardView.swift 75KB

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