Skip to content

Commit 7df381c

Browse files
committed
added interactiveDismissDisabled modifier
1 parent 3108f77 commit 7df381c

File tree

7 files changed

+48
-4
lines changed

7 files changed

+48
-4
lines changed

Examples/Sources/WindowingExample/WindowingApp.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ struct SheetDemo: View {
133133
}
134134
.sheet(isPresented: $showNextChild) {
135135
DoubleNestedSheetBody(dismissParent: { dismiss() })
136+
.interactiveDismissDisabled()
136137
}
137138
Button("dismiss parent sheet") {
138139
dismissParent()

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,10 @@ public final class AppKitBackend: AppBackend {
17661766
sheet.contentView = container
17671767
}
17681768
}
1769+
1770+
public func setInteractiveDismissDisabled(for sheet: NSCustomSheet, to disabled: Bool) {
1771+
sheet.interactiveDismissDisabled = disabled
1772+
}
17691773
}
17701774

17711775
public final class NSCustomSheet: NSCustomWindow, NSWindowDelegate, SheetImplementation {
@@ -1777,13 +1781,17 @@ public final class NSCustomSheet: NSCustomWindow, NSWindowDelegate, SheetImpleme
17771781
}
17781782
public var onDismiss: (() -> Void)?
17791783

1784+
public var interactiveDismissDisabled: Bool = false
1785+
17801786
public func dismiss() {
17811787
onDismiss?()
17821788
self.contentViewController?.dismiss(self)
17831789
}
17841790

17851791
@objc override public func cancelOperation(_ sender: Any?) {
1786-
dismiss()
1792+
if !interactiveDismissDisabled {
1793+
dismiss()
1794+
}
17871795
}
17881796
}
17891797

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,18 @@ public protocol AppBackend: Sendable {
677677
/// - color: rgba background color
678678
func setPresentationBackground(of sheet: Sheet, to color: Color)
679679

680+
/// Sets the interactive dismissablility of a sheet.
681+
/// when disabled the sheet can only be closed programmatically,
682+
/// not through users swiping, escape keys or similar.
683+
///
684+
/// This method is called when the sheet content has a `interactiveDismissDisabled`
685+
/// modifier applied at its top level.
686+
///
687+
/// - Parameters:
688+
/// - sheet: The sheet to apply the detents to.
689+
/// - disabled: wether its disabled
690+
func setInteractiveDismissDisabled(for sheet: Sheet, to disabled: Bool)
691+
680692
/// Presents an 'Open file' dialog to the user for selecting files or
681693
/// folders.
682694
///
@@ -1289,4 +1301,8 @@ extension AppBackend {
12891301
func setPresentationBackground(of sheet: Sheet, to color: Color) {
12901302
todo()
12911303
}
1304+
1305+
func setInteractiveDismissDisabled(for sheet: Sheet, to disabled: Bool) {
1306+
todo()
1307+
}
12921308
}

Sources/SwiftCrossUI/ViewGraph/PreferenceValues.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ public struct PreferenceValues: Sendable {
66
presentationDetents: nil,
77
presentationCornerRadius: nil,
88
presentationDragIndicatorVisibility: nil,
9-
presentationBackground: nil
9+
presentationBackground: nil,
10+
interactiveDismissDisabled: nil
1011
)
1112

1213
public var onOpenURL: (@Sendable @MainActor (URL) -> Void)?
@@ -17,23 +18,28 @@ public struct PreferenceValues: Sendable {
1718
/// The corner radius for a sheet presentation. Only applies to the top-level view in a sheet.
1819
public var presentationCornerRadius: Double?
1920

20-
/// The drag indicator visibiity for a sheet presentation. Only applies to the top-level view in a sheet.
21+
/// The drag indicator visibility for a sheet presentation. Only applies to the top-level view in a sheet.
2122
public var presentationDragIndicatorVisibility: PresentationDragIndicatorVisibility?
2223

24+
/// The backgroundcolor of a sheet. Only applies to the top-level view in a sheet
2325
public var presentationBackground: Color?
2426

27+
public var interactiveDismissDisabled: Bool?
28+
2529
public init(
2630
onOpenURL: (@Sendable @MainActor (URL) -> Void)?,
2731
presentationDetents: [PresentationDetent]? = nil,
2832
presentationCornerRadius: Double? = nil,
2933
presentationDragIndicatorVisibility: PresentationDragIndicatorVisibility? = nil,
30-
presentationBackground: Color?
34+
presentationBackground: Color? = nil,
35+
interactiveDismissDisabled: Bool? = nil
3136
) {
3237
self.onOpenURL = onOpenURL
3338
self.presentationDetents = presentationDetents
3439
self.presentationCornerRadius = presentationCornerRadius
3540
self.presentationDragIndicatorVisibility = presentationDragIndicatorVisibility
3641
self.presentationBackground = presentationBackground
42+
self.interactiveDismissDisabled = interactiveDismissDisabled
3743
}
3844

3945
public init(merging children: [PreferenceValues]) {
@@ -53,5 +59,6 @@ public struct PreferenceValues: Sendable {
5359
presentationCornerRadius = children.first?.presentationCornerRadius
5460
presentationDragIndicatorVisibility = children.first?.presentationDragIndicatorVisibility
5561
presentationBackground = children.first?.presentationBackground
62+
interactiveDismissDisabled = children.first?.interactiveDismissDisabled
5663
}
5764
}

Sources/SwiftCrossUI/Views/Modifiers/Style/PresentationModifiers.swift renamed to Sources/SwiftCrossUI/Views/Modifiers/PresentationModifiers.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ extension View {
3838
public func presentationBackground(_ color: Color) -> some View {
3939
preference(key: \.presentationBackground, value: color)
4040
}
41+
42+
public func interactiveDismissDisabled(_ isDisabled: Bool = true) -> some View {
43+
preference(key: \.interactiveDismissDisabled, value: isDisabled)
44+
}
4145
}

Sources/SwiftCrossUI/Views/Modifiers/SheetModifier.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
120120
backend.setPresentationBackground(of: sheet, to: presentationBackground)
121121
}
122122

123+
if let interactiveDismissDisabled = preferences.interactiveDismissDisabled {
124+
backend.setInteractiveDismissDisabled(for: sheet, to: interactiveDismissDisabled)
125+
}
126+
123127
backend.showSheet(
124128
sheet,
125129
window: .some(environment.window! as! Backend.Window)

Sources/UIKitBackend/UIKitBackend+Sheet.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ extension UIKitBackend {
109109
public func setPresentationBackground(of sheet: CustomSheet, to color: Color) {
110110
sheet.view.backgroundColor = color.uiColor
111111
}
112+
113+
public func setInteractiveDismissDisabled(for sheet: Sheet, to disabled: Bool) {
114+
sheet.isModalInPresentation = disabled
115+
}
112116
}
113117

114118
public final class CustomSheet: UIViewController, SheetImplementation {

0 commit comments

Comments
 (0)