Geen omschrijving

MyProfilePageView.swift 69KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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. /// Inset of form content from the card border (left/right); explicit constraints so fields stay inside the card chrome.
  120. private static let cardContentHorizontalInset: CGFloat = 28
  121. private let scrollView = NSScrollView()
  122. private let documentView = NSView()
  123. private let cardView = NSView()
  124. private let formStack = NSStackView()
  125. private let profileNameField = NSTextField()
  126. private let fullNameField = NSTextField()
  127. private let emailField = NSTextField()
  128. private let phoneField = NSTextField()
  129. private let jobTitleField = NSTextField()
  130. private let addressField = NSTextField()
  131. private let careerField = NSTextField()
  132. private let certificatesField = NSTextField()
  133. private let interestsField = NSTextField()
  134. private let languagesField = NSTextField()
  135. private let referralField = NSTextField()
  136. private let saveButton = ProfilePrimaryButton(title: "Save Profile →", target: nil, action: nil)
  137. private var nameEmailRow: ProfileDualFieldRow!
  138. private var phoneJobRow: ProfileDualFieldRow!
  139. private let topChrome = NSView()
  140. private let backButton = NSButton(title: "← All profiles", target: nil, action: nil)
  141. private let contextLabel = NSTextField(labelWithString: "")
  142. private var editingProfileID: UUID?
  143. /// Called from the back control and after a successful save (returns to the profiles list).
  144. var onDismiss: (() -> Void)?
  145. private let workExperienceRowsStack = NSStackView()
  146. private var workExperienceEntries: [WorkExperienceEntryView] = []
  147. private let educationRowsStack = NSStackView()
  148. private var educationEntries: [EducationEntryView] = []
  149. private var lastCompactLayout: Bool?
  150. private var referralHelperLabel: NSTextField?
  151. /// Force left-to-right geometry so profile fields span the full width even when the window uses RTL layout.
  152. override var userInterfaceLayoutDirection: NSUserInterfaceLayoutDirection {
  153. get { .leftToRight }
  154. set { super.userInterfaceLayoutDirection = .leftToRight }
  155. }
  156. override init(frame frameRect: NSRect) {
  157. super.init(frame: frameRect)
  158. setup()
  159. }
  160. required init?(coder: NSCoder) {
  161. super.init(coder: coder)
  162. setup()
  163. }
  164. override func viewDidMoveToWindow() {
  165. super.viewDidMoveToWindow()
  166. guard window != nil else { return }
  167. ProfileLayoutEnforcement.applyForcedLTRSubtree(from: self)
  168. needsLayout = true
  169. }
  170. override func layout() {
  171. super.layout()
  172. if let layer = cardView.layer, layer.shadowOpacity > 0 {
  173. let r = layer.cornerRadius
  174. layer.shadowPath = CGPath(roundedRect: cardView.bounds, cornerWidth: r, cornerHeight: r, transform: nil)
  175. }
  176. updateMultilinePreferredLayoutWidths()
  177. applyResponsiveRowsIfNeeded()
  178. }
  179. /// Wrapping `NSTextField`s report a tiny intrinsic width until `preferredMaxLayoutWidth` tracks the chrome width, which otherwise collapses the stack to a narrow trailing column.
  180. private func updateMultilinePreferredLayoutWidths() {
  181. let horizontalInset: CGFloat = 24
  182. applyPreferredWrapWidth(to: profileNameField, horizontalInset: horizontalInset)
  183. applyPreferredWrapWidth(to: fullNameField, horizontalInset: horizontalInset)
  184. applyPreferredWrapWidth(to: emailField, horizontalInset: horizontalInset)
  185. applyPreferredWrapWidth(to: phoneField, horizontalInset: horizontalInset)
  186. applyPreferredWrapWidth(to: jobTitleField, horizontalInset: horizontalInset)
  187. applyPreferredWrapWidth(to: addressField, horizontalInset: horizontalInset)
  188. applyPreferredWrapWidth(to: referralField, horizontalInset: horizontalInset)
  189. applyPreferredWrapWidth(to: careerField, horizontalInset: horizontalInset)
  190. applyPreferredWrapWidth(to: certificatesField, horizontalInset: horizontalInset)
  191. applyPreferredWrapWidth(to: interestsField, horizontalInset: horizontalInset)
  192. applyPreferredWrapWidth(to: languagesField, horizontalInset: horizontalInset)
  193. if let helper = referralHelperLabel, let stack = helper.superview, stack.bounds.width > 2 {
  194. let w = max(1, stack.bounds.width - 8)
  195. if abs(helper.preferredMaxLayoutWidth - w) > 0.5 {
  196. helper.preferredMaxLayoutWidth = w
  197. }
  198. }
  199. }
  200. private func applyPreferredWrapWidth(to field: NSTextField, horizontalInset: CGFloat) {
  201. guard let wrap = field.superview, wrap.bounds.width > 2 else { return }
  202. let w = max(1, wrap.bounds.width - horizontalInset)
  203. if abs(field.preferredMaxLayoutWidth - w) > 0.5 {
  204. field.preferredMaxLayoutWidth = w
  205. }
  206. }
  207. private func setup() {
  208. wantsLayer = true
  209. layer?.backgroundColor = ProfilePagePalette.pageBackground.cgColor
  210. userInterfaceLayoutDirection = .leftToRight
  211. ProfileLayoutEnforcement.applyForcedLTR(to: self)
  212. scrollView.translatesAutoresizingMaskIntoConstraints = false
  213. scrollView.userInterfaceLayoutDirection = .leftToRight
  214. ProfileLayoutEnforcement.applyForcedLTR(to: scrollView)
  215. scrollView.hasVerticalScroller = true
  216. scrollView.hasHorizontalScroller = false
  217. scrollView.autohidesScrollers = true
  218. scrollView.drawsBackground = false
  219. scrollView.borderType = .noBorder
  220. scrollView.scrollerStyle = .overlay
  221. scrollView.automaticallyAdjustsContentInsets = false
  222. scrollView.contentView.userInterfaceLayoutDirection = .leftToRight
  223. ProfileLayoutEnforcement.applyForcedLTR(to: scrollView.contentView)
  224. if #available(macOS 10.11, *) {
  225. scrollView.horizontalScrollElasticity = .none
  226. }
  227. documentView.translatesAutoresizingMaskIntoConstraints = false
  228. documentView.userInterfaceLayoutDirection = .leftToRight
  229. ProfileLayoutEnforcement.applyForcedLTR(to: documentView)
  230. cardView.translatesAutoresizingMaskIntoConstraints = false
  231. cardView.wantsLayer = true
  232. cardView.layer?.backgroundColor = ProfilePagePalette.cardBackground.cgColor
  233. cardView.layer?.cornerRadius = 18
  234. cardView.layer?.borderWidth = 1
  235. cardView.layer?.borderColor = ProfilePagePalette.border.cgColor
  236. cardView.layer?.masksToBounds = false
  237. cardView.layer?.shadowColor = NSColor.black.cgColor
  238. cardView.layer?.shadowOpacity = 0.06
  239. cardView.layer?.shadowRadius = 20
  240. cardView.layer?.shadowOffset = CGSize(width: 0, height: 10)
  241. cardView.userInterfaceLayoutDirection = .leftToRight
  242. ProfileLayoutEnforcement.applyForcedLTR(to: cardView)
  243. if #available(macOS 11.0, *) {
  244. cardView.layer?.cornerCurve = .continuous
  245. }
  246. formStack.translatesAutoresizingMaskIntoConstraints = false
  247. formStack.orientation = .vertical
  248. formStack.alignment = .leading
  249. formStack.distribution = .fill
  250. formStack.spacing = 24
  251. formStack.edgeInsets = NSEdgeInsets(top: 32, left: 0, bottom: 32, right: 0)
  252. formStack.userInterfaceLayoutDirection = .leftToRight
  253. ProfileLayoutEnforcement.applyForcedLTR(to: formStack)
  254. formStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  255. formStack.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  256. topChrome.translatesAutoresizingMaskIntoConstraints = false
  257. topChrome.userInterfaceLayoutDirection = .leftToRight
  258. backButton.translatesAutoresizingMaskIntoConstraints = false
  259. backButton.bezelStyle = .rounded
  260. backButton.isBordered = false
  261. backButton.font = .systemFont(ofSize: 13, weight: .medium)
  262. backButton.contentTintColor = ProfilePagePalette.brandBlue
  263. backButton.target = self
  264. backButton.action = #selector(didTapBack)
  265. contextLabel.translatesAutoresizingMaskIntoConstraints = false
  266. contextLabel.font = .systemFont(ofSize: 15, weight: .semibold)
  267. contextLabel.textColor = ProfilePagePalette.primaryText
  268. contextLabel.stringValue = "New profile"
  269. contextLabel.backgroundColor = .clear
  270. contextLabel.isBordered = false
  271. contextLabel.isEditable = false
  272. contextLabel.isSelectable = false
  273. ProfileLayoutEnforcement.applyLeftAlignedTextField(contextLabel)
  274. topChrome.addSubview(backButton)
  275. topChrome.addSubview(contextLabel)
  276. NSLayoutConstraint.activate([
  277. backButton.leadingAnchor.constraint(equalTo: topChrome.leadingAnchor, constant: 4),
  278. backButton.centerYAnchor.constraint(equalTo: topChrome.centerYAnchor),
  279. contextLabel.leadingAnchor.constraint(equalTo: backButton.trailingAnchor, constant: 10),
  280. contextLabel.centerYAnchor.constraint(equalTo: topChrome.centerYAnchor),
  281. contextLabel.trailingAnchor.constraint(lessThanOrEqualTo: topChrome.trailingAnchor, constant: -8)
  282. ])
  283. addSubview(topChrome)
  284. addSubview(scrollView)
  285. scrollView.documentView = documentView
  286. documentView.addSubview(cardView)
  287. cardView.addSubview(formStack)
  288. NSLayoutConstraint.activate([
  289. topChrome.leftAnchor.constraint(equalTo: leftAnchor),
  290. topChrome.rightAnchor.constraint(equalTo: rightAnchor),
  291. topChrome.topAnchor.constraint(equalTo: topAnchor, constant: 4),
  292. topChrome.heightAnchor.constraint(equalToConstant: 40),
  293. scrollView.leftAnchor.constraint(equalTo: leftAnchor),
  294. scrollView.rightAnchor.constraint(equalTo: rightAnchor),
  295. scrollView.topAnchor.constraint(equalTo: topChrome.bottomAnchor, constant: 2),
  296. scrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
  297. // Pin the document to the clip view’s geometric width so LTR/RTL semantics cannot slide the form.
  298. documentView.leftAnchor.constraint(equalTo: scrollView.contentView.leftAnchor),
  299. documentView.rightAnchor.constraint(equalTo: scrollView.contentView.rightAnchor),
  300. documentView.topAnchor.constraint(equalTo: scrollView.contentView.topAnchor),
  301. documentView.bottomAnchor.constraint(equalTo: cardView.bottomAnchor, constant: Self.horizontalPageInset),
  302. // 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.
  303. cardView.leftAnchor.constraint(equalTo: documentView.leftAnchor, constant: Self.horizontalPageInset),
  304. cardView.rightAnchor.constraint(equalTo: documentView.rightAnchor, constant: -Self.horizontalPageInset),
  305. cardView.topAnchor.constraint(equalTo: documentView.topAnchor, constant: Self.horizontalPageInset),
  306. formStack.leadingAnchor.constraint(equalTo: cardView.leadingAnchor, constant: Self.cardContentHorizontalInset),
  307. formStack.trailingAnchor.constraint(equalTo: cardView.trailingAnchor, constant: -Self.cardContentHorizontalInset),
  308. formStack.topAnchor.constraint(equalTo: cardView.topAnchor),
  309. formStack.bottomAnchor.constraint(equalTo: cardView.bottomAnchor)
  310. ])
  311. addFullWidthArrangedSubview(
  312. labeledGroup(title: "Profile Name *", field: profileNameField, placeholder: "Marketing Director Profile")
  313. )
  314. addFullWidthArrangedSubview(sectionHeading("Personal Information"))
  315. let nameGroup = labeledGroup(title: "Full Name *", field: fullNameField, placeholder: "John Doe")
  316. let emailGroup = labeledGroup(title: "Email *", field: emailField, placeholder: "john@example.com")
  317. nameEmailRow = ProfileDualFieldRow(left: nameGroup, right: emailGroup, spacing: 12)
  318. addFullWidthArrangedSubview(nameEmailRow)
  319. let phoneGroup = labeledGroup(title: "Phone", field: phoneField, placeholder: "+1 (555) 123-4567")
  320. let jobGroup = labeledGroup(title: "Job Title *", field: jobTitleField, placeholder: "Software Engineer")
  321. phoneJobRow = ProfileDualFieldRow(left: phoneGroup, right: jobGroup, spacing: 12)
  322. addFullWidthArrangedSubview(phoneJobRow)
  323. addFullWidthArrangedSubview(
  324. labeledGroup(title: "Address", field: addressField, placeholder: "123 Main St, City, State, ZIP")
  325. )
  326. addFullWidthArrangedSubview(careerSummaryBlock())
  327. addFullWidthArrangedSubview(horizontalSeparator())
  328. addFullWidthArrangedSubview(workExperienceSection())
  329. addFullWidthArrangedSubview(horizontalSeparator())
  330. addFullWidthArrangedSubview(educationSection())
  331. addFullWidthArrangedSubview(horizontalSeparator())
  332. addFullWidthArrangedSubview(
  333. multilineProfileBlock(
  334. title: "Certificates / Rewards",
  335. placeholder: "List your certificates and awards...",
  336. field: certificatesField,
  337. minHeight: 100
  338. )
  339. )
  340. addFullWidthArrangedSubview(horizontalSeparator())
  341. addFullWidthArrangedSubview(
  342. multilineProfileBlock(
  343. title: "Interests",
  344. placeholder: "List your interests and hobbies...",
  345. field: interestsField,
  346. minHeight: 100
  347. )
  348. )
  349. addFullWidthArrangedSubview(horizontalSeparator())
  350. addFullWidthArrangedSubview(
  351. multilineProfileBlock(
  352. title: "Languages",
  353. placeholder: "List languages you speak (e.g., English - Native, Spanish - Fluent)...",
  354. field: languagesField,
  355. minHeight: 100
  356. )
  357. )
  358. addFullWidthArrangedSubview(horizontalSeparator())
  359. addFullWidthArrangedSubview(referralBlock())
  360. addFullWidthArrangedSubview(saveButtonHost())
  361. saveButton.target = self
  362. saveButton.action = #selector(didTapSave)
  363. appendWorkExperienceEntry()
  364. appendEducationEntry()
  365. ProfileLayoutEnforcement.applyForcedLTRSubtree(from: self)
  366. }
  367. func prepareNewProfile() {
  368. editingProfileID = nil
  369. contextLabel.stringValue = "New profile"
  370. applyForm(
  371. from: SavedProfile(
  372. id: UUID(),
  373. profileDisplayName: "",
  374. personal: .empty,
  375. careerSummary: "",
  376. workExperiences: [.empty],
  377. educations: [.empty],
  378. certificates: "",
  379. interests: "",
  380. languages: "",
  381. referral: ""
  382. )
  383. )
  384. }
  385. func loadSavedProfile(_ profile: SavedProfile) {
  386. editingProfileID = profile.id
  387. contextLabel.stringValue = "Edit profile"
  388. applyForm(from: profile)
  389. }
  390. private func applyForm(from profile: SavedProfile) {
  391. profileNameField.stringValue = profile.profileDisplayName
  392. applyPersonalInformation(profile.personal)
  393. careerField.stringValue = profile.careerSummary
  394. certificatesField.stringValue = profile.certificates
  395. interestsField.stringValue = profile.interests
  396. languagesField.stringValue = profile.languages
  397. referralField.stringValue = profile.referral
  398. let workCount = max(1, profile.workExperiences.count)
  399. syncWorkExperienceRowCount(to: workCount)
  400. if profile.workExperiences.isEmpty {
  401. workExperienceEntries[0].applyPayload(.empty)
  402. } else {
  403. for (i, payload) in profile.workExperiences.enumerated() where i < workExperienceEntries.count {
  404. workExperienceEntries[i].applyPayload(payload)
  405. }
  406. }
  407. let eduCount = max(1, profile.educations.count)
  408. syncEducationRowCount(to: eduCount)
  409. if profile.educations.isEmpty {
  410. educationEntries[0].applyPayload(.empty)
  411. } else {
  412. for (i, payload) in profile.educations.enumerated() where i < educationEntries.count {
  413. educationEntries[i].applyPayload(payload)
  414. }
  415. }
  416. lastCompactLayout = nil
  417. needsLayout = true
  418. }
  419. private func syncWorkExperienceRowCount(to target: Int) {
  420. let n = max(1, target)
  421. while workExperienceEntries.count < n {
  422. appendWorkExperienceEntry()
  423. }
  424. while workExperienceEntries.count > n {
  425. guard let last = workExperienceEntries.last, workExperienceEntries.count > 1 else { break }
  426. removeWorkExperienceEntry(last)
  427. }
  428. }
  429. private func syncEducationRowCount(to target: Int) {
  430. let n = max(1, target)
  431. while educationEntries.count < n {
  432. appendEducationEntry()
  433. }
  434. while educationEntries.count > n {
  435. guard let last = educationEntries.last, educationEntries.count > 1 else { break }
  436. removeEducationEntry(last)
  437. }
  438. }
  439. private func captureSavedProfileForSave() -> SavedProfile {
  440. let id = editingProfileID ?? UUID()
  441. return SavedProfile(
  442. id: id,
  443. profileDisplayName: profileNameField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  444. personal: collectPersonalInformationFromFields(),
  445. careerSummary: careerField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  446. workExperiences: workExperienceEntries.map { $0.capturePayload() },
  447. educations: educationEntries.map { $0.capturePayload() },
  448. certificates: certificatesField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  449. interests: interestsField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  450. languages: languagesField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  451. referral: referralField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  452. )
  453. }
  454. @objc private func didTapBack() {
  455. onDismiss?()
  456. }
  457. private func applyPersonalInformation(_ info: PersonalInformation) {
  458. fullNameField.stringValue = info.fullName
  459. emailField.stringValue = info.email
  460. phoneField.stringValue = info.phone
  461. jobTitleField.stringValue = info.jobTitle
  462. addressField.stringValue = info.address
  463. }
  464. private func collectPersonalInformationFromFields() -> PersonalInformation {
  465. PersonalInformation(
  466. fullName: fullNameField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  467. email: emailField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  468. phone: phoneField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  469. jobTitle: jobTitleField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  470. address: addressField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  471. )
  472. }
  473. private func applyResponsiveRowsIfNeeded() {
  474. let w = cardView.bounds.width
  475. guard w > 1 else { return }
  476. let formWidth = max(0, w - 2 * Self.cardContentHorizontalInset - formStack.edgeInsets.left - formStack.edgeInsets.right)
  477. let compact = formWidth < Self.compactFormWidth
  478. guard compact != lastCompactLayout else { return }
  479. lastCompactLayout = compact
  480. nameEmailRow.setCompact(compact)
  481. phoneJobRow.setCompact(compact)
  482. for entry in workExperienceEntries {
  483. entry.applyCompactLayout(compact)
  484. }
  485. for entry in educationEntries {
  486. entry.applyCompactLayout(compact)
  487. }
  488. }
  489. private func addFullWidthArrangedSubview(_ view: NSView) {
  490. formStack.addArrangedSubview(view)
  491. view.setContentHuggingPriority(.defaultLow, for: .horizontal)
  492. view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  493. view.widthAnchor.constraint(equalTo: formStack.widthAnchor).isActive = true
  494. }
  495. private func sectionHeading(_ text: String) -> NSView {
  496. let label = NSTextField(labelWithString: text)
  497. label.font = .systemFont(ofSize: 15, weight: .semibold)
  498. label.textColor = ProfilePagePalette.primaryText
  499. label.baseWritingDirection = .leftToRight
  500. label.translatesAutoresizingMaskIntoConstraints = false
  501. label.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  502. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  503. let spacer = NSView()
  504. spacer.translatesAutoresizingMaskIntoConstraints = false
  505. spacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  506. let row = NSStackView(views: [label, spacer])
  507. row.orientation = .horizontal
  508. row.alignment = .centerY
  509. row.distribution = .fill
  510. row.spacing = 0
  511. row.userInterfaceLayoutDirection = .leftToRight
  512. ProfileLayoutEnforcement.applyForcedLTR(to: row)
  513. row.translatesAutoresizingMaskIntoConstraints = false
  514. NSLayoutConstraint.activate([
  515. label.leftAnchor.constraint(equalTo: row.leftAnchor)
  516. ])
  517. return row
  518. }
  519. private func labeledGroup(title: String, field: NSTextField, placeholder: String) -> NSView {
  520. let label = NSTextField(labelWithString: title)
  521. label.font = .systemFont(ofSize: 12, weight: .medium)
  522. label.textColor = ProfilePagePalette.secondaryText
  523. label.translatesAutoresizingMaskIntoConstraints = false
  524. label.setContentHuggingPriority(.defaultLow, for: .horizontal)
  525. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  526. styleSingleLineField(field, placeholder: placeholder)
  527. let wrap = roundedFieldChrome(containing: field, minHeight: 40)
  528. let stack = NSStackView(views: [label, wrap])
  529. stack.orientation = .vertical
  530. stack.spacing = 8
  531. // `.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).
  532. stack.alignment = .leading
  533. stack.translatesAutoresizingMaskIntoConstraints = false
  534. stack.userInterfaceLayoutDirection = .leftToRight
  535. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  536. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  537. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  538. wrap.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  539. NSLayoutConstraint.activate([
  540. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  541. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  542. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  543. ])
  544. return stack
  545. }
  546. private func styleSingleLineField(_ field: NSTextField, placeholder: String) {
  547. field.translatesAutoresizingMaskIntoConstraints = false
  548. field.isBordered = false
  549. field.drawsBackground = false
  550. field.focusRingType = .none
  551. field.font = .systemFont(ofSize: 14, weight: .regular)
  552. field.textColor = ProfilePagePalette.primaryText
  553. field.setContentHuggingPriority(.defaultLow, for: .horizontal)
  554. field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  555. let paragraph = ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  556. field.placeholderAttributedString = NSAttributedString(
  557. string: placeholder,
  558. attributes: [
  559. .foregroundColor: ProfilePagePalette.secondaryText,
  560. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  561. .paragraphStyle: paragraph
  562. ]
  563. )
  564. field.cell?.usesSingleLineMode = true
  565. field.cell?.wraps = false
  566. field.cell?.isScrollable = true
  567. ProfileLayoutEnforcement.applyLeftAlignedTextField(field)
  568. }
  569. private func roundedFieldChrome(containing field: NSTextField, minHeight: CGFloat) -> NSView {
  570. let wrap = NSView()
  571. wrap.translatesAutoresizingMaskIntoConstraints = false
  572. wrap.wantsLayer = true
  573. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  574. wrap.layer?.cornerRadius = 10
  575. wrap.layer?.borderWidth = 1
  576. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  577. if #available(macOS 11.0, *) {
  578. wrap.layer?.cornerCurve = .continuous
  579. }
  580. wrap.addSubview(field)
  581. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  582. NSLayoutConstraint.activate([
  583. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  584. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  585. field.centerYAnchor.constraint(equalTo: wrap.centerYAnchor),
  586. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  587. ])
  588. return wrap
  589. }
  590. private func careerSummaryBlock() -> NSView {
  591. let label = NSTextField(labelWithString: "Career Summary")
  592. label.font = .systemFont(ofSize: 12, weight: .medium)
  593. label.textColor = ProfilePagePalette.secondaryText
  594. label.translatesAutoresizingMaskIntoConstraints = false
  595. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  596. careerField.translatesAutoresizingMaskIntoConstraints = false
  597. careerField.isEditable = true
  598. careerField.isSelectable = true
  599. careerField.isBordered = false
  600. careerField.drawsBackground = false
  601. careerField.focusRingType = .none
  602. careerField.font = .systemFont(ofSize: 14, weight: .regular)
  603. careerField.textColor = ProfilePagePalette.primaryText
  604. careerField.maximumNumberOfLines = 0
  605. careerField.cell?.wraps = true
  606. careerField.cell?.isScrollable = false
  607. careerField.cell?.usesSingleLineMode = false
  608. careerField.stringValue = ""
  609. careerField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  610. careerField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  611. careerField.placeholderAttributedString = NSAttributedString(
  612. string: "Brief overview of your professional background and key achievements...",
  613. attributes: [
  614. .foregroundColor: ProfilePagePalette.secondaryText,
  615. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  616. .paragraphStyle: ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  617. ]
  618. )
  619. ProfileLayoutEnforcement.applyLeftAlignedTextField(careerField)
  620. let wrap = NSView()
  621. wrap.translatesAutoresizingMaskIntoConstraints = false
  622. wrap.wantsLayer = true
  623. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  624. wrap.layer?.cornerRadius = 10
  625. wrap.layer?.borderWidth = 1
  626. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  627. if #available(macOS 11.0, *) {
  628. wrap.layer?.cornerCurve = .continuous
  629. }
  630. wrap.addSubview(careerField)
  631. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  632. NSLayoutConstraint.activate([
  633. careerField.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  634. careerField.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  635. careerField.topAnchor.constraint(equalTo: wrap.topAnchor, constant: 10),
  636. careerField.bottomAnchor.constraint(equalTo: wrap.bottomAnchor, constant: -10),
  637. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: 168)
  638. ])
  639. let stack = NSStackView(views: [label, wrap])
  640. stack.orientation = .vertical
  641. stack.spacing = 8
  642. stack.alignment = .leading
  643. stack.translatesAutoresizingMaskIntoConstraints = false
  644. stack.userInterfaceLayoutDirection = .leftToRight
  645. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  646. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  647. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  648. NSLayoutConstraint.activate([
  649. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  650. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  651. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  652. ])
  653. return stack
  654. }
  655. private func multilineProfileBlock(title: String, placeholder: String, field: NSTextField, minHeight: CGFloat) -> NSView {
  656. let label = NSTextField(labelWithString: title)
  657. label.font = .systemFont(ofSize: 12, weight: .medium)
  658. label.textColor = ProfilePagePalette.secondaryText
  659. label.translatesAutoresizingMaskIntoConstraints = false
  660. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  661. field.translatesAutoresizingMaskIntoConstraints = false
  662. field.isEditable = true
  663. field.isSelectable = true
  664. field.isBordered = false
  665. field.drawsBackground = false
  666. field.focusRingType = .none
  667. field.font = .systemFont(ofSize: 14, weight: .regular)
  668. field.textColor = ProfilePagePalette.primaryText
  669. field.maximumNumberOfLines = 0
  670. field.cell?.wraps = true
  671. field.cell?.isScrollable = false
  672. field.cell?.usesSingleLineMode = false
  673. field.stringValue = ""
  674. field.setContentHuggingPriority(.defaultLow, for: .horizontal)
  675. field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  676. field.placeholderAttributedString = NSAttributedString(
  677. string: placeholder,
  678. attributes: [
  679. .foregroundColor: ProfilePagePalette.secondaryText,
  680. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  681. .paragraphStyle: ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  682. ]
  683. )
  684. ProfileLayoutEnforcement.applyLeftAlignedTextField(field)
  685. let wrap = NSView()
  686. wrap.translatesAutoresizingMaskIntoConstraints = false
  687. wrap.wantsLayer = true
  688. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  689. wrap.layer?.cornerRadius = 10
  690. wrap.layer?.borderWidth = 1
  691. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  692. if #available(macOS 11.0, *) {
  693. wrap.layer?.cornerCurve = .continuous
  694. }
  695. wrap.addSubview(field)
  696. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  697. NSLayoutConstraint.activate([
  698. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  699. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  700. field.topAnchor.constraint(equalTo: wrap.topAnchor, constant: 10),
  701. field.bottomAnchor.constraint(equalTo: wrap.bottomAnchor, constant: -10),
  702. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  703. ])
  704. let stack = NSStackView(views: [label, wrap])
  705. stack.orientation = .vertical
  706. stack.spacing = 8
  707. stack.alignment = .leading
  708. stack.translatesAutoresizingMaskIntoConstraints = false
  709. stack.userInterfaceLayoutDirection = .leftToRight
  710. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  711. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  712. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  713. NSLayoutConstraint.activate([
  714. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  715. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  716. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  717. ])
  718. return stack
  719. }
  720. private func referralBlock() -> NSView {
  721. let label = NSTextField(labelWithString: "Referral (Optional)")
  722. label.font = .systemFont(ofSize: 12, weight: .medium)
  723. label.textColor = ProfilePagePalette.secondaryText
  724. label.translatesAutoresizingMaskIntoConstraints = false
  725. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  726. styleSingleLineField(referralField, placeholder: "Referred by (Company/Person Name)")
  727. let wrap = roundedFieldChrome(containing: referralField, minHeight: 40)
  728. let helper = NSTextField(wrappingLabelWithString: "If someone referred you for this job, enter their name or company here")
  729. helper.font = .systemFont(ofSize: 11, weight: .regular)
  730. helper.textColor = ProfilePagePalette.secondaryText
  731. helper.translatesAutoresizingMaskIntoConstraints = false
  732. ProfileLayoutEnforcement.applyLeftAlignedTextField(helper)
  733. referralHelperLabel = helper
  734. let stack = NSStackView(views: [label, wrap, helper])
  735. stack.orientation = .vertical
  736. stack.spacing = 8
  737. stack.alignment = .leading
  738. stack.translatesAutoresizingMaskIntoConstraints = false
  739. stack.userInterfaceLayoutDirection = .leftToRight
  740. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  741. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  742. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  743. if #available(macOS 10.11, *) {
  744. stack.setCustomSpacing(6, after: wrap)
  745. }
  746. NSLayoutConstraint.activate([
  747. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  748. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  749. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor),
  750. helper.leftAnchor.constraint(equalTo: stack.leftAnchor),
  751. helper.widthAnchor.constraint(equalTo: stack.widthAnchor)
  752. ])
  753. return stack
  754. }
  755. private func horizontalSeparator() -> NSView {
  756. let box = NSBox()
  757. box.boxType = .separator
  758. box.translatesAutoresizingMaskIntoConstraints = false
  759. return box
  760. }
  761. private func workExperienceSection() -> NSView {
  762. let title = NSTextField(labelWithString: "Work Experience")
  763. title.font = .systemFont(ofSize: 15, weight: .semibold)
  764. title.textColor = ProfilePagePalette.primaryText
  765. title.translatesAutoresizingMaskIntoConstraints = false
  766. title.setContentHuggingPriority(.defaultLow, for: .horizontal)
  767. ProfileLayoutEnforcement.applyLeftAlignedTextField(title)
  768. let addButton = NSButton(title: "+ Add Another", target: self, action: #selector(didTapAddWorkExperience))
  769. addButton.translatesAutoresizingMaskIntoConstraints = false
  770. addButton.bezelStyle = .rounded
  771. addButton.isBordered = true
  772. addButton.font = .systemFont(ofSize: 12, weight: .medium)
  773. addButton.controlSize = .regular
  774. addButton.setContentHuggingPriority(.required, for: .horizontal)
  775. let spacer = NSView()
  776. spacer.translatesAutoresizingMaskIntoConstraints = false
  777. spacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  778. let headerRow = NSStackView(views: [title, spacer, addButton])
  779. headerRow.orientation = .horizontal
  780. headerRow.alignment = .centerY
  781. headerRow.distribution = .fill
  782. headerRow.spacing = 12
  783. headerRow.userInterfaceLayoutDirection = .leftToRight
  784. ProfileLayoutEnforcement.applyForcedLTR(to: headerRow)
  785. headerRow.translatesAutoresizingMaskIntoConstraints = false
  786. workExperienceRowsStack.translatesAutoresizingMaskIntoConstraints = false
  787. workExperienceRowsStack.orientation = .vertical
  788. workExperienceRowsStack.spacing = 20
  789. workExperienceRowsStack.alignment = .leading
  790. workExperienceRowsStack.userInterfaceLayoutDirection = .leftToRight
  791. ProfileLayoutEnforcement.applyForcedLTR(to: workExperienceRowsStack)
  792. let outer = NSStackView(views: [headerRow, workExperienceRowsStack])
  793. outer.orientation = .vertical
  794. outer.spacing = 16
  795. outer.alignment = .leading
  796. outer.translatesAutoresizingMaskIntoConstraints = false
  797. outer.userInterfaceLayoutDirection = .leftToRight
  798. ProfileLayoutEnforcement.applyForcedLTR(to: outer)
  799. outer.pinAllArrangedSubviewWidthsEqualToStackWidth()
  800. return outer
  801. }
  802. private func educationSection() -> NSView {
  803. let title = NSTextField(labelWithString: "Education")
  804. title.font = .systemFont(ofSize: 15, weight: .semibold)
  805. title.textColor = ProfilePagePalette.primaryText
  806. title.translatesAutoresizingMaskIntoConstraints = false
  807. title.setContentHuggingPriority(.defaultLow, for: .horizontal)
  808. ProfileLayoutEnforcement.applyLeftAlignedTextField(title)
  809. let addButton = NSButton(title: "+ Add Another", target: self, action: #selector(didTapAddEducation))
  810. addButton.translatesAutoresizingMaskIntoConstraints = false
  811. addButton.bezelStyle = .rounded
  812. addButton.isBordered = true
  813. addButton.font = .systemFont(ofSize: 12, weight: .medium)
  814. addButton.setContentHuggingPriority(.required, for: .horizontal)
  815. let spacer = NSView()
  816. spacer.translatesAutoresizingMaskIntoConstraints = false
  817. spacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  818. let headerRow = NSStackView(views: [title, spacer, addButton])
  819. headerRow.orientation = .horizontal
  820. headerRow.alignment = .centerY
  821. headerRow.distribution = .fill
  822. headerRow.spacing = 12
  823. headerRow.userInterfaceLayoutDirection = .leftToRight
  824. ProfileLayoutEnforcement.applyForcedLTR(to: headerRow)
  825. headerRow.translatesAutoresizingMaskIntoConstraints = false
  826. educationRowsStack.translatesAutoresizingMaskIntoConstraints = false
  827. educationRowsStack.orientation = .vertical
  828. educationRowsStack.spacing = 16
  829. educationRowsStack.alignment = .leading
  830. educationRowsStack.userInterfaceLayoutDirection = .leftToRight
  831. ProfileLayoutEnforcement.applyForcedLTR(to: educationRowsStack)
  832. let outer = NSStackView(views: [headerRow, educationRowsStack])
  833. outer.orientation = .vertical
  834. outer.spacing = 16
  835. outer.alignment = .leading
  836. outer.translatesAutoresizingMaskIntoConstraints = false
  837. outer.userInterfaceLayoutDirection = .leftToRight
  838. ProfileLayoutEnforcement.applyForcedLTR(to: outer)
  839. outer.pinAllArrangedSubviewWidthsEqualToStackWidth()
  840. return outer
  841. }
  842. private func appendWorkExperienceEntry() {
  843. let entry = WorkExperienceEntryView()
  844. entry.translatesAutoresizingMaskIntoConstraints = false
  845. if let compact = lastCompactLayout {
  846. entry.applyCompactLayout(compact)
  847. }
  848. entry.onDelete = { [weak self, weak entry] in
  849. guard let self, let entry else { return }
  850. self.removeWorkExperienceEntry(entry)
  851. }
  852. workExperienceEntries.append(entry)
  853. workExperienceRowsStack.addArrangedSubview(entry)
  854. entry.widthAnchor.constraint(equalTo: workExperienceRowsStack.widthAnchor).isActive = true
  855. renumberWorkExperienceEntries()
  856. refreshWorkExperienceDeleteButtons()
  857. }
  858. private func removeWorkExperienceEntry(_ entry: WorkExperienceEntryView) {
  859. guard workExperienceEntries.count > 1 else { return }
  860. workExperienceEntries.removeAll { $0 === entry }
  861. workExperienceRowsStack.removeArrangedSubview(entry)
  862. entry.removeFromSuperview()
  863. renumberWorkExperienceEntries()
  864. refreshWorkExperienceDeleteButtons()
  865. }
  866. private func renumberWorkExperienceEntries() {
  867. for (i, entry) in workExperienceEntries.enumerated() {
  868. entry.setExperienceIndex(i + 1)
  869. }
  870. }
  871. private func refreshWorkExperienceDeleteButtons() {
  872. let hide = workExperienceEntries.count <= 1
  873. for entry in workExperienceEntries {
  874. entry.setDeleteHidden(hide)
  875. }
  876. }
  877. private func appendEducationEntry() {
  878. let entry = EducationEntryView()
  879. entry.translatesAutoresizingMaskIntoConstraints = false
  880. if let compact = lastCompactLayout {
  881. entry.applyCompactLayout(compact)
  882. }
  883. entry.onDelete = { [weak self, weak entry] in
  884. guard let self, let entry else { return }
  885. self.removeEducationEntry(entry)
  886. }
  887. educationEntries.append(entry)
  888. educationRowsStack.addArrangedSubview(entry)
  889. entry.widthAnchor.constraint(equalTo: educationRowsStack.widthAnchor).isActive = true
  890. renumberEducationEntries()
  891. refreshEducationDeleteButtons()
  892. }
  893. private func removeEducationEntry(_ entry: EducationEntryView) {
  894. guard educationEntries.count > 1 else { return }
  895. educationEntries.removeAll { $0 === entry }
  896. educationRowsStack.removeArrangedSubview(entry)
  897. entry.removeFromSuperview()
  898. renumberEducationEntries()
  899. refreshEducationDeleteButtons()
  900. }
  901. private func renumberEducationEntries() {
  902. for (i, entry) in educationEntries.enumerated() {
  903. entry.setEducationIndex(i + 1)
  904. }
  905. }
  906. private func refreshEducationDeleteButtons() {
  907. let hide = educationEntries.count <= 1
  908. for entry in educationEntries {
  909. entry.setDeleteHidden(hide)
  910. }
  911. }
  912. @objc private func didTapAddWorkExperience() {
  913. appendWorkExperienceEntry()
  914. }
  915. @objc private func didTapAddEducation() {
  916. appendEducationEntry()
  917. }
  918. private func saveButtonHost() -> NSView {
  919. saveButton.translatesAutoresizingMaskIntoConstraints = false
  920. saveButton.setContentHuggingPriority(.defaultLow, for: .horizontal)
  921. let host = NSView()
  922. host.translatesAutoresizingMaskIntoConstraints = false
  923. host.userInterfaceLayoutDirection = .leftToRight
  924. ProfileLayoutEnforcement.applyForcedLTR(to: host)
  925. host.addSubview(saveButton)
  926. let preferredWidth = saveButton.widthAnchor.constraint(equalToConstant: 292)
  927. preferredWidth.priority = .defaultHigh
  928. NSLayoutConstraint.activate([
  929. saveButton.centerXAnchor.constraint(equalTo: host.centerXAnchor),
  930. saveButton.topAnchor.constraint(equalTo: host.topAnchor),
  931. saveButton.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  932. saveButton.heightAnchor.constraint(equalToConstant: 54),
  933. preferredWidth,
  934. saveButton.widthAnchor.constraint(lessThanOrEqualTo: host.widthAnchor)
  935. ])
  936. return host
  937. }
  938. @objc private func didTapSave() {
  939. window?.makeFirstResponder(nil)
  940. let profile = captureSavedProfileForSave()
  941. var missing: [String] = []
  942. if profile.profileDisplayName.isEmpty { missing.append("Profile name") }
  943. if profile.personal.fullName.isEmpty { missing.append("Full Name") }
  944. if profile.personal.email.isEmpty { missing.append("Email") }
  945. if profile.personal.jobTitle.isEmpty { missing.append("Job Title") }
  946. guard missing.isEmpty else {
  947. let alert = NSAlert()
  948. alert.messageText = "Complete required fields"
  949. alert.informativeText = "Please fill in: " + missing.joined(separator: ", ") + "."
  950. alert.alertStyle = .informational
  951. alert.addButton(withTitle: "OK")
  952. if let window = window {
  953. alert.beginSheetModal(for: window) { _ in }
  954. } else {
  955. alert.runModal()
  956. }
  957. return
  958. }
  959. SavedProfilesStore.upsert(profile)
  960. editingProfileID = profile.id
  961. onDismiss?()
  962. }
  963. }
  964. // MARK: - Work experience & education rows
  965. private enum ProfileEntryCardLayout {
  966. /// Horizontal inset of fields inside each work/education entry card (matches main profile form).
  967. static let horizontalInset: CGFloat = 28
  968. }
  969. private final class WorkExperienceEntryView: NSView {
  970. var onDelete: (() -> Void)?
  971. private let subtitleLabel = NSTextField(labelWithString: "Experience 1")
  972. private let deleteButton = NSButton()
  973. private let jobTitleField = NSTextField()
  974. private let companyField = NSTextField()
  975. private let durationField = NSTextField()
  976. private let descriptionField = NSTextField()
  977. private var jobCompanyRow: ProfileDualFieldRow!
  978. override init(frame frameRect: NSRect) {
  979. super.init(frame: frameRect)
  980. configure()
  981. }
  982. required init?(coder: NSCoder) {
  983. fatalError("init(coder:) has not been implemented")
  984. }
  985. func setExperienceIndex(_ index: Int) {
  986. subtitleLabel.stringValue = "Experience \(index)"
  987. }
  988. func setDeleteHidden(_ hidden: Bool) {
  989. deleteButton.isHidden = hidden
  990. }
  991. func applyCompactLayout(_ compact: Bool) {
  992. jobCompanyRow.setCompact(compact)
  993. }
  994. func capturePayload() -> WorkExperiencePayload {
  995. WorkExperiencePayload(
  996. jobTitle: jobTitleField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  997. company: companyField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  998. duration: durationField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  999. description: descriptionField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  1000. )
  1001. }
  1002. func applyPayload(_ payload: WorkExperiencePayload) {
  1003. jobTitleField.stringValue = payload.jobTitle
  1004. companyField.stringValue = payload.company
  1005. durationField.stringValue = payload.duration
  1006. descriptionField.stringValue = payload.description
  1007. }
  1008. private func configure() {
  1009. userInterfaceLayoutDirection = .leftToRight
  1010. ProfileLayoutEnforcement.applyForcedLTR(to: self)
  1011. wantsLayer = true
  1012. layer?.cornerRadius = 14
  1013. layer?.borderWidth = 1
  1014. layer?.borderColor = ProfilePagePalette.border.cgColor
  1015. layer?.backgroundColor = ProfilePagePalette.cardBackground.cgColor
  1016. layer?.masksToBounds = false
  1017. layer?.shadowColor = NSColor.black.cgColor
  1018. layer?.shadowOpacity = 0.05
  1019. layer?.shadowRadius = 12
  1020. layer?.shadowOffset = CGSize(width: 0, height: 6)
  1021. if #available(macOS 11.0, *) {
  1022. layer?.cornerCurve = .continuous
  1023. }
  1024. subtitleLabel.font = .systemFont(ofSize: 12, weight: .medium)
  1025. subtitleLabel.textColor = ProfilePagePalette.secondaryText
  1026. subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
  1027. ProfileLayoutEnforcement.applyLeftAlignedTextField(subtitleLabel)
  1028. deleteButton.translatesAutoresizingMaskIntoConstraints = false
  1029. deleteButton.isBordered = false
  1030. deleteButton.bezelStyle = .regularSquare
  1031. deleteButton.focusRingType = .none
  1032. deleteButton.contentTintColor = ProfilePagePalette.destructive
  1033. deleteButton.target = self
  1034. deleteButton.action = #selector(didTapDelete)
  1035. if #available(macOS 11.0, *) {
  1036. deleteButton.image = NSImage(systemSymbolName: "trash", accessibilityDescription: "Remove experience")
  1037. deleteButton.imagePosition = .imageOnly
  1038. } else {
  1039. deleteButton.title = "Remove"
  1040. deleteButton.font = .systemFont(ofSize: 12, weight: .medium)
  1041. }
  1042. let headerSpacer = NSView()
  1043. headerSpacer.translatesAutoresizingMaskIntoConstraints = false
  1044. headerSpacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1045. let headerRow = NSStackView(views: [subtitleLabel, headerSpacer, deleteButton])
  1046. headerRow.orientation = .horizontal
  1047. headerRow.alignment = .centerY
  1048. headerRow.distribution = .fill
  1049. headerRow.spacing = 8
  1050. headerRow.userInterfaceLayoutDirection = .leftToRight
  1051. ProfileLayoutEnforcement.applyForcedLTR(to: headerRow)
  1052. headerRow.translatesAutoresizingMaskIntoConstraints = false
  1053. let jobGroup = Self.labeledFieldStack(title: "Job Title *", field: jobTitleField, placeholder: "e.g., Software Engineer")
  1054. let companyGroup = Self.labeledFieldStack(title: "Company Name *", field: companyField, placeholder: "e.g., Google")
  1055. jobCompanyRow = ProfileDualFieldRow(left: jobGroup, right: companyGroup, spacing: 12)
  1056. let durationGroup = Self.labeledFieldStack(title: "Duration *", field: durationField, placeholder: "e.g., Jan 2020 - Present")
  1057. let descriptionGroup = Self.multilineLabeledStack(
  1058. title: "Description",
  1059. field: descriptionField,
  1060. placeholder: "Describe your responsibilities and achievements...",
  1061. minHeight: 120
  1062. )
  1063. let inner = NSStackView(views: [headerRow, jobCompanyRow, durationGroup, descriptionGroup])
  1064. inner.orientation = .vertical
  1065. inner.spacing = 16
  1066. inner.alignment = .leading
  1067. inner.translatesAutoresizingMaskIntoConstraints = false
  1068. inner.edgeInsets = NSEdgeInsets()
  1069. inner.userInterfaceLayoutDirection = .leftToRight
  1070. ProfileLayoutEnforcement.applyForcedLTR(to: inner)
  1071. inner.pinAllArrangedSubviewWidthsEqualToStackWidth()
  1072. addSubview(inner)
  1073. NSLayoutConstraint.activate([
  1074. inner.leadingAnchor.constraint(equalTo: leadingAnchor, constant: ProfileEntryCardLayout.horizontalInset),
  1075. inner.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -ProfileEntryCardLayout.horizontalInset),
  1076. inner.topAnchor.constraint(equalTo: topAnchor, constant: 16),
  1077. inner.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -16)
  1078. ])
  1079. }
  1080. override func layout() {
  1081. super.layout()
  1082. for field in [jobTitleField, companyField, durationField, descriptionField] {
  1083. if let wrap = field.superview, wrap.bounds.width > 2 {
  1084. let w = max(1, wrap.bounds.width - 24)
  1085. if abs(field.preferredMaxLayoutWidth - w) > 0.5 {
  1086. field.preferredMaxLayoutWidth = w
  1087. }
  1088. }
  1089. }
  1090. guard let layer = layer, layer.shadowOpacity > 0 else { return }
  1091. let r = layer.cornerRadius
  1092. layer.shadowPath = CGPath(roundedRect: bounds, cornerWidth: r, cornerHeight: r, transform: nil)
  1093. }
  1094. @objc private func didTapDelete() {
  1095. onDelete?()
  1096. }
  1097. fileprivate static func labeledFieldStack(title: String, field: NSTextField, placeholder: String) -> NSView {
  1098. let label = NSTextField(labelWithString: title)
  1099. label.font = .systemFont(ofSize: 12, weight: .medium)
  1100. label.textColor = ProfilePagePalette.secondaryText
  1101. label.translatesAutoresizingMaskIntoConstraints = false
  1102. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  1103. styleSingleLineField(field, placeholder: placeholder)
  1104. let wrap = roundedChrome(around: field, minHeight: 40)
  1105. let stack = NSStackView(views: [label, wrap])
  1106. stack.orientation = .vertical
  1107. stack.spacing = 8
  1108. stack.alignment = .leading
  1109. stack.translatesAutoresizingMaskIntoConstraints = false
  1110. stack.userInterfaceLayoutDirection = .leftToRight
  1111. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  1112. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1113. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1114. wrap.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  1115. NSLayoutConstraint.activate([
  1116. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  1117. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  1118. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  1119. ])
  1120. return stack
  1121. }
  1122. private static func multilineLabeledStack(title: String, field: NSTextField, placeholder: String, minHeight: CGFloat) -> NSView {
  1123. let label = NSTextField(labelWithString: title)
  1124. label.font = .systemFont(ofSize: 12, weight: .medium)
  1125. label.textColor = ProfilePagePalette.secondaryText
  1126. label.translatesAutoresizingMaskIntoConstraints = false
  1127. ProfileLayoutEnforcement.applyLeftAlignedTextField(label)
  1128. field.translatesAutoresizingMaskIntoConstraints = false
  1129. field.isEditable = true
  1130. field.isSelectable = true
  1131. field.isBordered = false
  1132. field.drawsBackground = false
  1133. field.focusRingType = .none
  1134. field.font = .systemFont(ofSize: 14, weight: .regular)
  1135. field.textColor = ProfilePagePalette.primaryText
  1136. field.maximumNumberOfLines = 0
  1137. field.cell?.wraps = true
  1138. field.cell?.isScrollable = false
  1139. field.cell?.usesSingleLineMode = false
  1140. field.placeholderAttributedString = NSAttributedString(
  1141. string: placeholder,
  1142. attributes: [
  1143. .foregroundColor: ProfilePagePalette.secondaryText,
  1144. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  1145. .paragraphStyle: ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  1146. ]
  1147. )
  1148. ProfileLayoutEnforcement.applyLeftAlignedTextField(field)
  1149. let wrap = NSView()
  1150. wrap.translatesAutoresizingMaskIntoConstraints = false
  1151. wrap.wantsLayer = true
  1152. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  1153. wrap.layer?.cornerRadius = 10
  1154. wrap.layer?.borderWidth = 1
  1155. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  1156. if #available(macOS 11.0, *) {
  1157. wrap.layer?.cornerCurve = .continuous
  1158. }
  1159. wrap.addSubview(field)
  1160. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  1161. NSLayoutConstraint.activate([
  1162. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  1163. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  1164. field.topAnchor.constraint(equalTo: wrap.topAnchor, constant: 10),
  1165. field.bottomAnchor.constraint(equalTo: wrap.bottomAnchor, constant: -10),
  1166. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  1167. ])
  1168. let stack = NSStackView(views: [label, wrap])
  1169. stack.orientation = .vertical
  1170. stack.spacing = 8
  1171. stack.alignment = .leading
  1172. stack.translatesAutoresizingMaskIntoConstraints = false
  1173. stack.userInterfaceLayoutDirection = .leftToRight
  1174. ProfileLayoutEnforcement.applyForcedLTR(to: stack)
  1175. stack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1176. wrap.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1177. NSLayoutConstraint.activate([
  1178. label.leftAnchor.constraint(equalTo: stack.leftAnchor),
  1179. label.widthAnchor.constraint(equalTo: stack.widthAnchor),
  1180. wrap.widthAnchor.constraint(equalTo: stack.widthAnchor)
  1181. ])
  1182. return stack
  1183. }
  1184. private static func styleSingleLineField(_ field: NSTextField, placeholder: String) {
  1185. field.translatesAutoresizingMaskIntoConstraints = false
  1186. field.isBordered = false
  1187. field.drawsBackground = false
  1188. field.focusRingType = .none
  1189. field.font = .systemFont(ofSize: 14, weight: .regular)
  1190. field.textColor = ProfilePagePalette.primaryText
  1191. field.placeholderAttributedString = NSAttributedString(
  1192. string: placeholder,
  1193. attributes: [
  1194. .foregroundColor: ProfilePagePalette.secondaryText,
  1195. .font: NSFont.systemFont(ofSize: 14, weight: .regular),
  1196. .paragraphStyle: ProfileLayoutEnforcement.leftAlignedParagraphStyle()
  1197. ]
  1198. )
  1199. field.cell?.usesSingleLineMode = true
  1200. field.cell?.wraps = false
  1201. field.cell?.isScrollable = true
  1202. ProfileLayoutEnforcement.applyLeftAlignedTextField(field)
  1203. }
  1204. private static func roundedChrome(around field: NSTextField, minHeight: CGFloat) -> NSView {
  1205. let wrap = NSView()
  1206. wrap.translatesAutoresizingMaskIntoConstraints = false
  1207. wrap.wantsLayer = true
  1208. wrap.layer?.backgroundColor = ProfilePagePalette.fieldFill.cgColor
  1209. wrap.layer?.cornerRadius = 10
  1210. wrap.layer?.borderWidth = 1
  1211. wrap.layer?.borderColor = ProfilePagePalette.border.cgColor
  1212. if #available(macOS 11.0, *) {
  1213. wrap.layer?.cornerCurve = .continuous
  1214. }
  1215. wrap.addSubview(field)
  1216. ProfileLayoutEnforcement.applyForcedLTR(to: wrap)
  1217. NSLayoutConstraint.activate([
  1218. field.leftAnchor.constraint(equalTo: wrap.leftAnchor, constant: 12),
  1219. field.rightAnchor.constraint(equalTo: wrap.rightAnchor, constant: -12),
  1220. field.centerYAnchor.constraint(equalTo: wrap.centerYAnchor),
  1221. wrap.heightAnchor.constraint(greaterThanOrEqualToConstant: minHeight)
  1222. ])
  1223. return wrap
  1224. }
  1225. }
  1226. private final class EducationEntryView: NSView {
  1227. var onDelete: (() -> Void)?
  1228. private let subtitleLabel = NSTextField(labelWithString: "Education 1")
  1229. private let deleteButton = NSButton()
  1230. private let degreeField = NSTextField()
  1231. private let institutionField = NSTextField()
  1232. private let yearField = NSTextField()
  1233. private var degreeInstitutionRow: ProfileDualFieldRow!
  1234. override init(frame frameRect: NSRect) {
  1235. super.init(frame: frameRect)
  1236. configure()
  1237. }
  1238. required init?(coder: NSCoder) {
  1239. fatalError("init(coder:) has not been implemented")
  1240. }
  1241. func setEducationIndex(_ index: Int) {
  1242. subtitleLabel.stringValue = "Education \(index)"
  1243. }
  1244. func setDeleteHidden(_ hidden: Bool) {
  1245. deleteButton.isHidden = hidden
  1246. }
  1247. func applyCompactLayout(_ compact: Bool) {
  1248. degreeInstitutionRow.setCompact(compact)
  1249. }
  1250. func capturePayload() -> EducationPayload {
  1251. EducationPayload(
  1252. degree: degreeField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  1253. institution: institutionField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines),
  1254. year: yearField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  1255. )
  1256. }
  1257. func applyPayload(_ payload: EducationPayload) {
  1258. degreeField.stringValue = payload.degree
  1259. institutionField.stringValue = payload.institution
  1260. yearField.stringValue = payload.year
  1261. }
  1262. private func configure() {
  1263. userInterfaceLayoutDirection = .leftToRight
  1264. ProfileLayoutEnforcement.applyForcedLTR(to: self)
  1265. wantsLayer = true
  1266. layer?.cornerRadius = 14
  1267. layer?.borderWidth = 1
  1268. layer?.borderColor = ProfilePagePalette.border.cgColor
  1269. layer?.backgroundColor = ProfilePagePalette.cardBackground.cgColor
  1270. layer?.masksToBounds = false
  1271. layer?.shadowColor = NSColor.black.cgColor
  1272. layer?.shadowOpacity = 0.04
  1273. layer?.shadowRadius = 10
  1274. layer?.shadowOffset = CGSize(width: 0, height: 5)
  1275. if #available(macOS 11.0, *) {
  1276. layer?.cornerCurve = .continuous
  1277. }
  1278. subtitleLabel.font = .systemFont(ofSize: 12, weight: .medium)
  1279. subtitleLabel.textColor = ProfilePagePalette.secondaryText
  1280. subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
  1281. ProfileLayoutEnforcement.applyLeftAlignedTextField(subtitleLabel)
  1282. deleteButton.translatesAutoresizingMaskIntoConstraints = false
  1283. deleteButton.isBordered = false
  1284. deleteButton.bezelStyle = .regularSquare
  1285. deleteButton.focusRingType = .none
  1286. deleteButton.contentTintColor = ProfilePagePalette.destructive
  1287. deleteButton.target = self
  1288. deleteButton.action = #selector(didTapDelete)
  1289. if #available(macOS 11.0, *) {
  1290. deleteButton.image = NSImage(systemSymbolName: "trash", accessibilityDescription: "Remove education")
  1291. deleteButton.imagePosition = .imageOnly
  1292. } else {
  1293. deleteButton.title = "Remove"
  1294. deleteButton.font = .systemFont(ofSize: 12, weight: .medium)
  1295. }
  1296. let headerSpacer = NSView()
  1297. headerSpacer.translatesAutoresizingMaskIntoConstraints = false
  1298. headerSpacer.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1299. let headerRow = NSStackView(views: [subtitleLabel, headerSpacer, deleteButton])
  1300. headerRow.orientation = .horizontal
  1301. headerRow.alignment = .centerY
  1302. headerRow.distribution = .fill
  1303. headerRow.spacing = 8
  1304. headerRow.userInterfaceLayoutDirection = .leftToRight
  1305. ProfileLayoutEnforcement.applyForcedLTR(to: headerRow)
  1306. headerRow.translatesAutoresizingMaskIntoConstraints = false
  1307. let degreeGroup = WorkExperienceEntryView.labeledFieldStack(
  1308. title: "Degree / program *",
  1309. field: degreeField,
  1310. placeholder: "e.g., BSc Computer Science"
  1311. )
  1312. let institutionGroup = WorkExperienceEntryView.labeledFieldStack(
  1313. title: "Institution *",
  1314. field: institutionField,
  1315. placeholder: "e.g., MIT"
  1316. )
  1317. degreeInstitutionRow = ProfileDualFieldRow(left: degreeGroup, right: institutionGroup, spacing: 12)
  1318. let yearGroup = WorkExperienceEntryView.labeledFieldStack(
  1319. title: "Year *",
  1320. field: yearField,
  1321. placeholder: "e.g., 2020"
  1322. )
  1323. let inner = NSStackView(views: [headerRow, degreeInstitutionRow, yearGroup])
  1324. inner.orientation = .vertical
  1325. inner.spacing = 14
  1326. inner.alignment = .leading
  1327. inner.translatesAutoresizingMaskIntoConstraints = false
  1328. inner.edgeInsets = NSEdgeInsets()
  1329. inner.userInterfaceLayoutDirection = .leftToRight
  1330. ProfileLayoutEnforcement.applyForcedLTR(to: inner)
  1331. inner.pinAllArrangedSubviewWidthsEqualToStackWidth()
  1332. addSubview(inner)
  1333. NSLayoutConstraint.activate([
  1334. inner.leadingAnchor.constraint(equalTo: leadingAnchor, constant: ProfileEntryCardLayout.horizontalInset),
  1335. inner.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -ProfileEntryCardLayout.horizontalInset),
  1336. inner.topAnchor.constraint(equalTo: topAnchor, constant: 14),
  1337. inner.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -14)
  1338. ])
  1339. }
  1340. override func layout() {
  1341. super.layout()
  1342. for field in [degreeField, institutionField, yearField] {
  1343. if let wrap = field.superview, wrap.bounds.width > 2 {
  1344. let w = max(1, wrap.bounds.width - 24)
  1345. if abs(field.preferredMaxLayoutWidth - w) > 0.5 {
  1346. field.preferredMaxLayoutWidth = w
  1347. }
  1348. }
  1349. }
  1350. guard let layer = layer, layer.shadowOpacity > 0 else { return }
  1351. let r = layer.cornerRadius
  1352. layer.shadowPath = CGPath(roundedRect: bounds, cornerWidth: r, cornerHeight: r, transform: nil)
  1353. }
  1354. @objc private func didTapDelete() {
  1355. onDelete?()
  1356. }
  1357. }
  1358. // MARK: - Primary CTA
  1359. private final class ProfilePrimaryButton: NSButton {
  1360. private var trackingArea: NSTrackingArea?
  1361. private var didPushCursor = false
  1362. override init(frame frameRect: NSRect) {
  1363. super.init(frame: frameRect)
  1364. commonInit()
  1365. }
  1366. required init?(coder: NSCoder) {
  1367. super.init(coder: coder)
  1368. commonInit()
  1369. }
  1370. convenience init(title: String, target: AnyObject?, action: Selector?) {
  1371. self.init(frame: .zero)
  1372. self.title = title
  1373. self.target = target
  1374. self.action = action
  1375. }
  1376. private func commonInit() {
  1377. bezelStyle = .rounded
  1378. isBordered = false
  1379. font = .systemFont(ofSize: 16, weight: .semibold)
  1380. contentTintColor = .white
  1381. wantsLayer = true
  1382. layer?.cornerRadius = 14
  1383. if #available(macOS 11.0, *) {
  1384. layer?.cornerCurve = .continuous
  1385. }
  1386. layer?.backgroundColor = ProfilePagePalette.brandBlue.cgColor
  1387. }
  1388. override func updateTrackingAreas() {
  1389. super.updateTrackingAreas()
  1390. if let trackingArea { removeTrackingArea(trackingArea) }
  1391. let area = NSTrackingArea(
  1392. rect: bounds,
  1393. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1394. owner: self,
  1395. userInfo: nil
  1396. )
  1397. addTrackingArea(area)
  1398. trackingArea = area
  1399. }
  1400. override func mouseEntered(with event: NSEvent) {
  1401. super.mouseEntered(with: event)
  1402. layer?.backgroundColor = ProfilePagePalette.brandBlueHover.cgColor
  1403. if !didPushCursor {
  1404. NSCursor.pointingHand.push()
  1405. didPushCursor = true
  1406. }
  1407. }
  1408. override func mouseExited(with event: NSEvent) {
  1409. super.mouseExited(with: event)
  1410. layer?.backgroundColor = ProfilePagePalette.brandBlue.cgColor
  1411. if didPushCursor {
  1412. NSCursor.pop()
  1413. didPushCursor = false
  1414. }
  1415. }
  1416. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1417. super.viewWillMove(toWindow: newWindow)
  1418. if newWindow == nil, didPushCursor {
  1419. NSCursor.pop()
  1420. didPushCursor = false
  1421. }
  1422. }
  1423. }