Sin descripción

DashboardView.swift 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 linkText = NSColor(calibratedWhite: 0.82, alpha: 1)
  19. }
  20. private let contentStack = NSStackView()
  21. private let documentContainer = NSView()
  22. private let chromeContainer = NSView()
  23. private let sidebar = NSStackView()
  24. private let mainHost = NSView()
  25. private let mainOverlay = NSStackView()
  26. private let greetingLabel = NSTextField(labelWithString: "")
  27. private let subtitleLabel = NSTextField(labelWithString: "")
  28. private let insightsCard = NSView()
  29. private let insightsTitleLabel = NSTextField(labelWithString: "")
  30. private let insightsBodyLabel = NSTextField(labelWithString: "")
  31. private let insightsLinkButton = NSButton(title: "", target: nil, action: nil)
  32. private let togglesLabel = NSTextField(labelWithString: "Dashboard Toggles:")
  33. private let savedToggleButton = NSButton(title: "Saved", target: nil, action: nil)
  34. private let interviewsToggleButton = NSButton(title: "Interviews", target: nil, action: nil)
  35. private let sparkleView = NSImageView()
  36. private let scrollView = NSScrollView()
  37. override init(frame frameRect: NSRect) {
  38. super.init(frame: frameRect)
  39. setupLayout()
  40. }
  41. required init?(coder: NSCoder) {
  42. super.init(coder: coder)
  43. setupLayout()
  44. }
  45. override func layout() {
  46. super.layout()
  47. updateDocumentLayout()
  48. }
  49. func render(_ data: DashboardData) {
  50. greetingLabel.stringValue = "Welcome back, \(data.greetingName)! 👋"
  51. subtitleLabel.stringValue = data.subtitle
  52. insightsTitleLabel.stringValue = data.profileInsightsTitle
  53. insightsBodyLabel.stringValue = data.profileInsightsBody
  54. insightsLinkButton.title = data.profileInsightsLinkTitle
  55. configureSidebar(data.sidebarItems)
  56. updateDocumentLayout()
  57. }
  58. private func setupLayout() {
  59. wantsLayer = true
  60. layer?.backgroundColor = Theme.pageBackground.cgColor
  61. scrollView.translatesAutoresizingMaskIntoConstraints = false
  62. scrollView.hasVerticalScroller = true
  63. scrollView.drawsBackground = false
  64. addSubview(scrollView)
  65. contentStack.orientation = .horizontal
  66. contentStack.spacing = 20
  67. contentStack.translatesAutoresizingMaskIntoConstraints = false
  68. contentStack.alignment = .top
  69. contentStack.edgeInsets = NSEdgeInsets(top: 24, left: 24, bottom: 24, right: 24)
  70. documentContainer.translatesAutoresizingMaskIntoConstraints = true
  71. documentContainer.autoresizingMask = [.width]
  72. documentContainer.frame = NSRect(x: 0, y: 0, width: 1040, height: 900)
  73. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  74. chromeContainer.wantsLayer = true
  75. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  76. chromeContainer.layer?.cornerRadius = 0
  77. documentContainer.addSubview(chromeContainer)
  78. chromeContainer.addSubview(contentStack)
  79. scrollView.documentView = documentContainer
  80. sidebar.orientation = .vertical
  81. sidebar.spacing = 10
  82. sidebar.distribution = .fill
  83. sidebar.alignment = .leading
  84. sidebar.wantsLayer = true
  85. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  86. sidebar.layer?.cornerRadius = 16
  87. sidebar.edgeInsets = NSEdgeInsets(top: 18, left: 14, bottom: 18, right: 14)
  88. sidebar.translatesAutoresizingMaskIntoConstraints = false
  89. mainHost.translatesAutoresizingMaskIntoConstraints = false
  90. mainHost.wantsLayer = true
  91. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  92. mainHost.layer?.cornerRadius = 16
  93. mainHost.layer?.masksToBounds = true
  94. mainHost.addSubview(mainOverlay)
  95. mainOverlay.orientation = .vertical
  96. mainOverlay.spacing = 0
  97. mainOverlay.alignment = .centerX
  98. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  99. mainOverlay.setContentHuggingPriority(.required, for: .vertical)
  100. greetingLabel.font = .systemFont(ofSize: 32, weight: .bold)
  101. greetingLabel.textColor = Theme.primaryText
  102. greetingLabel.alignment = .center
  103. greetingLabel.maximumNumberOfLines = 1
  104. subtitleLabel.font = .systemFont(ofSize: 15, weight: .regular)
  105. subtitleLabel.textColor = Theme.secondaryText
  106. subtitleLabel.alignment = .center
  107. subtitleLabel.maximumNumberOfLines = 2
  108. let topInset = NSView()
  109. topInset.translatesAutoresizingMaskIntoConstraints = false
  110. topInset.heightAnchor.constraint(equalToConstant: 32).isActive = true
  111. configureInsightsCard()
  112. let titleBlock = NSStackView(views: [greetingLabel, subtitleLabel])
  113. titleBlock.orientation = .vertical
  114. titleBlock.spacing = 10
  115. titleBlock.alignment = .centerX
  116. let headerStack = NSStackView(views: [titleBlock, insightsLinkButton])
  117. headerStack.orientation = .vertical
  118. headerStack.spacing = 18
  119. headerStack.alignment = .centerX
  120. let midSpacer = NSView()
  121. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  122. midSpacer.heightAnchor.constraint(equalToConstant: 20).isActive = true
  123. mainOverlay.addArrangedSubview(topInset)
  124. mainOverlay.addArrangedSubview(headerStack)
  125. mainOverlay.addArrangedSubview(midSpacer)
  126. mainOverlay.addArrangedSubview(insightsCard)
  127. sparkleView.translatesAutoresizingMaskIntoConstraints = false
  128. sparkleView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 22, weight: .regular)
  129. sparkleView.image = NSImage(systemSymbolName: "sparkle", accessibilityDescription: "Accent")
  130. sparkleView.contentTintColor = NSColor(calibratedWhite: 0.9, alpha: 0.55)
  131. mainHost.addSubview(sparkleView)
  132. contentStack.addArrangedSubview(sidebar)
  133. contentStack.addArrangedSubview(mainHost)
  134. NSLayoutConstraint.activate([
  135. scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
  136. scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
  137. scrollView.topAnchor.constraint(equalTo: topAnchor),
  138. scrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
  139. chromeContainer.leadingAnchor.constraint(equalTo: documentContainer.leadingAnchor),
  140. chromeContainer.trailingAnchor.constraint(equalTo: documentContainer.trailingAnchor),
  141. chromeContainer.topAnchor.constraint(equalTo: documentContainer.topAnchor),
  142. chromeContainer.bottomAnchor.constraint(equalTo: documentContainer.bottomAnchor),
  143. contentStack.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor),
  144. contentStack.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor),
  145. contentStack.topAnchor.constraint(equalTo: chromeContainer.topAnchor),
  146. contentStack.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor),
  147. sidebar.widthAnchor.constraint(equalToConstant: 225),
  148. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  149. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  150. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  151. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  152. mainHost.bottomAnchor.constraint(equalTo: mainOverlay.bottomAnchor, constant: 24),
  153. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 24),
  154. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -24),
  155. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  156. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor),
  157. insightsLinkButton.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  158. insightsLinkButton.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor),
  159. sparkleView.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor, constant: -28),
  160. sparkleView.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -28)
  161. ])
  162. }
  163. private func configureInsightsCard() {
  164. insightsCard.wantsLayer = true
  165. insightsCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  166. insightsCard.layer?.cornerRadius = 18
  167. insightsCard.translatesAutoresizingMaskIntoConstraints = false
  168. insightsTitleLabel.font = .systemFont(ofSize: 20, weight: .semibold)
  169. insightsTitleLabel.textColor = Theme.primaryText
  170. insightsTitleLabel.alignment = .center
  171. insightsTitleLabel.maximumNumberOfLines = 1
  172. insightsBodyLabel.font = .systemFont(ofSize: 13, weight: .regular)
  173. insightsBodyLabel.textColor = Theme.secondaryText
  174. insightsBodyLabel.alignment = .center
  175. insightsBodyLabel.maximumNumberOfLines = 4
  176. insightsBodyLabel.lineBreakMode = .byWordWrapping
  177. insightsBodyLabel.preferredMaxLayoutWidth = 400
  178. insightsLinkButton.bezelStyle = .inline
  179. insightsLinkButton.isBordered = false
  180. insightsLinkButton.font = .systemFont(ofSize: 13, weight: .medium)
  181. insightsLinkButton.contentTintColor = Theme.linkText
  182. insightsLinkButton.setButtonType(.momentaryPushIn)
  183. togglesLabel.font = .systemFont(ofSize: 12, weight: .medium)
  184. togglesLabel.textColor = Theme.tertiaryText
  185. togglesLabel.alignment = .center
  186. togglesLabel.maximumNumberOfLines = 1
  187. styleToggle(savedToggleButton)
  188. styleToggle(interviewsToggleButton)
  189. let toggleRow = NSStackView(views: [savedToggleButton, interviewsToggleButton])
  190. toggleRow.orientation = .horizontal
  191. toggleRow.spacing = 10
  192. toggleRow.alignment = .centerY
  193. let inner = NSStackView(views: [
  194. insightsTitleLabel,
  195. insightsBodyLabel,
  196. togglesLabel,
  197. toggleRow
  198. ])
  199. inner.orientation = .vertical
  200. inner.spacing = 14
  201. inner.alignment = .centerX
  202. inner.translatesAutoresizingMaskIntoConstraints = false
  203. insightsCard.addSubview(inner)
  204. NSLayoutConstraint.activate([
  205. inner.leadingAnchor.constraint(equalTo: insightsCard.leadingAnchor, constant: 32),
  206. inner.trailingAnchor.constraint(equalTo: insightsCard.trailingAnchor, constant: -32),
  207. inner.topAnchor.constraint(equalTo: insightsCard.topAnchor, constant: 28),
  208. inner.bottomAnchor.constraint(equalTo: insightsCard.bottomAnchor, constant: -28),
  209. insightsCard.widthAnchor.constraint(equalToConstant: 440)
  210. ])
  211. }
  212. private func styleToggle(_ button: NSButton) {
  213. button.bezelStyle = .rounded
  214. button.font = .systemFont(ofSize: 12, weight: .medium)
  215. button.contentTintColor = Theme.secondaryText
  216. button.wantsLayer = true
  217. button.layer?.backgroundColor = Theme.toggleBackground.cgColor
  218. button.layer?.cornerRadius = 8
  219. button.translatesAutoresizingMaskIntoConstraints = false
  220. button.widthAnchor.constraint(equalToConstant: 108).isActive = true
  221. button.heightAnchor.constraint(equalToConstant: 30).isActive = true
  222. }
  223. private func configureSidebar(_ items: [SidebarItem]) {
  224. sidebar.arrangedSubviews.forEach {
  225. sidebar.removeArrangedSubview($0)
  226. $0.removeFromSuperview()
  227. }
  228. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  229. brand.font = .systemFont(ofSize: 18, weight: .bold)
  230. brand.textColor = Theme.primaryText
  231. brand.alignment = .left
  232. sidebar.addArrangedSubview(brand)
  233. let titleToMenuSpacer = NSView()
  234. titleToMenuSpacer.translatesAutoresizingMaskIntoConstraints = false
  235. titleToMenuSpacer.heightAnchor.constraint(equalToConstant: 24).isActive = true
  236. sidebar.addArrangedSubview(titleToMenuSpacer)
  237. items.enumerated().forEach { index, item in
  238. let row = NSStackView()
  239. row.orientation = .horizontal
  240. row.spacing = 8
  241. row.alignment = .centerY
  242. row.wantsLayer = true
  243. row.layer?.cornerRadius = 8
  244. row.edgeInsets = NSEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
  245. let isSelected = index == 0
  246. if isSelected {
  247. row.layer?.backgroundColor = Theme.selectionFill.cgColor
  248. }
  249. let icon = NSImageView()
  250. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  251. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  252. icon.contentTintColor = isSelected ? Theme.primaryText : Theme.secondaryText
  253. let text = NSTextField(labelWithString: item.title)
  254. text.font = .systemFont(ofSize: 14, weight: .medium)
  255. text.textColor = isSelected ? Theme.primaryText : Theme.secondaryText
  256. row.addArrangedSubview(icon)
  257. row.addArrangedSubview(text)
  258. if let badge = item.badge {
  259. let badgeField = NSTextField(labelWithString: badge)
  260. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  261. badgeField.textColor = Theme.primaryText
  262. badgeField.wantsLayer = true
  263. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  264. badgeField.layer?.cornerRadius = 8
  265. badgeField.alignment = .center
  266. badgeField.maximumNumberOfLines = 1
  267. badgeField.lineBreakMode = .byClipping
  268. badgeField.translatesAutoresizingMaskIntoConstraints = false
  269. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  270. row.addArrangedSubview(NSView())
  271. row.addArrangedSubview(badgeField)
  272. }
  273. sidebar.addArrangedSubview(row)
  274. }
  275. let sidebarBottomSpacer = NSView()
  276. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  277. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  278. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  279. sidebar.addArrangedSubview(sidebarBottomSpacer)
  280. }
  281. private func updateDocumentLayout() {
  282. documentContainer.layoutSubtreeIfNeeded()
  283. let fittingHeight = max(chromeContainer.fittingSize.height, bounds.height)
  284. documentContainer.frame = NSRect(x: 0, y: 0, width: bounds.width, height: fittingHeight)
  285. }
  286. }