Skip to content

Commit

Permalink
onPreferenceChanged: Use DispatchQueue.main.sync if we are running on…
Browse files Browse the repository at this point in the history
… the main-actor
  • Loading branch information
Calculable committed Dec 16, 2024
1 parent f96a75c commit 33d1cd7
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 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,15 +26,28 @@ public struct UBPopupWrapper<V: View>: 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

0 comments on commit 33d1cd7

Please sign in to comment.