Skip to content
This repository was archived by the owner on Dec 27, 2020. It is now read-only.

Commit 76a06be

Browse files
committed
fixed columns
1 parent 9b57859 commit 76a06be

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Sources/Grid/FixedColumnsGridStyle.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ public struct FixedColumnsGridStyle: GridStyle {
4444
}
4545

4646
public func gridHeight(with geometry: GeometryProxy, itemsCount: Int) -> CGFloat {
47-
self.gridHeight(with: geometry, itemsCount: itemsCount, columns: self.columns, itemHeight: self.itemHeight, padding: self.padding, hSpacing: self.hSpacing)
47+
self.gridHeight(
48+
with: geometry,
49+
itemsCount: itemsCount,
50+
columns: self.columns,
51+
itemHeight: self.itemHeight,
52+
padding: self.padding,
53+
hSpacing: self.hSpacing,
54+
vSpacing: self.vSpacing
55+
)
4856
}
4957

5058
@inlinable func availableWidth(with geometry: GeometryProxy, padding: EdgeInsets, hSpacing: CGFloat) -> CGFloat {
@@ -56,19 +64,21 @@ public struct FixedColumnsGridStyle: GridStyle {
5664
let row = index / max(1, columns)
5765
let itemIndexAtRow = index % max(1, columns)
5866
let itemWidth = self.itemWidth(for: geometry, columns: columns, padding: padding, hSpacing: hSpacing)
59-
let x = ((itemWidth / 2) + CGFloat(itemIndexAtRow) * itemWidth) + padding.leading
67+
let hSpacingForItem = CGFloat(itemIndexAtRow) * hSpacing
68+
let x = ((itemWidth / 2) + CGFloat(itemIndexAtRow) * itemWidth) + padding.leading + hSpacingForItem
6069
let y = ((itemHeight / 2) + CGFloat(row) * itemHeight) + padding.top + (CGFloat(row) * vSpacing)
6170
return CGPoint(x: x, y: y)
6271
}
6372

6473
@inlinable func itemWidth(for geometry: GeometryProxy, columns: Int, padding: EdgeInsets, hSpacing: CGFloat) -> CGFloat {
6574
let availableWidth = self.availableWidth(with: geometry, padding: padding, hSpacing: hSpacing)
66-
return availableWidth / CGFloat(columns)
75+
let usableWidth = availableWidth - (CGFloat(columns - 1) * hSpacing)
76+
return usableWidth / CGFloat(columns)
6777
}
6878

69-
@inlinable func gridHeight(with geometry: GeometryProxy, itemsCount: Int, columns: Int, itemHeight: CGFloat, padding: EdgeInsets, hSpacing: CGFloat) -> CGFloat {
79+
@inlinable func gridHeight(with geometry: GeometryProxy, itemsCount: Int, columns: Int, itemHeight: CGFloat, padding: EdgeInsets, hSpacing: CGFloat, vSpacing: CGFloat) -> CGFloat {
7080
let rowCount = Int((CGFloat(itemsCount) / max(1.0, CGFloat(columns))).rounded(.up))
7181
let verticalPadding = padding.top + padding.bottom
72-
return CGFloat(rowCount) * itemHeight + verticalPadding
82+
return CGFloat(rowCount) * itemHeight + verticalPadding + (CGFloat(rowCount - 1) * vSpacing)
7383
}
7484
}

0 commit comments

Comments
 (0)