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

DashboardView.swift 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //
  2. // DashboardView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. final class DashboardView: NSView {
  7. private enum Theme {
  8. static let pageBackground = NSColor(calibratedWhite: 0.02, alpha: 1)
  9. static let chromeBackground = NSColor(calibratedWhite: 0.04, alpha: 1)
  10. static let sidebarBackground = NSColor(calibratedWhite: 0.07, alpha: 1)
  11. static let mainHostBackground = NSColor(calibratedWhite: 0.06, alpha: 1)
  12. static let selectionFill = NSColor(calibratedWhite: 1, alpha: 0.1)
  13. static let cardBackground = NSColor(calibratedWhite: 0.11, alpha: 1)
  14. static let toggleBackground = NSColor(calibratedWhite: 0.16, alpha: 1)
  15. static let primaryText = NSColor(calibratedWhite: 0.96, alpha: 1)
  16. static let secondaryText = NSColor(calibratedWhite: 0.62, alpha: 1)
  17. static let tertiaryText = NSColor(calibratedWhite: 0.48, alpha: 1)
  18. static let proCardFill = NSColor(calibratedRed: 0.1, green: 0.09, blue: 0.12, alpha: 1)
  19. static let proCardBorder = NSColor(calibratedWhite: 1, alpha: 0.09)
  20. static let proAccent = NSColor(calibratedRed: 0.98, green: 0.82, blue: 0.38, alpha: 1)
  21. static let proCTABackground = NSColor(calibratedWhite: 0.97, alpha: 1)
  22. static let proCTAText = NSColor(calibratedWhite: 0.05, alpha: 1)
  23. }
  24. private let contentStack = NSStackView()
  25. private let documentContainer = NSView()
  26. private let chromeContainer = NSView()
  27. private let sidebar = NSStackView()
  28. private let mainHost = NSView()
  29. private let mainOverlay = NSStackView()
  30. private let greetingLabel = NSTextField(labelWithString: "")
  31. private let subtitleLabel = NSTextField(labelWithString: "")
  32. private let insightsCard = NSView()
  33. private let insightsTitleLabel = NSTextField(labelWithString: "")
  34. private let insightsBodyLabel = NSTextField(labelWithString: "")
  35. private let togglesLabel = NSTextField(labelWithString: "Show:")
  36. private let savedToggleButton = NSButton(title: "Saved", target: nil, action: nil)
  37. private let interviewsToggleButton = NSButton(title: "Interviews", target: nil, action: nil)
  38. private let sparkleView = NSImageView()
  39. private let scrollView = NSScrollView()
  40. override init(frame frameRect: NSRect) {
  41. super.init(frame: frameRect)
  42. setupLayout()
  43. }
  44. required init?(coder: NSCoder) {
  45. super.init(coder: coder)
  46. setupLayout()
  47. }
  48. override func layout() {
  49. super.layout()
  50. updateDocumentLayout()
  51. }
  52. func render(_ data: DashboardData) {
  53. greetingLabel.stringValue = "Welcome"
  54. subtitleLabel.stringValue = data.subtitle
  55. insightsTitleLabel.stringValue = data.profileInsightsTitle
  56. insightsBodyLabel.stringValue = data.profileInsightsBody
  57. configureSidebar(data.sidebarItems)
  58. updateDocumentLayout()
  59. }
  60. private func setupLayout() {
  61. wantsLayer = true
  62. layer?.backgroundColor = Theme.pageBackground.cgColor
  63. scrollView.translatesAutoresizingMaskIntoConstraints = false
  64. scrollView.hasVerticalScroller = true
  65. scrollView.drawsBackground = false
  66. addSubview(scrollView)
  67. contentStack.orientation = .horizontal
  68. contentStack.spacing = 20
  69. contentStack.translatesAutoresizingMaskIntoConstraints = false
  70. contentStack.alignment = .height
  71. contentStack.edgeInsets = NSEdgeInsets(top: 24, left: 24, bottom: 24, right: 24)
  72. documentContainer.translatesAutoresizingMaskIntoConstraints = true
  73. documentContainer.autoresizingMask = [.width]
  74. documentContainer.frame = NSRect(x: 0, y: 0, width: 1040, height: 900)
  75. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  76. chromeContainer.wantsLayer = true
  77. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  78. chromeContainer.layer?.cornerRadius = 0
  79. documentContainer.addSubview(chromeContainer)
  80. chromeContainer.addSubview(contentStack)
  81. scrollView.documentView = documentContainer
  82. sidebar.orientation = .vertical
  83. sidebar.spacing = 10
  84. sidebar.distribution = .fill
  85. sidebar.alignment = .leading
  86. sidebar.wantsLayer = true
  87. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  88. sidebar.layer?.cornerRadius = 16
  89. sidebar.edgeInsets = NSEdgeInsets(top: 18, left: 14, bottom: 18, right: 14)
  90. sidebar.translatesAutoresizingMaskIntoConstraints = false
  91. mainHost.translatesAutoresizingMaskIntoConstraints = false
  92. mainHost.wantsLayer = true
  93. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  94. mainHost.layer?.cornerRadius = 16
  95. mainHost.layer?.masksToBounds = true
  96. mainHost.addSubview(mainOverlay)
  97. mainOverlay.orientation = .vertical
  98. mainOverlay.spacing = 0
  99. mainOverlay.alignment = .centerX
  100. mainOverlay.distribution = .fill
  101. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  102. mainOverlay.setContentHuggingPriority(.defaultLow, for: .vertical)
  103. greetingLabel.font = .systemFont(ofSize: 32, weight: .bold)
  104. greetingLabel.textColor = Theme.primaryText
  105. greetingLabel.alignment = .center
  106. greetingLabel.maximumNumberOfLines = 1
  107. subtitleLabel.font = .systemFont(ofSize: 15, weight: .regular)
  108. subtitleLabel.textColor = Theme.secondaryText
  109. subtitleLabel.alignment = .center
  110. subtitleLabel.maximumNumberOfLines = 2
  111. let topInset = NSView()
  112. topInset.translatesAutoresizingMaskIntoConstraints = false
  113. topInset.heightAnchor.constraint(equalToConstant: 32).isActive = true
  114. configureInsightsCard()
  115. let titleBlock = NSStackView(views: [greetingLabel, subtitleLabel])
  116. titleBlock.orientation = .vertical
  117. titleBlock.spacing = 10
  118. titleBlock.alignment = .centerX
  119. let midSpacer = NSView()
  120. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  121. midSpacer.heightAnchor.constraint(equalToConstant: 20).isActive = true
  122. let overlayBottomSpacer = NSView()
  123. overlayBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  124. overlayBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  125. overlayBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  126. mainOverlay.addArrangedSubview(topInset)
  127. mainOverlay.addArrangedSubview(titleBlock)
  128. mainOverlay.addArrangedSubview(midSpacer)
  129. mainOverlay.addArrangedSubview(insightsCard)
  130. mainOverlay.addArrangedSubview(overlayBottomSpacer)
  131. sparkleView.translatesAutoresizingMaskIntoConstraints = false
  132. sparkleView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 22, weight: .regular)
  133. sparkleView.image = NSImage(systemSymbolName: "sparkle", accessibilityDescription: "Accent")
  134. sparkleView.contentTintColor = NSColor(calibratedWhite: 0.9, alpha: 0.55)
  135. mainHost.addSubview(sparkleView)
  136. contentStack.addArrangedSubview(sidebar)
  137. contentStack.addArrangedSubview(mainHost)
  138. NSLayoutConstraint.activate([
  139. scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
  140. scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
  141. scrollView.topAnchor.constraint(equalTo: topAnchor),
  142. scrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
  143. chromeContainer.leadingAnchor.constraint(equalTo: documentContainer.leadingAnchor),
  144. chromeContainer.trailingAnchor.constraint(equalTo: documentContainer.trailingAnchor),
  145. chromeContainer.topAnchor.constraint(equalTo: documentContainer.topAnchor),
  146. chromeContainer.bottomAnchor.constraint(equalTo: documentContainer.bottomAnchor),
  147. contentStack.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor),
  148. contentStack.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor),
  149. contentStack.topAnchor.constraint(equalTo: chromeContainer.topAnchor),
  150. contentStack.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor),
  151. sidebar.widthAnchor.constraint(equalToConstant: 225),
  152. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  153. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  154. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  155. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  156. mainOverlay.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  157. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 24),
  158. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -24),
  159. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  160. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor),
  161. sparkleView.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor, constant: -28),
  162. sparkleView.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -28)
  163. ])
  164. }
  165. private func configureInsightsCard() {
  166. insightsCard.wantsLayer = true
  167. insightsCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  168. insightsCard.layer?.cornerRadius = 18
  169. insightsCard.translatesAutoresizingMaskIntoConstraints = false
  170. insightsTitleLabel.font = .systemFont(ofSize: 20, weight: .semibold)
  171. insightsTitleLabel.textColor = Theme.primaryText
  172. insightsTitleLabel.alignment = .center
  173. insightsTitleLabel.maximumNumberOfLines = 1
  174. insightsBodyLabel.font = .systemFont(ofSize: 13, weight: .regular)
  175. insightsBodyLabel.textColor = Theme.secondaryText
  176. insightsBodyLabel.alignment = .center
  177. insightsBodyLabel.maximumNumberOfLines = 4
  178. insightsBodyLabel.lineBreakMode = .byWordWrapping
  179. insightsBodyLabel.preferredMaxLayoutWidth = 400
  180. togglesLabel.font = .systemFont(ofSize: 12, weight: .medium)
  181. togglesLabel.textColor = Theme.tertiaryText
  182. togglesLabel.alignment = .center
  183. togglesLabel.maximumNumberOfLines = 1
  184. styleToggle(savedToggleButton)
  185. styleToggle(interviewsToggleButton)
  186. let toggleRow = NSStackView(views: [savedToggleButton, interviewsToggleButton])
  187. toggleRow.orientation = .horizontal
  188. toggleRow.spacing = 10
  189. toggleRow.alignment = .centerY
  190. let inner = NSStackView(views: [
  191. insightsTitleLabel,
  192. insightsBodyLabel,
  193. togglesLabel,
  194. toggleRow
  195. ])
  196. inner.orientation = .vertical
  197. inner.spacing = 10
  198. inner.alignment = .centerX
  199. inner.distribution = .fill
  200. inner.translatesAutoresizingMaskIntoConstraints = false
  201. insightsCard.setContentHuggingPriority(.defaultHigh, for: .vertical)
  202. insightsCard.addSubview(inner)
  203. NSLayoutConstraint.activate([
  204. inner.leadingAnchor.constraint(equalTo: insightsCard.leadingAnchor, constant: 32),
  205. inner.trailingAnchor.constraint(equalTo: insightsCard.trailingAnchor, constant: -32),
  206. inner.topAnchor.constraint(equalTo: insightsCard.topAnchor, constant: 22),
  207. inner.bottomAnchor.constraint(equalTo: insightsCard.bottomAnchor, constant: -22),
  208. insightsCard.widthAnchor.constraint(equalToConstant: 440)
  209. ])
  210. }
  211. private func styleToggle(_ button: NSButton) {
  212. button.bezelStyle = .rounded
  213. button.font = .systemFont(ofSize: 12, weight: .medium)
  214. button.contentTintColor = Theme.secondaryText
  215. button.wantsLayer = true
  216. button.layer?.backgroundColor = Theme.toggleBackground.cgColor
  217. button.layer?.cornerRadius = 8
  218. button.translatesAutoresizingMaskIntoConstraints = false
  219. button.widthAnchor.constraint(equalToConstant: 108).isActive = true
  220. button.heightAnchor.constraint(equalToConstant: 30).isActive = true
  221. }
  222. private func configureSidebar(_ items: [SidebarItem]) {
  223. sidebar.arrangedSubviews.forEach {
  224. sidebar.removeArrangedSubview($0)
  225. $0.removeFromSuperview()
  226. }
  227. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  228. brand.font = .systemFont(ofSize: 18, weight: .bold)
  229. brand.textColor = Theme.primaryText
  230. brand.alignment = .left
  231. sidebar.addArrangedSubview(brand)
  232. let titleToMenuSpacer = NSView()
  233. titleToMenuSpacer.translatesAutoresizingMaskIntoConstraints = false
  234. titleToMenuSpacer.heightAnchor.constraint(equalToConstant: 24).isActive = true
  235. sidebar.addArrangedSubview(titleToMenuSpacer)
  236. items.enumerated().forEach { index, item in
  237. let row = NSStackView()
  238. row.orientation = .horizontal
  239. row.spacing = 8
  240. row.alignment = .centerY
  241. row.wantsLayer = true
  242. row.layer?.cornerRadius = 8
  243. row.edgeInsets = NSEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
  244. let isSelected = index == 0
  245. if isSelected {
  246. row.layer?.backgroundColor = Theme.selectionFill.cgColor
  247. }
  248. let icon = NSImageView()
  249. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  250. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  251. icon.contentTintColor = isSelected ? Theme.primaryText : Theme.secondaryText
  252. let text = NSTextField(labelWithString: item.title)
  253. text.font = .systemFont(ofSize: 14, weight: .medium)
  254. text.textColor = isSelected ? Theme.primaryText : Theme.secondaryText
  255. row.addArrangedSubview(icon)
  256. row.addArrangedSubview(text)
  257. if let badge = item.badge {
  258. let badgeField = NSTextField(labelWithString: badge)
  259. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  260. badgeField.textColor = Theme.primaryText
  261. badgeField.wantsLayer = true
  262. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  263. badgeField.layer?.cornerRadius = 8
  264. badgeField.alignment = .center
  265. badgeField.maximumNumberOfLines = 1
  266. badgeField.lineBreakMode = .byClipping
  267. badgeField.translatesAutoresizingMaskIntoConstraints = false
  268. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  269. row.addArrangedSubview(NSView())
  270. row.addArrangedSubview(badgeField)
  271. }
  272. sidebar.addArrangedSubview(row)
  273. }
  274. let sidebarBottomSpacer = NSView()
  275. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  276. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  277. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  278. sidebar.addArrangedSubview(sidebarBottomSpacer)
  279. let upgradeCard = NSView()
  280. upgradeCard.translatesAutoresizingMaskIntoConstraints = false
  281. upgradeCard.wantsLayer = true
  282. upgradeCard.layer?.backgroundColor = Theme.proCardFill.cgColor
  283. upgradeCard.layer?.cornerRadius = 14
  284. upgradeCard.layer?.borderWidth = 1
  285. upgradeCard.layer?.borderColor = Theme.proCardBorder.cgColor
  286. upgradeCard.layer?.masksToBounds = true
  287. let accentBar = NSView()
  288. accentBar.translatesAutoresizingMaskIntoConstraints = false
  289. accentBar.wantsLayer = true
  290. accentBar.layer?.backgroundColor = Theme.proAccent.cgColor
  291. let inner = NSStackView()
  292. inner.translatesAutoresizingMaskIntoConstraints = false
  293. inner.orientation = .vertical
  294. inner.spacing = 10
  295. inner.alignment = .leading
  296. let proIcon = NSImageView()
  297. proIcon.translatesAutoresizingMaskIntoConstraints = false
  298. proIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  299. proIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: nil)
  300. proIcon.contentTintColor = Theme.proAccent
  301. let proEyebrow = NSTextField(labelWithString: "Premium")
  302. proEyebrow.font = .systemFont(ofSize: 11, weight: .heavy)
  303. proEyebrow.textColor = Theme.proAccent
  304. proEyebrow.alignment = .left
  305. let eyebrowRow = NSStackView(views: [proIcon, proEyebrow])
  306. eyebrowRow.orientation = .horizontal
  307. eyebrowRow.spacing = 6
  308. eyebrowRow.alignment = .centerY
  309. let headline = NSTextField(labelWithString: "Upgrade to Pro")
  310. headline.font = .systemFont(ofSize: 16, weight: .bold)
  311. headline.textColor = Theme.primaryText
  312. headline.alignment = .left
  313. let upgradeDescription = NSTextField(wrappingLabelWithString: "Unlimited AI matches, smart alerts, and interview prep—all in one place.")
  314. upgradeDescription.font = .systemFont(ofSize: 12, weight: .regular)
  315. upgradeDescription.textColor = Theme.secondaryText
  316. upgradeDescription.preferredMaxLayoutWidth = 165
  317. let upgradeButton = NSButton(title: "Upgrade to Pro", target: self, action: #selector(didTapUpgradeToPro))
  318. upgradeButton.isBordered = false
  319. upgradeButton.bezelStyle = .rounded
  320. upgradeButton.font = .systemFont(ofSize: 13, weight: .bold)
  321. upgradeButton.contentTintColor = Theme.proCTAText
  322. upgradeButton.wantsLayer = true
  323. upgradeButton.layer?.backgroundColor = Theme.proCTABackground.cgColor
  324. upgradeButton.layer?.cornerRadius = 20
  325. upgradeButton.translatesAutoresizingMaskIntoConstraints = false
  326. upgradeButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  327. inner.addArrangedSubview(eyebrowRow)
  328. inner.addArrangedSubview(headline)
  329. inner.addArrangedSubview(upgradeDescription)
  330. inner.addArrangedSubview(upgradeButton)
  331. upgradeCard.addSubview(accentBar)
  332. upgradeCard.addSubview(inner)
  333. let cardWidth: CGFloat = 197
  334. NSLayoutConstraint.activate([
  335. upgradeCard.widthAnchor.constraint(equalToConstant: cardWidth),
  336. accentBar.topAnchor.constraint(equalTo: upgradeCard.topAnchor),
  337. accentBar.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor),
  338. accentBar.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor),
  339. accentBar.heightAnchor.constraint(equalToConstant: 2),
  340. inner.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor, constant: 14),
  341. inner.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor, constant: -14),
  342. inner.topAnchor.constraint(equalTo: accentBar.bottomAnchor, constant: 12),
  343. inner.bottomAnchor.constraint(equalTo: upgradeCard.bottomAnchor, constant: -14),
  344. upgradeButton.widthAnchor.constraint(equalTo: inner.widthAnchor)
  345. ])
  346. sidebar.addArrangedSubview(upgradeCard)
  347. }
  348. @objc private func didTapUpgradeToPro() {
  349. guard let url = URL(string: "https://www.indeed.com") else { return }
  350. NSWorkspace.shared.open(url)
  351. }
  352. private func updateDocumentLayout() {
  353. documentContainer.layoutSubtreeIfNeeded()
  354. let fittingHeight = max(chromeContainer.fittingSize.height, bounds.height)
  355. documentContainer.frame = NSRect(x: 0, y: 0, width: bounds.width, height: fittingHeight)
  356. }
  357. }