Sen descrición

MyProfilePageView.swift 75KB

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