Fix tvOS pasteboard usage and simplify AlertState error copy flow

This commit is contained in:
世界
2026-02-09 15:54:03 +08:00
parent 2867ff5607
commit 76541292d6
+19 -13
View File
@@ -52,7 +52,7 @@ public struct AlertState: Equatable {
} }
private static func copyErrorMessage(_ text: String) { private static func copyErrorMessage(_ text: String) {
#if canImport(UIKit) #if canImport(UIKit) && !os(tvOS)
UIPasteboard.general.string = text UIPasteboard.general.string = text
#elseif canImport(AppKit) #elseif canImport(AppKit)
NSPasteboard.general.clearContents() NSPasteboard.general.clearContents()
@@ -60,26 +60,32 @@ public struct AlertState: Equatable {
#endif #endif
} }
public init(action: String, error: Error, dismiss: (() -> Void)? = nil) { private static var supportsErrorCopy: Bool {
self.init( #if canImport(UIKit) && !os(tvOS)
errorMessage: Self.formatErrorMessage(action: action, error: error), true
dismiss: dismiss, #elseif canImport(AppKit)
allowsCopy: true true
) #else
false
#endif
} }
public init(errorMessage: String, dismiss: (() -> Void)? = nil, allowsCopy: Bool = false) { public init(action: String, error: Error, dismiss: (() -> Void)? = nil) {
title = String(localized: "Error") let errorMessage = Self.formatErrorMessage(action: action, error: error)
message = errorMessage self.init(errorMessage: errorMessage, dismiss: dismiss)
if allowsCopy { if Self.supportsErrorCopy {
primaryButton = .default(String(localized: "Copy")) { primaryButton = .default(String(localized: "Copy")) {
Self.copyErrorMessage(errorMessage) Self.copyErrorMessage(errorMessage)
} }
secondaryButton = .default(String(localized: "Ok"), action: dismiss) secondaryButton = .default(String(localized: "Ok"), action: dismiss)
} else { }
}
public init(errorMessage: String, dismiss: (() -> Void)? = nil) {
title = String(localized: "Error")
message = errorMessage
primaryButton = .default(String(localized: "Ok"), action: dismiss) primaryButton = .default(String(localized: "Ok"), action: dismiss)
secondaryButton = nil secondaryButton = nil
}
onDismiss = nil onDismiss = nil
} }