Skip to content

Commit

Permalink
✨ Add SwiftUI.View+Frame
Browse files Browse the repository at this point in the history
  • Loading branch information
olejnjak committed Nov 28, 2024
1 parent 336fe80 commit 2090a6d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ on:
jobs:
carthage:
name: Carthage
runs-on: macos-latest
runs-on: macos-15
steps:
- uses: actions/checkout@v4
- uses: AckeeCZ/load-xcode-version@v1
- name: Build
run: carthage build --no-skip-current --cache-builds --use-xcframeworks
- uses: actions/cache@v3
Expand All @@ -23,10 +22,9 @@ jobs:
${{ runner.os }}-carthage-
spm:
name: SPM
runs-on: macos-latest
runs-on: macos-15
steps:
- uses: actions/checkout@v4
- uses: AckeeCZ/load-xcode-version@v1
- name: Build
run: swift build -c release
- uses: actions/cache@v3
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/docbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ jobs:
# Must be set to this for deploying to GitHub Pages
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: macos-latest
runs-on: macos-15
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Check Xcode version ✅
uses: AckeeCZ/load-xcode-version@v1
- name: Build DocC
run: |
xcodebuild docbuild -scheme ACKategories \
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ on: [workflow_call]
jobs:
xcodebuild:
name: Xcodebuild
runs-on: macos-latest
runs-on: macos-15
env:
IOS_DEVICE: iPhone 15 Pro Max
IOS_DEVICE: iPhone 16 Pro
steps:
- uses: actions/checkout@v4
- uses: AckeeCZ/load-xcode-version@v1
- name: iOS tests
run: set -o pipefail && xcodebuild test -scheme ACKategories -resultBundlePath Tests-iOS.xcresult -sdk iphonesimulator -destination "platform=iOS Simulator,name=$IOS_DEVICE,OS=latest" ONLY_ACTIVE_ARCH=YES | xcpretty
- uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -48,10 +47,9 @@ jobs:
path: Tests-tvOS.xcresult
spm:
name: SPM
runs-on: macos-latest
runs-on: macos-15
steps:
- uses: actions/checkout@v4
- uses: AckeeCZ/load-xcode-version@v1
- name: SPM build
run: swift build
- name: SPM tests
Expand Down
4 changes: 4 additions & 0 deletions ACKategories.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
69ACD71B2AFD13480021127B /* DateRandomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69E0A6B12AFD114600C8E8D9 /* DateRandomTests.swift */; };
69ACD71C2AFD13480021127B /* IntRandomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69E0A6B22AFD114600C8E8D9 /* IntRandomTests.swift */; };
69ACD71D2AFD13480021127B /* StringRandomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69E0A6B32AFD114600C8E8D9 /* StringRandomTests.swift */; };
69BC15952CF8A42C00A4B55F /* View+Frame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69BC15942CF8A42700A4B55F /* View+Frame.swift */; };
69C6BE902B0BC80E008A4ECF /* UIWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69E0A6A82AFD114600C8E8D9 /* UIWindow.swift */; };
69DF227B2B459D0D0025C555 /* PushManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69DF22792B459D0D0025C555 /* PushManager.swift */; };
69DF227C2B459D0D0025C555 /* UNNotificationSettingsExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69DF227A2B459D0D0025C555 /* UNNotificationSettingsExtensions.swift */; };
Expand Down Expand Up @@ -266,6 +267,7 @@
69ACC1812BBDF8DC00A109DB /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
69ACD6C22AFD130C0021127B /* ACKategories.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ACKategories.framework; sourceTree = BUILT_PRODUCTS_DIR; };
69ACD6C92AFD130D0021127B /* ACKategoriesTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ACKategoriesTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
69BC15942CF8A42700A4B55F /* View+Frame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Frame.swift"; sourceTree = "<group>"; };
69DF225A2B459CC50025C555 /* PushNotifications.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PushNotifications.framework; sourceTree = BUILT_PRODUCTS_DIR; };
69DF22792B459D0D0025C555 /* PushManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PushManager.swift; sourceTree = "<group>"; };
69DF227A2B459D0D0025C555 /* UNNotificationSettingsExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UNNotificationSettingsExtensions.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -651,6 +653,7 @@
69E0A6032AFD10BE00C8E8D9 /* SwiftUIExtensions */ = {
isa = PBXGroup;
children = (
69BC15942CF8A42700A4B55F /* View+Frame.swift */,
69246CFA2BDFE77400AB31A1 /* SwiftUIColorsTheme.swift */,
69E0A6042AFD10BE00C8E8D9 /* FontModifier.swift */,
693B39B62BF2359B00DF7C5E /* ACKHostingController.swift */,
Expand Down Expand Up @@ -1292,6 +1295,7 @@
69ACD6F12AFD133A0021127B /* UIControlEvents.swift in Sources */,
69ACD6F22AFD133A0021127B /* UIDeviceExtensions.swift in Sources */,
69ACD6F32AFD133A0021127B /* UIImageExtensions.swift in Sources */,
69BC15952CF8A42C00A4B55F /* View+Frame.swift in Sources */,
69ACD6F42AFD133A0021127B /* UILabelExtensions.swift in Sources */,
69ACD6F52AFD133A0021127B /* UINavigationControllerExtensions.swift in Sources */,
69ACD6F62AFD133A0021127B /* UISearchBarExtensions.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

## Next

- Add `readSize` and `readFrame` to SwiftUI views ([#150](https://github.com/AckeeCZ/ACKategories/pull/150), kudos to @olejnjak)

## 6.15.0

- SwiftUI improvements ([#149](https://github.com/AckeeCZ/ACKategories/pull/149), kudos to @olejnjak)
Expand Down
42 changes: 42 additions & 0 deletions Sources/ACKategories/SwiftUIExtensions/View+Frame.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import SwiftUI

@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
public extension View {
func readSize(
onChange: @escaping (CGSize) -> Void
) -> some View {
readFrame(in: .local) { onChange($0.size) }
}

func readFrame(
in coordinateSpace: CoordinateSpace,
onChange: @escaping (CGRect) -> Void
) -> some View {
background(
GeometryReader { geometryProxy in
Color.clear
.preference(key: FramePreferenceKey.self, value: geometryProxy.frame(in: coordinateSpace))
}
)
.onPreferenceChange(FramePreferenceKey.self, perform: onChange)
}
}

private struct FramePreferenceKey: PreferenceKey {
static var defaultValue: CGRect = .zero
static func reduce(value: inout CGRect, nextValue: () -> CGRect) { }
}

@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
#Preview {
Text("Hello, World!")
.readSize {
print("Size:", $0)
}
.readFrame(in: .local) {
print("Local frame:", $0)
}
.readFrame(in: .global) {
print("Global frame:", $0)
}
}

0 comments on commit 2090a6d

Please sign in to comment.