-
Notifications
You must be signed in to change notification settings - Fork 70
Add image attachment support to Text #941
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
Sources/OpenSwiftUICore/View/Text/Text/Text+Image.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // | ||
| // Text+Image.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Complete | ||
| // ID: A6FE71E8A6E76FC7E51CBB0D97E0D052 (SwiftUICore) | ||
|
|
||
| // MARK: - Text + Image | ||
|
|
||
| @available(OpenSwiftUI_v2_0, *) | ||
| extension Text { | ||
| /// Creates an instance that wraps an `Image`, suitable for concatenating | ||
| /// with other `Text` | ||
| @available(OpenSwiftUI_v2_0, *) | ||
| public init(_ image: Image) { | ||
| self.init(anyTextStorage: AttachmentTextStorage(image: image)) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - LocalizedStringKey.StringInterpolation + Image | ||
|
|
||
| @available(OpenSwiftUI_v2_0, *) | ||
| extension LocalizedStringKey.StringInterpolation { | ||
| /// Appends an image to a string interpolation. | ||
| /// | ||
| /// Don't call this method directly; it's used by the compiler when | ||
| /// interpreting string interpolations. | ||
| /// | ||
| /// - Parameter image: The image to append. | ||
| @_semantics("openswiftui.localized.appendInterpolation_@_specifier") | ||
| @_semantics("swiftui.localized.appendInterpolation_@_specifier") | ||
| public mutating func appendInterpolation(_ image: Image) { | ||
| appendInterpolation(Text(image)) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - AttachmentTextStorage | ||
|
|
||
| final private class AttachmentTextStorage: AnyTextStorage, @unchecked Sendable { | ||
| let image: Image | ||
|
|
||
| init(image: Image) { | ||
| self.image = image | ||
| } | ||
|
|
||
| override func resolve<T>( | ||
| into result: inout T, | ||
| in environment: EnvironmentValues, | ||
| with options: Text.ResolveOptions | ||
| ) where T: ResolvedTextContainer { | ||
| let context = ImageResolutionContext( | ||
| environment: environment, | ||
| textStyle: result.style | ||
| ) | ||
| guard resolveAndWriteAuxiliaryMetadataIfNeeded( | ||
| into: &result, | ||
| context: context, | ||
| environment: environment, | ||
| options: options | ||
| ) else { | ||
| result.append( | ||
| image.resolve(in: context), | ||
| in: environment, | ||
| with: options | ||
| ) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| override func resolvesToEmpty( | ||
| in environment: EnvironmentValues, | ||
| with options: Text.ResolveOptions | ||
| ) -> Bool { | ||
| false | ||
| } | ||
|
|
||
| override func isEqual(to other: AnyTextStorage) -> Bool { | ||
| guard let other = other as? AttachmentTextStorage else { | ||
| return false | ||
| } | ||
| return image == other.image | ||
| } | ||
|
|
||
| override func isStyled(options: Text.ResolveOptions) -> Bool { | ||
| true | ||
| } | ||
|
|
||
| func resolveAndWriteAuxiliaryMetadataIfNeeded<T>( | ||
| into result: inout T, | ||
| context: ImageResolutionContext, | ||
| environment: EnvironmentValues, | ||
| options: Text.ResolveOptions | ||
| ) -> Bool where T: ResolvedTextContainer { | ||
| guard options.contains(.writeAuxiliaryMetadata), | ||
| let image = image.resolveNamedImage(in: context) | ||
| else { | ||
| return false | ||
| } | ||
| result.append( | ||
| image, | ||
| in: environment, | ||
| with: options | ||
| ) | ||
| return true | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // | ||
| // TextImageTests.swift | ||
| // OpenSwiftUICoreTests | ||
|
|
||
| import OpenSwiftUICore | ||
| import Testing | ||
|
|
||
| struct TextImageTests { | ||
| @Test | ||
| func attachmentParticipatesInEquality() { | ||
| #expect(Text(Image(systemName: "star")) == Text(Image(systemName: "star"))) | ||
| #expect(Text(Image(systemName: "star")) != Text(Image(systemName: "circle"))) | ||
| } | ||
|
|
||
| @Test | ||
| func attachmentResolvesToObjectReplacementCharacter() { | ||
| let text = Text(Image.redacted) | ||
|
|
||
| #expect(text.resolveString(in: EnvironmentValues()) == "\u{fffc}") | ||
| } | ||
|
|
||
| @Test | ||
| func imageInterpolationUsesAttachmentText() { | ||
| let key: LocalizedStringKey = "Symbol: \(Image.redacted)" | ||
| let text = Text(key) | ||
|
|
||
| #expect(text.resolveString(in: EnvironmentValues()) == "Symbol: \u{fffc}") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sources/OpenSwiftUICore/View/Text/Text/Text+Image.swift:L96:image.resolveNamedImage(in:)internally callsresolve(in:); when it returnsnil(e.g., missing named asset) the fallback path resolves again, which can duplicate the external warning log and do the resolution work twice on the error path.Severity: low
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.