Skip to content

Commit 3671bb4

Browse files
committed
More sendability improvements (please tell me this compiles on Windows)
1 parent 2074a63 commit 3671bb4

File tree

6 files changed

+20
-12
lines changed

6 files changed

+20
-12
lines changed

Sources/SwiftCrossUI/Environment/Actions/PresentAlertAction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// selected action. By default, the alert will have a single button labelled
44
/// `OK`. All buttons will dismiss the alert even if you provide your own
55
/// actions.
6+
@MainActor
67
public struct PresentAlertAction {
78
let environment: EnvironmentValues
89

Sources/SwiftCrossUI/Environment/Actions/PresentFileSaveDialogAction.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Foundation
22

33
/// Presents a 'Save file' dialog fit for selecting a save destination. Returns
44
/// `nil` if the user cancels the operation.
5-
public struct PresentFileSaveDialogAction {
5+
public struct PresentFileSaveDialogAction: Sendable {
66
let backend: any AppBackend
7-
let window: Any?
7+
let window: MainActorBox<Any?>
88

99
public func callAsFunction(
1010
title: String = "Save",
@@ -19,7 +19,7 @@ public struct PresentFileSaveDialogAction {
1919
return await withCheckedContinuation { continuation in
2020
backend.runInMainThread {
2121
let window: Backend.Window? =
22-
if let window = self.window {
22+
if let window = self.window.value {
2323
.some(window as! Backend.Window)
2424
} else {
2525
nil

Sources/SwiftCrossUI/Environment/Actions/PresentSingleFileOpenDialogAction.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import Foundation
33
/// Presents an 'Open file' dialog fit for selecting a single file. Some
44
/// backends only allow selecting either files or directories but not both
55
/// in a single dialog. Returns `nil` if the user cancels the operation.
6-
public struct PresentSingleFileOpenDialogAction {
6+
public struct PresentSingleFileOpenDialogAction: Sendable {
77
let backend: any AppBackend
8-
let window: Any?
8+
let window: MainActorBox<Any?>
99

1010
public func callAsFunction(
1111
title: String = "Open",
@@ -20,7 +20,7 @@ public struct PresentSingleFileOpenDialogAction {
2020
await withCheckedContinuation { continuation in
2121
backend.runInMainThread {
2222
let window: Backend.Window? =
23-
if let window = self.window {
23+
if let window = self.window.value {
2424
.some(window as! Backend.Window)
2525
} else {
2626
nil

Sources/SwiftCrossUI/Environment/EnvironmentValues.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ public struct EnvironmentValues {
9797
/// accessed outside of a scene's view graph (in which case the backend
9898
/// can decide whether to make it an app modal, a standalone window, or a
9999
/// window of its choosing).
100+
@MainActor
100101
public var chooseFile: PresentSingleFileOpenDialogAction {
101102
return PresentSingleFileOpenDialogAction(
102103
backend: backend,
103-
window: window
104+
window: .init(value: window)
104105
)
105106
}
106107

@@ -110,17 +111,19 @@ public struct EnvironmentValues {
110111
/// scene's view graph (in which case the backend can decide whether to
111112
/// make it an app modal, a standalone window, or a modal for a window of
112113
/// its chooosing).
114+
@MainActor
113115
public var chooseFileSaveDestination: PresentFileSaveDialogAction {
114116
return PresentFileSaveDialogAction(
115117
backend: backend,
116-
window: window
118+
window: .init(value: window)
117119
)
118120
}
119121

120122
/// Presents an alert for the current window, or the entire app if accessed
121123
/// outside of a scene's view graph (in which case the backend can decide
122124
/// whether to make it an app modal, a standalone window, or a modal for a
123125
/// window of its choosing).
126+
@MainActor
124127
public var presentAlert: PresentAlertAction {
125128
return PresentAlertAction(
126129
environment: self
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@MainActor
2+
struct MainActorBox<T>: Sendable {
3+
var value: T
4+
}

Sources/WinUIBackend/WinUIBackend.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public final class WinUIBackend: AppBackend {
5555
var sliderChangeActions: [ObjectIdentifier: (Double) -> Void] = [:]
5656
var textFieldChangeActions: [ObjectIdentifier: (String) -> Void] = [:]
5757
var textFieldSubmitActions: [ObjectIdentifier: () -> Void] = [:]
58-
var dispatcherQueue: WinAppSDK.DispatcherQueue?
5958
var themeChangeAction: (() -> Void)?
6059
}
6160

6261
private var internalState: InternalState
62+
nonisolated(unsafe) private var dispatcherQueue: WinAppSDK.DispatcherQueue?
6363
/// WinUI only allows one dialog at a time (subsequent dialogs throw
6464
/// exceptions), so we limit ourselves.
6565
private var dialogSemaphore = DispatchSemaphore(value: 1)
@@ -128,8 +128,8 @@ public final class WinUIBackend: AppBackend {
128128
}
129129
}
130130

131-
if internalState.dispatcherQueue == nil {
132-
internalState.dispatcherQueue = window.dispatcherQueue
131+
if self.dispatcherQueue == nil {
132+
self.dispatcherQueue = window.dispatcherQueue
133133
}
134134

135135
// import WinSDK
@@ -238,7 +238,7 @@ public final class WinUIBackend: AppBackend {
238238
}
239239

240240
public func runInMainThread(action: @escaping @MainActor () -> Void) {
241-
_ = try! internalState.dispatcherQueue!.tryEnqueue(.normal) {
241+
_ = try! dispatcherQueue!.tryEnqueue(.normal) {
242242
action()
243243
}
244244
}

0 commit comments

Comments
 (0)