Embed in a macOS app
Show an Octapus bundle in a SwiftUI or AppKit window.
Add the OctapusAppKit product (Installation) and copy dist/mac from oct build --target mac --bake into your app as a folder.
SwiftUI
import OctapusAppKit
import OctapusCore
import SwiftUI
struct ContentView: View {
let url = Bundle.main.url(forResource: "mac", withExtension: nil)!
var body: some View {
OctapusView(bundle: OctapusBundle(directory: url), hydration: .load) { host in
host.root.onEvent = { event in
if case .error(let message) = event { print("Octapus:", message) }
}
}
}
}OctapusView launches the bundle as soon as it has a size. The closure runs once with the underlying OctapusHostView, which is where you attach event handlers.
AppKit
import OctapusAppKit
import OctapusCore
let host = OctapusHostView(hydration: .load)
host.root.onEvent = { event in /* metrics, batches, console output, errors */ }
window.contentView = host
host.launch(OctapusBundle(directory: url))OctapusHostView is a scroll view. It scrolls when your page is taller than the window, uses your page's background color behind the content, and lays the page out again when the window resizes.
Window size
The snapshot holds no geometry: the view lays your first screen out at its real size when it launches, so any window size works. Responsive CSS (media queries, viewport units) is resolved for that size too. If the window is resized or the appearance changes while the snapshot is still showing, it is laid out again before your app takes over.
Shipping the app
To hand the screen to another app as prebuilt binaries (Hermes linked in, nothing else to embed), see Ship as a Swift package.
If you build the app in Xcode, adding the package is enough: Xcode embeds the Hermes framework in the app for you.
If you build a SwiftPM executable and package the .app yourself, copy hermesvm.framework into Contents/Frameworks and add @executable_path/../Frameworks to the executable's rpath. The repository's examples/mac/package-app.sh does exactly this and is a good template.
Props and native modules
OctapusView(bundle: bundle, props: ["user": "ada"], modules: [ClipboardModule()])
let host = OctapusHostView(modules: [ClipboardModule()]) // builtinModules: true by default
host.launch(bundle, props: ["user": "ada"])See Native modules.
Several screens
Every OctapusHostView runs its own Hermes runtime and keeps no global state, so you can show several bundles at once, in separate windows or side by side.