Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix onPreferenceChange modifier on Xcode 16.2 #115

Merged
merged 3 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct UBPopupPreferenceKey: PreferenceKey {
}
}

@MainActor
struct UBPopupPreference: Equatable {
let id = UUID()
let isPresented: Binding<Bool>
Expand All @@ -36,7 +37,7 @@ struct UBPopupPreference: Equatable {
self.content = content
}

static func == (lhs: UBPopupPreference, rhs: UBPopupPreference) -> Bool {
nonisolated static func == (lhs: UBPopupPreference, rhs: UBPopupPreference) -> Bool {
lhs.id == rhs.id
&& lhs.isPresented.wrappedValue == rhs.isPresented.wrappedValue
&& lhs.date == rhs.date
Expand Down
2 changes: 1 addition & 1 deletion Sources/UBUserInterface/SwiftUI/Popup/UBPopupStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import SwiftUI

public struct UBPopupStyle: Equatable {
public struct UBPopupStyle: Equatable, Sendable {
let extendsToEdges: Bool
let backgroundColor: Color
let backdropColor: Color
Expand Down
22 changes: 19 additions & 3 deletions Sources/UBUserInterface/SwiftUI/Popup/UBPopupWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#if arch(arm64) || arch(x86_64)

import Foundation
import UBFoundation
import SwiftUI

public struct UBPopupWrapper<V: View>: View {
Expand All @@ -25,13 +26,28 @@ public struct UBPopupWrapper<V: View>: View {
UBPopupWindowManager.shared.setupWindow()
}
.onPreferenceChange(UBPopupPreferenceKey.self) { popupPreference in
if let popupPreference, popupPreference.isPresented.wrappedValue {
UBPopupWindowManager.shared.showPopup(isPresented: popupPreference.isPresented, style: popupPreference.customStyle ?? style, content: popupPreference.content)
if Thread.isMainThread {
MainActor.assumeIsolated {
popupPreferenceChanged(popupPreference: popupPreference)
}
} else {
UBPopupWindowManager.shared.hideWindow()
Log.reportError("onPreferenceChange called on non-main thread")
DispatchQueue.main.async {
MainActor.assumeIsolated {
popupPreferenceChanged(popupPreference: popupPreference)
}
}
}
}
}

private func popupPreferenceChanged(popupPreference: UBPopupPreference?) {
if let popupPreference, popupPreference.isPresented.wrappedValue {
UBPopupWindowManager.shared.showPopup(isPresented: popupPreference.isPresented, style: popupPreference.customStyle ?? style, content: popupPreference.content)
} else {
UBPopupWindowManager.shared.hideWindow()
}
}
}

#endif
Loading