Без опису

MyProfilePageView.swift 59KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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. private enum ProfilePagePalette {
  10. static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  11. static let brandBlueHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  12. static let pageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  13. static let cardBackground = NSColor(srgbRed: 252 / 255, green: 252 / 255, blue: 252 / 255, alpha: 1)
  14. static let fieldFill = NSColor(srgbRed: 247 / 255, green: 247 / 255, blue: 247 / 255, alpha: 1)
  15. static let primaryText = NSColor(srgbRed: 45 / 255, green: 45 / 255, blue: 45 / 255, alpha: 1)
  16. static let secondaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  17. static let border = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  18. static let destructive = NSColor(srgbRed: 220 / 255, green: 38 / 255, blue: 38 / 255, alpha: 1)
  19. }
  20. /// Keeps profile text left-aligned and LTR so fields do not collapse to a narrow trailing strip under RTL / natural alignment.
  21. private enum ProfileLayoutEnforcement {
  22. static func applyForcedLTRSubtree(from root: NSView) {
  23. var stack: [NSView] = [root]
  24. while let view = stack.popLast() {
  25. applyForcedLTR(to: view)
  26. stack.append(contentsOf: view.subviews)
  27. }
  28. }
  29. static func applyForcedLTR(to view: NSView) {
  30. view.userInterfaceLayoutDirection = .leftToRight
  31. }
  32. static func applyLeftAlignedTextField(_ field: NSTextField) {
  33. applyForcedLTR(to: field)
  34. field.baseWritingDirection = .leftToRight
  35. field.alignment = .left
  36. if let cell = field.cell as? NSTextFieldCell {
  37. cell.alignment = .left
  38. cell.baseWritingDirection = .leftToRight
  39. }
  40. }
  41. static func leftAlignedParagraphStyle() -> NSParagraphStyle {
  42. let p = NSMutableParagraphStyle()
  43. p.alignment = .left
  44. p.baseWritingDirection = .leftToRight
  45. return p
  46. }
  47. }
  48. private extension NSStackView {
  49. /// For vertical stacks using `.leading` alignment (geometric left under mixed RTL), pin each arranged subview’s width to the stack so labels/fields stay full-width.
  50. func pinAllArrangedSubviewWidthsEqualToStackWidth() {
  51. for subview in arrangedSubviews {
  52. subview.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
  53. }
  54. }
  55. }
  56. /// Two fields side‑by‑side with a true 50/50 split, or stacked full‑width when compact. Avoids `NSStackView` collapsing paired columns to a narrow strip on the trailing edge.
  57. private final class ProfileDualFieldRow: NSView {
  58. private let leftView: NSView
  59. private let rightView: NSView
  60. private let spacing: CGFloat
  61. private var horizontalConstraints: [NSLayoutConstraint] = []
  62. private var verticalConstraints: [NSLayoutConstraint] = []
  63. private var isCompact = false
  64. init(left: NSView, right: NSView, spacing: CGFloat = 12) {
  65. self.leftView = left
  66. self.rightView = right
  67. self.spacing = spacing
  68. super.init(frame: .zero)
  69. translatesAutoresizingMaskIntoConstraints = false
  70. ProfileLayoutEnforcement.applyForcedLTR(to: self)
  71. addSubview(leftView)
  72. addSubview(rightView)
  73. leftView.translatesAutoresizingMaskIntoConstraints = false
  74. rightView.translatesAutoresizingMaskIntoConstraints = false
  75. leftView.setContentHuggingPriority(.defaultLow, for: .horizontal)
  76. rightView.setContentHuggingPriority(.defaultLow, for: .horizontal)
  77. leftView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  78. rightView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  79. horizontalConstraints = [
  80. leftView.leftAnchor.constraint(equalTo: leftAnchor),
  81. leftView.topAnchor.constraint(equalTo: topAnchor),
  82. leftView.bottomAnchor.constraint(equalTo: bottomAnchor),
  83. rightView.rightAnchor.constraint(equalTo: rightAnchor),
  84. rightView.topAnchor.constraint(equalTo: topAnchor),
  85. rightView.bottomAnchor.constraint(equalTo: bottomAnchor),
  86. leftView.rightAnchor.constraint(equalTo: rightView.leftAnchor, constant: -spacing),
  87. leftView.widthAnchor.constraint(equalTo: rightView.widthAnchor)
  88. ]
  89. verticalConstraints = [
  90. leftView.leftAnchor.constraint(equalTo: leftAnchor),
  91. leftView.rightAnchor.constraint(equalTo: rightAnchor),
  92. leftView.topAnchor.constraint(equalTo: topAnchor),
  93. rightView.leftAnchor.constraint(equalTo: leftAnchor),
  94. rightView.rightAnchor.constraint(equalTo: rightAnchor),
  95. rightView.topAnchor.constraint(equalTo: leftView.bottomAnchor, constant: spacing),
  96. rightView.bottomAnchor.constraint(equalTo: bottomAnchor)
  97. ]
  98. NSLayoutConstraint.activate(horizontalConstraints)
  99. }
  100. required init?(coder: NSCoder) {
  101. fatalError("init(coder:) has not been implemented")
  102. }
  103. func setCompact(_ compact: Bool) {
  104. guard compact != isCompact else { return }
  105. isCompact = compact
  106. if compact {
  107. NSLayoutConstraint.deactivate(horizontalConstraints)
  108. NSLayoutConstraint.activate(verticalConstraints)
  109. } else {
  110. NSLayoutConstraint.deactivate(verticalConstraints)
  111. NSLayoutConstraint.activate(horizontalConstraints)
  112. }
  113. }
  114. }
  115. final class MyProfilePageView: NSView {
  116. /// Below this form content width, two-column rows stack vertically.
  117. private static let compactFormWidth: CGFloat = 640
  118. private static let horizontalPageInset: CGFloat = 24
  119. private let scrollView = NSScrollView()
  120. private let documentView = NSView()
  121. private let cardView = NSView()
  122. private let formStack = NSStackView()
  123. private let profileNameField = NSTextField()
  124. private let fullNameField = NSTextField()
  125. private let emailField = NSTextField()
  126. private let phoneField = NSTextField()
  127. private let jobTitleField = NSTextField()
  128. private let addressField = NSTextField()
  129. private let careerField = NSTextField()
  130. private let certificatesField = NSTextField()
  131. private let interestsField = NSTextField()
  132. private let languagesField = NSTextField()
  133. private let referralField = NSTextField()
  134. private let saveButton = ProfilePrimaryButton(title: "Save Profile →", target: nil, action: nil)
  135. private var nameEmailRow: ProfileDualFieldRow!
  136. private var phoneJobRow: ProfileDualFieldRow!
  137. private let workExperienceRowsStack = NSStackView()
  138. private var workExperienceEntries: [WorkExperienceEntryView] = []
  139. private let educationRowsStack = NSStackView()
  140. private var educationEntries: [EducationEntryView] = []
  141. private var lastCompactLayout: Bool?
  142. private var referralHelperLabel: NSTextField?
  143. /// Force left-to-right geometry so profile fields span the full width even when the window uses RTL layout.
  144. override var userInterfaceLayoutDirection: NSUserInterfaceLayoutDirection {
  145. get { .leftToRight }
  146. set { super.userInterfaceLayoutDirection = .leftToRight }
  147. }
  148. override init(frame frameRect: NSRect) {
  149. super.init(frame: frameRect)
  150. setup()
  151. }
  152. required init?(coder: NSCoder) {
  153. super.init(coder: coder)
  154. setup()
  155. }
  156. override func viewDidMoveToWindow() {
  157. super.viewDidMoveToWindow()
  158. guard window != nil else { return }
  159. ProfileLayoutEnforcement.applyForcedLTRSubtree(from: self)
  160. needsLayout = true
  161. }
  162. override func layout() {
  163. super.layout()
  164. if let layer = cardView.layer, layer.shadowOpacity > 0 {
  165. let r = layer.cornerRadius
  166. layer.shadowPath = CGPath(roundedRect: cardView.bounds, cornerWidth: r, cornerHeight: r, transform: nil)
  167. }
  168. updateMultilinePreferredLayoutWidths()
  169. applyResponsiveRowsIfNeeded()
  170. }
  171. /// Wrapping `NSTextField`s report a tiny intrinsic width until `preferredMaxLayoutWidth` tracks the chrome width, which otherwise collapses the stack to a narrow trailing column.
  172. private func updateMultilinePreferredLayoutWidths() {
  173. let horizontalInset: CGFloat = 24
  174. applyPreferredWrapWidth(to: profileNameField, horizontalInset: horizontalInset)
  175. applyPreferredWrapWidth(to: fullNameField, horizontalInset: horizontalInset)
  176. applyPreferredWrapWidth(to: emailField, horizontalInset: horizontalInset)
  177. applyPreferredWrapWidth(to: phoneField, horizontalInset: horizontalInset)
  178. applyPreferredWrapWidth(to: jobTitleField, horizontalInset: horizontalInset)
  179. applyPreferredWrapWidth(to: addressField, horizontalInset: horizontalInset)
  180. applyPreferredWrapWidth(to: referralField, horizontalInset: horizontalInset)
  181. applyPreferredWrapWidth(to: careerField, horizontalInset: horizontalInset)
  182. applyPreferredWrapWidth(to: certificatesField, horizontalInset: horizontalInset)
  183. applyPreferredWrapWidth(to: interestsField, horizontalInset: horizontalInset)
  184. applyPreferredWrapWidth(to: languagesField, horizontalInset: horizontalInset)
  185. if let helper = referralHelperLabel, let stack = helper.superview, stack.bounds.width > 2 {
  186. let w = max(1, stack.bounds.width - 8)
  187. if abs(helper.preferredMaxLayoutWidth - w) > 0.5 {
  188. helper.preferredMaxLayoutWidth = w
  189. }
  190. }
  191. }
  192. private func applyPreferredWrapWidth(to field: NSTextField, horizontalInset: CGFloat) {
  193. guard let wrap = field.superview, wrap.bounds.width > 2 else { return }
  194. let w = max(1, wrap.bounds.width - horizontalInset)
  195. if abs(field.preferredMaxLayoutWidth - w) > 0.5 {
  196. field.preferredMaxLayoutWidth = w
  197. }
  198. }
  199. private func setup() {
  200. wantsLayer = true
  201. layer?.backgroundColor = ProfilePagePalette.pageBackground.cgColor
  202. userInterfaceLayoutDirection = .leftToRight
  203. ProfileLayoutEnforcement.applyForcedLTR(to: self)
  204. scrollView.translatesAutoresizingMaskIntoConstraints = false
  205. scrollView.userInterfaceLayoutDirection = .leftToRight
  206. ProfileLayoutEnforcement.applyForcedLTR(to: scrollView)
  207. scrollView.hasVerticalScroller = true
  208. scrollView.hasHorizontalScroller = false
  209. scrollView.autohidesScrollers = true
  210. scrollView.drawsBackground = false
  211. scrollView.borderType = .noBorder
  212. scrollView.scrollerStyle = .overlay
  213. scrollView.automaticallyAdjustsContentInsets = false
  214. scrollView.contentView.userInterfaceLayoutDirection = .leftToRight
  215. ProfileLayoutEnforcement.applyForcedLTR(to: scrollView.contentView)
  216. if #available(macOS 10.11, *) {
  217. scrollView.horizontalScrollElasticity = .none
  218. }
  219. documentView.translatesAutoresizingMaskIntoConstraints = false
  220. documentView.userInterfaceLayoutDirection = .leftToRight
  221. ProfileLayoutEnforcement.applyForcedLTR(to: documentView)
  222. cardView.translatesAutoresizingMaskIntoConstraints = false
  223. cardView.wantsLayer = true
  224. cardView.layer?.backgroundColor = ProfilePagePalette.cardBackground.cgColor
  225. cardView.layer?.cornerRadius = 18
  226. cardView.layer?.borderWidth = 1
  227. cardView.layer?.borderColor = ProfilePagePalette.border.cgColor
  228. cardView.layer?.masksToBounds = false
  229. cardView.layer?.shadowColor = NSColor.black.cgColor
  230. cardView.layer?.shadowOpacity = 0.06
  231. cardView.layer?.shadowRadius = 20
  232. cardView.layer?.shadowOffset = CGSize(width: 0, height: 10)
  233. cardView.userInterfaceLayoutDirection = .leftToRight
  234. ProfileLayoutEnforcement.applyForcedLTR(to: cardView)
  235. if #available(macOS 11.0, *) {
  236. cardView.layer?.cornerCurve = .continuous
  237. }
  238. formStack.translatesAutoresizingMaskIntoConstraints = false
  239. formStack.orientation = .vertical
  240. formStack.alignment = .leading
  241. formStack.distribution = .fill
  242. formStack.spacing = 24
  243. formStack.edgeInsets = NSEdgeInsets(top: 32, left: 28, bottom: 32, right: 28)
  244. formStack.userInterfaceLayoutDirection = .leftToRight
  245. ProfileLayoutEnforcement.applyForcedLTR(to: formStack)
  246. formStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  247. formStack.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  248. addSubview(scrollView)
  249. scrollView.documentView = documentView
  250. documentView.addSubview(cardView)
  251. cardView.addSubview(formStack)
  252. NSLayoutConstraint.activate([
  253. scrollView.leftAnchor.constraint(equalTo: leftAnchor),
  254. scrollView.rightAnchor.constraint(equalTo: rightAnchor),
  255. scrollView.topAnchor.constraint(equalTo: topAnchor),
  256. scrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
  257. // Pin the document to the clip view’s geometric width so LTR/RTL semantics cannot slide the form.
  258. documentView.leftAnchor.constraint(equalTo: scrollView.contentView.leftAnchor),
  259. documentView.rightAnchor.constraint(equalTo: scrollView.contentView.rightAnchor),
  260. documentView.topAnchor.constraint(equalTo: scrollView.contentView.topAnchor),
  261. documentView.bottomAnchor.constraint(equalTo: cardView.bottomAnchor, constant: Self.horizontalPageInset),
  262. // Pin both edges so the card always spans the clip width minus insets; a separate width equal to a large constant can conflict with the clip and slide the card to the trailing edge.
  263. cardView.leftAnchor.constraint(equalTo: documentView.leftAnchor, constant: Self.horizontalPageInset),
  264. cardView.rightAnchor.constraint(equalTo: documentView.rightAnchor, constant: -Self.horizontalPageInset),
  265. cardView.topAnchor.constraint(equalTo: documentView.topAnchor, constant: Self.horizontalPageInset),
  266. formStack.leftAnchor.constraint(equalTo: cardView.leftAnchor),
  267. formStack.rightAnchor.constraint(equalTo: cardView.rightAnchor),
  268. formStack.widthAnchor.constraint(equalTo: cardView.widthAnchor),
  269. formStack.topAnchor.constraint(equalTo: cardView.topAnchor),
  270. formStack.bottomAnchor.constraint(equalTo: cardView.bottomAnchor)
  271. ])
  272. addFullWidthArrangedSubview(
  273. labeledGroup(title: "Profile Name *", field: profileNameField, placeholder: "Marketing Director Profile")
  274. )
  275. addFullWidthArrangedSubview(sectionHeading("Personal Information"))
  276. let nameGroup = labeledGroup(title: "Full Name *", field: fullNameField, placeholder: "John Doe")
  277. let emailGroup = labeledGroup(title: "Email *", field: emailField, placeholder: "john@example.com")
  278. nameEmailRow = ProfileDualFieldRow(left: nameGroup, right: emailGroup, spacing: 12)
  279. addFullWidthArrangedSubview(nameEmailRow)
  280. let phoneGroup = labeledGroup(title: "Phone", field: phoneField, placeholder: "+1 (555) 123-4567")
  281. let jobGroup = labeledGroup(title: "Job Title *", field: jobTitleField, placeholder: "Software Engineer")
  282. phoneJobRow = ProfileDualFieldRow(left: phoneGroup, right: jobGroup, spacing: 12)
  283. addFullWidthArrangedSubview(phoneJobRow)
  284. addFullWidthArrangedSubview(
  285. labeledGroup(title: "Address", field: addressField, placeholder: "123 Main St, City, State, ZIP")
  286. )
  287. addFullWidthArrangedSubview(careerSummaryBlock())
  288. addFullWidthArrangedSubview(horizontalSeparator())
  289. addFullWidthArrangedSubview(workExperienceSection())
  290. addFullWidthArrangedSubview(horizontalSeparator())
  291. addFullWidthArrangedSubview(educationSection())
  292. addFullWidthArrangedSubview(horizontalSeparator())
  293. addFullWidthArrangedSubview(
  294. multilineProfileBlock(
  295. title: "Certificates / Rewards",
  296. placeholder: "List your certificates and awards...",
  297. field: certificatesField,
  298. minHeight: 100
  299. )
  300. )
  301. addFullWidthArrangedSubview(horizontalSeparator())
  302. addFullWidthArrangedSubview(
  303. multilineProfileBlock(
  304. title: "Interests",
  305. placeholder: "List your interests and hobbies...",
  306. field: interestsField,
  307. minHeight: 100
  308. )
  309. )
  310. addFullWidthArrangedSubview(horizontalSeparator())
  311. addFullWidthArrangedSubview(
  312. multilineProfileBlock(
  313. title: "Languages",
  314. placeholder: "List languages you speak (e.g., English - Native, Spanish - Fluent)...",
  315. field: languagesField,
  316. minHeight: 100
  317. )
  318. )
  319. addFullWidthArrangedSubview(horizontalSeparator())
  320. addFullWidthArrangedSubview(referralBlock())
  321. addFullWidthArrangedSubview(saveButtonHost())
  322. saveButton.target = self
  323. saveButton.action = #selector(didTapSave)
  324. appendWorkExperienceEntry()
  325. appendEducationEntry()
  326. ProfileLayoutEnforcement.applyForcedLTRSubtree(from: self)
  327. }
  328. private func applyResponsiveRowsIfNeeded() {
  329. let w = cardView.bounds.width
  330. guard w > 1 else { return }
  331. let formWidth = max(0, w - formStack.edgeInsets.left - formStack.edgeInsets.right)
  332. let compact = formWidth < Self.compactFormWidth
  333. guard compact != lastCompactLayout else { return }
  334. lastCompactLayout = compact
  335. nameEmailRow.setCompact(compact)
  336. phoneJobRow.setCompact(compact)
  337. for entry in workExperienceEntries {
  338. entry.applyCompactLayout(compact)
  339. }
  340. for entry in educationEntries {
  341. entry.applyCompactLayout(compact)
  342. }
  343. }
  344. private func addFullWidthArrangedSubview(_ view: NSView) {
  345. formStack.addArrangedSubview(view)
  346. view.setContentHuggingPriority(.defaultLow, for: .horizontal)
  347. view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  348. view.widthAnchor.constraint(equalTo: formStack.widthAnchor).isActive = true
  349. }
  350. private func sectionHeading(_ text: String) -> NSView {
  351. let label = NSTextField(labelWithString: text)
  352. label.font = .systemFont(ofSize: 15, weight: .semibold)
  353. label.textColor = ProfilePagePalette.primaryText
  354. label.baseWritingDirection = .leftToRight
  355. label.translatesAutoresizingMaskIntoConstraints = false
  356. label.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  357. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  358. let spacer = NSView()
  359. spacer.translatesAutoresizingMaskIntoConstraints = false
  360. spacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  361. let row = NSStackView(views: [label, spacer])
  362. row.orientation = .horizontal
  363. row.alignment = .centerY
  364. row.distribution = .fill
  365. row.spacing = 0
  366. row.userInterfaceLayoutDirection = .leftToRight
  367. ProfileLayoutEnforcement.applyForcedLTR(to: row)
  368. row.translatesAutoresizingMaskIntoConstraints = false
  369. NSLayoutConstraint.activate([
  370. label.leftAnchor.constraint(equalTo: row.leftAnchor)
  371. ])
  372. return row
  373. }
  374. private func labeledGroup(title: String, field: NSTextField, placeholder: String) -> NSView {
  375. let label = NSTextField(labelWithString: title)
  376. label.font = .systemFont(ofSize: 12, weight: .medium)
  377. label.textColor = ProfilePagePalette.secondaryText
  378. label.translatesAutoresizingMaskIntoConstraints = false
  379. label.setContentHuggingPriority(.defaultLow, for: .horizontal)
  380. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  381. styleSingleLineField(field, placeholder: placeholder)
  382. let wrap = roundedFieldChrome(containing: field, minHeight: 40)
  383. let stack = NSStackView(views: [label, wrap])
  384. stack.orientation = .vertical
  385. stack.spacing = 8
  386. // `.leading` keeps rows on the geometric left; explicit widths keep labels/fields full-width (`.width` alone can still hug the trailing edge under RTL-style layout).
  387. stack.alignment = .leading
  388. stack.translatesAutoresizingMaskIntoConstraints = false
  389. stack.userInterfaceLayoutDirection = .leftToRight
  390. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  391. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  392. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  393. wrap.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  394. NSLayoutConstraint.activate([
  395. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  396. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  397. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  398. ])
  399. return stack
  400. }
  401. private func styleSingleLineField(_ field: NSTextField, placeholder: String) {
  402. field.translatesAutoresizingMaskIntoConstraints = false
  403. field.isBordered = false
  404. field.drawsBackground = false
  405. field.focusRingType = .none
  406. field.font = .systemFont(ofSize: 14, weight: .regular)
  407. field.textColor = ProfilePagePalette.primaryText
  408. field.setContentHuggingPriority(.defaultLow, for: .horizontal)
  409. field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  410. let paragraph = ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  411. field.placeholderAttributedString = NSAttributedString(
  412. string: placeholder,
  413. attributes: [
  414. .foregroundColor: ProfilePagePalette.secondaryText,
  415. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  416. .paragraphStyle: paragraph
  417. ]
  418. )
  419. field.cell?.usesSingleLineMode = true
  420. field.cell?.wraps = false
  421. field.cell?.isScrollable = true
  422. ProfileLayoutEnforcement.applyLeftAlignedTextField(field)
  423. }
  424. private func roundedFieldChrome(containing field: NSTextField, minHeight: CGFloat) -> NSView {
  425. let wrap = NSView()
  426. wrap.translatesAutoresizingMaskIntoConstraints = false
  427. wrap.wantsLayer = true
  428. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  429. wrap.layer?.cornerRadius = 10
  430. wrap.layer?.borderWidth = 1
  431. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  432. if #available(macOS 11.0, *) {
  433. wrap.layer?.cornerCurve = .continuous
  434. }
  435. wrap.addSubview(field)
  436. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  437. NSLayoutConstraint.activate([
  438. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  439. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  440. field.centerYAnchor.constraint(equalTo: wrap.centerYAnchor),
  441. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  442. ])
  443. return wrap
  444. }
  445. private func careerSummaryBlock() -> NSView {
  446. let label = NSTextField(labelWithString: "Career Summary")
  447. label.font = .systemFont(ofSize: 12, weight: .medium)
  448. label.textColor = ProfilePagePalette.secondaryText
  449. label.translatesAutoresizingMaskIntoConstraints = false
  450. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  451. careerField.translatesAutoresizingMaskIntoConstraints = false
  452. careerField.isEditable = true
  453. careerField.isSelectable = true
  454. careerField.isBordered = false
  455. careerField.drawsBackground = false
  456. careerField.focusRingType = .none
  457. careerField.font = .systemFont(ofSize: 14, weight: .regular)
  458. careerField.textColor = ProfilePagePalette.primaryText
  459. careerField.maximumNumberOfLines = 0
  460. careerField.cell?.wraps = true
  461. careerField.cell?.isScrollable = false
  462. careerField.cell?.usesSingleLineMode = false
  463. careerField.stringValue = ""
  464. careerField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  465. careerField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  466. careerField.placeholderAttributedString = NSAttributedString(
  467. string: "Brief overview of your professional background and key achievements...",
  468. attributes: [
  469. .foregroundColor: ProfilePagePalette.secondaryText,
  470. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  471. .paragraphStyle: ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  472. ]
  473. )
  474. ProfileLayoutEnforcement.applyLeftAlignedTextField(careerField)
  475. let wrap = NSView()
  476. wrap.translatesAutoresizingMaskIntoConstraints = false
  477. wrap.wantsLayer = true
  478. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  479. wrap.layer?.cornerRadius = 10
  480. wrap.layer?.borderWidth = 1
  481. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  482. if #available(macOS 11.0, *) {
  483. wrap.layer?.cornerCurve = .continuous
  484. }
  485. wrap.addSubview(careerField)
  486. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  487. NSLayoutConstraint.activate([
  488. careerField.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  489. careerField.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  490. careerField.topAnchor.constraint(equalTo: wrap.topAnchor, constant: 10),
  491. careerField.bottomAnchor.constraint(equalTo: wrap.bottomAnchor, constant: -10),
  492. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: 168)
  493. ])
  494. let stack = NSStackView(views: [label, wrap])
  495. stack.orientation = .vertical
  496. stack.spacing = 8
  497. stack.alignment = .leading
  498. stack.translatesAutoresizingMaskIntoConstraints = false
  499. stack.userInterfaceLayoutDirection = .leftToRight
  500. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  501. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  502. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  503. NSLayoutConstraint.activate([
  504. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  505. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  506. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  507. ])
  508. return stack
  509. }
  510. private func multilineProfileBlock(title: String, placeholder: String, field: NSTextField, minHeight: CGFloat) -> NSView {
  511. let label = NSTextField(labelWithString: title)
  512. label.font = .systemFont(ofSize: 12, weight: .medium)
  513. label.textColor = ProfilePagePalette.secondaryText
  514. label.translatesAutoresizingMaskIntoConstraints = false
  515. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  516. field.translatesAutoresizingMaskIntoConstraints = false
  517. field.isEditable = true
  518. field.isSelectable = true
  519. field.isBordered = false
  520. field.drawsBackground = false
  521. field.focusRingType = .none
  522. field.font = .systemFont(ofSize: 14, weight: .regular)
  523. field.textColor = ProfilePagePalette.primaryText
  524. field.maximumNumberOfLines = 0
  525. field.cell?.wraps = true
  526. field.cell?.isScrollable = false
  527. field.cell?.usesSingleLineMode = false
  528. field.stringValue = ""
  529. field.setContentHuggingPriority(.defaultLow, for: .horizontal)
  530. field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  531. field.placeholderAttributedString = NSAttributedString(
  532. string: placeholder,
  533. attributes: [
  534. .foregroundColor: ProfilePagePalette.secondaryText,
  535. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  536. .paragraphStyle: ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  537. ]
  538. )
  539. ProfileLayoutEnforcement.applyLeftAlignedTextField(field)
  540. let wrap = NSView()
  541. wrap.translatesAutoresizingMaskIntoConstraints = false
  542. wrap.wantsLayer = true
  543. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  544. wrap.layer?.cornerRadius = 10
  545. wrap.layer?.borderWidth = 1
  546. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  547. if #available(macOS 11.0, *) {
  548. wrap.layer?.cornerCurve = .continuous
  549. }
  550. wrap.addSubview(field)
  551. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  552. NSLayoutConstraint.activate([
  553. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  554. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  555. field.topAnchor.constraint(equalTo: wrap.topAnchor, constant: 10),
  556. field.bottomAnchor.constraint(equalTo: wrap.bottomAnchor, constant: -10),
  557. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  558. ])
  559. let stack = NSStackView(views: [label, wrap])
  560. stack.orientation = .vertical
  561. stack.spacing = 8
  562. stack.alignment = .leading
  563. stack.translatesAutoresizingMaskIntoConstraints = false
  564. stack.userInterfaceLayoutDirection = .leftToRight
  565. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  566. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  567. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  568. NSLayoutConstraint.activate([
  569. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  570. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  571. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  572. ])
  573. return stack
  574. }
  575. private func referralBlock() -> NSView {
  576. let label = NSTextField(labelWithString: "Referral (Optional)")
  577. label.font = .systemFont(ofSize: 12, weight: .medium)
  578. label.textColor = ProfilePagePalette.secondaryText
  579. label.translatesAutoresizingMaskIntoConstraints = false
  580. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  581. styleSingleLineField(referralField, placeholder: "Referred by (Company/Person Name)")
  582. let wrap = roundedFieldChrome(containing: referralField, minHeight: 40)
  583. let helper = NSTextField(wrappingLabelWithString: "If someone referred you for this job, enter their name or company here")
  584. helper.font = .systemFont(ofSize: 11, weight: .regular)
  585. helper.textColor = ProfilePagePalette.secondaryText
  586. helper.translatesAutoresizingMaskIntoConstraints = false
  587. ProfileLayoutEnforcement.applyLeftAlignedTextField(helper)
  588. referralHelperLabel = helper
  589. let stack = NSStackView(views: [label, wrap, helper])
  590. stack.orientation = .vertical
  591. stack.spacing = 8
  592. stack.alignment = .leading
  593. stack.translatesAutoresizingMaskIntoConstraints = false
  594. stack.userInterfaceLayoutDirection = .leftToRight
  595. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  596. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  597. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  598. if #available(macOS 10.11, *) {
  599. stack.setCustomSpacing(6, after: wrap)
  600. }
  601. NSLayoutConstraint.activate([
  602. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  603. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  604. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor),
  605. helper.leftAnchor.constraint(equalTo: stack.leftAnchor),
  606. helper.widthAnchor.constraint(equalTo: stack.widthAnchor)
  607. ])
  608. return stack
  609. }
  610. private func horizontalSeparator() -> NSView {
  611. let box = NSBox()
  612. box.boxType = .separator
  613. box.translatesAutoresizingMaskIntoConstraints = false
  614. return box
  615. }
  616. private func workExperienceSection() -> NSView {
  617. let title = NSTextField(labelWithString: "Work Experience")
  618. title.font = .systemFont(ofSize: 15, weight: .semibold)
  619. title.textColor = ProfilePagePalette.primaryText
  620. title.translatesAutoresizingMaskIntoConstraints = false
  621. title.setContentHuggingPriority(.defaultLow, for: .horizontal)
  622. ProfileLayoutEnforcement.applyLeftAlignedTextField(title)
  623. let addButton = NSButton(title: "+ Add Another", target: self, action: #selector(didTapAddWorkExperience))
  624. addButton.translatesAutoresizingMaskIntoConstraints = false
  625. addButton.bezelStyle = .rounded
  626. addButton.isBordered = true
  627. addButton.font = .systemFont(ofSize: 12, weight: .medium)
  628. addButton.controlSize = .regular
  629. addButton.setContentHuggingPriority(.required, for: .horizontal)
  630. let spacer = NSView()
  631. spacer.translatesAutoresizingMaskIntoConstraints = false
  632. spacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  633. let headerRow = NSStackView(views: [title, spacer, addButton])
  634. headerRow.orientation = .horizontal
  635. headerRow.alignment = .centerY
  636. headerRow.distribution = .fill
  637. headerRow.spacing = 12
  638. headerRow.userInterfaceLayoutDirection = .leftToRight
  639. ProfileLayoutEnforcement.applyForcedLTR(to: headerRow)
  640. headerRow.translatesAutoresizingMaskIntoConstraints = false
  641. workExperienceRowsStack.translatesAutoresizingMaskIntoConstraints = false
  642. workExperienceRowsStack.orientation = .vertical
  643. workExperienceRowsStack.spacing = 20
  644. workExperienceRowsStack.alignment = .leading
  645. workExperienceRowsStack.userInterfaceLayoutDirection = .leftToRight
  646. ProfileLayoutEnforcement.applyForcedLTR(to: workExperienceRowsStack)
  647. let outer = NSStackView(views: [headerRow, workExperienceRowsStack])
  648. outer.orientation = .vertical
  649. outer.spacing = 16
  650. outer.alignment = .leading
  651. outer.translatesAutoresizingMaskIntoConstraints = false
  652. outer.userInterfaceLayoutDirection = .leftToRight
  653. ProfileLayoutEnforcement.applyForcedLTR(to: outer)
  654. outer.pinAllArrangedSubviewWidthsEqualToStackWidth()
  655. return outer
  656. }
  657. private func educationSection() -> NSView {
  658. let title = NSTextField(labelWithString: "Education")
  659. title.font = .systemFont(ofSize: 15, weight: .semibold)
  660. title.textColor = ProfilePagePalette.primaryText
  661. title.translatesAutoresizingMaskIntoConstraints = false
  662. title.setContentHuggingPriority(.defaultLow, for: .horizontal)
  663. ProfileLayoutEnforcement.applyLeftAlignedTextField(title)
  664. let addButton = NSButton(title: "+ Add Another", target: self, action: #selector(didTapAddEducation))
  665. addButton.translatesAutoresizingMaskIntoConstraints = false
  666. addButton.bezelStyle = .rounded
  667. addButton.isBordered = true
  668. addButton.font = .systemFont(ofSize: 12, weight: .medium)
  669. addButton.setContentHuggingPriority(.required, for: .horizontal)
  670. let spacer = NSView()
  671. spacer.translatesAutoresizingMaskIntoConstraints = false
  672. spacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  673. let headerRow = NSStackView(views: [title, spacer, addButton])
  674. headerRow.orientation = .horizontal
  675. headerRow.alignment = .centerY
  676. headerRow.distribution = .fill
  677. headerRow.spacing = 12
  678. headerRow.userInterfaceLayoutDirection = .leftToRight
  679. ProfileLayoutEnforcement.applyForcedLTR(to: headerRow)
  680. headerRow.translatesAutoresizingMaskIntoConstraints = false
  681. educationRowsStack.translatesAutoresizingMaskIntoConstraints = false
  682. educationRowsStack.orientation = .vertical
  683. educationRowsStack.spacing = 16
  684. educationRowsStack.alignment = .leading
  685. educationRowsStack.userInterfaceLayoutDirection = .leftToRight
  686. ProfileLayoutEnforcement.applyForcedLTR(to: educationRowsStack)
  687. let outer = NSStackView(views: [headerRow, educationRowsStack])
  688. outer.orientation = .vertical
  689. outer.spacing = 16
  690. outer.alignment = .leading
  691. outer.translatesAutoresizingMaskIntoConstraints = false
  692. outer.userInterfaceLayoutDirection = .leftToRight
  693. ProfileLayoutEnforcement.applyForcedLTR(to: outer)
  694. outer.pinAllArrangedSubviewWidthsEqualToStackWidth()
  695. return outer
  696. }
  697. private func appendWorkExperienceEntry() {
  698. let entry = WorkExperienceEntryView()
  699. entry.translatesAutoresizingMaskIntoConstraints = false
  700. if let compact = lastCompactLayout {
  701. entry.applyCompactLayout(compact)
  702. }
  703. entry.onDelete = { [weak self, weak entry] in
  704. guard let self, let entry else { return }
  705. self.removeWorkExperienceEntry(entry)
  706. }
  707. workExperienceEntries.append(entry)
  708. workExperienceRowsStack.addArrangedSubview(entry)
  709. entry.widthAnchor.constraint(equalTo: workExperienceRowsStack.widthAnchor).isActive = true
  710. renumberWorkExperienceEntries()
  711. refreshWorkExperienceDeleteButtons()
  712. }
  713. private func removeWorkExperienceEntry(_ entry: WorkExperienceEntryView) {
  714. guard workExperienceEntries.count > 1 else { return }
  715. workExperienceEntries.removeAll { $0 === entry }
  716. workExperienceRowsStack.removeArrangedSubview(entry)
  717. entry.removeFromSuperview()
  718. renumberWorkExperienceEntries()
  719. refreshWorkExperienceDeleteButtons()
  720. }
  721. private func renumberWorkExperienceEntries() {
  722. for (i, entry) in workExperienceEntries.enumerated() {
  723. entry.setExperienceIndex(i + 1)
  724. }
  725. }
  726. private func refreshWorkExperienceDeleteButtons() {
  727. let hide = workExperienceEntries.count <= 1
  728. for entry in workExperienceEntries {
  729. entry.setDeleteHidden(hide)
  730. }
  731. }
  732. private func appendEducationEntry() {
  733. let entry = EducationEntryView()
  734. entry.translatesAutoresizingMaskIntoConstraints = false
  735. if let compact = lastCompactLayout {
  736. entry.applyCompactLayout(compact)
  737. }
  738. entry.onDelete = { [weak self, weak entry] in
  739. guard let self, let entry else { return }
  740. self.removeEducationEntry(entry)
  741. }
  742. educationEntries.append(entry)
  743. educationRowsStack.addArrangedSubview(entry)
  744. entry.widthAnchor.constraint(equalTo: educationRowsStack.widthAnchor).isActive = true
  745. renumberEducationEntries()
  746. refreshEducationDeleteButtons()
  747. }
  748. private func removeEducationEntry(_ entry: EducationEntryView) {
  749. guard educationEntries.count > 1 else { return }
  750. educationEntries.removeAll { $0 === entry }
  751. educationRowsStack.removeArrangedSubview(entry)
  752. entry.removeFromSuperview()
  753. renumberEducationEntries()
  754. refreshEducationDeleteButtons()
  755. }
  756. private func renumberEducationEntries() {
  757. for (i, entry) in educationEntries.enumerated() {
  758. entry.setEducationIndex(i + 1)
  759. }
  760. }
  761. private func refreshEducationDeleteButtons() {
  762. let hide = educationEntries.count <= 1
  763. for entry in educationEntries {
  764. entry.setDeleteHidden(hide)
  765. }
  766. }
  767. @objc private func didTapAddWorkExperience() {
  768. appendWorkExperienceEntry()
  769. }
  770. @objc private func didTapAddEducation() {
  771. appendEducationEntry()
  772. }
  773. private func saveButtonHost() -> NSView {
  774. saveButton.translatesAutoresizingMaskIntoConstraints = false
  775. let host = NSView()
  776. host.translatesAutoresizingMaskIntoConstraints = false
  777. host.userInterfaceLayoutDirection = .leftToRight
  778. ProfileLayoutEnforcement.applyForcedLTR(to: host)
  779. host.addSubview(saveButton)
  780. NSLayoutConstraint.activate([
  781. saveButton.leadingAnchor.constraint(equalTo: host.leadingAnchor),
  782. saveButton.topAnchor.constraint(equalTo: host.topAnchor),
  783. saveButton.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  784. saveButton.heightAnchor.constraint(equalToConstant: 48),
  785. saveButton.trailingAnchor.constraint(lessThanOrEqualTo: host.trailingAnchor)
  786. ])
  787. return host
  788. }
  789. @objc private func didTapSave() {
  790. // UI shell only; wire persistence when profiles are stored.
  791. }
  792. }
  793. // MARK: - Work experience & education rows
  794. private final class WorkExperienceEntryView: NSView {
  795. var onDelete: (() -> Void)?
  796. private let subtitleLabel = NSTextField(labelWithString: "Experience 1")
  797. private let deleteButton = NSButton()
  798. private let jobTitleField = NSTextField()
  799. private let companyField = NSTextField()
  800. private let durationField = NSTextField()
  801. private let descriptionField = NSTextField()
  802. private var jobCompanyRow: ProfileDualFieldRow!
  803. override init(frame frameRect: NSRect) {
  804. super.init(frame: frameRect)
  805. configure()
  806. }
  807. required init?(coder: NSCoder) {
  808. fatalError("init(coder:) has not been implemented")
  809. }
  810. func setExperienceIndex(_ index: Int) {
  811. subtitleLabel.stringValue = "Experience \(index)"
  812. }
  813. func setDeleteHidden(_ hidden: Bool) {
  814. deleteButton.isHidden = hidden
  815. }
  816. func applyCompactLayout(_ compact: Bool) {
  817. jobCompanyRow.setCompact(compact)
  818. }
  819. private func configure() {
  820. userInterfaceLayoutDirection = .leftToRight
  821. ProfileLayoutEnforcement.applyForcedLTR(to: self)
  822. wantsLayer = true
  823. layer?.cornerRadius = 14
  824. layer?.borderWidth = 1
  825. layer?.borderColor = ProfilePagePalette.border.cgColor
  826. layer?.backgroundColor = ProfilePagePalette.cardBackground.cgColor
  827. layer?.masksToBounds = false
  828. layer?.shadowColor = NSColor.black.cgColor
  829. layer?.shadowOpacity = 0.05
  830. layer?.shadowRadius = 12
  831. layer?.shadowOffset = CGSize(width: 0, height: 6)
  832. if #available(macOS 11.0, *) {
  833. layer?.cornerCurve = .continuous
  834. }
  835. subtitleLabel.font = .systemFont(ofSize: 12, weight: .medium)
  836. subtitleLabel.textColor = ProfilePagePalette.secondaryText
  837. subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
  838. ProfileLayoutEnforcement.applyLeftAlignedTextField(subtitleLabel)
  839. deleteButton.translatesAutoresizingMaskIntoConstraints = false
  840. deleteButton.isBordered = false
  841. deleteButton.bezelStyle = .regularSquare
  842. deleteButton.focusRingType = .none
  843. deleteButton.contentTintColor = ProfilePagePalette.destructive
  844. deleteButton.target = self
  845. deleteButton.action = #selector(didTapDelete)
  846. if #available(macOS 11.0, *) {
  847. deleteButton.image = NSImage(systemSymbolName: "trash", accessibilityDescription: "Remove experience")
  848. deleteButton.imagePosition = .imageOnly
  849. } else {
  850. deleteButton.title = "Remove"
  851. deleteButton.font = .systemFont(ofSize: 12, weight: .medium)
  852. }
  853. let headerSpacer = NSView()
  854. headerSpacer.translatesAutoresizingMaskIntoConstraints = false
  855. headerSpacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  856. let headerRow = NSStackView(views: [subtitleLabel, headerSpacer, deleteButton])
  857. headerRow.orientation = .horizontal
  858. headerRow.alignment = .centerY
  859. headerRow.distribution = .fill
  860. headerRow.spacing = 8
  861. headerRow.userInterfaceLayoutDirection = .leftToRight
  862. ProfileLayoutEnforcement.applyForcedLTR(to: headerRow)
  863. headerRow.translatesAutoresizingMaskIntoConstraints = false
  864. let jobGroup = Self.labeledFieldStack(title: "Job Title *", field: jobTitleField, placeholder: "e.g., Software Engineer")
  865. let companyGroup = Self.labeledFieldStack(title: "Company Name *", field: companyField, placeholder: "e.g., Google")
  866. jobCompanyRow = ProfileDualFieldRow(left: jobGroup, right: companyGroup, spacing: 12)
  867. let durationGroup = Self.labeledFieldStack(title: "Duration *", field: durationField, placeholder: "e.g., Jan 2020 - Present")
  868. let descriptionGroup = Self.multilineLabeledStack(
  869. title: "Description",
  870. field: descriptionField,
  871. placeholder: "Describe your responsibilities and achievements...",
  872. minHeight: 120
  873. )
  874. let inner = NSStackView(views: [headerRow, jobCompanyRow, durationGroup, descriptionGroup])
  875. inner.orientation = .vertical
  876. inner.spacing = 16
  877. inner.alignment = .leading
  878. inner.translatesAutoresizingMaskIntoConstraints = false
  879. inner.edgeInsets = NSEdgeInsets(top: 16, left: 18, bottom: 16, right: 18)
  880. inner.userInterfaceLayoutDirection = .leftToRight
  881. ProfileLayoutEnforcement.applyForcedLTR(to: inner)
  882. inner.pinAllArrangedSubviewWidthsEqualToStackWidth()
  883. addSubview(inner)
  884. NSLayoutConstraint.activate([
  885. inner.leftAnchor.constraint(equalTo: leftAnchor),
  886. inner.rightAnchor.constraint(equalTo: rightAnchor),
  887. inner.topAnchor.constraint(equalTo: topAnchor),
  888. inner.bottomAnchor.constraint(equalTo: bottomAnchor)
  889. ])
  890. }
  891. override func layout() {
  892. super.layout()
  893. for field in [jobTitleField, companyField, durationField, descriptionField] {
  894. if let wrap = field.superview, wrap.bounds.width > 2 {
  895. let w = max(1, wrap.bounds.width - 24)
  896. if abs(field.preferredMaxLayoutWidth - w) > 0.5 {
  897. field.preferredMaxLayoutWidth = w
  898. }
  899. }
  900. }
  901. guard let layer = layer, layer.shadowOpacity > 0 else { return }
  902. let r = layer.cornerRadius
  903. layer.shadowPath = CGPath(roundedRect: bounds, cornerWidth: r, cornerHeight: r, transform: nil)
  904. }
  905. @objc private func didTapDelete() {
  906. onDelete?()
  907. }
  908. fileprivate static func labeledFieldStack(title: String, field: NSTextField, placeholder: String) -> NSView {
  909. let label = NSTextField(labelWithString: title)
  910. label.font = .systemFont(ofSize: 12, weight: .medium)
  911. label.textColor = ProfilePagePalette.secondaryText
  912. label.translatesAutoresizingMaskIntoConstraints = false
  913. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  914. styleSingleLineField(field, placeholder: placeholder)
  915. let wrap = roundedChrome(around: field, minHeight: 40)
  916. let stack = NSStackView(views: [label, wrap])
  917. stack.orientation = .vertical
  918. stack.spacing = 8
  919. stack.alignment = .leading
  920. stack.translatesAutoresizingMaskIntoConstraints = false
  921. stack.userInterfaceLayoutDirection = .leftToRight
  922. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  923. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  924. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  925. wrap.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  926. NSLayoutConstraint.activate([
  927. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  928. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  929. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  930. ])
  931. return stack
  932. }
  933. private static func multilineLabeledStack(title: String, field: NSTextField, placeholder: String, minHeight: CGFloat) -> NSView {
  934. let label = NSTextField(labelWithString: title)
  935. label.font = .systemFont(ofSize: 12, weight: .medium)
  936. label.textColor = ProfilePagePalette.secondaryText
  937. label.translatesAutoresizingMaskIntoConstraints = false
  938. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  939. field.translatesAutoresizingMaskIntoConstraints = false
  940. field.isEditable = true
  941. field.isSelectable = true
  942. field.isBordered = false
  943. field.drawsBackground = false
  944. field.focusRingType = .none
  945. field.font = .systemFont(ofSize: 14, weight: .regular)
  946. field.textColor = ProfilePagePalette.primaryText
  947. field.maximumNumberOfLines = 0
  948. field.cell?.wraps = true
  949. field.cell?.isScrollable = false
  950. field.cell?.usesSingleLineMode = false
  951. field.placeholderAttributedString = NSAttributedString(
  952. string: placeholder,
  953. attributes: [
  954. .foregroundColor: ProfilePagePalette.secondaryText,
  955. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  956. .paragraphStyle: ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  957. ]
  958. )
  959. ProfileLayoutEnforcement.applyLeftAlignedTextField(field)
  960. let wrap = NSView()
  961. wrap.translatesAutoresizingMaskIntoConstraints = false
  962. wrap.wantsLayer = true
  963. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  964. wrap.layer?.cornerRadius = 10
  965. wrap.layer?.borderWidth = 1
  966. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  967. if #available(macOS 11.0, *) {
  968. wrap.layer?.cornerCurve = .continuous
  969. }
  970. wrap.addSubview(field)
  971. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  972. NSLayoutConstraint.activate([
  973. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  974. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  975. field.topAnchor.constraint(equalTo: wrap.topAnchor, constant: 10),
  976. field.bottomAnchor.constraint(equalTo: wrap.bottomAnchor, constant: -10),
  977. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  978. ])
  979. let stack = NSStackView(views: [label, wrap])
  980. stack.orientation = .vertical
  981. stack.spacing = 8
  982. stack.alignment = .leading
  983. stack.translatesAutoresizingMaskIntoConstraints = false
  984. stack.userInterfaceLayoutDirection = .leftToRight
  985. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  986. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  987. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  988. NSLayoutConstraint.activate([
  989. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  990. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  991. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  992. ])
  993. return stack
  994. }
  995. private static func styleSingleLineField(_ field: NSTextField, placeholder: String) {
  996. field.translatesAutoresizingMaskIntoConstraints = false
  997. field.isBordered = false
  998. field.drawsBackground = false
  999. field.focusRingType = .none
  1000. field.font = .systemFont(ofSize: 14, weight: .regular)
  1001. field.textColor = ProfilePagePalette.primaryText
  1002. field.placeholderAttributedString = NSAttributedString(
  1003. string: placeholder,
  1004. attributes: [
  1005. .foregroundColor: ProfilePagePalette.secondaryText,
  1006. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  1007. .paragraphStyle: ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  1008. ]
  1009. )
  1010. field.cell?.usesSingleLineMode = true
  1011. field.cell?.wraps = false
  1012. field.cell?.isScrollable = true
  1013. ProfileLayoutEnforcement.applyLeftAlignedTextField(field)
  1014. }
  1015. private static func roundedChrome(around field: NSTextField, minHeight: CGFloat) -> NSView {
  1016. let wrap = NSView()
  1017. wrap.translatesAutoresizingMaskIntoConstraints = false
  1018. wrap.wantsLayer = true
  1019. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  1020. wrap.layer?.cornerRadius = 10
  1021. wrap.layer?.borderWidth = 1
  1022. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  1023. if #available(macOS 11.0, *) {
  1024. wrap.layer?.cornerCurve = .continuous
  1025. }
  1026. wrap.addSubview(field)
  1027. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  1028. NSLayoutConstraint.activate([
  1029. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  1030. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  1031. field.centerYAnchor.constraint(equalTo: wrap.centerYAnchor),
  1032. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  1033. ])
  1034. return wrap
  1035. }
  1036. }
  1037. private final class EducationEntryView: NSView {
  1038. var onDelete: (() -> Void)?
  1039. private let subtitleLabel = NSTextField(labelWithString: "Education 1")
  1040. private let deleteButton = NSButton()
  1041. private let degreeField = NSTextField()
  1042. private let institutionField = NSTextField()
  1043. private let yearField = NSTextField()
  1044. private var degreeInstitutionRow: ProfileDualFieldRow!
  1045. override init(frame frameRect: NSRect) {
  1046. super.init(frame: frameRect)
  1047. configure()
  1048. }
  1049. required init?(coder: NSCoder) {
  1050. fatalError("init(coder:) has not been implemented")
  1051. }
  1052. func setEducationIndex(_ index: Int) {
  1053. subtitleLabel.stringValue = "Education \(index)"
  1054. }
  1055. func setDeleteHidden(_ hidden: Bool) {
  1056. deleteButton.isHidden = hidden
  1057. }
  1058. func applyCompactLayout(_ compact: Bool) {
  1059. degreeInstitutionRow.setCompact(compact)
  1060. }
  1061. private func configure() {
  1062. userInterfaceLayoutDirection = .leftToRight
  1063. ProfileLayoutEnforcement.applyForcedLTR(to: self)
  1064. wantsLayer = true
  1065. layer?.cornerRadius = 14
  1066. layer?.borderWidth = 1
  1067. layer?.borderColor = ProfilePagePalette.border.cgColor
  1068. layer?.backgroundColor = ProfilePagePalette.cardBackground.cgColor
  1069. layer?.masksToBounds = false
  1070. layer?.shadowColor = NSColor.black.cgColor
  1071. layer?.shadowOpacity = 0.04
  1072. layer?.shadowRadius = 10
  1073. layer?.shadowOffset = CGSize(width: 0, height: 5)
  1074. if #available(macOS 11.0, *) {
  1075. layer?.cornerCurve = .continuous
  1076. }
  1077. subtitleLabel.font = .systemFont(ofSize: 12, weight: .medium)
  1078. subtitleLabel.textColor = ProfilePagePalette.secondaryText
  1079. subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
  1080. ProfileLayoutEnforcement.applyLeftAlignedTextField(subtitleLabel)
  1081. deleteButton.translatesAutoresizingMaskIntoConstraints = false
  1082. deleteButton.isBordered = false
  1083. deleteButton.bezelStyle = .regularSquare
  1084. deleteButton.focusRingType = .none
  1085. deleteButton.contentTintColor = ProfilePagePalette.destructive
  1086. deleteButton.target = self
  1087. deleteButton.action = #selector(didTapDelete)
  1088. if #available(macOS 11.0, *) {
  1089. deleteButton.image = NSImage(systemSymbolName: "trash", accessibilityDescription: "Remove education")
  1090. deleteButton.imagePosition = .imageOnly
  1091. } else {
  1092. deleteButton.title = "Remove"
  1093. deleteButton.font = .systemFont(ofSize: 12, weight: .medium)
  1094. }
  1095. let headerSpacer = NSView()
  1096. headerSpacer.translatesAutoresizingMaskIntoConstraints = false
  1097. headerSpacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1098. let headerRow = NSStackView(views: [subtitleLabel, headerSpacer, deleteButton])
  1099. headerRow.orientation = .horizontal
  1100. headerRow.alignment = .centerY
  1101. headerRow.distribution = .fill
  1102. headerRow.spacing = 8
  1103. headerRow.userInterfaceLayoutDirection = .leftToRight
  1104. ProfileLayoutEnforcement.applyForcedLTR(to: headerRow)
  1105. headerRow.translatesAutoresizingMaskIntoConstraints = false
  1106. let degreeGroup = WorkExperienceEntryView.labeledFieldStack(
  1107. title: "Degree / program *",
  1108. field: degreeField,
  1109. placeholder: "e.g., BSc Computer Science"
  1110. )
  1111. let institutionGroup = WorkExperienceEntryView.labeledFieldStack(
  1112. title: "Institution *",
  1113. field: institutionField,
  1114. placeholder: "e.g., MIT"
  1115. )
  1116. degreeInstitutionRow = ProfileDualFieldRow(left: degreeGroup, right: institutionGroup, spacing: 12)
  1117. let yearGroup = WorkExperienceEntryView.labeledFieldStack(
  1118. title: "Year *",
  1119. field: yearField,
  1120. placeholder: "e.g., 2020"
  1121. )
  1122. let inner = NSStackView(views: [headerRow, degreeInstitutionRow, yearGroup])
  1123. inner.orientation = .vertical
  1124. inner.spacing = 14
  1125. inner.alignment = .leading
  1126. inner.translatesAutoresizingMaskIntoConstraints = false
  1127. inner.edgeInsets = NSEdgeInsets(top: 14, left: 18, bottom: 14, right: 18)
  1128. inner.userInterfaceLayoutDirection = .leftToRight
  1129. ProfileLayoutEnforcement.applyForcedLTR(to: inner)
  1130. inner.pinAllArrangedSubviewWidthsEqualToStackWidth()
  1131. addSubview(inner)
  1132. NSLayoutConstraint.activate([
  1133. inner.leftAnchor.constraint(equalTo: leftAnchor),
  1134. inner.rightAnchor.constraint(equalTo: rightAnchor),
  1135. inner.topAnchor.constraint(equalTo: topAnchor),
  1136. inner.bottomAnchor.constraint(equalTo: bottomAnchor)
  1137. ])
  1138. }
  1139. override func layout() {
  1140. super.layout()
  1141. for field in [degreeField, institutionField, yearField] {
  1142. if let wrap = field.superview, wrap.bounds.width > 2 {
  1143. let w = max(1, wrap.bounds.width - 24)
  1144. if abs(field.preferredMaxLayoutWidth - w) > 0.5 {
  1145. field.preferredMaxLayoutWidth = w
  1146. }
  1147. }
  1148. }
  1149. guard let layer = layer, layer.shadowOpacity > 0 else { return }
  1150. let r = layer.cornerRadius
  1151. layer.shadowPath = CGPath(roundedRect: bounds, cornerWidth: r, cornerHeight: r, transform: nil)
  1152. }
  1153. @objc private func didTapDelete() {
  1154. onDelete?()
  1155. }
  1156. }
  1157. // MARK: - Primary CTA
  1158. private final class ProfilePrimaryButton: NSButton {
  1159. private var trackingArea: NSTrackingArea?
  1160. private var didPushCursor = false
  1161. override init(frame frameRect: NSRect) {
  1162. super.init(frame: frameRect)
  1163. commonInit()
  1164. }
  1165. required init?(coder: NSCoder) {
  1166. super.init(coder: coder)
  1167. commonInit()
  1168. }
  1169. convenience init(title: String, target: AnyObject?, action: Selector?) {
  1170. self.init(frame: .zero)
  1171. self.title = title
  1172. self.target = target
  1173. self.action = action
  1174. }
  1175. private func commonInit() {
  1176. bezelStyle = .rounded
  1177. isBordered = false
  1178. font = .systemFont(ofSize: 15, weight: .semibold)
  1179. contentTintColor = .white
  1180. wantsLayer = true
  1181. layer?.cornerRadius = 12
  1182. if #available(macOS 11.0, *) {
  1183. layer?.cornerCurve = .continuous
  1184. }
  1185. layer?.backgroundColor = ProfilePagePalette.brandBlue.cgColor
  1186. }
  1187. override func updateTrackingAreas() {
  1188. super.updateTrackingAreas()
  1189. if let trackingArea { removeTrackingArea(trackingArea) }
  1190. let area = NSTrackingArea(
  1191. rect: bounds,
  1192. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1193. owner: self,
  1194. userInfo: nil
  1195. )
  1196. addTrackingArea(area)
  1197. trackingArea = area
  1198. }
  1199. override func mouseEntered(with event: NSEvent) {
  1200. super.mouseEntered(with: event)
  1201. layer?.backgroundColor = ProfilePagePalette.brandBlueHover.cgColor
  1202. if !didPushCursor {
  1203. NSCursor.pointingHand.push()
  1204. didPushCursor = true
  1205. }
  1206. }
  1207. override func mouseExited(with event: NSEvent) {
  1208. super.mouseExited(with: event)
  1209. layer?.backgroundColor = ProfilePagePalette.brandBlue.cgColor
  1210. if didPushCursor {
  1211. NSCursor.pop()
  1212. didPushCursor = false
  1213. }
  1214. }
  1215. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1216. super.viewWillMove(toWindow: newWindow)
  1217. if newWindow == nil, didPushCursor {
  1218. NSCursor.pop()
  1219. didPushCursor = false
  1220. }
  1221. }
  1222. }