Skip to content

Commit

Permalink
minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
dkk committed Dec 3, 2022
1 parent 110c501 commit 321dccb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
27 changes: 15 additions & 12 deletions Sources/WrappingHStack/InternalWrappingHStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@ struct InternalWrappingHStack: View {
case .newLine:
return (firstItemOfEachLine + [contentIterator.offset], width)
case .any(let anyView) where Self.isVisible(view: anyView):
#if os(iOS)
let hostingController = UIHostingController(rootView: HStack(spacing: spacing.estimatedSpacing) { anyView })
#else
let hostingController = NSHostingController(rootView: HStack(spacing: spacing.estimatedSpacing) { anyView })
#endif
let itemWidth = hostingController.sizeThatFits(in: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width

if result.currentLineWidth + itemWidth + spacing.estimatedSpacing > width {
let itemWidth = Self.getWidth(of: anyView)
if result.currentLineWidth + itemWidth + spacing.minSpacing > width {
currentLineWidth = itemWidth
firstItemOfEachLine.append(contentIterator.offset)
} else {
currentLineWidth += itemWidth + spacing.estimatedSpacing
currentLineWidth += itemWidth + spacing.minSpacing
}
return (firstItemOfEachLine, currentLineWidth)
default:
return result
}
}.0
}

static func getWidth(of anyView: AnyView) -> Double {
#if os(iOS)
let hostingController = UIHostingController(rootView: HStack { anyView })
#else
let hostingController = NSHostingController(rootView: HStack { anyView })
#endif
return hostingController.sizeThatFits(in: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width
}

var totalLines: Int {
firstItemOfEachLine.count
Expand Down Expand Up @@ -91,7 +94,7 @@ struct InternalWrappingHStack: View {
if case .dynamicIncludingBorders = spacing,
startOf(line: lineIndex) == $0
{
Spacer(minLength: spacing.estimatedSpacing)
Spacer(minLength: spacing.minSpacing)
}

if case .any(let anyView) = content[$0], Self.isVisible(view: anyView) {
Expand All @@ -104,11 +107,11 @@ struct InternalWrappingHStack: View {
Spacer(minLength: 0)
.frame(width: exactSpacing)
} else {
Spacer(minLength: spacing.estimatedSpacing)
Spacer(minLength: spacing.minSpacing)
}
}
} else if case .dynamicIncludingBorders = spacing {
Spacer(minLength: spacing.estimatedSpacing)
Spacer(minLength: spacing.minSpacing)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/WrappingHStack/WrappingHStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct WrappingHStack: View {
case dynamic(minSpacing: CGFloat)
case dynamicIncludingBorders(minSpacing: CGFloat)

internal var estimatedSpacing: CGFloat {
internal var minSpacing: CGFloat {
switch self {
case .constant(let constantSpacing):
return constantSpacing
Expand Down

0 comments on commit 321dccb

Please sign in to comment.