-
Notifications
You must be signed in to change notification settings - Fork 61
Fix named image mask rendering and load system vector image issue #815
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
+217
−30
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48b7469
Add ImageRenderingMode API
Kyle-Ye c6e26b9
Add example and test case
Kyle-Ye b309a0b
Fix styleResolverMode
Kyle-Ye fb0aec0
Fix image render
Kyle-Ye f9634e9
Fix matchType for loadVectorInfo
Kyle-Ye 419b9c7
Update test case and example
Kyle-Ye 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
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
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
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
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
124 changes: 124 additions & 0 deletions
124
Sources/OpenSwiftUICore/View/Image/ImageRenderingMode.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,124 @@ | ||
| // | ||
| // ImageRenderingMode.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Complete | ||
| // ID: 6CBDCABF463BFE9CC4C20C83C3F5C7C1 (SwiftUICore) | ||
|
|
||
| // MARK: - Image + renderingMode | ||
|
|
||
| @available(OpenSwiftUI_v1_0, *) | ||
| extension Image { | ||
|
|
||
| /// Indicates whether OpenSwiftUI renders an image as-is, or | ||
| /// by using a different mode. | ||
| /// | ||
| /// The ``TemplateRenderingMode`` enumeration has two cases: | ||
| /// ``TemplateRenderingMode/original`` and ``TemplateRenderingMode/template``. | ||
| /// The original mode renders pixels as they appear in the original source | ||
| /// image. Template mode renders all nontransparent pixels as the | ||
| /// foreground color, which you can use for purposes like creating image | ||
| /// masks. | ||
| /// | ||
| /// The following example shows both rendering modes, as applied to an icon | ||
| /// image of a green circle with darker green border: | ||
| /// | ||
| /// Image("dot_green") | ||
| /// .renderingMode(.original) | ||
| /// Image("dot_green") | ||
| /// .renderingMode(.template) | ||
| /// | ||
| ///  | ||
| /// | ||
| /// You also use `renderingMode` to produce multicolored system graphics | ||
| /// from the SF Symbols set. Use the ``TemplateRenderingMode/original`` | ||
| /// mode to apply a foreground color to all parts of the symbol except | ||
| /// those that have a distinct color in the graphic. The following | ||
| /// example shows three uses of the `person.crop.circle.badge.plus` symbol | ||
| /// to achieve different effects: | ||
| /// | ||
| /// * A default appearance with no foreground color or template rendering | ||
| /// mode specified. The symbol appears all black in light mode, and all | ||
| /// white in Dark Mode. | ||
| /// * The multicolor behavior achieved by using `original` template | ||
| /// rendering mode, along with a blue foreground color. This mode causes the | ||
| /// graphic to override the foreground color for distinctive parts of the | ||
| /// image, in this case the plus icon. | ||
| /// * A single-color template behavior achieved by using `template` | ||
| /// rendering mode with a blue foreground color. This mode applies the | ||
| /// foreground color to the entire image, regardless of the user's Appearance preferences. | ||
| /// | ||
| ///```swift | ||
| ///HStack { | ||
| /// Image(systemName: "person.crop.circle.badge.plus") | ||
| /// Image(systemName: "person.crop.circle.badge.plus") | ||
| /// .renderingMode(.original) | ||
| /// .foregroundColor(.blue) | ||
| /// Image(systemName: "person.crop.circle.badge.plus") | ||
| /// .renderingMode(.template) | ||
| /// .foregroundColor(.blue) | ||
| ///} | ||
| ///.font(.largeTitle) | ||
| ///``` | ||
| /// | ||
| ///  | ||
| /// | ||
| /// Use the SF Symbols app to find system images that offer the multicolor | ||
| /// feature. Keep in mind that some multicolor symbols use both the | ||
| /// foreground and accent colors. | ||
| /// | ||
| /// - Parameter renderingMode: The mode OpenSwiftUI uses to render images. | ||
| /// - Returns: A modified ``Image``. | ||
| public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> Image { | ||
| Image( | ||
| RenderingModeProvider( | ||
| base: self, | ||
| renderingMode: renderingMode | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| // MARK: - RenderingModeProvider | ||
|
|
||
| private struct RenderingModeProvider: ImageProvider { | ||
| var base: Image | ||
|
|
||
| var renderingMode: Image.TemplateRenderingMode? | ||
|
|
||
| func resolve(in context: ImageResolutionContext) -> Image.Resolved { | ||
| var context = context | ||
| if !context.environment.imageIsTemplate(renderingMode: renderingMode), | ||
| context.symbolRenderingMode == nil { | ||
| context.symbolRenderingMode = .multicolor | ||
| } | ||
| var resolved = base.resolve(in: context) | ||
| switch resolved.image.contents { | ||
| case .vectorGlyph: | ||
| break | ||
| default: | ||
| resolved.image.maskColor = context.environment.imageIsTemplate(renderingMode: renderingMode) ? .white : nil | ||
| } | ||
| return resolved | ||
| } | ||
|
|
||
| func resolveNamedImage(in context: ImageResolutionContext) -> Image.NamedResolved? { | ||
| var context = context | ||
| if !context.environment.imageIsTemplate(renderingMode: renderingMode), | ||
| context.symbolRenderingMode == nil { | ||
| context.symbolRenderingMode = .multicolor | ||
| } | ||
| guard var resolved = base.resolveNamedImage(in: context) else { return nil } | ||
| resolved.isTemplate = context.environment.imageIsTemplate(renderingMode: renderingMode) | ||
| return resolved | ||
| } | ||
| } | ||
| } | ||
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
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
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.
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.
The condition that sets
context.symbolRenderingMode = .multicolortriggers whenever the effective template mode is non-template, which also includes therenderingMode: nil(reset-to-default) case; that may unintentionally change default SF Symbol behavior compared to not callingrenderingModeat all. (Also applies to the same logic inresolveNamedImagebelow.)Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.