diff --git a/Sources/UBUserInterface/SwiftUI/Popup/UBPopupWrapper.swift b/Sources/UBUserInterface/SwiftUI/Popup/UBPopupWrapper.swift index c4f4a64e..7488c584 100644 --- a/Sources/UBUserInterface/SwiftUI/Popup/UBPopupWrapper.swift +++ b/Sources/UBUserInterface/SwiftUI/Popup/UBPopupWrapper.swift @@ -8,6 +8,7 @@ #if arch(arm64) || arch(x86_64) import Foundation +import UBFoundation import SwiftUI public struct UBPopupWrapper: View { @@ -25,15 +26,28 @@ public struct UBPopupWrapper: View { UBPopupWindowManager.shared.setupWindow() } .onPreferenceChange(UBPopupPreferenceKey.self) { popupPreference in - DispatchQueue.main.async { - if let popupPreference, popupPreference.isPresented.wrappedValue { - UBPopupWindowManager.shared.showPopup(isPresented: popupPreference.isPresented, style: popupPreference.customStyle ?? style, content: popupPreference.content) - } else { - UBPopupWindowManager.shared.hideWindow() + if Thread.isMainThread { + MainActor.assumeIsolated { + popupPreferenceChanged(popupPreference: popupPreference) + } + } else { + Log.reportError("onPreferenceChange called on non-main thread") + DispatchQueue.main.sync { + 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