Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Jan 15, 2024
1 parent 699296c commit 043eee6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 77 deletions.
5 changes: 3 additions & 2 deletions Sources/UIComponent/Core/ComponentView/ComponentEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public class ComponentEngine {
isRendering = false
}

ComponentViewManager.shared.push(view: componentView)
EnvironmentManager.shared.push(key: CurrentComponentViewEnvironmentKey.self, value: componentView)
let renderNode: any RenderNode
if let currentRenderNode = self.renderNode {
renderNode = currentRenderNode
Expand All @@ -198,7 +198,8 @@ public class ComponentEngine {
let visibleFrame = (contentView?.convert(bounds, from: view) ?? bounds).inset(by: visibleFrameInsets)

var newVisibleRenderable = renderNode.visibleRenderables(in: visibleFrame)
ComponentViewManager.shared.pop()
EnvironmentManager.shared.pop()

if contentSize != renderNode.size * zoomScale {
// update contentSize if it is changed. Some renderNodes update
// its size when visibleRenderables(in: visibleFrame) is called. e.g. InfiniteLayout
Expand Down
38 changes: 0 additions & 38 deletions Sources/UIComponent/Core/ComponentView/ComponentViewManager.swift

This file was deleted.

63 changes: 39 additions & 24 deletions Sources/UIComponent/Core/Model/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,46 @@

import Foundation

private class EnvironmentManager {
static let shared = EnvironmentManager()
internal class EnvironmentManager {
internal static let shared = EnvironmentManager()

private struct EnvironmentValues {
let values: [ObjectIdentifier: Any]
init() {
values = [:]
}
init<Value>(existing: EnvironmentValues, key: any EnvironmentKey<Value>.Type, value: Value) {
values = existing.values.merging([ObjectIdentifier(key): value]) { $1 }
}
subscript<Value>(key: any EnvironmentKey<Value>.Type) -> Value {
values[ObjectIdentifier(key)] as? Value ?? key.defaultValue
}
}

private var stack: [EnvironmentValues] = []

var current: EnvironmentValues {
private var current: EnvironmentValues {
stack.last ?? EnvironmentValues()
}

func push<K, Value>(key: K.Type, value: Value) where K : EnvironmentKey, K.Value == Value {
internal func value<Value>(key: any EnvironmentKey<Value>.Type) -> Value {
current[key]
}

internal func push<Value>(key: any EnvironmentKey<Value>.Type, value: Value) {
stack.append(EnvironmentValues(existing: current, key: key, value: value))
}

func pop() {
internal func pop() {
stack.removeLast()
}

internal func with<Value, Result>(key: any EnvironmentKey<Value>.Type, value: Value, accessor: () throws -> Result) rethrows -> Result {
push(key: key, value: value)
let result = try accessor()
pop()
return result
}
}

@frozen @propertyWrapper public struct Environment<Value> {
Expand All @@ -28,7 +52,7 @@ private class EnvironmentManager {
}

public var wrappedValue: Value {
EnvironmentManager.shared.current[key]
EnvironmentManager.shared.value(key: key)
}
}

Expand All @@ -37,30 +61,15 @@ public protocol EnvironmentKey<Value> {
static var defaultValue: Self.Value { get }
}

public struct EnvironmentValues {
let values: [ObjectIdentifier: Any]
public init() {
values = [:]
}
public init<Value>(existing: EnvironmentValues, key: any EnvironmentKey<Value>.Type, value: Value) {
values = existing.values.merging([ObjectIdentifier(key): value]) { $1 }
}
public subscript<Value>(key: any EnvironmentKey<Value>.Type) -> Value {
values[ObjectIdentifier(key)] as? Value ?? key.defaultValue
}

}

public struct EnvironmentComponent<Value, Child: Component>: Component {
let key: any EnvironmentKey<Value>.Type
let value: Value
let child: Child

public func layout(_ constraint: Constraint) -> Child.R {
EnvironmentManager.shared.push(key: key, value: value)
let result = child.layout(constraint)
EnvironmentManager.shared.pop()
return result
EnvironmentManager.shared.with(key: key, value: value) {
child.layout(constraint)
}
}
}

Expand All @@ -69,3 +78,9 @@ public extension Component {
EnvironmentComponent(key: key, value: value, child: self)
}
}

public struct CurrentComponentViewEnvironmentKey: EnvironmentKey {
public static var defaultValue: ComponentDisplayableView? {
nil
}
}
30 changes: 17 additions & 13 deletions Sources/UIComponent/Extensions/CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@
import UIKit

extension CGSize {
static public let constraintMaxSize: CGSize = CGSize(width: CGFloat.infinity, height: CGFloat.infinity)
static public let infinity: CGSize = CGSize(width: CGFloat.infinity, height: CGFloat.infinity)

static public let constraintMinSize: CGSize = CGSize(width: -CGFloat.infinity, height: -CGFloat.infinity)
static public let negativeInfinity: CGSize = CGSize(width: -CGFloat.infinity, height: -CGFloat.infinity)

static public let constraintMaxSize: CGSize = .infinity

static public let constraintMinSize: CGSize = .negativeInfinity
}

extension CGPoint {
@inlinable public func distance(_ point: CGPoint) -> CGFloat {
@inlinable func distance(_ point: CGPoint) -> CGFloat {
hypot(point.x - x, point.y - y)
}

@inlinable public static func + (left: CGPoint, right: CGPoint) -> CGPoint {
@inlinable static func + (left: CGPoint, right: CGPoint) -> CGPoint {
CGPoint(x: left.x + right.x, y: left.y + right.y)
}

@inlinable public static func - (left: CGPoint, right: CGPoint) -> CGPoint {
@inlinable static func - (left: CGPoint, right: CGPoint) -> CGPoint {
CGPoint(x: left.x - right.x, y: left.y - right.y)
}

@inlinable public static func += (left: inout CGPoint, right: CGPoint) {
@inlinable static func += (left: inout CGPoint, right: CGPoint) {
left.x += right.x
left.y += right.y
}
}

extension CGSize {
@inlinable public static func * (left: CGSize, right: CGFloat) -> CGSize {
@inlinable static func * (left: CGSize, right: CGFloat) -> CGSize {
CGSize(width: left.width * right, height: left.height * right)
}

public func inset(by insets: UIEdgeInsets) -> CGSize {
@inlinable func inset(by insets: UIEdgeInsets) -> CGSize {
CGSize(width: width - insets.left - insets.right, height: height - insets.top - insets.bottom)
}
}
Expand All @@ -47,23 +51,23 @@ extension CGRect {
CGRect(origin: .zero, size: size)
}

@inlinable public static func + (left: CGRect, right: CGPoint) -> CGRect {
@inlinable static func + (left: CGRect, right: CGPoint) -> CGRect {
CGRect(origin: left.origin + right, size: left.size)
}

@inlinable public static func - (left: CGRect, right: CGPoint) -> CGRect {
@inlinable static func - (left: CGRect, right: CGPoint) -> CGRect {
CGRect(origin: left.origin - right, size: left.size)
}
}

extension Comparable {
public func clamp(_ minValue: Self, _ maxValue: Self) -> Self {
@inlinable func clamp(_ minValue: Self, _ maxValue: Self) -> Self {
self < minValue ? minValue : (self > maxValue ? maxValue : self)
}
}

extension UIEdgeInsets {
static public prefix func - (inset: UIEdgeInsets) -> UIEdgeInsets {
@inlinable static prefix func - (inset: UIEdgeInsets) -> UIEdgeInsets {
UIEdgeInsets(top: -inset.top, left: -inset.left, bottom: -inset.bottom, right: -inset.right)
}
}
Expand All @@ -73,7 +77,7 @@ extension Collection {
/// but not including the index N, and is false for all elements
/// starting with index N.
/// Behavior is undefined if there is no such N.
public func binarySearch(predicate: (Iterator.Element) -> Bool) -> Index {
func binarySearch(predicate: (Iterator.Element) -> Bool) -> Index {
var low = startIndex
var high = endIndex
while low != high {
Expand Down

0 comments on commit 043eee6

Please sign in to comment.