Brak opisu

MyProfilePageView.swift 73KB

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