Skip to content

Commit 9d6542e

Browse files
committed
Should fix UIKitBackend compile issue with visionOS and AppBackend Conformance of WinUIBackend and Gtk3Backend
1 parent 941131e commit 9d6542e

File tree

6 files changed

+89
-66
lines changed

6 files changed

+89
-66
lines changed

Sources/Gtk3Backend/Gtk3Backend.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public final class Gtk3Backend: AppBackend {
2222
public typealias Widget = Gtk3.Widget
2323
public typealias Menu = Gtk3.Menu
2424
public typealias Alert = Gtk3.MessageDialog
25+
public typealias Sheet = Gtk3.Window
2526

2627
public final class Path {
2728
var path: SwiftCrossUI.Path?
@@ -1516,3 +1517,9 @@ struct Gtk3Error: LocalizedError {
15161517
"gerror: code=\(code), domain=\(domain), message=\(message)"
15171518
}
15181519
}
1520+
1521+
extension Gtk3.Window: SheetImplementation {
1522+
public var sheetSize: SIMD2<Int> {
1523+
SIMD2(x: size.width, y: size.height)
1524+
}
1525+
}

Sources/GtkBackend/GtkBackend.swift

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,35 @@ public final class GtkBackend: AppBackend {
7474
let backend = Unmanaged<GtkBackend>.fromOpaque(userData).takeUnretainedValue()
7575
let key = OpaquePointer(instance)
7676
guard let ctx = backend.sheetContexts[key] else { return 1 }
77-
77+
7878
if ctx.interactiveDismissDisabled { return 1 }
79-
79+
8080
if ctx.isProgrammaticDismiss {
8181
ctx.isProgrammaticDismiss = false
8282
return 1
8383
}
84-
84+
8585
backend.runInMainThread {
8686
ctx.onDismiss()
8787
}
8888
return 1
8989
}
90-
90+
9191
// C-convention thunk for key-pressed
92-
private let escapeKeyPressedThunk: @convention(c) (
93-
UnsafeMutableRawPointer?, guint, guint, GdkModifierType, gpointer?
94-
) -> gboolean = { controller, keyval, keycode, state, userData in
95-
// TRUE (1) = consume event
96-
if keyval == GDK_KEY_Escape {
97-
guard let userData else { return 1 }
98-
let box = Unmanaged<ValueBox<() -> Void>>.fromOpaque(userData).takeUnretainedValue()
99-
box.value()
100-
return 1
92+
private let escapeKeyPressedThunk:
93+
@convention(c) (
94+
UnsafeMutableRawPointer?, guint, guint, GdkModifierType, gpointer?
95+
) -> gboolean = { controller, keyval, keycode, state, userData in
96+
// TRUE (1) = consume event
97+
if keyval == GDK_KEY_Escape {
98+
guard let userData else { return 1 }
99+
let box = Unmanaged<ValueBox<() -> Void>>.fromOpaque(userData).takeUnretainedValue()
100+
box.value()
101+
return 1
102+
}
103+
return 0
101104
}
102-
return 0
103-
}
104-
105+
105106
// A separate initializer to satisfy ``AppBackend``'s requirements.
106107
public convenience init() {
107108
self.init(appIdentifier: nil)
@@ -1637,7 +1638,7 @@ public final class GtkBackend: AppBackend {
16371638

16381639
let ctx = getOrCreateSheetContext(for: sheet)
16391640
ctx.onDismiss = onDismiss
1640-
1641+
16411642
sheet.css.set(property: .cornerRadius(defaultSheetCornerRadius))
16421643

16431644
if connectedCloseHandlers.insert(key).inserted {
@@ -1650,19 +1651,21 @@ public final class GtkBackend: AppBackend {
16501651
nil,
16511652
GConnectFlags(0)
16521653
)
1653-
1654+
16541655
let escapeHandler = gtk_event_controller_key_new()
16551656
gtk_event_controller_set_propagation_phase(escapeHandler, GTK_PHASE_BUBBLE)
1656-
g_signal_connect_data (
1657+
g_signal_connect_data(
16571658
UnsafeMutableRawPointer(escapeHandler),
16581659
"key-pressed",
16591660
unsafeBitCast(escapeKeyPressedThunk, to: GCallback.self),
1660-
Unmanaged.passRetained(ValueBox(value: {
1661-
if ctx.interactiveDismissDisabled { return }
1662-
self.runInMainThread {
1663-
ctx.onDismiss()
1664-
}
1665-
})).toOpaque(),
1661+
Unmanaged.passRetained(
1662+
ValueBox(value: {
1663+
if ctx.interactiveDismissDisabled { return }
1664+
self.runInMainThread {
1665+
ctx.onDismiss()
1666+
}
1667+
})
1668+
).toOpaque(),
16661669
{ data, _ in
16671670
if let data {
16681671
Unmanaged<ValueBox<() -> Void>>.fromOpaque(data).release()
@@ -1697,15 +1700,15 @@ public final class GtkBackend: AppBackend {
16971700

16981701
public func setInteractiveDismissDisabled(for sheet: Gtk.Window, to disabled: Bool) {
16991702
let ctx = getOrCreateSheetContext(for: sheet)
1700-
1703+
17011704
ctx.interactiveDismissDisabled = disabled
17021705
}
1703-
1706+
17041707
public func setPresentationCornerRadius(of sheet: Gtk.Window, to radius: Double) {
17051708
let radius = Int(radius)
17061709
sheet.css.set(property: .cornerRadius(radius))
17071710
}
1708-
1711+
17091712
private func getOrCreateSheetContext(for sheet: Gtk.Window) -> SheetContext {
17101713
let key: OpaquePointer = OpaquePointer(sheet.widgetPointer)
17111714
if let ctx = sheetContexts[key] {

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,11 +1298,11 @@ extension AppBackend {
12981298
ignored()
12991299
}
13001300

1301-
func setPresentationBackground(of sheet: Sheet, to color: Color) {
1301+
public func setPresentationBackground(of sheet: Sheet, to color: Color) {
13021302
todo()
13031303
}
13041304

1305-
func setInteractiveDismissDisabled(for sheet: Sheet, to disabled: Bool) {
1305+
public func setInteractiveDismissDisabled(for sheet: Sheet, to disabled: Bool) {
13061306
todo()
13071307
}
13081308
}

Sources/SwiftCrossUI/Views/Modifiers/PresentationModifiers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension View {
2727
public func presentationCornerRadius(_ radius: Double) -> some View {
2828
preference(key: \.presentationCornerRadius, value: radius)
2929
}
30-
30+
3131
/// Sets the visibility of a sheet's drag indicator.
3232
///
3333
/// This modifier only affects the sheet presentation itself when applied to the
@@ -42,7 +42,7 @@ extension View {
4242
) -> some View {
4343
preference(key: \.presentationDragIndicatorVisibility, value: visibility)
4444
}
45-
45+
4646
/// Sets the background of a sheet.
4747
///
4848
/// This modifier only affects the sheet presentation itself when applied to the
@@ -53,7 +53,7 @@ extension View {
5353
public func presentationBackground(_ color: Color) -> some View {
5454
preference(key: \.presentationBackground, value: color)
5555
}
56-
56+
5757
/// Sets wether the user should be able to dismiss the sheet themself.
5858
///
5959
/// This modifier only affects the sheet presentation itself when applied to the

Sources/UIKitBackend/UIKitBackend+Sheet.swift

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,36 @@ extension UIKitBackend {
3939

4040
public func setPresentationDetents(of sheet: CustomSheet, to detents: [PresentationDetent]) {
4141
if #available(iOS 15.0, *) {
42-
if let sheetPresentation = sheet.sheetPresentationController {
43-
sheetPresentation.detents = detents.map {
44-
switch $0 {
45-
case .medium: return .medium()
46-
case .large: return .large()
47-
case .fraction(let fraction):
48-
if #available(iOS 16.0, *) {
49-
return .custom(
50-
identifier: .init("Fraction:\(fraction)"),
51-
resolver: { context in
52-
context.maximumDetentValue * fraction
53-
})
54-
} else {
55-
return .medium()
56-
}
57-
case .height(let height):
58-
if #available(iOS 16.0, *) {
59-
return .custom(
60-
identifier: .init("Height:\(height)"),
61-
resolver: { context in
62-
height
63-
})
64-
} else {
65-
return .medium()
66-
}
42+
#if !os(visionOS)
43+
if let sheetPresentation = sheet.sheetPresentationController {
44+
sheetPresentation.detents = detents.map {
45+
switch $0 {
46+
case .medium: return .medium()
47+
case .large: return .large()
48+
case .fraction(let fraction):
49+
if #available(iOS 16.0, *) {
50+
return .custom(
51+
identifier: .init("Fraction:\(fraction)"),
52+
resolver: { context in
53+
context.maximumDetentValue * fraction
54+
})
55+
} else {
56+
return .medium()
57+
}
58+
case .height(let height):
59+
if #available(iOS 16.0, *) {
60+
return .custom(
61+
identifier: .init("Height:\(height)"),
62+
resolver: { context in
63+
height
64+
})
65+
} else {
66+
return .medium()
67+
}
68+
}
6769
}
6870
}
69-
}
71+
#endif
7072
} else {
7173
#if DEBUG
7274
print(
@@ -78,9 +80,11 @@ extension UIKitBackend {
7880

7981
public func setPresentationCornerRadius(of sheet: CustomSheet, to radius: Double) {
8082
if #available(iOS 15.0, *) {
81-
if let sheetController = sheet.sheetPresentationController {
82-
sheetController.preferredCornerRadius = radius
83-
}
83+
#if !os(visionOS)
84+
if let sheetController = sheet.sheetPresentationController {
85+
sheetController.preferredCornerRadius = radius
86+
}
87+
#endif
8488
} else {
8589
#if DEBUG
8690
print(
@@ -94,9 +98,11 @@ extension UIKitBackend {
9498
of sheet: Sheet, to visibility: PresentationDragIndicatorVisibility
9599
) {
96100
if #available(iOS 15.0, *) {
97-
if let sheetController = sheet.sheetPresentationController {
98-
sheetController.prefersGrabberVisible = visibility == .visible ? true : false
99-
}
101+
#if !os(visionOS)
102+
if let sheetController = sheet.sheetPresentationController {
103+
sheetController.prefersGrabberVisible = visibility == .visible ? true : false
104+
}
105+
#endif
100106
} else {
101107
#if DEBUG
102108
print(

Sources/WinUIBackend/WinUIBackend.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class WinUIBackend: AppBackend {
3434
public typealias Menu = Void
3535
public typealias Alert = WinUI.ContentDialog
3636
public typealias Path = GeometryGroupHolder
37+
public typealias Sheet = CustomWindow //only for be protocol conform. doesn't currently support it
3738

3839
public let defaultTableRowContentHeight = 20
3940
public let defaultTableCellVerticalPadding = 4
@@ -1869,7 +1870,7 @@ class SwiftIInitializeWithWindow: WindowsFoundation.IUnknown {
18691870
}
18701871
}
18711872

1872-
public class CustomWindow: WinUI.Window {
1873+
public class CustomWindow: WinUI.Window, SheetImplementation {
18731874
/// Hardcoded menu bar height from MenuBar_themeresources.xaml in the
18741875
/// microsoft-ui-xaml repository.
18751876
static let menuBarHeight = 0
@@ -1879,6 +1880,12 @@ public class CustomWindow: WinUI.Window {
18791880
var grid: WinUI.Grid
18801881
var cachedAppWindow: WinAppSDK.AppWindow!
18811882

1883+
//only for AppBackend conformance, no support yet
1884+
var sheetSize: SIMD2<Int> {
1885+
let size = self.cachedAppWindow.size
1886+
return SIMD2<Int>(x: size.width, y: size.height)
1887+
}
1888+
18821889
var scaleFactor: Double {
18831890
// I'm leaving this code here for future travellers. Be warned that this always
18841891
// seems to return 100% even if the scale factor is set to 125% in settings.

0 commit comments

Comments
 (0)