From 321dccbf872d156f518cb17235535d5c88751bf4 Mon Sep 17 00:00:00 2001 From: Daniel Kloeck Date: Sat, 3 Dec 2022 11:39:18 +0100 Subject: [PATCH] minor improvement --- .../InternalWrappingHStack.swift | 27 ++++++++++--------- Sources/WrappingHStack/WrappingHStack.swift | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Sources/WrappingHStack/InternalWrappingHStack.swift b/Sources/WrappingHStack/InternalWrappingHStack.swift index 70ca2af..e3aad32 100644 --- a/Sources/WrappingHStack/InternalWrappingHStack.swift +++ b/Sources/WrappingHStack/InternalWrappingHStack.swift @@ -25,18 +25,12 @@ 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: @@ -44,6 +38,15 @@ struct InternalWrappingHStack: View { } }.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 @@ -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) { @@ -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) } } diff --git a/Sources/WrappingHStack/WrappingHStack.swift b/Sources/WrappingHStack/WrappingHStack.swift index f4f408e..e28f851 100644 --- a/Sources/WrappingHStack/WrappingHStack.swift +++ b/Sources/WrappingHStack/WrappingHStack.swift @@ -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