Skip to content

Commit

Permalink
Added widget wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Sep 10, 2023
1 parent d64b4bf commit 87f135f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/SwiftUIExtension/Extensions/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ extension View {
public func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
self.modifier(DeviceRotationViewModifier(action: action))
}

public func containerBackgroundForWidget<Background>(background: @escaping () -> Background) -> some View where Background: View {
modifier(ContainerBackgroundForWidgetModifier(background: background))
}
}

struct DeviceRotationViewModifier: ViewModifier {
Expand All @@ -23,3 +27,23 @@ struct DeviceRotationViewModifier: ViewModifier {
}
}
}

struct ContainerBackgroundForWidgetModifier<Background>: ViewModifier where Background: View {

let background: () -> Background

func body(content: Content) -> some View {
if #available(iOS 17.0, iOSApplicationExtension 17.0, watchOS 10.0, watchOSApplicationExtension 10.0, macOS 14.0, *) {
content
.containerBackground(for: .widget) {
background()
}
} else {
ZStack {
background()
content
.padding()
}
}
}
}

0 comments on commit 87f135f

Please sign in to comment.