Skip to content

Commit

Permalink
Add grid alignment modifier per element
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-obukhov committed Dec 15, 2020
1 parent 4107fde commit 63c5e70
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- ExyteGrid (1.2.1.beta)
- ExyteGrid (1.3.1.beta)

DEPENDENCIES:
- ExyteGrid (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
ExyteGrid: 07bfd36208451abf592c6d4500b2863d3623898e
ExyteGrid: 616f4790a25339ed8664db1c1d9e38a1e49c6a11

PODFILE CHECKSUM: 76084cf935f120b28a0e2dcc60936504183d3608

Expand Down
2 changes: 1 addition & 1 deletion ExyteGrid.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'ExyteGrid'
s.version = '1.2.1.beta'
s.version = '1.3.1.beta'
s.summary = 'The most powerful Grid container missed in SwiftUI'

s.homepage = 'https://github.com/exyte/Grid.git'
Expand Down
6 changes: 6 additions & 0 deletions Grid.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
363AD4B2253045A200C27F2F /* GridCacheMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3633AB8D24E678290033A172 /* GridCacheMode.swift */; };
363AD4B3253045A200C27F2F /* GridGroup+Init.swift in Sources */ = {isa = PBXBuildFile; fileRef = 365DFA302523543C0019BB80 /* GridGroup+Init.swift */; };
363AD4B4253045A200C27F2F /* GridContentMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 361723742475221000866539 /* GridContentMode.swift */; };
36435B1C25887A590011E2AC /* GridAlignment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36435B1B25887A590011E2AC /* GridAlignment.swift */; };
36435B1D25887A590011E2AC /* GridAlignment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36435B1B25887A590011E2AC /* GridAlignment.swift */; };
364FFB2D24C1C1680054D275 /* GridPreference.swift in Sources */ = {isa = PBXBuildFile; fileRef = 364FFB2C24C1C1680054D275 /* GridPreference.swift */; };
365DFA362523543C0019BB80 /* GridBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 365DFA2E2523543C0019BB80 /* GridBuilder.swift */; };
365DFA372523543C0019BB80 /* GridGroup+Inits_Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 365DFA2F2523543C0019BB80 /* GridGroup+Inits_Data.swift */; };
Expand Down Expand Up @@ -151,6 +153,7 @@
363AD4732530455F00C27F2F /* GridMac.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GridMac.framework; sourceTree = BUILT_PRODUCTS_DIR; };
363AD4752530455F00C27F2F /* GridMac.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GridMac.h; sourceTree = "<group>"; };
363AD4762530455F00C27F2F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
36435B1B25887A590011E2AC /* GridAlignment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GridAlignment.swift; sourceTree = "<group>"; };
364FFB2C24C1C1680054D275 /* GridPreference.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GridPreference.swift; sourceTree = "<group>"; };
365DFA2E2523543C0019BB80 /* GridBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GridBuilder.swift; sourceTree = "<group>"; };
365DFA2F2523543C0019BB80 /* GridGroup+Inits_Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "GridGroup+Inits_Data.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -306,6 +309,7 @@
3617237D2475221000866539 /* ArrangedItem.swift */,
3617237E2475221000866539 /* LayoutArrangement.swift */,
3617237F2475221000866539 /* PositionedItem.swift */,
36435B1B25887A590011E2AC /* GridAlignment.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -581,6 +585,7 @@
361723832475221000866539 /* View+GridPreferences.swift in Sources */,
361723952475221000866539 /* GridContentMode.swift in Sources */,
3617238D2475221000866539 /* Constants.swift in Sources */,
36435B1C25887A590011E2AC /* GridAlignment.swift in Sources */,
365DFA362523543C0019BB80 /* GridBuilder.swift in Sources */,
3617239A2475221000866539 /* GridTrack.swift in Sources */,
365DFA3B2523543C0019BB80 /* GridElement+asGridElements.swift in Sources */,
Expand Down Expand Up @@ -630,6 +635,7 @@
363AD494253045A200C27F2F /* GridElement.swift in Sources */,
363AD498253045A200C27F2F /* LayoutArrangement+description.swift in Sources */,
363AD49A253045A200C27F2F /* GridSpacing.swift in Sources */,
36435B1D25887A590011E2AC /* GridAlignment.swift in Sources */,
363AD49F253045A200C27F2F /* GridFlow.swift in Sources */,
363AD4A5253045A200C27F2F /* GridStart.swift in Sources */,
363AD4A2253045A200C27F2F /* GridGroup+Inits_Data.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Configuration/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public struct Constants {
public static let defaultFlow: GridFlow = .rows
public static let defaultPacking: GridPacking = .sparse
public static let defaultCacheMode: GridCacheMode = .inMemoryCache
public static let defaultItemsAlignment: Alignment = .center
public static let defaultItemsAlignment: GridAlignment = .center
}
11 changes: 11 additions & 0 deletions Sources/Models/GridAlignment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// GridItemAlignment.swift
// Grid
//
// Created by Denis Obukhov on 15.12.2020.
// Copyright © 2020 Exyte. All rights reserved.
//

import SwiftUI

public typealias GridAlignment = Alignment
5 changes: 4 additions & 1 deletion Sources/Models/Preferences/GridPreference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct GridPreference: Equatable {
var positionedItem: PositionedItem?
var span: GridSpan?
var start: GridStart?
var alignment: GridAlignment?

static let empty = ItemInfo()
}
Expand Down Expand Up @@ -44,9 +45,11 @@ extension Array where Element == GridPreference.ItemInfo {
let positionedItem = self.compactMap(\.positionedItem).first
let span = self.compactMap(\.span).first ?? .default
let start = self.compactMap(\.start).first ?? .default
let alignment = self.compactMap(\.alignment).first
let itemInfo = GridPreference.ItemInfo(positionedItem: positionedItem,
span: span,
start: start)
start: start,
alignment: alignment)
return [itemInfo]
}

Expand Down
27 changes: 19 additions & 8 deletions Sources/View/Grid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import SwiftUI

public struct Grid: View, LayoutArranging, LayoutPositioning {
@State var positions: PositionedLayout = .empty
@State var isLoaded: Bool = false
@State private var positions: PositionedLayout = .empty
@State private var isLoaded: Bool = false
@State private var alignments: [GridElement: GridAlignment] = [:]
#if os(iOS) || os(watchOS) || os(tvOS)
@State var internalLayoutCache = Cache<ArrangingTask, LayoutArrangement>()
@State var internalPositionsCache = Cache<PositioningTask, PositionedLayout>()
@State private var internalLayoutCache = Cache<ArrangingTask, LayoutArrangement>()
@State private var internalPositionsCache = Cache<PositioningTask, PositionedLayout>()
#endif
@Environment(\.gridContentMode) private var environmentContentMode
@Environment(\.gridFlow) private var environmentFlow
Expand All @@ -29,7 +30,7 @@ public struct Grid: View, LayoutArranging, LayoutPositioning {
var internalPacking: GridPacking?
var internalContentMode: GridContentMode?
var internalCacheMode: GridCacheMode?
var internalItemsAlignment: Alignment?
var internalItemsAlignment: GridAlignment?

private var flow: GridFlow {
self.internalFlow ?? self.environmentFlow ?? Constants.defaultFlow
Expand All @@ -47,7 +48,7 @@ public struct Grid: View, LayoutArranging, LayoutPositioning {
self.internalCacheMode ?? self.environmentCacheMode ?? Constants.defaultCacheMode
}

private var itemsAlignment: Alignment {
private var itemsAlignment: GridAlignment {
self.internalItemsAlignment ?? self.environmentItemsAlignment ?? Constants.defaultItemsAlignment
}

Expand Down Expand Up @@ -87,7 +88,7 @@ public struct Grid: View, LayoutArranging, LayoutPositioning {
.frame(flow: self.flow,
size: self.positions[item]?.bounds.size,
contentMode: self.contentMode,
alignment: itemsAlignment)
alignment: self.alignments[item] ?? itemsAlignment)
.alignmentGuide(.leading, computeValue: { _ in self.leadingGuide(item: item) })
.alignmentGuide(.top, computeValue: { _ in self.topGuide(item: item) })
.backgroundPreferenceValue(GridBackgroundPreferenceKey.self) { preference in
Expand All @@ -109,6 +110,7 @@ public struct Grid: View, LayoutArranging, LayoutPositioning {
.onPreferenceChange(GridPreferenceKey.self) { preference in
self.calculateLayout(preference: preference,
boundingSize: mainGeometry.size)
self.saveAlignmentsFrom(preference: preference)
}
}
.id(self.isLoaded)
Expand Down Expand Up @@ -158,7 +160,16 @@ public struct Grid: View, LayoutArranging, LayoutPositioning {
self.positions = positions
self.isLoaded = true
}


private func saveAlignmentsFrom(preference: GridPreference) {
var alignments: [GridElement: GridAlignment] = [:]
preference.itemsInfo.forEach {
guard let gridElement = $0.positionedItem?.gridElement else { return }
alignments[gridElement] = $0.alignment
}
self.alignments = alignments
}

private func corrected(size: CGSize) -> CGSize {
return CGSize(width: size.width - self.spacing.horizontal,
height: size.height - self.spacing.vertical)
Expand Down
2 changes: 1 addition & 1 deletion Sources/View/Inits/Grid+Init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import SwiftUI

extension Grid {
public init(tracks: [GridTrack] = 1, contentMode: GridContentMode? = nil, flow: GridFlow? = nil, packing: GridPacking? = nil, spacing: GridSpacing = Constants.defaultSpacing, itemsAlignment: Alignment? = nil, cache: GridCacheMode? = nil, @GridBuilder content: () -> ConstructionItem) {
public init(tracks: [GridTrack] = 1, contentMode: GridContentMode? = nil, flow: GridFlow? = nil, packing: GridPacking? = nil, spacing: GridSpacing = Constants.defaultSpacing, itemsAlignment: GridAlignment? = nil, cache: GridCacheMode? = nil, @GridBuilder content: () -> ConstructionItem) {
self.trackSizes = tracks
self.spacing = spacing
self.internalContentMode = contentMode
Expand Down
6 changes: 3 additions & 3 deletions Sources/View/Inits/Grid+Inits_Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import SwiftUI

extension Grid {
public init<Data, ID>(_ data: Data, id: KeyPath<Data.Element, ID>, tracks: [GridTrack] = 1, contentMode: GridContentMode? = nil, flow: GridFlow? = nil, packing: GridPacking? = nil, spacing: GridSpacing = Constants.defaultSpacing, itemsAlignment: Alignment? = nil, cache: GridCacheMode? = nil, @GridBuilder item: @escaping (Data.Element) -> ConstructionItem) where Data: RandomAccessCollection, ID: Hashable {
public init<Data, ID>(_ data: Data, id: KeyPath<Data.Element, ID>, tracks: [GridTrack] = 1, contentMode: GridContentMode? = nil, flow: GridFlow? = nil, packing: GridPacking? = nil, spacing: GridSpacing = Constants.defaultSpacing, itemsAlignment: GridAlignment? = nil, cache: GridCacheMode? = nil, @GridBuilder item: @escaping (Data.Element) -> ConstructionItem) where Data: RandomAccessCollection, ID: Hashable {
var index = 0
self.items = data.flatMap {
item($0).contentViews.asGridElements(index: &index,
Expand All @@ -26,7 +26,7 @@ extension Grid {
self.internalItemsAlignment = itemsAlignment
}

public init(_ data: Range<Int>, tracks: [GridTrack] = 1, contentMode: GridContentMode? = nil, flow: GridFlow? = nil, packing: GridPacking? = nil, spacing: GridSpacing = Constants.defaultSpacing, itemsAlignment: Alignment? = nil, cache: GridCacheMode? = nil, @GridBuilder item: @escaping (Int) -> ConstructionItem) {
public init(_ data: Range<Int>, tracks: [GridTrack] = 1, contentMode: GridContentMode? = nil, flow: GridFlow? = nil, packing: GridPacking? = nil, spacing: GridSpacing = Constants.defaultSpacing, itemsAlignment: GridAlignment? = nil, cache: GridCacheMode? = nil, @GridBuilder item: @escaping (Int) -> ConstructionItem) {
var index = 0
self.items = data.flatMap {
item($0).contentViews.asGridElements(index: &index)
Expand All @@ -40,7 +40,7 @@ extension Grid {
self.internalItemsAlignment = itemsAlignment
}

public init<Data>(_ data: Data, tracks: [GridTrack] = 1, contentMode: GridContentMode? = nil, flow: GridFlow? = nil, packing: GridPacking? = nil, spacing: GridSpacing = Constants.defaultSpacing, itemsAlignment: Alignment? = nil, cache: GridCacheMode? = nil, @GridBuilder item: @escaping (Data.Element) -> ConstructionItem) where Data: RandomAccessCollection, Data.Element: Identifiable {
public init<Data>(_ data: Data, tracks: [GridTrack] = 1, contentMode: GridContentMode? = nil, flow: GridFlow? = nil, packing: GridPacking? = nil, spacing: GridSpacing = Constants.defaultSpacing, itemsAlignment: GridAlignment? = nil, cache: GridCacheMode? = nil, @GridBuilder item: @escaping (Data.Element) -> ConstructionItem) where Data: RandomAccessCollection, Data.Element: Identifiable {
var index = 0
self.items = data.flatMap {
item($0).contentViews.asGridElements(index: &index,
Expand Down
6 changes: 3 additions & 3 deletions Sources/View/View+Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension View {
return self.environment(\.gridCache, cache)
}

public func gridItemsAlignment(_ alignment: Alignment?) -> some View {
public func gridItemsAlignment(_ alignment: GridAlignment?) -> some View {
return self.environment(\.gridItemsAlignemnt, alignment)
}
}
Expand Down Expand Up @@ -60,7 +60,7 @@ extension EnvironmentValues {
set { self[EnvironmentKeys.GridCache.self] = newValue }
}

var gridItemsAlignemnt: Alignment? {
var gridItemsAlignemnt: GridAlignment? {
get { self[EnvironmentKeys.GridItemsAlignment.self] }
set { self[EnvironmentKeys.GridItemsAlignment.self] = newValue }
}
Expand Down Expand Up @@ -88,6 +88,6 @@ private struct EnvironmentKeys {
}

struct GridItemsAlignment: EnvironmentKey {
static let defaultValue: Alignment? = .center
static let defaultValue: GridAlignment? = .center
}
}
8 changes: 8 additions & 0 deletions Sources/View/View+GridPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ extension View {
preferences.itemsInfo = [info]
})
}

public func gridAlignment(_ alignment: GridAlignment) -> some View {
transformPreference(GridPreferenceKey.self, { preferences in
var info = preferences.itemsInfo.first ?? .empty
info.alignment = alignment
preferences.itemsInfo = [info]
})
}

public func gridCellOverlay<Content: View>(
@ViewBuilder content: @escaping (CGSize?) -> Content
Expand Down

0 comments on commit 63c5e70

Please sign in to comment.