Skip to content

Commit

Permalink
Usher in the future of cross-platform swift + GUI.
Browse files Browse the repository at this point in the history
Signed-off-by: furby™ <devs@wabi.foundation>
  • Loading branch information
furby-tm committed Feb 15, 2025
1 parent 5365fce commit 3399e8c
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 40 deletions.
114 changes: 111 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "3abc8be558cfafce9b6bfe2e4459c53be9490bc02aa0976397031b7ca60ab377",
"originHash" : "35f355cf71a66e4bb76f82485e8ff46d4e2422f327423b219d652e953b7845f4",
"pins" : [
{
"identity" : "icu",
Expand All @@ -10,13 +10,31 @@
"version" : "76.1.2"
}
},
{
"identity" : "jpeg",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stackotter/jpeg",
"state" : {
"revision" : "a27e47f49479993b2541bc5f5c95d9354ed567a0",
"version" : "1.0.2"
}
},
{
"identity" : "libpng",
"kind" : "remoteSourceControl",
"location" : "https://github.com/the-swift-collective/libpng",
"state" : {
"revision" : "0eff23aca92a086b7892831f5cb4f58e15be9449",
"version" : "1.6.45"
}
},
{
"identity" : "metaversekit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wabiverse/MetaverseKit",
"state" : {
"revision" : "e23651486710c0a442c70adc709acdf91c0c74ff",
"version" : "1.9.0"
"branch" : "main",
"revision" : "0434bf2bd815156ca8900a3442c95a315bc0fe58"
}
},
{
Expand Down Expand Up @@ -46,6 +64,24 @@
"version" : "1.4.0"
}
},
{
"identity" : "swift-cross-ui",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wabiverse/swift-cross-ui",
"state" : {
"branch" : "c5981ed",
"revision" : "c5981edad491143d79c7d36996ffea033c702fd6"
}
},
{
"identity" : "swift-cwinrt",
"kind" : "remoteSourceControl",
"location" : "https://github.com/thebrowsercompany/swift-cwinrt",
"state" : {
"branch" : "main",
"revision" : "3e3d5a0270ebe8fb0bc738d24d4786a6cd0621eb"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
Expand All @@ -64,6 +100,24 @@
"version" : "1.0.0"
}
},
{
"identity" : "swift-image-formats",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stackotter/swift-image-formats",
"state" : {
"revision" : "05a0169a2a5e9365a058e9aa13da5937be6e2586",
"version" : "0.3.1"
}
},
{
"identity" : "swift-libwebp",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stackotter/swift-libwebp",
"state" : {
"branch" : "61dc378",
"revision" : "61dc3787c764022ad2f5ab4f9994a569afe86f9f"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
Expand All @@ -73,6 +127,15 @@
"version" : "1.6.1"
}
},
{
"identity" : "swift-macro-toolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stackotter/swift-macro-toolkit",
"state" : {
"revision" : "e706aa98bc28f82677923f7b8f560bba6f90fac2",
"version" : "0.6.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
Expand All @@ -82,6 +145,42 @@
"version" : "600.0.1"
}
},
{
"identity" : "swift-uwp",
"kind" : "remoteSourceControl",
"location" : "https://github.com/thebrowsercompany/swift-uwp",
"state" : {
"branch" : "main",
"revision" : "4fe2fa05652f688ba5af57d0ce9e93961f6dea98"
}
},
{
"identity" : "swift-windowsappsdk",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wabiverse/swift-windowsappsdk",
"state" : {
"branch" : "main",
"revision" : "36e2f6c261fcbffc303c107b24f705605f3908be"
}
},
{
"identity" : "swift-windowsfoundation",
"kind" : "remoteSourceControl",
"location" : "https://github.com/thebrowsercompany/swift-windowsfoundation",
"state" : {
"branch" : "main",
"revision" : "dbe14563b6bb0eb9c761d8aff7f465afddf185f9"
}
},
{
"identity" : "swift-winui",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wabiverse/swift-winui",
"state" : {
"branch" : "main",
"revision" : "0529457d9f360904b2009fd67cba90046ff252c4"
}
},
{
"identity" : "version",
"kind" : "remoteSourceControl",
Expand All @@ -90,6 +189,15 @@
"revision" : "1fe824b80d89201652e7eca7c9252269a1d85e25",
"version" : "2.0.1"
}
},
{
"identity" : "zlib",
"kind" : "remoteSourceControl",
"location" : "https://github.com/the-swift-collective/zlib.git",
"state" : {
"revision" : "f35d438c14187cb369dfd82603c3427dc66fbed5",
"version" : "1.3.1"
}
}
],
"version" : 3
Expand Down
10 changes: 7 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,9 @@ let package = Package(
),
],
dependencies: [
// prepare for SwiftCrossUI, for a cross-platform UsdView.
// .package(url: "https://github.com/stackotter/swift-cross-ui", revision: "5c5d8c8"),
.package(url: "https://github.com/wabiverse/swift-cross-ui", revision: "c5981ed"),
.package(url: "https://github.com/wabiverse/icu.git", from: "76.1.2"),
.package(url: "https://github.com/wabiverse/MetaverseKit", from: "1.9.0"),
.package(url: "https://github.com/wabiverse/MetaverseKit", branch: "main"),
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.1"),
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.4.3"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
Expand Down Expand Up @@ -1718,6 +1717,11 @@ let package = Package(
name: "UsdView",
dependencies: [
.target(name: "PixarUSD"),
.product(name: "SwiftCrossUI", package: "swift-cross-ui"),
.product(name: "AppKitBackend", package: "swift-cross-ui", condition: .when(platforms: [.macOS])),
.product(name: "UIKitBackend", package: "swift-cross-ui", condition: .when(platforms: [.iOS, .visionOS, .tvOS, .watchOS])),
.product(name: "GtkBackend", package: "swift-cross-ui", condition: .when(platforms: [.linux])),
// .product(name: "WinUIBackend", package: "swift-cross-ui", condition: .when(platforms: [.windows])),
],
resources: [
.process("Resources")
Expand Down
10 changes: 8 additions & 2 deletions Sources/UsdView/Hydra/Hydra+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import Foundation
import PixarUSD
import SwiftCrossUI

#if canImport(Metal)
import Metal
Expand All @@ -24,8 +25,13 @@ public extension Hydra
typealias Viewport = Hydra.MTLView
#elseif os(tvOS) || os(watchOS)
typealias Viewport = UIViewRepresentable
#else
struct Viewport {}
#else // os(Linux) || os(Windows)
struct Viewport: View
{
var body: some View {
Text("Hello World.")
}
}
#endif // os(macOS) || os(iOS) || os(visionOS)
}

Expand Down
11 changes: 6 additions & 5 deletions Sources/UsdView/Hydra/MTL/Hydra+NS+MTLView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
import Foundation
import PixarUSD

#if canImport(SwiftUI)
import Combine
import SwiftUI
#if canImport(SwiftCrossUI)
import SwiftCrossUI

#if os(macOS)
import Combine
import Metal
import MetalKit
import AppKitBackend

public extension Hydra
{
Expand Down Expand Up @@ -63,7 +64,7 @@ import PixarUSD
return Coordinator(mtkView: mtkView)
}

public func makeNSView(context: Context) -> MTKView
public func makeNSView(context: NSViewRepresentableContext<Coordinator>) -> MTKView
{
let metalView = context.coordinator.metalView

Expand All @@ -78,7 +79,7 @@ import PixarUSD
return metalView
}

public func updateNSView(_ view: MTKView, context _: Context)
public func updateNSView(_ view: MTKView, context _: NSViewRepresentableContext<Coordinator>)
{
renderer.draw(in: view)
}
Expand Down
11 changes: 6 additions & 5 deletions Sources/UsdView/Hydra/MTL/Hydra+UI+MTLView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
import Foundation
import PixarUSD

#if canImport(SwiftUI)
import Combine
import SwiftUI
#if canImport(SwiftCrossUI)
import SwiftCrossUI

#if os(iOS) || os(visionOS)
import Combine
import Metal
import MetalKit
import UIKitBackend

public extension Hydra
{
Expand Down Expand Up @@ -56,7 +57,7 @@ import PixarUSD
return Coordinator(mtkView: mtkView)
}

public func makeUIView(context: Context) -> MTKView
public func makeUIView(context: UIViewRepresentableContext<Coordinator>) -> MTKView
{
let metalView = context.coordinator.metalView

Expand All @@ -71,7 +72,7 @@ import PixarUSD
return metalView
}

public func updateUIView(_ view: MTKView, context _: Context)
public func updateUIView(_ view: MTKView, context _: UIViewRepresentableContext<Coordinator>)
{
renderer.draw(in: view)
}
Expand Down
33 changes: 33 additions & 0 deletions Sources/UsdView/UI/UI+Backend.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ----------------------------------------------------------------
* :: : M E T A V E R S E : ::
* ----------------------------------------------------------------
* Licensed under the terms set forth in the LICENSE.txt file, this
* file is available at https://openusd.org/license.
*
* Copyright (C) 2016 Pixar.
* Copyright (C) 2024 Wabi Foundation. All Rights Reserved.
* ----------------------------------------------------------------
* . x x x . o o o . x x x . : : : . o x o . : : : .
* ---------------------------------------------------------------- */

import Foundation
// #if os(Linux) || os(Windows)
// import SwiftCrossUI
// #endif
import SwiftCrossUI

/* setup the platform backend. */

#if os(Linux)
import GtkBackend
public typealias PlatformBackend = GtkBackend
#elseif os(Windows)
import WinUIBackend
public typealias PlatformBackend = WinUIBackend
#elseif os(macOS)
import AppKitBackend
public typealias PlatformBackend = AppKitBackend
#else
import UIKitBackend
public typealias PlatformBackend = UIKitBackend
#endif
19 changes: 6 additions & 13 deletions Sources/UsdView/UsdView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@

import Foundation
import PixarUSD

#if canImport(SwiftUI)
import SwiftUI

public protocol PixarApp: App
{}
#else
public protocol PixarApp
{}
#endif
import SwiftCrossUI

/**
* ``UsdView``
Expand All @@ -32,8 +23,10 @@ import PixarUSD
* the purposes of demonstrating the usage of USD,
* from the Swift programming language. */
@main
struct UsdView: PixarApp
struct UsdView: App
{
typealias Backend = PlatformBackend

/// the active usd stage.
let stage: UsdStageRefPtr
/// the hydra rendering engine.
Expand All @@ -42,7 +35,7 @@ struct UsdView: PixarApp
public init()
{
// register all usd plugins & resources.
registerPlugins()
Pixar.Bundler.shared.setup(.resources)

// create a new usd stage.
stage = UsdView.createScene()
Expand All @@ -55,7 +48,7 @@ struct UsdView: PixarApp

var body: some Scene
{
WindowGroup("UsdView", id: "usdview")
WindowGroup("UsdView")
{
Hydra.Viewport(engine: engine)
}
Expand Down
9 changes: 0 additions & 9 deletions Sources/UsdView/Utils/AppUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
import Foundation
import PixarUSD

/// register all usd plugins & resources with plug registry,
/// automatically install the plugins and resources if they
/// cannot be found.
public func registerPlugins()
{
/* Setup all usd resources (python, plugins, resources). */
Pixar.Bundler.shared.setup(.resources)
}

/// path to a user's documents directory.
public func documentsDirPath() -> String
{
Expand Down

0 comments on commit 3399e8c

Please sign in to comment.