-
Notifications
You must be signed in to change notification settings - Fork 58
Update Image API #718
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
+431
−9
Merged
Update Image API #718
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e3cf31
Update CGImageProvider
Kyle-Ye a58e457
Add ImageResizing
Kyle-Ye 5bb368a
Add ImageInterpolation
Kyle-Ye c4a62d6
Add Image+PlatformRepresentation
Kyle-Ye e536bf4
Fix resolveNamedImage
Kyle-Ye 0a35069
Fix CGImage missing issue
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,94 @@ | ||
| // | ||
| // CGImageProvider.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Blocked by Image.Resolved | ||
| // ID: BB7900A03A030BC988C08113497314C3 (SwiftUICore?) | ||
|
|
||
| public import OpenCoreGraphicsShims | ||
|
|
||
| @available(OpenSwiftUI_v1_0, *) | ||
| extension Image { | ||
|
|
||
| /// Creates a labeled image based on a Core Graphics image instance, usable | ||
| /// as content for controls. | ||
| /// | ||
| /// - Parameters: | ||
| /// - cgImage: The base graphical image. | ||
| /// - scale: The scale factor for the image, | ||
| /// with a value like `1.0`, `2.0`, or `3.0`. | ||
| /// - orientation: The orientation of the image. The default is | ||
| /// ``Image/Orientation/up``. | ||
| /// - label: The label associated with the image. OpenSwiftUI uses the label | ||
| /// for accessibility. | ||
| public init( | ||
| _ cgImage: CGImage, | ||
| scale: CGFloat, | ||
| orientation: Image.Orientation = .up, | ||
| label: Text | ||
| ) { | ||
| self.init( | ||
| CGImageProvider( | ||
| image: cgImage, | ||
| scale: scale, | ||
| orientation: orientation, | ||
| label: label, | ||
| decorative: false | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /// Creates an unlabeled, decorative image based on a Core Graphics image | ||
| /// instance. | ||
| /// | ||
| /// OpenSwiftUI ignores this image for accessibility purposes. | ||
| /// | ||
| /// - Parameters: | ||
| /// - cgImage: The base graphical image. | ||
| /// - scale: The scale factor for the image, | ||
| /// with a value like `1.0`, `2.0`, or `3.0`. | ||
| /// - orientation: The orientation of the image. The default is | ||
| /// ``Image/Orientation/up``. | ||
| public init( | ||
| decorative cgImage: CGImage, | ||
| scale: CGFloat, | ||
| orientation: Image.Orientation = .up | ||
| ) { | ||
| self.init( | ||
| CGImageProvider( | ||
| image: cgImage, | ||
| scale: scale, | ||
| orientation: orientation, | ||
| label: nil, | ||
| decorative: true | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private struct CGImageProvider: ImageProvider { | ||
| var image: CGImage | ||
| var scale: CGFloat | ||
| var orientation: Image.Orientation | ||
| var label: Text? | ||
| var decorative: Bool | ||
|
|
||
| func resolve(in context: ImageResolutionContext) -> Image.Resolved { | ||
| _openSwiftUIUnimplementedFailure() | ||
| } | ||
|
|
||
| func resolveNamedImage(in context: ImageResolutionContext) -> Image.NamedResolved? { | ||
| nil | ||
| } | ||
| } | ||
|
|
||
| extension CGImage { | ||
| package var size: CGSize { | ||
| #if canImport(CoreGraphics) | ||
| CGSize(width: CGFloat(width), height: CGFloat(height)) | ||
| #else | ||
| _openSwiftUIPlatformUnimplementedFailure() | ||
| #endif | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
Sources/OpenSwiftUICore/View/Image/Image+PlatformRepresentation.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,79 @@ | ||
| // | ||
| // Image+PlatformRepresentation.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Complete | ||
| // ID: 9FE4F19E3F2D6B2A0FD05C040386BBC3 (SwiftUICore) | ||
|
|
||
| package import OpenAttributeGraphShims | ||
|
|
||
| // MARK: - PlatformImageRepresentable | ||
|
|
||
| package protocol PlatformImageRepresentable { | ||
| static func shouldMakeRepresentation(inputs: _ViewInputs) -> Bool | ||
|
|
||
| static func makeRepresentation(inputs: _ViewInputs, context: Attribute<Context>, outputs: inout _ViewOutputs) | ||
|
|
||
| typealias Context = PlatformImageRepresentableContext | ||
| } | ||
|
|
||
| package struct PlatformImageRepresentableContext { | ||
| package var image: Image.Resolved | ||
|
|
||
| package var tintColor: Color? | ||
|
|
||
| package var foregroundStyle: AnyShapeStyle? | ||
| } | ||
|
|
||
| extension _ViewInputs { | ||
| package var requestedImageRepresentation: (any PlatformImageRepresentable.Type)? { | ||
| get { base.requestedImageRepresentation } | ||
| set { base.requestedImageRepresentation = newValue } | ||
| } | ||
| } | ||
|
|
||
| extension _GraphInputs { | ||
| private struct ImageRepresentationKey: GraphInput { | ||
| static var defaultValue: (any PlatformImageRepresentable.Type)? { nil } | ||
| } | ||
|
|
||
| package var requestedImageRepresentation: (any PlatformImageRepresentable.Type)? { | ||
| get { self[ImageRepresentationKey.self] } | ||
| set { self[ImageRepresentationKey.self] = newValue } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - PlatformNamedImageRepresentable | ||
|
|
||
| package protocol PlatformNamedImageRepresentable { | ||
| static func shouldMakeRepresentation(inputs: _ViewInputs) -> Bool | ||
|
|
||
| static func makeRepresentation(inputs: _ViewInputs, context: Attribute<Context>, outputs: inout _ViewOutputs) | ||
|
|
||
| typealias Context = PlatformNamedImageRepresentableContext | ||
| } | ||
|
|
||
| package struct PlatformNamedImageRepresentableContext { | ||
| package var image: Image | ||
|
|
||
| package var environment: EnvironmentValues | ||
| } | ||
|
|
||
| extension _ViewInputs { | ||
| package var requestedNamedImageRepresentation: (any PlatformNamedImageRepresentable.Type)? { | ||
| get { base.requestedNamedImageRepresentation } | ||
| set { base.requestedNamedImageRepresentation = newValue } | ||
| } | ||
| } | ||
|
|
||
| extension _GraphInputs { | ||
| private struct NamedImageRepresentationKey: GraphInput { | ||
| static var defaultValue: (any PlatformNamedImageRepresentable.Type)? { nil } | ||
| } | ||
|
|
||
| package var requestedNamedImageRepresentation: (any PlatformNamedImageRepresentable.Type)? { | ||
| get { self[NamedImageRepresentationKey.self] } | ||
| set { self[NamedImageRepresentationKey.self] = newValue } | ||
| } | ||
| } |
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
131 changes: 131 additions & 0 deletions
131
Sources/OpenSwiftUICore/View/Image/ImageInterpolation.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,131 @@ | ||
| // | ||
| // ImageInterpolation.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Blocked by Image.Resolved | ||
| // ID: B65D626E77C8D6CB107EB45FECFC60F0 (SwiftUICore?) | ||
|
|
||
| // MARK: - Image.Interpolation | ||
|
|
||
| @available(OpenSwiftUI_v1_0, *) | ||
| extension Image { | ||
|
|
||
| /// The level of quality for rendering an image that requires interpolation, | ||
| /// such as a scaled image. | ||
| /// | ||
| /// The ``Image/interpolation(_:)`` modifier specifies the interpolation | ||
| /// behavior when using the ``Image/resizable(capInsets:resizingMode:)`` | ||
| /// modifier on an ``Image``. Use this behavior to prioritize rendering | ||
| /// performance or image quality. | ||
| public enum Interpolation: Sendable { | ||
|
|
||
| /// A value that indicates OpenSwiftUI doesn't interpolate image data. | ||
| case none | ||
|
|
||
| /// A value that indicates a low level of interpolation quality, which may | ||
| /// speed up image rendering. | ||
| case low | ||
|
|
||
| /// A value that indicates a medium level of interpolation quality, | ||
| /// between the low- and high-quality values. | ||
| case medium | ||
|
|
||
| /// A value that indicates a high level of interpolation quality, which | ||
| /// may slow down image rendering. | ||
| case high | ||
| } | ||
| } | ||
|
|
||
| // MARK: - InterpolatedProvider & AntialiasedProvider [WIP] | ||
|
|
||
| private struct InterpolatedProvider: ImageProvider { | ||
| var base: Image | ||
|
|
||
| var interpolation: Image.Interpolation | ||
|
|
||
| func resolve(in context: ImageResolutionContext) -> Image.Resolved { | ||
| var resolved = base.resolve(in: context) | ||
| // TODO | ||
| return resolved | ||
| } | ||
|
|
||
| func resolveNamedImage(in context: ImageResolutionContext) -> Image.NamedResolved? { | ||
| base.resolveNamedImage(in: context) | ||
| } | ||
| } | ||
|
|
||
| private struct AntialiasedProvider: ImageProvider { | ||
| var base: Image | ||
|
|
||
| var isAntialiased: Bool | ||
|
|
||
| func resolve(in context: ImageResolutionContext) -> Image.Resolved { | ||
| var resolved = base.resolve(in: context) | ||
| // TODO | ||
| return resolved | ||
| } | ||
|
|
||
| func resolveNamedImage(in context: ImageResolutionContext) -> Image.NamedResolved? { | ||
| base.resolveNamedImage(in: context) | ||
| } | ||
| } | ||
|
|
||
| @available(OpenSwiftUI_v1_0, *) | ||
| extension Image { | ||
|
|
||
| /// Specifies the current level of quality for rendering an | ||
| /// image that requires interpolation. | ||
| /// | ||
| /// See the article <doc:Fitting-Images-into-Available-Space> for examples | ||
| /// of using `interpolation(_:)` when scaling an ``Image``. | ||
| /// - Parameter interpolation: The quality level, expressed as a value of | ||
| /// the `Interpolation` type, that OpenSwiftUI applies when interpolating | ||
| /// an image. | ||
| /// - Returns: An image with the given interpolation value set. | ||
| public func interpolation(_ interpolation: Image.Interpolation) -> Image { | ||
| Image( | ||
| InterpolatedProvider( | ||
| base: self, | ||
| interpolation: interpolation | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /// Specifies whether OpenSwiftUI applies antialiasing when rendering | ||
| /// the image. | ||
| /// - Parameter isAntialiased: A Boolean value that specifies whether to | ||
| /// allow antialiasing. Pass `true` to allow antialising, `false` otherwise. | ||
| /// - Returns: An image with the antialiasing behavior set. | ||
| public func antialiased(_ isAntialiased: Bool) -> Image { | ||
| Image( | ||
| AntialiasedProvider( | ||
| base: self, | ||
| isAntialiased: isAntialiased | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Image.Interpolation + ProtobufEnum | ||
|
|
||
| extension Image.Interpolation: ProtobufEnum { | ||
| package var protobufValue: UInt { | ||
| switch self { | ||
| case .none: 0 | ||
| case .low: 1 | ||
| case .medium: 2 | ||
| case .high: 3 | ||
| } | ||
| } | ||
|
|
||
| package init?(protobufValue value: UInt) { | ||
| switch value { | ||
| case 0: self = .none | ||
| case 1: self = .low | ||
| case 2: self = .medium | ||
| case 3: self = .high | ||
| default: return nil | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.