Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contentFIt mode #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions Example/Example/ContentModeExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,30 @@ struct ContentModeExample: View {
let color = GridColor.random.lighter(by: 50)
}

@State var contentMode: GridContentMode = .scroll
@State var contentMode: GridContentMode = .contentFit

var body: some View {
VStack {
self.modesPicker

if self.contentMode == .contentFit {
ScrollView {
myView
}
} else {
myView
}
}
}

var myView: some View {
Grid(models, id: \.self, tracks: 3) {
VCardView(text: $0.text, color: $0.color)
.gridSpan($0.span)
}
.gridContentMode(self.contentMode)
.gridFlow(.rows)
}
.background(Color.blue)
}

private let models: [Model] = [
Expand All @@ -46,9 +57,12 @@ struct ContentModeExample: View {

private var modesPicker: some View {
Picker("Mode", selection: $contentMode) {
ForEach([GridContentMode.scroll, GridContentMode.fill], id: \.self) {
Text($0 == .scroll ? "Scroll" : "Fill")
.tag($0)
ForEach([GridContentMode.contentFit, GridContentMode.scroll, GridContentMode.fill], id: \.self) {
switch($0) {
case .contentFit: Text("ContentFit").tag($0)
case .scroll: Text("Scroll").tag($0)
case .fill: Text("Fill").tag($0)
}
}
}
.pickerStyle(SegmentedPickerStyle())
Expand Down
4 changes: 4 additions & 0 deletions Sources/GridLayoutMath/LayoutPositioning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ extension LayoutPositioning {
let growingTracks: [GridTrack]
let growingBoundingSize: CGFloat
switch task.contentMode {
case .contentFit:
fallthrough
case .scroll:
growingTracks = [GridTrack](repeating: .fit, count: arrangement[keyPath: flow.arrangementCount(.growing)])
growingBoundingSize = .infinity
Expand Down Expand Up @@ -187,6 +189,8 @@ extension LayoutPositioning {
let growingSize = boundingSize[keyPath: flow.size(.growing)] / CGFloat(arrangement[keyPath: flow.arrangementCount(.growing)])
itemGrowingSize = growingSize * CGFloat(arrangedItem[keyPath: flow.arrangedItemCount(.growing)])
growingPosition = growingSize * CGFloat(arrangedItem.startIndex[keyPath: flow.index(.growing)])
case .contentFit:
fallthrough
case .scroll:
let indexRange = arrangedItem.startIndex[keyPath: flow.index(.growing)]...arrangedItem.endIndex[keyPath: flow.index(.growing)]
itemGrowingSize = indexRange.reduce(0, { result, index in
Expand Down
3 changes: 3 additions & 0 deletions Sources/Models/GridContentMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public enum GridContentMode {
/// Scrolls inside parent container
case scroll

/// Fits the content
case contentFit

/// Fills the entire space of the parent container
case fill
}
7 changes: 7 additions & 0 deletions Sources/View/Grid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public struct Grid: View, LayoutArranging, LayoutPositioning {
self.saveAlignmentsFrom(preference: preference)
}
}
.if(contentMode == .contentFit) { content in
content.frame(height: self.positions.totalSize?.height ?? 0)
}
.id(self.isLoaded)
}

Expand Down Expand Up @@ -204,6 +207,8 @@ public struct Grid: View, LayoutArranging, LayoutPositioning {
switch self.contentMode {
case .fill:
return []
case .contentFit:
fallthrough
case .scroll:
return self.flow == .rows ? .vertical : .horizontal
}
Expand Down Expand Up @@ -268,6 +273,8 @@ extension View {
case .fill:
width = size?.width
height = size?.height
case .contentFit:
fallthrough
case .scroll:
width = (flow == .rows ? size?.width : nil)
height = (flow == .columns ? size?.height : nil)
Expand Down