Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions Sources/OpenSwiftUICore/View/Image/CGImageProvider.swift
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
}
}
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 }
}
}
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUICore/View/Image/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public struct Image: Equatable, Sendable {
}

package func resolveNamedImage(in context: ImageResolutionContext) -> Image.NamedResolved? {
provider.resolve(in: context)
provider.resolveNamedImage(in: context)
}

public static func == (lhs: Image, rhs: Image) -> Bool {
Expand Down
131 changes: 131 additions & 0 deletions Sources/OpenSwiftUICore/View/Image/ImageInterpolation.swift
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
}
}
}
Loading
Loading