Fix: ContainerView respects frame width constraints

This commit is contained in:
phranck
2026-02-09 01:28:28 +01:00
parent ddd51cc417
commit 2a67398589
2 changed files with 27 additions and 1 deletions
+6 -1
View File
@@ -357,9 +357,14 @@ private struct _ContainerViewCore<Content: View, Footer: View>: View, Renderable
}
// Calculate inner width from title, body, AND footer.
// If a frame constraint is set (availableWidth is smaller than terminal),
// use that as the maximum width.
let titleWidth = title.map { $0.count + 4 } ?? 0 // " Title " + borders
let footerNaturalWidth = initialFooterBuffer?.width ?? 0
let innerWidth = max(titleWidth, bodyBuffer.width, footerNaturalWidth)
let contentBasedWidth = max(titleWidth, bodyBuffer.width, footerNaturalWidth)
// Respect frame constraints: innerWidth should not exceed innerContext.availableWidth
let innerWidth = min(contentBasedWidth, innerContext.availableWidth)
// Re-render footer constrained to the final innerWidth so that
// Spacer() fills exactly the container's inner width.
+21
View File
@@ -198,4 +198,25 @@ struct ListRenderingTests {
#expect(list.selectionMode == .single)
}
@Test("List respects frame width constraint")
func listRespectsFrameWidth() {
let context = createTestContext(width: 80)
var selection: String?
let list = List("Items", selection: Binding(
get: { selection },
set: { selection = $0 }
)) {
ForEach(["Alpha", "Beta", "Gamma"], id: \.self) { item in
Text(item)
}
}
.frame(width: 20)
let buffer = renderToBuffer(list, context: context)
// The list should be constrained to 20 characters width
#expect(buffer.width == 20)
}
}