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

Commit 8c8fc9b

Browse files
authored
Merge pull request #13 from spacenation/zstack
ZStack and StyleStructs
2 parents eafea07 + df32971 commit 8c8fc9b

18 files changed

+443
-140
lines changed

Examples/GridExamples iOS/ContentView.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,46 @@ struct ContentView: View {
55

66
var body: some View {
77
TabView(selection: $selection) {
8-
OneColumnLayoutView()
8+
AutoColumnsLayoutView()
99
.tabItem {
1010
VStack {
11-
Image(systemName: "rectangle.grid.1x2.fill")
12-
Text("One Column")
11+
Image(systemName: "square.grid.3x2.fill")
12+
Text("Auto Columns")
1313
}
1414
}
1515
.tag(0)
16-
AutoColumnLayoutView()
16+
FixedColumnsLayoutView()
1717
.tabItem {
1818
VStack {
19-
Image(systemName: "square.grid.3x2.fill")
20-
Text("Auto Column")
19+
Image(systemName: "rectangle.split.3x3.fill")
20+
Text("Fixed Columns")
2121
}
2222
}
2323
.tag(1)
24+
SingleColumnLayoutView()
25+
.tabItem {
26+
VStack {
27+
Image(systemName: "rectangle.grid.1x2.fill")
28+
Text("One Column")
29+
}
30+
}
31+
.tag(2)
2432
PerformanceLayoutView()
2533
.tabItem {
2634
VStack {
2735
Image(systemName: "square.grid.4x3.fill")
2836
Text("Performance")
2937
}
3038
}
31-
.tag(2)
39+
.tag(3)
3240
BuilderLayoutView()
3341
.tabItem {
3442
VStack {
3543
Image(systemName: "rectangle.3.offgrid.fill")
3644
Text("Builder")
3745
}
3846
}
39-
.tag(3)
47+
.tag(4)
4048
}
4149
.accentColor(.purple)
4250
}

Examples/GridExamples macOS/AppDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
99
func applicationDidFinishLaunching(_ aNotification: Notification) {
1010
// Insert code here to initialize your application
1111
window = NSWindow(
12-
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
12+
contentRect: NSRect(x: 0, y: 0, width: 600, height: 600),
1313
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
1414
backing: .buffered, defer: false)
1515
window.center()
16+
window.minSize = CGSize(width: 600, height: 600)
1617
window.setFrameAutosaveName("Main Window")
1718

1819
window.contentView = NSHostingView(rootView: ContentView())

Examples/GridExamples macOS/ContentView.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,38 @@ struct ContentView: View {
66

77
var body: some View {
88
TabView(selection: $selection) {
9-
OneColumnLayoutView()
9+
AutoColumnsLayoutView()
1010
.tabItem {
11-
Text("One Column")
11+
Text("Auto Columns")
1212
}
1313
.tag(0)
14-
AutoColumnLayoutView()
14+
.frame(minWidth: 300)
15+
FixedColumnsLayoutView()
1516
.tabItem {
16-
Text("Auto Column")
17+
Text("Fixed Columns")
1718
}
1819
.tag(1)
20+
.frame(minWidth: 300)
21+
SingleColumnLayoutView()
22+
.tabItem {
23+
Text("One Column")
24+
}
25+
.tag(2)
26+
.frame(minWidth: 300)
1927
PerformanceLayoutView()
2028
.tabItem {
2129
Text("Performance")
2230
}
23-
.tag(2)
31+
.tag(3)
32+
.frame(minWidth: 300)
2433
BuilderLayoutView()
2534
.tabItem {
2635
Text("Builder")
2736
}
28-
.tag(2)
37+
.tag(4)
38+
.frame(minWidth: 300)
2939
}
30-
.frame(minWidth: 300, maxWidth: .infinity, maxHeight: .infinity)
40+
.frame(minWidth: 600, maxWidth: .infinity, maxHeight: .infinity)
3141
.padding()
3242
}
3343
}

Examples/GridExamples.xcodeproj/project.pbxproj

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
FA61E94F2312FE6D006A5B6B /* SingleColumnLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA61E94E2312FE6D006A5B6B /* SingleColumnLayoutView.swift */; };
11+
FA61E9502312FE6D006A5B6B /* SingleColumnLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA61E94E2312FE6D006A5B6B /* SingleColumnLayoutView.swift */; };
12+
FA61E9522312FF0F006A5B6B /* FixedColumnsLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA61E9512312FF0F006A5B6B /* FixedColumnsLayoutView.swift */; };
13+
FA61E9532312FF0F006A5B6B /* FixedColumnsLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA61E9512312FF0F006A5B6B /* FixedColumnsLayoutView.swift */; };
1014
FA6ED893231090FF00651DD9 /* BuilderLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA6ED892231090FF00651DD9 /* BuilderLayoutView.swift */; };
1115
FA6ED894231090FF00651DD9 /* BuilderLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA6ED892231090FF00651DD9 /* BuilderLayoutView.swift */; };
1216
FA902115230B32EA00BF9341 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902114230B32EA00BF9341 /* AppDelegate.swift */; };
@@ -26,15 +30,15 @@
2630
FA902162230B379D00BF9341 /* Color+Random.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902160230B379D00BF9341 /* Color+Random.swift */; };
2731
FA902164230B389900BF9341 /* Card.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902163230B389900BF9341 /* Card.swift */; };
2832
FA902165230B389900BF9341 /* Card.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902163230B389900BF9341 /* Card.swift */; };
29-
FA902167230B398200BF9341 /* OneColumnLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902166230B398200BF9341 /* OneColumnLayoutView.swift */; };
30-
FA902168230B398200BF9341 /* OneColumnLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902166230B398200BF9341 /* OneColumnLayoutView.swift */; };
31-
FA90216A230B39C800BF9341 /* AutoColumnLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902169230B39C800BF9341 /* AutoColumnLayoutView.swift */; };
32-
FA90216B230B39C800BF9341 /* AutoColumnLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902169230B39C800BF9341 /* AutoColumnLayoutView.swift */; };
33+
FA90216A230B39C800BF9341 /* AutoColumnsLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902169230B39C800BF9341 /* AutoColumnsLayoutView.swift */; };
34+
FA90216B230B39C800BF9341 /* AutoColumnsLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA902169230B39C800BF9341 /* AutoColumnsLayoutView.swift */; };
3335
FA90216D230B3EC100BF9341 /* PerformanceLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA90216C230B3EC100BF9341 /* PerformanceLayoutView.swift */; };
3436
FA90216E230B3EC100BF9341 /* PerformanceLayoutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA90216C230B3EC100BF9341 /* PerformanceLayoutView.swift */; };
3537
/* End PBXBuildFile section */
3638

