Skip to content
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

[Feature] Update Environment related implementation #46

Merged
merged 7 commits into from
Mar 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/OpenSwiftUIProject/OpenGraph",
"state" : {
"branch" : "main",
"revision" : "2435a0a02b3ab3b97799a8f387284686fe9e6a7a"
"revision" : "ed01e7196afe56bf953f2f14b0b463208dda9c8d"
}
},
{
Expand Down
16 changes: 16 additions & 0 deletions Sources/OpenSwiftUI/Core/Attribute/AsyncAttribute.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// AsyncAttribute.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

internal import OpenGraphShims

protocol AsyncAttribute: _AttributeBody {}

extension Attribute {
func syncMainIfReferences<V>(do body: (Value) -> V) -> V {
fatalError("TODO")
}
}
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/Core/Data/Boxes/Indirect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
struct Indirect<A> {
var box : MutableBox<A>

var value: A { box.value }
var value: A { box.wrappedValue }
}

extension Indirect: Equatable where A: Equatable {
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/Core/Data/Boxes/MutableBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@propertyWrapper
final class MutableBox<A> {
var value: A
private var value: A

var wrappedValue: A {
get { value }
Expand Down
132 changes: 132 additions & 0 deletions Sources/OpenSwiftUI/Data/Environment/CachedEnvironment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// CachedEnvironment.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// ID: C424ABD9FC88B2DFD0B7B2319F2E7987

import Foundation
internal import OpenGraphShims

struct CachedEnvironment {
var environment: Attribute<EnvironmentValues>
private var items: [Item]
#if canImport(Darwin)
private var constants: [HashableConstant: OGAttribute]
#endif
private var animatedFrame: AnimatedFrame?
// private var resolvedFgStyles: [ResolvedFgStyle : Swift<_ShapeStyle_Resolved.ResolvedFg>]

init(_ environment: Attribute<EnvironmentValues>) {
self.environment = environment
items = []
#if canImport(Darwin)
constants = [:]
#endif
animatedFrame = nil
// resolvedFgStyles = [:]
}

func intern<Value>(_ value: Value, id: Int) -> Attribute<Value> {
fatalError("TODO")
}

func animatePosition(for inputs: _ViewInputs) -> Attribute<ViewOrigin> {
fatalError("TODO")
}

func animateSize(for inputs: _ViewInputs) -> Attribute<ViewSize> {
fatalError("TODO")
}

// func resolvedForegroundStyle() {}
}

extension CachedEnvironment {
private struct Item {
var key: PartialKeyPath<EnvironmentValues>
#if canImport(Darwin)
var value: OGAttribute
#endif
}
}

private protocol _Constant {
func hash(into hasher: inout Hasher)
func isEqual(to other: _Constant) -> Bool
}

private struct Constant<Value>: _Constant {
let value: Value
let id: Int

init(_ value: Value, id: Int) {
self.value = value
self.id = id
}

func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(Value.self))
hasher.combine(id)
}

func isEqual(to other: _Constant) -> Bool {
let otherConstant = other as? Constant<Value>
return otherConstant.map { id == $0.id } ?? false
}
}

private struct HashableConstant: Hashable {
let value: _Constant

init<Value>(_ value: Value, id: Int) {
self.value = Constant(value, id: id)
}

func hash(into hasher: inout Hasher) {
value.hash(into: &hasher)
}

static func == (lhs: HashableConstant, rhs: HashableConstant) -> Bool {
lhs.value.isEqual(to: rhs.value)
}
}

private struct AnimatedFrame {
let position: Attribute<ViewOrigin>
let size: Attribute<ViewSize>
let pixelLength: Attribute<CGFloat>
let time: Attribute<Time>
let transaction: Attribute<Transaction>
let viewPhase: Attribute<_GraphInputs.Phase>
let animatedFrame: Attribute<ViewFrame>
private var _animatedPosition: Attribute<ViewOrigin>?
private var _animatedSize: Attribute<ViewSize>?

mutating func animatePosition() -> Attribute<ViewOrigin> {
guard let _animatedPosition else {
// FIXME
let animatePosition = animatedFrame.unsafeOffset(
at: 0,
as:ViewOrigin.self
)
_animatedPosition = animatePosition
return animatePosition
}
return _animatedPosition
}

mutating func animateSize() -> Attribute<ViewSize> {
guard let _animatedSize else {
// FIXME
let animatedSize = animatedFrame.unsafeOffset(
at: MemoryLayout<ViewOrigin>.size,
as: ViewSize.self
)
_animatedSize = animatedSize
return animatedSize
}
return _animatedSize
}
}
Loading
Loading