Skip to content

Commit b1e436b

Browse files
committed
added presentationBackground
1 parent 79837ed commit b1e436b

File tree

7 files changed

+96
-18
lines changed

7 files changed

+96
-18
lines changed

Examples/Sources/WindowingExample/WindowingApp.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,29 @@ struct SheetDemo: View {
8585
SheetBody()
8686
.presentationDetents([.height(150), .medium, .large])
8787
.presentationDragIndicatorVisibility(.visible)
88+
.presentationBackground(.blue)
8889
}
8990
.sheet(isPresented: $isShortTermSheetPresented) {
9091
Text("I'm only here for 5s")
9192
.padding(20)
9293
.presentationDetents([.height(150), .medium, .large])
9394
.presentationCornerRadius(10)
95+
.presentationBackground(.red)
9496
}
9597
}
9698

9799
struct SheetBody: View {
98100
@State var isPresented = false
99101

100102
var body: some View {
101-
ZStack {
102-
Color.blue
103-
VStack {
104-
Text("Nice sheet content")
105-
.padding(20)
106-
Button("I want more sheet") {
107-
isPresented = true
108-
print("should get presented")
109-
}
110-
Spacer()
103+
VStack {
104+
Text("Nice sheet content")
105+
.padding(20)
106+
Button("I want more sheet") {
107+
isPresented = true
108+
print("should get presented")
111109
}
110+
Spacer()
112111
}
113112
.sheet(isPresented: $isPresented) {
114113
print("nested sheet dismissed")

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,8 +1708,8 @@ public final class AppKitBackend: AppBackend {
17081708
) {
17091709
let contentSize = naturalSize(of: content)
17101710

1711-
let width = max(contentSize.x, 80)
1712-
let height = max(contentSize.y, 80)
1711+
let width = max(contentSize.x, 10)
1712+
let height = max(contentSize.y, 10)
17131713
sheet.setContentSize(NSSize(width: width, height: height))
17141714

17151715
sheet.contentView = content
@@ -1734,6 +1734,38 @@ public final class AppKitBackend: AppBackend {
17341734
NSApplication.shared.stopModal()
17351735
}
17361736
}
1737+
1738+
public func setPresentationBackground(of sheet: NSCustomSheet, to color: Color) {
1739+
let backgroundView = NSView()
1740+
backgroundView.wantsLayer = true
1741+
backgroundView.layer?.backgroundColor = color.nsColor.cgColor
1742+
1743+
if let existingContentView = sheet.contentView {
1744+
let container = NSView()
1745+
container.translatesAutoresizingMaskIntoConstraints = false
1746+
1747+
container.addSubview(backgroundView)
1748+
backgroundView.translatesAutoresizingMaskIntoConstraints = false
1749+
backgroundView.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive =
1750+
true
1751+
backgroundView.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
1752+
backgroundView.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive =
1753+
true
1754+
backgroundView.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
1755+
1756+
container.addSubview(existingContentView)
1757+
existingContentView.translatesAutoresizingMaskIntoConstraints = false
1758+
existingContentView.leadingAnchor.constraint(equalTo: container.leadingAnchor)
1759+
.isActive = true
1760+
existingContentView.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
1761+
existingContentView.trailingAnchor.constraint(equalTo: container.trailingAnchor)
1762+
.isActive = true
1763+
existingContentView.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive =
1764+
true
1765+
1766+
sheet.contentView = container
1767+
}
1768+
}
17371769
}
17381770

17391771
public final class NSCustomSheet: NSCustomWindow, NSWindowDelegate, SheetImplementation {

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,28 @@ 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+
/// Sets the visibility for a sheet presentation.
658+
///
659+
/// This method is called when the sheet content has a `presentationDragIndicatorVisibility`
660+
/// modifier applied at its top level.
661+
///
662+
/// - Parameters:
663+
/// - sheet: The sheet to apply the detents to.
664+
/// - visibility: visibility of the drag indicator (visible or hidden)
657665
func setPresentationDragIndicatorVisibility(
658-
of sheet: Sheet, to visibility: PresentationDragIndicatorVisibility)
666+
of sheet: Sheet,
667+
to visibility: PresentationDragIndicatorVisibility
668+
)
669+
670+
/// Sets the background color for a sheet presentation.
671+
///
672+
/// This method is called when the sheet content has a `presentationBackground`
673+
/// modifier applied at its top level.
674+
///
675+
/// - Parameters:
676+
/// - sheet: The sheet to apply the detents to.
677+
/// - color: rgba background color
678+
func setPresentationBackground(of sheet: Sheet, to color: Color)
659679

660680
/// Presents an 'Open file' dialog to the user for selecting files or
661681
/// folders.
@@ -786,9 +806,11 @@ extension AppBackend {
786806
}
787807

788808
private func ignored(_ function: String = #function) {
789-
print(
790-
"\(type(of: self)): \(function) is being ignored\nConsult at the documentation for further information."
791-
)
809+
#if DEBUG
810+
print(
811+
"\(type(of: self)): \(function) is being ignored\nConsult at the documentation for further information."
812+
)
813+
#endif
792814
}
793815

794816
// MARK: System
@@ -1263,4 +1285,8 @@ extension AppBackend {
12631285
) {
12641286
ignored()
12651287
}
1288+
1289+
func setPresentationBackground(of sheet: Sheet, to color: Color) {
1290+
todo()
1291+
}
12661292
}

Sources/SwiftCrossUI/ViewGraph/PreferenceValues.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ public struct PreferenceValues: Sendable {
44
public static let `default` = PreferenceValues(
55
onOpenURL: nil,
66
presentationDetents: nil,
7-
presentationCornerRadius: nil
7+
presentationCornerRadius: nil,
8+
presentationDragIndicatorVisibility: nil,
9+
presentationBackground: nil
810
)
911

1012
public var onOpenURL: (@Sendable @MainActor (URL) -> Void)?
@@ -15,17 +17,23 @@ public struct PreferenceValues: Sendable {
1517
/// The corner radius for a sheet presentation. Only applies to the top-level view in a sheet.
1618
public var presentationCornerRadius: Double?
1719

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

23+
public var presentationBackground: Color?
24+
2025
public init(
2126
onOpenURL: (@Sendable @MainActor (URL) -> Void)?,
2227
presentationDetents: [PresentationDetent]? = nil,
2328
presentationCornerRadius: Double? = nil,
24-
presentationDragIndicatorVisibility: PresentationDragIndicatorVisibility? = nil
29+
presentationDragIndicatorVisibility: PresentationDragIndicatorVisibility? = nil,
30+
presentationBackground: Color?
2531
) {
2632
self.onOpenURL = onOpenURL
2733
self.presentationDetents = presentationDetents
2834
self.presentationCornerRadius = presentationCornerRadius
35+
self.presentationDragIndicatorVisibility = presentationDragIndicatorVisibility
36+
self.presentationBackground = presentationBackground
2937
}
3038

3139
public init(merging children: [PreferenceValues]) {
@@ -44,5 +52,6 @@ public struct PreferenceValues: Sendable {
4452
presentationDetents = children.first?.presentationDetents
4553
presentationCornerRadius = children.first?.presentationCornerRadius
4654
presentationDragIndicatorVisibility = children.first?.presentationDragIndicatorVisibility
55+
presentationBackground = children.first?.presentationBackground
4756
}
4857
}

Sources/SwiftCrossUI/Views/Modifiers/SheetModifier.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
111111
of: sheet, to: presentationDragIndicatorVisibility)
112112
}
113113

114+
if let presentationBackground = preferences.presentationBackground {
115+
backend.setPresentationBackground(of: sheet, to: presentationBackground)
116+
}
117+
114118
backend.showSheet(
115119
sheet,
116120
window: .some(environment.window! as! Backend.Window)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ extension View {
3434
) -> some View {
3535
preference(key: \.presentationDragIndicatorVisibility, value: visibility)
3636
}
37+
38+
public func presentationBackground(_ color: Color) -> some View {
39+
preference(key: \.presentationBackground, value: color)
40+
}
3741
}

Sources/UIKitBackend/UIKitBackend+Sheet.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ extension UIKitBackend {
9797
#endif
9898
}
9999
}
100+
101+
public func setPresentationBackground(of sheet: CustomSheet, to color: Color) {
102+
sheet.view.backgroundColor = color.uiColor
103+
}
100104
}
101105

102106
public final class CustomSheet: UIViewController, SheetImplementation {

0 commit comments

Comments
 (0)