Нет описания

MyProfilePageView.swift 78KB

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