3739
/* Begin PBXFileReference section */
40+
FA61E94E2312FE6D006A5B6B /* SingleColumnLayoutView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingleColumnLayoutView.swift; sourceTree = "<group>"; };
41+
FA61E9512312FF0F006A5B6B /* FixedColumnsLayoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FixedColumnsLayoutView.swift; sourceTree = "<group>"; };
3842
FA6ED892231090FF00651DD9 /* BuilderLayoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuilderLayoutView.swift; sourceTree = "<group>"; };
3943
FA6ED895231197C600651DD9 /* GridExamples iOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "GridExamples iOS.entitlements"; sourceTree = "<group>"; };
4044
FA902111230B32EA00BF9341 /* GridExamples iOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "GridExamples iOS.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -56,8 +60,7 @@
5660
FA902155230B341D00BF9341 /* GridExamples_macOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = GridExamples_macOS.entitlements; sourceTree = "<group>"; };
5761
FA902160230B379D00BF9341 /* Color+Random.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+Random.swift"; sourceTree = "<group>"; };
5862
FA902163230B389900BF9341 /* Card.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Card.swift; sourceTree = "<group>"; };
59-
FA902166230B398200BF9341 /* OneColumnLayoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OneColumnLayoutView.swift; sourceTree = "<group>"; };
60-
FA902169230B39C800BF9341 /* AutoColumnLayoutView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutoColumnLayoutView.swift; sourceTree = "<group>"; };
63+
FA902169230B39C800BF9341 /* AutoColumnsLayoutView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutoColumnsLayoutView.swift; sourceTree = "<group>"; };
6164
FA90216C230B3EC100BF9341 /* PerformanceLayoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PerformanceLayoutView.swift; sourceTree = "<group>"; };
6265
FA90216F230B4AC300BF9341 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
6366
/* End PBXFileReference section */
@@ -159,8 +162,9 @@
159162
FA90215C230B354900BF9341 /* GridExamples */ = {
160163
isa = PBXGroup;
161164
children = (
162-
FA902169230B39C800BF9341 /* AutoColumnLayoutView.swift */,
163-
FA902166230B398200BF9341 /* OneColumnLayoutView.swift */,
165+
FA902169230B39C800BF9341 /* AutoColumnsLayoutView.swift */,
166+
FA61E9512312FF0F006A5B6B /* FixedColumnsLayoutView.swift */,
167+
FA61E94E2312FE6D006A5B6B /* SingleColumnLayoutView.swift */,
164168
FA90216C230B3EC100BF9341 /* PerformanceLayoutView.swift */,
165169
FA6ED892231090FF00651DD9 /* BuilderLayoutView.swift */,
166170
FA902160230B379D00BF9341 /* Color+Random.swift */,
@@ -279,30 +283,32 @@
279283
isa = PBXSourcesBuildPhase;
280284
buildActionMask = 2147483647;
281285
files = (
286+
FA61E94F2312FE6D006A5B6B /* SingleColumnLayoutView.swift in Sources */,
282287
FA902115230B32EA00BF9341 /* AppDelegate.swift in Sources */,
283288
FA90216D230B3EC100BF9341 /* PerformanceLayoutView.swift in Sources */,
289+
FA61E9522312FF0F006A5B6B /* FixedColumnsLayoutView.swift in Sources */,
284290
FA902164230B389900BF9341 /* Card.swift in Sources */,
285291
FA6ED893231090FF00651DD9 /* BuilderLayoutView.swift in Sources */,
286292
FA902117230B32EA00BF9341 /* SceneDelegate.swift in Sources */,
287-
FA902167230B398200BF9341 /* OneColumnLayoutView.swift in Sources */,
288293
FA902119230B32EA00BF9341 /* ContentView.swift in Sources */,
289294
FA902161230B379D00BF9341 /* Color+Random.swift in Sources */,
290-
FA90216A230B39C800BF9341 /* AutoColumnLayoutView.swift in Sources */,
295+
FA90216A230B39C800BF9341 /* AutoColumnsLayoutView.swift in Sources */,
291296
);
292297
runOnlyForDeploymentPostprocessing = 0;
293298
};
294299
FA902142230B341C00BF9341 /* Sources */ = {
295300
isa = PBXSourcesBuildPhase;
296301
buildActionMask = 2147483647;
297302
files = (
303+
FA61E9502312FE6D006A5B6B /* SingleColumnLayoutView.swift in Sources */,
298304
FA902162230B379D00BF9341 /* Color+Random.swift in Sources */,
299305
FA90214B230B341C00BF9341 /* ContentView.swift in Sources */,
300306
FA6ED894231090FF00651DD9 /* BuilderLayoutView.swift in Sources */,
307+
FA61E9532312FF0F006A5B6B /* FixedColumnsLayoutView.swift in Sources */,
301308
FA90216E230B3EC100BF9341 /* PerformanceLayoutView.swift in Sources */,
302309
FA902165230B389900BF9341 /* Card.swift in Sources */,
303310
FA902149230B341C00BF9341 /* AppDelegate.swift in Sources */,
304-
FA902168230B398200BF9341 /* OneColumnLayoutView.swift in Sources */,
305-
FA90216B230B39C800BF9341 /* AutoColumnLayoutView.swift in Sources */,
311+
FA90216B230B39C800BF9341 /* AutoColumnsLayoutView.swift in Sources */,
306312
);
307313
runOnlyForDeploymentPostprocessing = 0;
308314
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import SwiftUI
22
import Grid
33

4-
struct AutoColumnLayoutView: View {
4+
struct AutoColumnsLayoutView: View {
55
var body: some View {
6-
Grid(0...100, minimumItemWidth: 120) { number in
6+
Grid(0...100) { number in
77
Card(title: "\(number)")
8-
.frame(height: 100)
98
}
9+
.padding()
1010
}
1111
}
1212

1313
#if DEBUG
1414
struct AutoColumnsGridView_Previews: PreviewProvider {
1515
static var previews: some View {
16-
AutoColumnLayoutView()
16+
AutoColumnsLayoutView()
1717
}
1818
}
1919
#endif

Examples/GridExamples/BuilderLayoutView.swift

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,62 @@ import SwiftUI
22
import Grid
33

44
struct BuilderLayoutView: View {
5+
@Environment(\.colorScheme) var colorScheme
6+
@State var itemsCount: CGFloat = 8
7+
@State var minItemWidth: CGFloat = 160
8+
@State var itemHeight: CGFloat = 160
9+
@State var hSpacing: CGFloat = 8
10+
@State var vSpacing: CGFloat = 8
11+
@State var cornerRadius: CGFloat = 0
12+
@State var padding: CGFloat = 0
13+
14+
private let itemsCountRange: ClosedRange<CGFloat> = 0...360
15+
private let minItemWidthRange: ClosedRange<CGFloat> = 8...360
16+
private let itemHeightRange: ClosedRange<CGFloat> = 8...360
17+
private let hSpacingRange: ClosedRange<CGFloat> = 0...36
18+
private let zSpacingRange: ClosedRange<CGFloat> = 0...36
19+
private let vSpacingRange: ClosedRange<CGFloat> = 0...36
20+
private var cornerRadiusRange: ClosedRange<CGFloat> = 0...36
21+
private var paddingRange: ClosedRange<CGFloat> = 0...16
22+
523
var body: some View {
6-
VStack(alignment: .center) {
7-
Section(header: Text("Section 1")) {
8-
Grid {
9-
ForEach(0...10, id: \.self) { _ in
10-
Rectangle()
11-
.foregroundColor(.random)
12-
.frame(height: 20)
13-
}
14-
}
24+
ZStack(alignment: .init(horizontal: .center, vertical: .bottom)) {
25+
Grid(0...Int(itemsCount)) { _ in
26+
Rectangle()
27+
.foregroundColor(.random)
28+
.cornerRadius(self.cornerRadius)
1529
}
30+
.padding(self.padding)
31+
.gridStyle(
32+
AutoColumnsGridStyle(minItemWidth: minItemWidth, itemHeight: itemHeight, hSpacing: hSpacing, vSpacing: vSpacing)
33+
)
1634

17-
Section(header: Text("Section 2")) {
18-
Grid {
19-
ForEach(0...10, id: \.self) { _ in
20-
Rectangle()
21-
.foregroundColor(.random)
22-
.frame(height: 20)
23-
}
35+
VStack {
36+
Slider(value: $itemsCount, in: itemsCountRange)
37+
Slider(value: $minItemWidth, in: minItemWidthRange)
38+
Slider(value: $itemHeight, in: hSpacingRange)
39+
Slider(value: $hSpacing, in: zSpacingRange)
40+
Slider(value: $vSpacing, in: vSpacingRange)
41+
Slider(value: $cornerRadius, in: 0...min(minItemWidth, itemHeight))
42+
Slider(value: $padding, in: paddingRange)
43+
Button("Randomize") {
44+
self.randomizeGridValues()
2445
}
2546
}
47+
.padding()
48+
.background(colorScheme == .light ? Color.white.opacity(0.9) : Color.black.opacity(0.6))
2649
}
2750
}
51+
52+
func randomizeGridValues() {
53+
itemsCount = CGFloat.random(in: itemsCountRange)
54+
minItemWidth = CGFloat.random(in: minItemWidthRange)
55+
itemHeight = CGFloat.random(in: itemHeightRange)
56+
hSpacing = CGFloat.random(in: hSpacingRange)
57+
vSpacing = CGFloat.random(in: vSpacingRange)
58+
cornerRadius = CGFloat.random(in: cornerRadiusRange)
59+
padding = CGFloat.random(in: paddingRange)
60+
}
2861
}
2962

3063
struct BuilderLayoutView_Previews: PreviewProvider {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import SwiftUI
2+
import Grid
3+
4+
struct FixedColumnsLayoutView: View {
5+
var body: some View {
6+
Grid(0...100) { number in
7+
Card(title: "\(number)")
8+
}
9+
.padding()
10+
.gridStyle(
11+
FixedColumnsGridStyle(columns: 3, itemHeight: 80)
12+
)
13+
}
14+
}
15+
16+
struct FixedColumnsLayoutView_Previews: PreviewProvider {
17+
static var previews: some View {
18+
FixedColumnsLayoutView()
19+
}
20+
}

Examples/GridExamples/OneColumnLayoutView.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.

Examples/GridExamples/PerformanceLayoutView.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import Grid
33

44
struct PerformanceLayoutView: View {
55
var body: some View {
6-
Grid(0...5000, minimumItemWidth: 20, spacing: 1) { number in
7-
Rectangle()
8-
.foregroundColor(.random)
9-
.frame(height: 20)
6+
Grid {
7+
ForEach(0...5000, id: \.self) { _ in
8+
Rectangle()
9+
.foregroundColor(.random)
10+
}
1011
}
12+
.gridStyle(
13+
AutoColumnsGridStyle(minItemWidth: 16, itemHeight: 16, spacing: 0)
14+
)
1115
}
1216
}
1317

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import SwiftUI
2+
import Grid
3+
4+
struct SingleColumnLayoutView: View {
5+
var body: some View {
6+
Grid(0...100) { number in
7+
Card(title: "\(number)")
8+
}
9+
.padding()
10+
.gridStyle(
11+
SingleColumnGridStyle(itemHeight: 160)
12+
)
13+
}
14+
}
15+
16+
#if DEBUG
17+
struct OneColumnLayoutView_Previews: PreviewProvider {
18+
static var previews: some View {
19+
SingleColumnLayoutView()
20+
}
21+
}
22+
#endif

0 commit comments

Comments
 (0)