swiftui

package
v0.6.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package swiftui loads app-owned Swift packages that expose SwiftUI content to AppKit.

The intended workflow is:

  1. Put your SwiftUI code in a Swift package that builds a dynamic library.
  2. Export an Objective-C-visible factory class from that package.
  3. Optionally prebuild a universal dylib with go generate.
  4. Optionally embed that dylib in your Go binary.
  5. 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

func Open(cfg Config) (*Bridge, error)

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

func (b *Bridge) ProductPath() string

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

type HostedView struct {
	appkit.NSView
	// contains filtered or unexported fields
}

HostedView owns a retained NSView returned by the Swift package factory.

func (*HostedView) Release

func (v *HostedView) Release()

Release releases the retained NSView.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL