Skip to content

A PlatformModule gives a platform SDK capability a typed C++ interface. It does not have to inherit a framework class or route every call through string messages.

Register factories in an ApplicationHook. A factory accepts Runtime& for an application-bound module or UiWindow& for a module needing the original native window. The catalog freezes before windows and queued work start. Each open creates an instance; registration itself does not.

This example opens a nonvisual device module during window installation and makes it available as that window’s service. The platform implementation supplies CreateDeviceInfo:

#include <huxerui/huxerui.h>
#include <memory>
#include <string>
using namespace huxerui;
struct DeviceInfo {
virtual ~DeviceInfo() = default;
virtual std::string Model() const = 0;
};
std::shared_ptr<DeviceInfo> CreateDeviceInfo(Runtime& runtime);
void RegisterDeviceInfo(ApplicationContext& context) {
context.RegisterPlatformModule<std::shared_ptr<DeviceInfo>>("app/device-info", CreateDeviceInfo);
}
void InstallDeviceInfo(WindowContext& context) {
context.Provide<DeviceInfo>(OpenPlatformModule<std::shared_ptr<DeviceInfo>>("app/device-info"));
}
View App() {
return Text(UseService<DeviceInfo>()->Model());
}
const Application application{
App,
{.application_hooks = {RegisterDeviceInfo}, .window_hooks = {InstallDeviceInfo}},
};

The factory’s host lifetime and the instance’s owning service are separate decisions. An application-bound factory can be opened without a window from ordinary application work after installation. A window-bound factory requires that original live window context. Do not replace one with whichever window is currently active.

OpenPlatformModule also works in committed Lifecycle setup, events and running tasks with the required host context. It is rejected during composition and application installation. For a component-owned instance, release it in Lifecycle cleanup when that component unmounts.

RegisterPlatformModule<Module, Options> accepts an exact strongly typed options value. Prefer an integration library’s Install/Use helper rather than repeating a string registration name at every call.

Java or JavaScript objects may need asynchronous named methods and events. Wrap PlatformChannel in the same typed service interface rather than exposing payload parsing throughout application code.

channel.Invoke<double>("batteryLevel", std::monostate{},
[level](PlatformResult<double> result) {
if (const auto* value = std::get_if<double>(&result)) {
level = *value;
}
});

The result type is explicit; primitive wire conversion is built in. Structured request/result types own static Encode and Decode operations. An event key supplies its Event<...> signature and stable Name; its structured value type owns conversion.

Keep the returned request identity if you need to cancel. Closing a channel affects its copies, invalidates pending work, detaches events, and disposes the platform instance. Completion is delivered at most once on the owning UI thread. Do not wait synchronously for it or assume that cancelled work still calls your completion.

PlatformPayload can carry std::shared_ptr<ExternalTexture> and FileReference alongside ordinary values. Forward the complete encoded envelope, including its capability tables. Do not reduce a document-provider reference to a filesystem path or a live texture to an integer handle.

  • Use ordinary C++ for shared algorithms.
  • Use an application service for shared lifetime or a window service for per-window coordination.
  • Register a PlatformModule for a platform implementation of that service.
  • Use PlatformView for an actual platform control.
  • Use ExternalTexture for changing pixels without platform-control input.

Test missing registration, exact options types, teardown during work, cancellation, late events, and thread delivery. See the registry and channel reference for individual contracts.

Android window modules and Activity results

Section titled “Android window modules and Activity results”

JavaPlatformModuleFactory uses HuxerUIPlatformModule.Factory and an application Context. JavaWindowPlatformModuleFactory uses HuxerUIPlatformModule.WindowFactory and receives the original HuxerUIView. Both factories are public classes with public no-argument constructors; their C++ facade wraps the created PlatformChannel.

For an activity-producing operation, capture window.getActivityResultLauncher() when the window factory creates the instance. Keep that launcher rather than looking up a later attachment. The host forwards onActivityResult with dispatchActivityResult, supplies ActivityResultHost, and updates resumed state. An unavailable or retired attachment fails the request instead of rerouting it through another Activity.

See the Android integration guide for setup and result delivery.

ObjectiveCPlatformModuleFactory uses an application-bound HUXPlatformModuleFactory. ObjectiveCWindowPlatformModuleFactory adapts HUXUIKitWindowPlatformModuleFactory and supplies its original UIViewController. A permission or picker-style operation needs the latter when it owns modal UI.

Capture HUXGetPresentationCoordinator(owner) during creation and enqueue native presentations through it. The coordinator serializes UI, pauses new admissions while inactive and keeps a canceled active slot until dismissal completes. Call its finished callback only when the presented UI is actually absent.

See iOS integration for a callback-to-task example and the exact cleanup boundary.

Use BufferReference in a typed PlatformPayload for large CPU data that can share an address-stable lifetime. It is not an immutable snapshot: wait for readers before reusing producer storage, and publish frame sequence separately from buffer identity. Bytes remains the correct choice for serialized or durable data.

Android uses HuxerUIBufferReference.wrap with a direct ByteBuffer. The current position-to-limit range is retained, and withBuffer exposes a read-only view only for that synchronous callback. Close wrappers you own; payloads and slices keep independent shares. A final-release callback is not a frame-completion signal and may run on any final releasing thread.

Apple exposes HUXBufferReference (Swift BufferReference) with a retained owner and scoped unsafe-byte access. Windows/Linux C++ factories can receive BufferReference directly. Web payload bridges reject it; do not simulate zero-copy by encoding a large buffer to JSON.

See BufferReference for construction, slicing and equality, and the buffer analysis source for a real platform producer.