Documentation
¶
Overview ¶
Package swiftui loads app-owned Swift packages that expose SwiftUI content to AppKit.
The intended workflow is:
- Put your SwiftUI code in a Swift package that builds a dynamic library.
- Export an Objective-C-visible factory class from that package.
- Optionally prebuild a universal dylib with go generate.
- Optionally embed that dylib in your Go binary.
- Use this Go package to load the embedded dylib first, then `Dir/lib<Product>.dylib`, then fall back to building the Swift package on demand from source.
The default contract expects the Swift factory class to provide `newRootViewController` and `newRootView` class methods. Those selectors use Cocoa's "new" ownership convention, so the Go caller owns the returned object and must call Release when finished with it.
Example Swift package:
import SwiftUI
import AppKit
struct ContentView: View {
var body: some View {
Text("Hello from SwiftUI").padding(24)
}
}
@objc(ExampleUIBridge)
public final class ExampleUIBridge: NSObject {
@objc public static func newRootViewController() -> NSViewController {
NSHostingController(rootView: ContentView())
}
@objc public static func newRootView() -> NSView {
NSHostingView(rootView: ContentView())
}
}
Example Go usage:
bridge, err := swiftui.Open(swiftui.Config{
Dir: "/path/to/ui",
Product: "ExampleUI",
FactoryClass: "ExampleUIBridge",
EmbeddedLibrary: embeddedExampleUI(),
})
if err != nil {
log.Fatal(err)
}
controller, err := bridge.NewViewController()
if err != nil {
log.Fatal(err)
}
defer controller.Release()
window.SetContentViewController(controller)
Typical embedding setup:
//go:generate go run ./ui/generate.go //go:build swiftui_embed //go:embed ui/libExampleUI.dylib var exampleUIDylib []byte
Callers should lock the main goroutine to the main thread before creating or using AppKit objects returned by this package.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bridge ¶
type Bridge struct {
// contains filtered or unexported fields
}
Bridge loads a Swift package product and resolves its SwiftUI factory class.
func Open ¶
Open builds and loads the Swift package product, then resolves the factory class in the Objective-C runtime.
func (*Bridge) NewView ¶
func (b *Bridge) NewView() (*HostedView, error)
NewView asks the Swift factory class for a retained NSView.
func (*Bridge) NewViewController ¶
func (b *Bridge) NewViewController() (*HostedViewController, error)
NewViewController asks the Swift factory class for a retained NSViewController.
func (*Bridge) ProductPath ¶
ProductPath returns the loaded dylib path.
type Config ¶
type Config struct {
// Dir is the Swift package directory containing Package.swift.
Dir string
// Product is the dynamic library product name from Package.swift.
Product string
// FactoryClass is the Objective-C runtime name of the exported factory class.
FactoryClass string
// EmbeddedLibrary is an optional prebuilt dylib payload. When present, Open
// materializes and loads this dylib before falling back to building from
// source in Dir.
EmbeddedLibrary []byte
// EmbeddedLibraryName is the filename used when materializing
// EmbeddedLibrary. Defaults to lib<Product>.dylib.
EmbeddedLibraryName string
// PrebuiltLibraryPath is an optional on-disk dylib path. When empty and Dir
// is set, Open also checks Dir/lib<Product>.dylib before falling back to
// swift build.
PrebuiltLibraryPath string
// ViewControllerSelector names the class method returning a retained
// NSViewController. Defaults to "newRootViewController".
ViewControllerSelector string
// ViewSelector names the class method returning a retained NSView.
// Defaults to "newRootView".
ViewSelector string
}
Config describes an app-owned Swift package that exposes SwiftUI content.
type HostedView ¶
HostedView owns a retained NSView returned by the Swift package factory.
type HostedViewController ¶
type HostedViewController struct {
appkit.NSViewController
// contains filtered or unexported fields
}
HostedViewController owns a retained NSViewController returned by the Swift package factory.
func (*HostedViewController) Release ¶
func (c *HostedViewController) Release()
Release releases the retained NSViewController.