Pārlūkot izejas kodu

Fix CV preview layout when editing in place.

Editable NSTextFields reported a tiny fitting width inside the preview stack, which collapsed the document to a vertical strip. Pin intrinsic width to the card, relax horizontal hugging, and refresh wrapping fields’ preferredMaxLayoutWidth on layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
AhtashamShahzad1 2 mēneši atpakaļ
vecāks
revīzija
196c3897c3
1 mainītis faili ar 49 papildinājumiem un 1 dzēšanām
  1. 49 1
      App for Indeed/Views/CVProfileDocumentView.swift

+ 49 - 1
App for Indeed/Views/CVProfileDocumentView.swift

@@ -180,6 +180,11 @@ private struct DocumentStyle {
 /// Full-width résumé layout that injects `SavedProfile` into the visual language of `CVTemplate`.
 final class CVProfileDocumentView: NSView {
 
+    /// Card width used in the CV preview; also the horizontal fitting size for this view.
+    /// Without this, a parent `NSStackView` that only pins `width ≤ …` sizes the document from
+    /// editable `NSTextField` intrinsic widths (~0) and the whole page collapses to a thin strip.
+    private static let cardWidth: CGFloat = 640
+
     private let profile: SavedProfile
     private let template: CVTemplate
     private let style: DocumentStyle
@@ -198,6 +203,8 @@ final class CVProfileDocumentView: NSView {
         wantsLayer = true
         layer?.backgroundColor = NSColor.clear.cgColor
         userInterfaceLayoutDirection = .leftToRight
+        // Let the preview stack stretch us to the scroll view width; don’t shrink to editable fields.
+        setContentHuggingPriority(.defaultLow, for: .horizontal)
 
         let card = NSView()
         card.translatesAutoresizingMaskIntoConstraints = false
@@ -218,7 +225,7 @@ final class CVProfileDocumentView: NSView {
             card.trailingAnchor.constraint(equalTo: trailingAnchor),
             card.topAnchor.constraint(equalTo: topAnchor),
             card.bottomAnchor.constraint(equalTo: bottomAnchor),
-            card.widthAnchor.constraint(equalToConstant: 640),
+            card.widthAnchor.constraint(equalToConstant: Self.cardWidth),
 
             root.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 36),
             root.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -36),
@@ -232,6 +239,41 @@ final class CVProfileDocumentView: NSView {
         fatalError("init(coder:) has not been implemented")
     }
 
+    override var intrinsicContentSize: NSSize {
+        NSSize(width: Self.cardWidth, height: NSView.noIntrinsicMetric)
+    }
+
+    override func layout() {
+        super.layout()
+        // Editable wrapping `NSTextField`s default to a very small intrinsic width until
+        // `preferredMaxLayoutWidth` tracks the column width — stacks then collapse and text
+        // reflows like a narrow strip (broken CV layout in “Edit text in place” mode).
+        updateWrappingTextPreferredWidths()
+    }
+
+    /// Any wrapping body (`maximumNumberOfLines == 0`) needs a concrete wrap width inside stack-driven layout.
+    private func updateWrappingTextPreferredWidths() {
+        for field in Self.collectWrappingTextFields(in: self) {
+            guard let parent = field.superview, parent.bounds.width > 2 else { continue }
+            let w = parent.bounds.width
+            if abs(field.preferredMaxLayoutWidth - w) > 0.5 {
+                field.preferredMaxLayoutWidth = w
+            }
+        }
+    }
+
+    private static func collectWrappingTextFields(in root: NSView) -> [NSTextField] {
+        var out: [NSTextField] = []
+        func visit(_ v: NSView) {
+            if let tf = v as? NSTextField, tf.maximumNumberOfLines == 0 {
+                out.append(tf)
+            }
+            for c in v.subviews { visit(c) }
+        }
+        visit(root)
+        return out
+    }
+
     // MARK: - Composition
 
     private func buildRoot() -> NSView {
@@ -1228,6 +1270,12 @@ final class CVProfileDocumentView: NSView {
             t.isBordered = false
             t.drawsBackground = false
             t.focusRingType = .default
+            t.usesSingleLineMode = false
+            if isWrapping, let cell = t.cell as? NSTextFieldCell {
+                cell.wraps = true
+                cell.isScrollable = false
+            }
+            t.setContentHuggingPriority(.defaultLow, for: .horizontal)
         }
         return t
     }