Skip to content

Commit 79837ed

Browse files
committed
added presentationDragIndicatorVisibility modifier
1 parent d6a63f2 commit 79837ed

File tree

9 files changed

+78
-43
lines changed

9 files changed

+78
-43
lines changed

Examples/Sources/WindowingExample/WindowingApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ struct SheetDemo: View {
8383
print("sheet dismissed")
8484
} content: {
8585
SheetBody()
86-
.frame(maxWidth: 200, maxHeight: 100)
8786
.presentationDetents([.height(150), .medium, .large])
87+
.presentationDragIndicatorVisibility(.visible)
8888
}
8989
.sheet(isPresented: $isShortTermSheetPresented) {
9090
Text("I'm only here for 5s")

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@ public protocol AppBackend: Sendable {
654654
/// - detents: An array of detents that the sheet can be resized to.
655655
func setPresentationDetents(of sheet: Sheet, to detents: [PresentationDetent])
656656

657+
func setPresentationDragIndicatorVisibility(
658+
of sheet: Sheet, to visibility: PresentationDragIndicatorVisibility)
659+
657660
/// Presents an 'Open file' dialog to the user for selecting files or
658661
/// folders.
659662
///
@@ -1254,4 +1257,10 @@ extension AppBackend {
12541257
public func setPresentationDetents(of sheet: Sheet, to detents: [PresentationDetent]) {
12551258
ignored()
12561259
}
1260+
1261+
public func setPresentationDragIndicatorVisibility(
1262+
of sheet: Sheet, to visibility: PresentationDragIndicatorVisibility
1263+
) {
1264+
ignored()
1265+
}
12571266
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public enum PresentationDragIndicatorVisibility {
2+
case hidden, visible
3+
}

Sources/SwiftCrossUI/ViewGraph/PreferenceValues.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ public struct PreferenceValues: Sendable {
1515
/// The corner radius for a sheet presentation. Only applies to the top-level view in a sheet.
1616
public var presentationCornerRadius: Double?
1717

18+
public var presentationDragIndicatorVisibility: PresentationDragIndicatorVisibility?
19+
1820
public init(
1921
onOpenURL: (@Sendable @MainActor (URL) -> Void)?,
2022
presentationDetents: [PresentationDetent]? = nil,
21-
presentationCornerRadius: Double? = nil
23+
presentationCornerRadius: Double? = nil,
24+
presentationDragIndicatorVisibility: PresentationDragIndicatorVisibility? = nil
2225
) {
2326
self.onOpenURL = onOpenURL
2427
self.presentationDetents = presentationDetents
@@ -40,5 +43,6 @@ public struct PreferenceValues: Sendable {
4043
// This ensures only the root view's presentation modifiers apply to the sheet
4144
presentationDetents = children.first?.presentationDetents
4245
presentationCornerRadius = children.first?.presentationCornerRadius
46+
presentationDragIndicatorVisibility = children.first?.presentationDragIndicatorVisibility
4347
}
4448
}

Sources/SwiftCrossUI/Views/Modifiers/SheetModifier.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
7171
)
7272

7373
if isPresented.wrappedValue && children.sheet == nil {
74-
//let sheetSize = dryRunResult.size.idealSize
75-
7674
let sheet = backend.createSheet()
7775

7876
let dryRunResult = children.sheetContentNode.update(
@@ -106,6 +104,13 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
106104
backend.setPresentationDetents(of: sheet, to: detents)
107105
}
108106

107+
if let presentationDragIndicatorVisibility = preferences
108+
.presentationDragIndicatorVisibility
109+
{
110+
backend.setPresentationDragIndicatorVisibility(
111+
of: sheet, to: presentationDragIndicatorVisibility)
112+
}
113+
109114
backend.showSheet(
110115
sheet,
111116
window: .some(environment.window! as! Backend.Window)

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
extension View {
2+
/// Sets the available detents (heights) for a sheet presentation.
3+
///
4+
/// This modifier only affects the sheet presentation itself when applied to the
5+
/// top-level view within a sheet. It allows users to resize the sheet to different
6+
/// predefined heights.
7+
///
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+
///
12+
/// - Parameter detents: A set of detents that the sheet can be resized to.
13+
/// - Returns: A view with the presentation detents preference set.
14+
public func presentationDetents(_ detents: Set<PresentationDetent>) -> some View {
15+
preference(key: \.presentationDetents, value: Array(detents))
16+
}
17+
18+
/// Sets the corner radius for a sheet presentation.
19+
///
20+
/// This modifier only affects the sheet presentation itself when applied to the
21+
/// top-level view within a sheet. It does not affect the content's corner radius.
22+
///
23+
/// supported platforms: iOS (ignored on unsupported platforms)
24+
/// ignored on: older than iOS 15
25+
///
26+
/// - Parameter radius: The corner radius in pixels.
27+
/// - Returns: A view with the presentation corner radius preference set.
28+
public func presentationCornerRadius(_ radius: Double) -> some View {
29+
preference(key: \.presentationCornerRadius, value: radius)
30+
}
31+
32+
public func presentationDragIndicatorVisibility(
33+
_ visibility: PresentationDragIndicatorVisibility
34+
) -> some View {
35+
preference(key: \.presentationDragIndicatorVisibility, value: visibility)
36+
}
37+
}

Sources/UIKitBackend/UIKitBackend+Sheet.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ extension UIKitBackend {
8181
#endif
8282
}
8383
}
84+
85+
public func setPresentationDragIndicatorVisibility(
86+
of sheet: Sheet, to visibility: PresentationDragIndicatorVisibility
87+
) {
88+
if #available(iOS 15.0, *) {
89+
if let sheetController = sheet.sheetPresentationController {
90+
sheetController.prefersGrabberVisible = visibility == .visible ? true : false
91+
}
92+
} else {
93+
#if DEBUG
94+
print(
95+
"Your current OS Version doesn't support setting sheet drag indicator visibility.\n Setting this is only available from iOS 15.0"
96+
)
97+
#endif
98+
}
99+
}
84100
}
85101

86102
public final class CustomSheet: UIViewController, SheetImplementation {

0 commit comments

Comments
 (0)