Skip to content

Commit

Permalink
Add basic UIHostingView support (#53)
Browse files Browse the repository at this point in the history
* Add UIHostingView

* Update Time and UIHostingView

* Move File location

* Add basic ViewGraph support

* Update ViewRendererHost implementation

* Update UIHostingView's updateOutputs

* Update VIew Layout related implementation

* Fix Linux build issue
  • Loading branch information
Kyle-Ye committed Mar 24, 2024
1 parent 0269c3a commit 9d03a47
Show file tree
Hide file tree
Showing 36 changed files with 993 additions and 122 deletions.
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" : "ed01e7196afe56bf953f2f14b0b463208dda9c8d"
"revision" : "ccb771c9fd939f1e1eefb46f69139e94ebb0dc82"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Scripts/openswiftui_swiftinterface.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ cd $OPENSWIFTUI_ROOT

export OPENSWIFTUI_SWIFT_TESTING=0
export OPENGRAPH_SWIFT_TESTING=0
swift build -c release -Xswiftc -emit-module-interface -Xswiftc -enable-library-evolution
swift build -Xswiftc -emit-module-interface -Xswiftc -enable-library-evolution
24 changes: 24 additions & 0 deletions Sources/OpenSwiftUI/App/ScenePhase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ScenePhase.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// ID: 130BB08D98602D712FD59CAC6992C14A

public enum ScenePhase: Comparable, Hashable {
case background
case inactive
case active
}

private struct ScenePhaseKey: EnvironmentKey {
static let defaultValue: ScenePhase = .background
}

extension EnvironmentValues {
public var scenePhase: ScenePhase {
get { self[ScenePhaseKey.self] }
set { self[ScenePhaseKey.self] = newValue }
}
}
33 changes: 0 additions & 33 deletions Sources/OpenSwiftUI/Core/Data/EnvironmentKeys/ColorSchemeKey.swift

This file was deleted.

7 changes: 4 additions & 3 deletions Sources/OpenSwiftUI/Core/Graph/GraphHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class GraphHost {
private(set) var hostPreferenceValues: OptionalAttribute<PreferenceList>
private(set) var lastHostPreferencesSeed: VersionSeed = .invalid
private var pendingTransactions: [AsyncTransaction] = []
private(set) var inTransaction = false
private(set) var continuations: [() -> Void] = []
/*private(set)*/ var inTransaction = false
/*private(set)*/ var continuations: [() -> Void] = []
private(set) var mayDeferUpdate = true
private(set) var removedState: RemovedState = []

Expand Down Expand Up @@ -156,7 +156,8 @@ class GraphHost {
}

final func updatePreferences() -> Bool {
fatalError("TODO")
// fatalError("TODO")
return false
}

final func updateRemovedState() {
Expand Down
2 changes: 2 additions & 0 deletions Sources/OpenSwiftUI/Core/Log/Signpost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct Signpost {
}
}

static let render = Signpost(style: .kdebug(0), stability: .published)
static let renderUpdate = Signpost(style: .kdebug(0), stability: .published)
static let viewHost = Signpost(style: .kdebug(0), stability: .published)
}

Expand Down
68 changes: 68 additions & 0 deletions Sources/OpenSwiftUI/Core/Render/DisplayLink.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// DisplayLink.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// ID: D912470A6161D66810B373079EE9F26A

#if canImport(Darwin) && os(iOS) // Disable macOS temporary due to CADisplayLink issue
import QuartzCore
#if os(iOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif

final class DisplayLink: NSObject {
private weak var host: AnyUIHostingView?
private var link: CADisplayLink?
private var nextUpdate: Time
private var currentUpdate: Time?
private var interval: Double
private var reasons: Set<UInt32>
private var currentThread: ThreadName
private var nextThread: ThreadName

#if os(iOS)
init(host: AnyUIHostingView, window: UIWindow) {
fatalError("TODO")
}
#elseif os(macOS)
init(host: AnyUIHostingView, window: NSWindow) {
fatalError("TODO")
}
#endif

var willRender: Bool {
nextUpdate < .infinity
}

func setNextUpdate(delay: Double, interval: Double, reasons: Set<UInt32>) {
// TODO
}

func invalidate() {
Update.lock.withLock {
// TODO
}
}

@inline(__always)
func startAsyncRendering() {
nextThread = .async
}

@inline(__always)
func cancelAsyncRendering() {
nextThread = .main
}
}

extension DisplayLink {
enum ThreadName: Hashable {
case main
case async
}
}
#endif
6 changes: 3 additions & 3 deletions Sources/OpenSwiftUI/Core/Update/Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension MovableLock {

enum Update {
static let trackHost: AnyObject = TraceHost()
private static let lock = MovableLock.create()
static let lock = MovableLock.create()
private static var depth = 0
private static var actions: [() -> Void] = []

Expand Down Expand Up @@ -63,15 +63,15 @@ enum Update {
}
}

@inlinable
@inline(__always)
static func dispatchActions() {
// FIXME
for action in actions {
action()
}
}

@inlinable
@inline(__always)
static func syncMain(_ body: () -> Void) {
// TODO
fatalError("TODO")
Expand Down
76 changes: 76 additions & 0 deletions Sources/OpenSwiftUI/Core/Util/Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// Audited for RELEASE_2021
// Status: Complete

#if canImport(QuartzCore)
import QuartzCore
#endif

struct Time: Comparable, Hashable {
var seconds : Double

Expand All @@ -14,4 +18,76 @@ struct Time: Comparable, Hashable {

static let zero = Time(seconds: .zero)
static let infinity = Time(seconds: .infinity)

#if canImport(QuartzCore)
@inline(__always)
static var now: Time {
Time(seconds: CACurrentMediaTime())
}
#endif

@inline(__always)
private init(seconds: Double) {
self.seconds = seconds
}

@inline(__always)
static func seconds(_ value: Double) -> Time {
Time(seconds: value)
}

@inline(__always)
static func seconds(_ value: Int) -> Time {
Time(seconds: Double(value))
}

@inline(__always)
static func microseconds(_ value: Double) -> Time {
Time(seconds: value * 1e-3)
}

@inline(__always)
static func milliseconds(_ value: Double) -> Time {
Time(seconds: value * 1e-6)
}

@inline(__always)
static func nanoseconds(_ value: Double) -> Time {
Time(seconds: value * 1e-9)
}

@inline(__always)
static func += (lhs: inout Time, rhs: Time) {
lhs.seconds = lhs.seconds + rhs.seconds
}

@inline(__always)
static func + (lhs: Time, rhs: Time) -> Time {
Time(seconds: lhs.seconds + rhs.seconds)
}

@inline(__always)
static func -= (lhs: inout Time, rhs: Time) {
lhs.seconds = lhs.seconds - rhs.seconds
}

@inline(__always)
static func - (lhs: Time, rhs: Time) -> Time {
Time(seconds: lhs.seconds - rhs.seconds)
}

@inline(__always)
mutating func advancing(by seconds: Double) {
self.seconds += seconds
}

@inline(__always)
func advanced(by seconds: Double) -> Time {
Time(seconds: self.seconds + seconds)
}

@inline(__always)
func distance(to other: Time) -> Double {
seconds.distance(to: other.seconds)
}
}
Loading

0 comments on commit 9d03a47

Please sign in to comment.