Skip to content

Commit

Permalink
ScrollView infinity safety
Browse files Browse the repository at this point in the history
  • Loading branch information
watt committed Apr 11, 2023
1 parent 44ab84c commit 21a3cd5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 deletions.
10 changes: 8 additions & 2 deletions BlueprintUICommonControls/Sources/ScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ extension ScrollView {
result.width += contentInset.left + contentInset.right
result.height += contentInset.top + contentInset.bottom

result.width = min(result.width, proposal.width.maximum)
result.height = min(result.height, proposal.height.maximum)
if let maxWidth = proposal.width.constrainedValue {
result.width = min(result.width, maxWidth)
}
if let maxHeight = proposal.height.constrainedValue {
result.height = min(result.height, maxHeight)
}

return result
}
Expand All @@ -210,6 +214,8 @@ extension ScrollView {
insetSize.height -= contentInset.top + contentInset.bottom

var itemSize = fittedSize(in: .init(insetSize), subelement: subelement)
.replacingInfinity(with: insetSize)

if contentSize == .fittingHeight {
itemSize.width = insetSize.width
} else if contentSize == .fittingWidth {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions BlueprintUICommonControls/Tests/Sources/ScrollViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,50 @@ import BlueprintUI

class ScrollViewTests: XCTestCase {

// When a scrollview places a subelement that has an infinite size, it defaults to replacing
// those dimensions with the size of the scrollview itself.
func test_infiniteContent() throws {

struct InfiniteBox: Element {
var content: ElementContent {
ElementContent(intrinsicSize: .infinity)
}

func backingViewDescription(with context: ViewDescriptionContext) -> ViewDescription? {
UIView.describe { view in
view[\.backgroundColor] = .red
}
}
}

compareSnapshot(
of: InfiniteBox()
.scrollable(.fittingContent)
.inset(uniform: 10),
size: CGSize(width: 100, height: 100),
identifier: "fittingContent",
layoutModes: [.caffeinated]
)

compareSnapshot(
of: InfiniteBox()
.scrollable(.fittingWidth)
.inset(uniform: 10),
size: CGSize(width: 100, height: 100),
identifier: "fittingWidth",
layoutModes: [.caffeinated]
)

compareSnapshot(
of: InfiniteBox()
.scrollable(.fittingHeight)
.inset(uniform: 10),
size: CGSize(width: 100, height: 100),
identifier: "fittingHeight",
layoutModes: [.caffeinated]
)
}

func test_calculateContentInset() {
// No inset

Expand Down
31 changes: 21 additions & 10 deletions BlueprintUICommonControls/Tests/Sources/XCTestCaseExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,33 @@ extension XCTestCase {
size: CGSize? = nil,
identifier: String? = nil,
scale: CGFloat = 1,
layoutModes: [LayoutMode] = LayoutMode.testModes,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
) {
let view = BlueprintView(element: element)
view.name = "Snapshot Host"
for layoutMode in layoutModes {
let view = BlueprintView(element: element)
view.name = "Snapshot Host"
view.layoutMode = layoutMode

if let size = size {
view.frame = CGRect(origin: .zero, size: size)
} else {
view.sizeToFit()
view.frame.size.width.round(.up, by: scale)
view.frame.size.height.round(.up, by: scale)
}
if let size = size {
view.frame = CGRect(origin: .zero, size: size)
} else {
view.sizeToFit()
view.frame.size.width.round(.up, by: scale)
view.frame.size.height.round(.up, by: scale)
}

compareSnapshot(of: view, identifier: identifier, scale: scale, file: file, testName: testName, line: line)
compareSnapshot(
of: view,
identifier: identifier,
scale: scale,
file: file,
testName: testName,
line: line
)
}
}

}
Expand Down

0 comments on commit 21a3cd5

Please sign in to comment.