Ingen beskrivning

DashboardView.swift 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 = 18
  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.topAnchor.constraint(equalTo: documentContainer.topAnchor, constant: 18),
  140. chromeContainer.bottomAnchor.constraint(equalTo: documentContainer.bottomAnchor, constant: -18),
  141. chromeContainer.centerXAnchor.constraint(equalTo: documentContainer.centerXAnchor),
  142. chromeContainer.widthAnchor.constraint(lessThanOrEqualToConstant: 1240),
  143. chromeContainer.widthAnchor.constraint(equalTo: documentContainer.widthAnchor, constant: -36),
  144. contentStack.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor),
  145. contentStack.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor),
  146. contentStack.topAnchor.constraint(equalTo: chromeContainer.topAnchor),
  147. contentStack.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor),
  148. sidebar.widthAnchor.constraint(equalToConstant: 225),
  149. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  150. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  151. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  152. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  153. mainHost.bottomAnchor.constraint(equalTo: mainOverlay.bottomAnchor, constant: 24),
  154. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 24),
  155. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -24),
  156. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  157. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor),
  158. insightsLinkButton.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  159. insightsLinkButton.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor),
  160. sparkleView.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor, constant: -28),
  161. sparkleView.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -28)
  162. ])
  163. }
  164. private func configureInsightsCard() {
  165. insightsCard.wantsLayer = true
  166. insightsCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  167. insightsCard.layer?.cornerRadius = 18
  168. insightsCard.translatesAutoresizingMaskIntoConstraints = false
  169. insightsTitleLabel.font = .systemFont(ofSize: 20, weight: .semibold)
  170. insightsTitleLabel.textColor = Theme.primaryText
  171. insightsTitleLabel.alignment = .center
  172. insightsTitleLabel.maximumNumberOfLines = 1
  173. insightsBodyLabel.font = .systemFont(ofSize: 13, weight: .regular)
  174. insightsBodyLabel.textColor = Theme.secondaryText
  175. insightsBodyLabel.alignment = .center
  176. insightsBodyLabel.maximumNumberOfLines = 4
  177. insightsBodyLabel.lineBreakMode = .byWordWrapping
  178. insightsBodyLabel.preferredMaxLayoutWidth = 400
  179. insightsLinkButton.bezelStyle = .inline
  180. insightsLinkButton.isBordered = false
  181. insightsLinkButton.font = .systemFont(ofSize: 13, weight: .medium)
  182. insightsLinkButton.contentTintColor = Theme.linkText
  183. insightsLinkButton.setButtonType(.momentaryPushIn)
  184. togglesLabel.font = .systemFont(ofSize: 12, weight: .medium)
  185. togglesLabel.textColor = Theme.tertiaryText
  186. togglesLabel.alignment = .center
  187. togglesLabel.maximumNumberOfLines = 1
  188. styleToggle(savedToggleButton)
  189. styleToggle(interviewsToggleButton)
  190. let toggleRow = NSStackView(views: [savedToggleButton, interviewsToggleButton])
  191. toggleRow.orientation = .horizontal
  192. toggleRow.spacing = 10
  193. toggleRow.alignment = .centerY
  194. let inner = NSStackView(views: [
  195. insightsTitleLabel,
  196. insightsBodyLabel,
  197. togglesLabel,
  198. toggleRow
  199. ])
  200. inner.orientation = .vertical
  201. inner.spacing = 14
  202. inner.alignment = .centerX
  203. inner.translatesAutoresizingMaskIntoConstraints = false
  204. insightsCard.addSubview(inner)
  205. NSLayoutConstraint.activate([
  206. inner.leadingAnchor.constraint(equalTo: insightsCard.leadingAnchor, constant: 32),
  207. inner.trailingAnchor.constraint(equalTo: insightsCard.trailingAnchor, constant: -32),
  208. inner.topAnchor.constraint(equalTo: insightsCard.topAnchor, constant: 28),
  209. inner.bottomAnchor.constraint(equalTo: insightsCard.bottomAnchor, constant: -28),
  210. insightsCard.widthAnchor.constraint(equalToConstant: 440)
  211. ])
  212. }
  213. private func styleToggle(_ button: NSButton) {
  214. button.bezelStyle = .rounded
  215. button.font = .systemFont(ofSize: 12, weight: .medium)
  216. button.contentTintColor = Theme.secondaryText
  217. button.wantsLayer = true
  218. button.layer?.backgroundColor = Theme.toggleBackground.cgColor
  219. button.layer?.cornerRadius = 8
  220. button.translatesAutoresizingMaskIntoConstraints = false
  221. button.widthAnchor.constraint(equalToConstant: 108).isActive = true
  222. button.heightAnchor.constraint(equalToConstant: 30).isActive = true
  223. }
  224. private func configureSidebar(_ items: [SidebarItem]) {
  225. sidebar.arrangedSubviews.forEach {
  226. sidebar.removeArrangedSubview($0)
  227. $0.removeFromSuperview()
  228. }
  229. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  230. brand.font = .systemFont(ofSize: 18, weight: .bold)
  231. brand.textColor = Theme.primaryText
  232. brand.alignment = .left
  233. sidebar.addArrangedSubview(brand)
  234. let titleToMenuSpacer = NSView()
  235. titleToMenuSpacer.translatesAutoresizingMaskIntoConstraints = false
  236. titleToMenuSpacer.heightAnchor.constraint(equalToConstant: 24).isActive = true
  237. sidebar.addArrangedSubview(titleToMenuSpacer)
  238. items.enumerated().forEach { index, item in
  239. let row = NSStackView()
  240. row.orientation = .horizontal
  241. row.spacing = 8
  242. row.alignment = .centerY
  243. row.wantsLayer = true
  244. row.layer?.cornerRadius = 8
  245. row.edgeInsets = NSEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
  246. let isSelected = index == 0
  247. if isSelected {
  248. row.layer?.backgroundColor = Theme.selectionFill.cgColor
  249. }
  250. let icon = NSImageView()
  251. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  252. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  253. icon.contentTintColor = isSelected ? Theme.primaryText : Theme.secondaryText
  254. let text = NSTextField(labelWithString: item.title)
  255. text.font = .systemFont(ofSize: 14, weight: .medium)
  256. text.textColor = isSelected ? Theme.primaryText : Theme.secondaryText
  257. row.addArrangedSubview(icon)
  258. row.addArrangedSubview(text)
  259. if let badge = item.badge {
  260. let badgeField = NSTextField(labelWithString: badge)
  261. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  262. badgeField.textColor = Theme.primaryText
  263. badgeField.wantsLayer = true
  264. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  265. badgeField.layer?.cornerRadius = 8
  266. badgeField.alignment = .center
  267. badgeField.maximumNumberOfLines = 1
  268. badgeField.lineBreakMode = .byClipping
  269. badgeField.translatesAutoresizingMaskIntoConstraints = false
  270. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  271. row.addArrangedSubview(NSView())
  272. row.addArrangedSubview(badgeField)
  273. }
  274. sidebar.addArrangedSubview(row)
  275. }
  276. let sidebarBottomSpacer = NSView()
  277. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  278. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  279. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  280. sidebar.addArrangedSubview(sidebarBottomSpacer)
  281. }
  282. private func updateDocumentLayout() {
  283. documentContainer.layoutSubtreeIfNeeded()
  284. let fittingHeight = max(chromeContainer.fittingSize.height + 36, bounds.height)
  285. documentContainer.frame = NSRect(x: 0, y: 0, width: bounds.width, height: fittingHeight)
  286. }
  287. }