Skip to content

Commit daca3e4

Browse files
committed
added presentationDetents and Radius to UIKitBackend
1 parent 19d772d commit daca3e4

File tree

8 files changed

+81
-16
lines changed

8 files changed

+81
-16
lines changed

Examples/Sources/WindowingExample/WindowingApp.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ struct SheetDemo: View {
8787
.sheet(isPresented: $isShortTermSheetPresented) {
8888
Text("I'm only here for 5s")
8989
.padding(20)
90-
.presentationCornerRadius(2)
90+
.presentationDetents([.height(150), .medium, .large])
91+
.presentationCornerRadius(10)
9192
}
9293
}
9394

@@ -147,6 +148,7 @@ struct WindowingApp: App {
147148
Divider()
148149

149150
SheetDemo()
151+
.padding(.bottom, 20)
150152
}
151153
.padding(20)
152154
}

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,10 +1734,6 @@ public final class AppKitBackend: AppBackend {
17341734
NSApplication.shared.stopModal()
17351735
}
17361736
}
1737-
1738-
public func setPresentationCornerRadius(of sheet: NSCustomSheet, to radius: Int) {
1739-
print("setting Sheet Corner Radius is unavailable on macOS and will be ignored")
1740-
}
17411737
}
17421738

17431739
public final class NSCustomSheet: NSCustomWindow, NSWindowDelegate {

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ public protocol AppBackend: Sendable {
641641
///
642642
/// - Parameters:
643643
/// - sheet: The sheet to apply the corner radius to.
644-
/// - radius: The corner radius in pixels.
645-
func setPresentationCornerRadius(of sheet: Sheet, to radius: Int)
644+
/// - radius: The corner radius
645+
func setPresentationCornerRadius(of sheet: Sheet, to radius: Double)
646646

647647
/// Sets the available detents (heights) for a sheet presentation.
648648
///
@@ -778,6 +778,12 @@ extension AppBackend {
778778
Foundation.exit(1)
779779
}
780780

781+
private func ignored(_ function: String = #function) {
782+
print(
783+
"\(type(of: self)): \(function) is being ignored\nConsult at the documentation for further information."
784+
)
785+
}
786+
781787
// MARK: System
782788

783789
public func openExternalURL(_ url: URL) throws {
@@ -1237,11 +1243,11 @@ extension AppBackend {
12371243
todo()
12381244
}
12391245

1240-
public func setPresentationCornerRadius(of sheet: Sheet, to radius: Int) {
1241-
todo()
1246+
public func setPresentationCornerRadius(of sheet: Sheet, to radius: Double) {
1247+
ignored()
12421248
}
12431249

12441250
public func setPresentationDetents(of sheet: Sheet, to detents: [PresentationDetent]) {
1245-
todo()
1251+
ignored()
12461252
}
12471253
}

Sources/SwiftCrossUI/Values/PresentationDetent.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ public enum PresentationDetent: Sendable, Hashable {
77
case large
88

99
/// A detent at a custom fractional height of the available space.
10+
/// falling back to medium on iOS 15
1011
/// - Parameter fraction: A value between 0 and 1 representing the fraction of available height.
1112
case fraction(Double)
1213

1314
/// A detent at a specific fixed height in pixels.
14-
/// - Parameter height: The height in pixels.
15-
case height(Int)
15+
/// falling back to medium on iOS 15
16+
/// - Parameter height: The height
17+
case height(Double)
1618
}

Sources/SwiftCrossUI/ViewGraph/PreferenceValues.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public struct PreferenceValues: Sendable {
1313
public var presentationDetents: [PresentationDetent]?
1414

1515
/// The corner radius for a sheet presentation. Only applies to the top-level view in a sheet.
16-
public var presentationCornerRadius: Int?
16+
public var presentationCornerRadius: Double?
1717

1818
public init(
1919
onOpenURL: (@Sendable @MainActor (URL) -> Void)?,
2020
presentationDetents: [PresentationDetent]? = nil,
21-
presentationCornerRadius: Int? = nil
21+
presentationCornerRadius: Double? = nil
2222
) {
2323
self.onOpenURL = onOpenURL
2424
self.presentationDetents = presentationDetents

Sources/SwiftCrossUI/Views/Modifiers/Style/PresentationCornerRadiusModifier.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ extension View {
1111
/// This modifier only affects the sheet presentation itself when applied to the
1212
/// top-level view within a sheet. It does not affect the content's corner radius.
1313
///
14+
/// supported platforms: iOS (ignored on unsupported platforms)
15+
/// ignored on: older than iOS 15
16+
///
1417
/// - Parameter radius: The corner radius in pixels.
1518
/// - Returns: A view with the presentation corner radius preference set.
16-
public func presentationCornerRadius(_ radius: Int) -> some View {
19+
public func presentationCornerRadius(_ radius: Double) -> some View {
1720
preference(key: \.presentationCornerRadius, value: radius)
1821
}
1922
}

Sources/SwiftCrossUI/Views/Modifiers/Style/PresentationDetentsModifier.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ extension View {
55
/// top-level view within a sheet. It allows users to resize the sheet to different
66
/// predefined heights.
77
///
8+
/// supported platforms: iOS (ignored on unsupported platforms)
9+
/// ignored on: older than iOS 15
10+
/// fraction and height fall back to medium on iOS 15 and work as you'd expect on >=16
11+
///
812
/// - Parameter detents: A set of detents that the sheet can be resized to.
913
/// - Returns: A view with the presentation detents preference set.
1014
public func presentationDetents(_ detents: Set<PresentationDetent>) -> some View {

Sources/UIKitBackend/UIKitBackend+Sheet.swift

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ extension UIKitBackend {
77
public func createSheet() -> CustomSheet {
88
let sheet = CustomSheet()
99
sheet.modalPresentationStyle = .formSheet
10-
//sheet.transitioningDelegate = CustomSheetTransitioningDelegate()
1110

1211
return sheet
1312
}
@@ -29,6 +28,59 @@ extension UIKitBackend {
2928
public func dismissSheet(_ sheet: CustomSheet, window: UIWindow?) {
3029
sheet.dismiss(animated: true)
3130
}
31+
32+
public func setPresentationDetents(of sheet: CustomSheet, to detents: [PresentationDetent]) {
33+
if #available(iOS 15.0, *) {
34+
if let sheetPresentation = sheet.sheetPresentationController {
35+
sheetPresentation.detents = detents.map {
36+
switch $0 {
37+
case .medium: return .medium()
38+
case .large: return .large()
39+
case .fraction(let fraction):
40+
if #available(iOS 16.0, *) {
41+
return .custom(
42+
identifier: .init("Fraction:\(fraction)"),
43+
resolver: { context in
44+
context.maximumDetentValue * fraction
45+
})
46+
} else {
47+
return .medium()
48+
}
49+
case .height(let height):
50+
if #available(iOS 16.0, *) {
51+
return .custom(
52+
identifier: .init("Height:\(height)"),
53+
resolver: { context in
54+
height
55+
})
56+
} else {
57+
return .medium()
58+
}
59+
}
60+
}
61+
}
62+
} else {
63+
#if DEBUG
64+
print(
65+
"your current OS Version doesn't support variable sheet heights.\n Setting presentationDetents is only available from iOS 15.0"
66+
)
67+
#endif
68+
}
69+
}
70+
71+
public func setPresentationCornerRadius(of sheet: CustomSheet, to radius: Double) {
72+
if #available(iOS 15.0, *) {
73+
if let sheetController = sheet.sheetPresentationController {
74+
sheetController.preferredCornerRadius = radius
75+
}
76+
} else {
77+
#if DEBUG
78+
print(
79+
"your current OS Version doesn't support variable sheet corner radii.\n Setting them is only available from iOS 15.0"
80+
)
81+
#endif
82+
}
83+
}
3284
}
3385

3486
public final class CustomSheet: UIViewController {

0 commit comments

Comments
 (0)