Нема описа

MyProfilePageView.swift 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. //
  2. // MyProfilePageView.swift
  3. // App for Indeed
  4. //
  5. // Light-theme profile editor: card layout, adaptive two-column rows, and
  6. // vertical scrolling when the window is short.
  7. //
  8. import Cocoa
  9. import UniformTypeIdentifiers
  10. private enum ProfilePagePalette {
  11. static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  12. static let brandBlueHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  13. static let pageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  14. static let cardBackground = NSColor(srgbRed: 252 / 255, green: 252 / 255, blue: 252 / 255, alpha: 1)
  15. static let fieldFill = NSColor(srgbRed: 247 / 255, green: 247 / 255, blue: 247 / 255, alpha: 1)
  16. static let primaryText = NSColor(srgbRed: 45 / 255, green: 45 / 255, blue: 45 / 255, alpha: 1)
  17. static let secondaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  18. static let border = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  19. static let avatarWell = NSColor(srgbRed: 232 / 255, green: 232 / 255, blue: 232 / 255, alpha: 1)
  20. }
  21. final class MyProfilePageView: NSView {
  22. private static let compactFormWidth: CGFloat = 520
  23. private let scrollView = NSScrollView()
  24. private let documentView = NSView()
  25. private let cardView = NSView()
  26. private let formStack = NSStackView()
  27. private let profileNameField = NSTextField()
  28. private let fullNameField = NSTextField()
  29. private let emailField = NSTextField()
  30. private let phoneField = NSTextField()
  31. private let jobTitleField = NSTextField()
  32. private let addressField = NSTextField()
  33. private let careerField = NSTextField()
  34. private let avatarImageView = NSImageView()
  35. private let uploadPhotoButton = NSButton(title: "Upload Photo", target: nil, action: nil)
  36. private let saveButton = ProfilePrimaryButton(title: "Save Profile →", target: nil, action: nil)
  37. private let nameEmailRow = NSStackView()
  38. private let phoneJobRow = NSStackView()
  39. private var lastCompactLayout: Bool?
  40. /// Force left-to-right geometry so profile fields span the full width even when the window uses RTL layout.
  41. override var userInterfaceLayoutDirection: NSUserInterfaceLayoutDirection {
  42. get { .leftToRight }
  43. set { super.userInterfaceLayoutDirection = .leftToRight }
  44. }
  45. override init(frame frameRect: NSRect) {
  46. super.init(frame: frameRect)
  47. setup()
  48. }
  49. required init?(coder: NSCoder) {
  50. super.init(coder: coder)
  51. setup()
  52. }
  53. override func layout() {
  54. super.layout()
  55. applyResponsiveRowsIfNeeded()
  56. }
  57. private func setup() {
  58. wantsLayer = true
  59. layer?.backgroundColor = ProfilePagePalette.pageBackground.cgColor
  60. userInterfaceLayoutDirection = .leftToRight
  61. scrollView.translatesAutoresizingMaskIntoConstraints = false
  62. scrollView.userInterfaceLayoutDirection = .leftToRight
  63. scrollView.hasVerticalScroller = true
  64. scrollView.hasHorizontalScroller = false
  65. scrollView.autohidesScrollers = true
  66. scrollView.drawsBackground = false
  67. scrollView.borderType = .noBorder
  68. scrollView.scrollerStyle = .overlay
  69. scrollView.automaticallyAdjustsContentInsets = false
  70. scrollView.contentView.userInterfaceLayoutDirection = .leftToRight
  71. documentView.translatesAutoresizingMaskIntoConstraints = false
  72. documentView.userInterfaceLayoutDirection = .leftToRight
  73. cardView.translatesAutoresizingMaskIntoConstraints = false
  74. cardView.wantsLayer = true
  75. cardView.layer?.backgroundColor = ProfilePagePalette.cardBackground.cgColor
  76. cardView.layer?.cornerRadius = 16
  77. cardView.layer?.borderWidth = 1
  78. cardView.layer?.borderColor = ProfilePagePalette.border.cgColor
  79. cardView.userInterfaceLayoutDirection = .leftToRight
  80. if #available(macOS 11.0, *) {
  81. cardView.layer?.cornerCurve = .continuous
  82. }
  83. formStack.translatesAutoresizingMaskIntoConstraints = false
  84. formStack.orientation = .vertical
  85. formStack.alignment = .width
  86. formStack.distribution = .fill
  87. formStack.spacing = 20
  88. formStack.edgeInsets = NSEdgeInsets(top: 28, left: 22, bottom: 28, right: 22)
  89. formStack.userInterfaceLayoutDirection = .leftToRight
  90. formStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  91. formStack.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  92. addSubview(scrollView)
  93. scrollView.documentView = documentView
  94. documentView.addSubview(cardView)
  95. cardView.addSubview(formStack)
  96. NSLayoutConstraint.activate([
  97. scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
  98. scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
  99. scrollView.topAnchor.constraint(equalTo: topAnchor),
  100. scrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
  101. // Pin left and right to the clip view’s geometric edges so the document spans the full visible
  102. // width (leading/trailing + width alone can leave a narrow strip on the wrong side in edge cases).
  103. documentView.leftAnchor.constraint(equalTo: scrollView.contentView.leftAnchor),
  104. documentView.rightAnchor.constraint(equalTo: scrollView.contentView.rightAnchor),
  105. documentView.topAnchor.constraint(equalTo: scrollView.contentView.topAnchor),
  106. cardView.leadingAnchor.constraint(equalTo: documentView.leadingAnchor, constant: 20),
  107. cardView.trailingAnchor.constraint(equalTo: documentView.trailingAnchor, constant: -20),
  108. cardView.topAnchor.constraint(equalTo: documentView.topAnchor, constant: 16),
  109. cardView.bottomAnchor.constraint(equalTo: documentView.bottomAnchor, constant: -24),
  110. formStack.leadingAnchor.constraint(equalTo: cardView.leadingAnchor),
  111. formStack.trailingAnchor.constraint(equalTo: cardView.trailingAnchor),
  112. formStack.topAnchor.constraint(equalTo: cardView.topAnchor),
  113. formStack.bottomAnchor.constraint(equalTo: cardView.bottomAnchor)
  114. ])
  115. formStack.addArrangedSubview(labeledGroup(title: "Profile Name *", field: profileNameField, placeholder: "Marketing Director Profile"))
  116. formStack.addArrangedSubview(sectionHeading("Personal Information"))
  117. let nameGroup = labeledGroup(title: "Full Name *", field: fullNameField, placeholder: "John Doe")
  118. let emailGroup = labeledGroup(title: "Email *", field: emailField, placeholder: "john@example.com")
  119. configureTwoColumnRow(nameEmailRow, left: nameGroup, right: emailGroup)
  120. pinEqualColumnWidths(in: nameEmailRow)
  121. formStack.addArrangedSubview(nameEmailRow)
  122. let phoneGroup = labeledGroup(title: "Phone", field: phoneField, placeholder: "+1 (555) 123-4567")
  123. let jobGroup = labeledGroup(title: "Job Title *", field: jobTitleField, placeholder: "Software Engineer")
  124. configureTwoColumnRow(phoneJobRow, left: phoneGroup, right: jobGroup)
  125. pinEqualColumnWidths(in: phoneJobRow)
  126. formStack.addArrangedSubview(phoneJobRow)
  127. formStack.addArrangedSubview(labeledGroup(title: "Address", field: addressField, placeholder: "123 Main St, City, State, ZIP"))
  128. formStack.addArrangedSubview(careerSummaryBlock())
  129. formStack.addArrangedSubview(profileImageBlock())
  130. formStack.addArrangedSubview(saveButtonHost())
  131. uploadPhotoButton.target = self
  132. uploadPhotoButton.action = #selector(didTapUploadPhoto)
  133. saveButton.target = self
  134. saveButton.action = #selector(didTapSave)
  135. }
  136. private func applyResponsiveRowsIfNeeded() {
  137. let w = cardView.bounds.width
  138. guard w > 1 else { return }
  139. let formWidth = max(0, w - formStack.edgeInsets.left - formStack.edgeInsets.right)
  140. let compact = formWidth < Self.compactFormWidth
  141. guard compact != lastCompactLayout else { return }
  142. lastCompactLayout = compact
  143. let orientation: NSUserInterfaceLayoutOrientation = compact ? .vertical : .horizontal
  144. let rowSpacing: CGFloat = compact ? 16 : 12
  145. nameEmailRow.orientation = orientation
  146. nameEmailRow.spacing = rowSpacing
  147. phoneJobRow.orientation = orientation
  148. phoneJobRow.spacing = rowSpacing
  149. nameEmailRow.distribution = compact ? .fill : .fillEqually
  150. phoneJobRow.distribution = compact ? .fill : .fillEqually
  151. }
  152. /// Keeps two columns the same width when the row is horizontal; avoids NSTextField intrinsic width fighting the stack.
  153. private func pinEqualColumnWidths(in row: NSStackView) {
  154. guard row.arrangedSubviews.count == 2 else { return }
  155. let left = row.arrangedSubviews[0]
  156. let right = row.arrangedSubviews[1]
  157. left.widthAnchor.constraint(equalTo: right.widthAnchor).isActive = true
  158. }
  159. private func configureTwoColumnRow(_ row: NSStackView, left: NSView, right: NSView) {
  160. row.translatesAutoresizingMaskIntoConstraints = false
  161. row.orientation = .horizontal
  162. row.spacing = 12
  163. row.distribution = .fillEqually
  164. row.alignment = .top
  165. row.userInterfaceLayoutDirection = .leftToRight
  166. row.setContentHuggingPriority(.defaultLow, for: .horizontal)
  167. row.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  168. row.addArrangedSubview(left)
  169. row.addArrangedSubview(right)
  170. }
  171. private func sectionHeading(_ text: String) -> NSView {
  172. let label = NSTextField(labelWithString: text)
  173. label.font = .systemFont(ofSize: 15, weight: .semibold)
  174. label.textColor = ProfilePagePalette.primaryText
  175. label.alignment = .left
  176. label.baseWritingDirection = .leftToRight
  177. label.translatesAutoresizingMaskIntoConstraints = false
  178. label.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  179. let spacer = NSView()
  180. spacer.translatesAutoresizingMaskIntoConstraints = false
  181. spacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  182. let row = NSStackView(views: [label, spacer])
  183. row.orientation = .horizontal
  184. row.alignment = .centerY
  185. row.spacing = 0
  186. row.userInterfaceLayoutDirection = .leftToRight
  187. row.translatesAutoresizingMaskIntoConstraints = false
  188. return row
  189. }
  190. private func labeledGroup(title: String, field: NSTextField, placeholder: String) -> NSView {
  191. let label = NSTextField(labelWithString: title)
  192. label.font = .systemFont(ofSize: 12, weight: .medium)
  193. label.textColor = ProfilePagePalette.secondaryText
  194. label.alignment = .left
  195. label.baseWritingDirection = .leftToRight
  196. label.translatesAutoresizingMaskIntoConstraints = false
  197. label.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  198. styleSingleLineField(field, placeholder: placeholder)
  199. let wrap = roundedFieldChrome(containing: field, minHeight: 40)
  200. let stack = NSStackView(views: [label, wrap])
  201. stack.orientation = .vertical
  202. stack.spacing = 8
  203. // Leading keeps the title at the left edge; width match keeps the field full width.
  204. stack.alignment = .leading
  205. stack.translatesAutoresizingMaskIntoConstraints = false
  206. stack.userInterfaceLayoutDirection = .leftToRight
  207. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  208. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  209. wrap.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  210. NSLayoutConstraint.activate([
  211. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  212. ])
  213. return stack
  214. }
  215. private func styleSingleLineField(_ field: NSTextField, placeholder: String) {
  216. field.translatesAutoresizingMaskIntoConstraints = false
  217. field.isBordered = false
  218. field.drawsBackground = false
  219. field.focusRingType = .none
  220. field.font = .systemFont(ofSize: 14, weight: .regular)
  221. field.textColor = ProfilePagePalette.primaryText
  222. field.setContentHuggingPriority(.defaultLow, for: .horizontal)
  223. field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  224. field.placeholderAttributedString = NSAttributedString(
  225. string: placeholder,
  226. attributes: [
  227. .foregroundColor: ProfilePagePalette.secondaryText,
  228. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  229. ]
  230. )
  231. field.cell?.usesSingleLineMode = true
  232. field.cell?.wraps = false
  233. field.cell?.isScrollable = true
  234. field.baseWritingDirection = .leftToRight
  235. field.alignment = .left
  236. }
  237. private func roundedFieldChrome(containing field: NSTextField, minHeight: CGFloat) -> NSView {
  238. let wrap = NSView()
  239. wrap.translatesAutoresizingMaskIntoConstraints = false
  240. wrap.wantsLayer = true
  241. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  242. wrap.layer?.cornerRadius = 10
  243. wrap.layer?.borderWidth = 1
  244. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  245. if #available(macOS 11.0, *) {
  246. wrap.layer?.cornerCurve = .continuous
  247. }
  248. wrap.addSubview(field)
  249. NSLayoutConstraint.activate([
  250. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  251. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  252. field.centerYAnchor.constraint(equalTo: wrap.centerYAnchor),
  253. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  254. ])
  255. return wrap
  256. }
  257. private func careerSummaryBlock() -> NSView {
  258. let label = NSTextField(labelWithString: "Career Summary")
  259. label.font = .systemFont(ofSize: 12, weight: .medium)
  260. label.textColor = ProfilePagePalette.secondaryText
  261. label.alignment = .left
  262. label.translatesAutoresizingMaskIntoConstraints = false
  263. careerField.translatesAutoresizingMaskIntoConstraints = false
  264. careerField.isEditable = true
  265. careerField.isSelectable = true
  266. careerField.isBordered = false
  267. careerField.drawsBackground = false
  268. careerField.focusRingType = .none
  269. careerField.font = .systemFont(ofSize: 14, weight: .regular)
  270. careerField.textColor = ProfilePagePalette.primaryText
  271. careerField.maximumNumberOfLines = 0
  272. careerField.cell?.wraps = true
  273. careerField.cell?.isScrollable = false
  274. careerField.cell?.usesSingleLineMode = false
  275. careerField.stringValue = ""
  276. careerField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  277. careerField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  278. careerField.baseWritingDirection = .leftToRight
  279. careerField.alignment = .left
  280. careerField.placeholderAttributedString = NSAttributedString(
  281. string: "Brief overview of your professional background and key achievements...",
  282. attributes: [
  283. .foregroundColor: ProfilePagePalette.secondaryText,
  284. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  285. ]
  286. )
  287. let wrap = NSView()
  288. wrap.translatesAutoresizingMaskIntoConstraints = false
  289. wrap.wantsLayer = true
  290. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  291. wrap.layer?.cornerRadius = 10
  292. wrap.layer?.borderWidth = 1
  293. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  294. if #available(macOS 11.0, *) {
  295. wrap.layer?.cornerCurve = .continuous
  296. }
  297. wrap.addSubview(careerField)
  298. NSLayoutConstraint.activate([
  299. careerField.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  300. careerField.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  301. careerField.topAnchor.constraint(equalTo: wrap.topAnchor, constant: 10),
  302. careerField.bottomAnchor.constraint(equalTo: wrap.bottomAnchor, constant: -10),
  303. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: 120)
  304. ])
  305. let stack = NSStackView(views: [label, wrap])
  306. stack.orientation = .vertical
  307. stack.spacing = 8
  308. stack.alignment = .width
  309. stack.translatesAutoresizingMaskIntoConstraints = false
  310. stack.userInterfaceLayoutDirection = .leftToRight
  311. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  312. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  313. return stack
  314. }
  315. private func profileImageBlock() -> NSView {
  316. let title = NSTextField(labelWithString: "Profile Image (Optional)")
  317. title.font = .systemFont(ofSize: 12, weight: .medium)
  318. title.textColor = ProfilePagePalette.secondaryText
  319. title.alignment = .left
  320. title.translatesAutoresizingMaskIntoConstraints = false
  321. let avatarHost = NSView()
  322. avatarHost.translatesAutoresizingMaskIntoConstraints = false
  323. avatarHost.wantsLayer = true
  324. avatarHost.layer?.backgroundColor = ProfilePagePalette.avatarWell.cgColor
  325. avatarHost.layer?.cornerRadius = 36
  326. avatarHost.layer?.masksToBounds = true
  327. avatarImageView.translatesAutoresizingMaskIntoConstraints = false
  328. avatarImageView.imageScaling = .scaleProportionallyUpOrDown
  329. avatarImageView.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 36, weight: .light)
  330. avatarImageView.image = NSImage(systemSymbolName: "person.crop.circle.fill", accessibilityDescription: nil)
  331. avatarImageView.contentTintColor = ProfilePagePalette.secondaryText
  332. avatarHost.addSubview(avatarImageView)
  333. NSLayoutConstraint.activate([
  334. avatarHost.widthAnchor.constraint(equalToConstant: 72),
  335. avatarHost.heightAnchor.constraint(equalToConstant: 72),
  336. avatarImageView.centerXAnchor.constraint(equalTo: avatarHost.centerXAnchor),
  337. avatarImageView.centerYAnchor.constraint(equalTo: avatarHost.centerYAnchor),
  338. avatarImageView.widthAnchor.constraint(equalToConstant: 44),
  339. avatarImageView.heightAnchor.constraint(equalToConstant: 44)
  340. ])
  341. uploadPhotoButton.translatesAutoresizingMaskIntoConstraints = false
  342. uploadPhotoButton.bezelStyle = .rounded
  343. uploadPhotoButton.controlSize = .large
  344. uploadPhotoButton.font = .systemFont(ofSize: 13, weight: .medium)
  345. if let image = NSImage(systemSymbolName: "arrow.up.circle", accessibilityDescription: nil) {
  346. uploadPhotoButton.image = image
  347. uploadPhotoButton.imagePosition = .imageLeading
  348. uploadPhotoButton.contentTintColor = ProfilePagePalette.brandBlue
  349. }
  350. let hint = NSTextField(wrappingLabelWithString: "Recommended: Square image, max 2MB")
  351. hint.font = .systemFont(ofSize: 11, weight: .regular)
  352. hint.textColor = ProfilePagePalette.secondaryText
  353. hint.alignment = .left
  354. hint.maximumNumberOfLines = 0
  355. let rightColumn = NSStackView(views: [uploadPhotoButton, hint])
  356. rightColumn.orientation = .vertical
  357. rightColumn.alignment = .leading
  358. rightColumn.spacing = 6
  359. rightColumn.translatesAutoresizingMaskIntoConstraints = false
  360. let row = NSStackView(views: [avatarHost, rightColumn])
  361. row.orientation = .horizontal
  362. row.alignment = .centerY
  363. row.spacing = 16
  364. row.translatesAutoresizingMaskIntoConstraints = false
  365. row.userInterfaceLayoutDirection = .leftToRight
  366. let stack = NSStackView(views: [title, row])
  367. stack.orientation = .vertical
  368. stack.spacing = 12
  369. stack.alignment = .width
  370. stack.translatesAutoresizingMaskIntoConstraints = false
  371. stack.userInterfaceLayoutDirection = .leftToRight
  372. row.setContentHuggingPriority(.defaultLow, for: .horizontal)
  373. return stack
  374. }
  375. private func saveButtonHost() -> NSView {
  376. saveButton.translatesAutoresizingMaskIntoConstraints = false
  377. let host = NSView()
  378. host.translatesAutoresizingMaskIntoConstraints = false
  379. host.userInterfaceLayoutDirection = .leftToRight
  380. host.addSubview(saveButton)
  381. NSLayoutConstraint.activate([
  382. saveButton.leadingAnchor.constraint(equalTo: host.leadingAnchor),
  383. saveButton.topAnchor.constraint(equalTo: host.topAnchor),
  384. saveButton.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  385. saveButton.heightAnchor.constraint(equalToConstant: 48),
  386. saveButton.trailingAnchor.constraint(lessThanOrEqualTo: host.trailingAnchor)
  387. ])
  388. return host
  389. }
  390. @objc private func didTapUploadPhoto() {
  391. let panel = NSOpenPanel()
  392. panel.allowedContentTypes = [UTType.image]
  393. panel.allowsMultipleSelection = false
  394. panel.canChooseDirectories = false
  395. guard let window else { return }
  396. panel.beginSheetModal(for: window) { [weak self] response in
  397. guard response == .OK, let url = panel.url else { return }
  398. if let image = NSImage(contentsOf: url) {
  399. self?.avatarImageView.image = image
  400. self?.avatarImageView.contentTintColor = nil
  401. self?.avatarImageView.imageScaling = .scaleAxesIndependently
  402. }
  403. }
  404. }
  405. @objc private func didTapSave() {
  406. // UI shell only; wire persistence when profiles are stored.
  407. }
  408. }
  409. // MARK: - Primary CTA
  410. private final class ProfilePrimaryButton: NSButton {
  411. private var trackingArea: NSTrackingArea?
  412. private var didPushCursor = false
  413. override init(frame frameRect: NSRect) {
  414. super.init(frame: frameRect)
  415. commonInit()
  416. }
  417. required init?(coder: NSCoder) {
  418. super.init(coder: coder)
  419. commonInit()
  420. }
  421. convenience init(title: String, target: AnyObject?, action: Selector?) {
  422. self.init(frame: .zero)
  423. self.title = title
  424. self.target = target
  425. self.action = action
  426. }
  427. private func commonInit() {
  428. bezelStyle = .rounded
  429. isBordered = false
  430. font = .systemFont(ofSize: 15, weight: .semibold)
  431. contentTintColor = .white
  432. wantsLayer = true
  433. layer?.cornerRadius = 12
  434. if #available(macOS 11.0, *) {
  435. layer?.cornerCurve = .continuous
  436. }
  437. layer?.backgroundColor = ProfilePagePalette.brandBlue.cgColor
  438. }
  439. override func updateTrackingAreas() {
  440. super.updateTrackingAreas()
  441. if let trackingArea { removeTrackingArea(trackingArea) }
  442. let area = NSTrackingArea(
  443. rect: bounds,
  444. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  445. owner: self,
  446. userInfo: nil
  447. )
  448. addTrackingArea(area)
  449. trackingArea = area
  450. }
  451. override func mouseEntered(with event: NSEvent) {
  452. super.mouseEntered(with: event)
  453. layer?.backgroundColor = ProfilePagePalette.brandBlueHover.cgColor
  454. if !didPushCursor {
  455. NSCursor.pointingHand.push()
  456. didPushCursor = true
  457. }
  458. }
  459. override func mouseExited(with event: NSEvent) {
  460. super.mouseExited(with: event)
  461. layer?.backgroundColor = ProfilePagePalette.brandBlue.cgColor
  462. if didPushCursor {
  463. NSCursor.pop()
  464. didPushCursor = false
  465. }
  466. }
  467. override func viewWillMove(toWindow newWindow: NSWindow?) {
  468. super.viewWillMove(toWindow: newWindow)
  469. if newWindow == nil, didPushCursor {
  470. NSCursor.pop()
  471. didPushCursor = false
  472. }
  473. }
  474. }