Skip to content

Commit d6a63f2

Browse files
committed
improved sheet rendering
1 parent daca3e4 commit d6a63f2

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

Examples/Sources/WindowingExample/WindowingApp.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ struct SheetDemo: View {
8383
print("sheet dismissed")
8484
} content: {
8585
SheetBody()
86+
.frame(maxWidth: 200, maxHeight: 100)
87+
.presentationDetents([.height(150), .medium, .large])
8688
}
8789
.sheet(isPresented: $isShortTermSheetPresented) {
8890
Text("I'm only here for 5s")
@@ -105,6 +107,7 @@ struct SheetDemo: View {
105107
isPresented = true
106108
print("should get presented")
107109
}
110+
Spacer()
108111
}
109112
}
110113
.sheet(isPresented: $isPresented) {

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,13 @@ public final class AppKitBackend: AppBackend {
17361736
}
17371737
}
17381738

1739-
public final class NSCustomSheet: NSCustomWindow, NSWindowDelegate {
1739+
public final class NSCustomSheet: NSCustomWindow, NSWindowDelegate, SheetImplementation {
1740+
public var size: SIMD2<Int> {
1741+
guard let size = self.contentView?.frame.size else {
1742+
return SIMD2(x: 0, y: 0)
1743+
}
1744+
return SIMD2(x: Int(size.width), y: Int(size.height))
1745+
}
17401746
public var onDismiss: (() -> Void)?
17411747

17421748
public func dismiss() {

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public protocol AppBackend: Sendable {
4747
associatedtype Menu
4848
associatedtype Alert
4949
associatedtype Path
50-
associatedtype Sheet
50+
associatedtype Sheet: SheetImplementation
5151

5252
/// Creates an instance of the backend.
5353
init()
@@ -771,6 +771,10 @@ extension AppBackend {
771771
}
772772
}
773773

774+
public protocol SheetImplementation {
775+
var size: SIMD2<Int> { get }
776+
}
777+
774778
extension AppBackend {
775779
/// Used by placeholder implementations of backend methods.
776780
private func todo(_ function: String = #function) -> Never {

Sources/SwiftCrossUI/Views/Modifiers/SheetModifier.swift

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

7373
if isPresented.wrappedValue && children.sheet == nil {
74+
//let sheetSize = dryRunResult.size.idealSize
75+
76+
let sheet = backend.createSheet()
77+
7478
let dryRunResult = children.sheetContentNode.update(
7579
with: sheetContent(),
76-
proposedSize: proposedSize,
80+
proposedSize: sheet.size,
7781
environment: environment,
7882
dryRun: true
7983
)
8084

81-
// Extract preferences from the sheet content
8285
let preferences = dryRunResult.preferences
8386

84-
let sheetSize = dryRunResult.size.idealSize
85-
8687
let _ = children.sheetContentNode.update(
8788
with: sheetContent(),
88-
proposedSize: sheetSize,
89+
proposedSize: sheet.size,
8990
environment: environment,
9091
dryRun: false
9192
)
9293

93-
let sheet = backend.createSheet()
94-
9594
backend.updateSheet(
9695
sheet,
9796
content: children.sheetContentNode.widget.into(),

Sources/UIKitBackend/UIKitBackend+Sheet.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ extension UIKitBackend {
8383
}
8484
}
8585

86-
public final class CustomSheet: UIViewController {
86+
public final class CustomSheet: UIViewController, SheetImplementation {
87+
public var size: SIMD2<Int> {
88+
let size = view.frame.size
89+
return SIMD2(x: Int(size.width), y: Int(size.height))
90+
}
91+
8792
var onDismiss: (() -> Void)?
8893

8994
public override func viewDidLoad() {

0 commit comments

Comments
 (0)