---
title: "Embedding a document layer in a window"
url: https://recursiveai.org/oneapp/docs/embedding/
description: "A library that brings its own document model (a script runtime, a web-component layer) can attach to a OneApp window instead of owning a window of its…"
version: "1.2.1"
source: https://github.com/recursive-ai-oss/oneapp-csharp/blob/b6147c0f31b7d374f537a82e4c393fc29024ae3a/docs/embedding.md
---

# Embedding a document layer in a window

A library that brings its own document model (a script runtime, a web-component layer) can attach to a OneApp window instead of owning a window of its own. OneApp stays engine-neutral: it offers the seams below and takes no reference to the library.

## Reaching the document

Every window's page is an `InteractivePage` over a `MutableDocument`, whether the window shows an HTML string or a component tree.

- `AppOptions.Setup` (and `AppWindowOptions.Setup`) runs on the UI thread when the page is made, before the first frame. It is the place to attach: `Setup = page => runtime.Attach(page)`.
- `AppWindow.Page` (and `AppHost.Page` for the first window) returns the page once it exists, and `page.MutableDocument` is the document. Reading `Page` brings the page up to date with edits made to the document, so what you read includes them.
- A library that configures an app copies the caller's options: `new AppOptions(options) { ElementInterop = hook, Setup = page => hook.Attach(page) }` (likewise `AppWindowOptions`).
- Keyboard events reach the page with both `key` and `code` (UI Events): `code` names the physical key ("KeyA", "ArrowRight", "ShiftLeft", "NumpadEnter") and is empty only for a key the platform could not identify.
- A page is resized in place, so what you attached in `Setup` survives size and scale changes.

Edit the document on the UI thread. From another thread, use `AppHost.Post`.

## Showing edits: `Invalidate`

An edit made outside input handling (a timer, a task, a script callback) is drawn after `AppWindow.Invalidate()`, or `AppHost.Invalidate()` for every window. It brings the page up to date and schedules a frame; no frame is drawn when nothing changed, so an app that is not invalidated sits at about 0% CPU.

## Per-frame work: `BeforeFrame`

`AppWindow.BeforeFrame` is raised on the UI thread once per frame the window draws, before the page is painted: the place for work that belongs to a frame, such as a rendering opportunity or animation callbacks. Edits the handler makes to the document are in that frame. A handler that needs another frame afterwards (an animation is running) calls `Invalidate()`; that schedules the next frame and does not recurse. A window with no surface to show (headless) raises it too, so a headless run behaves like a shown window; a minimized window raises nothing. The offscreen captures (`CaptureRasterized`, `CaptureGpu`) do not raise it.

## Custom elements: `IElementInterop`

A component template can use elements whose tag has a hyphen. Set `AppOptions.ElementInterop` (or `AppWindowOptions.ElementInterop`) to an `IElementInterop` and the native component host forwards to it, for such elements:

- `SetProperty(element, name, value)`: a property the component set (anything, not only `value` and `checked`). The value is typed: `null`, a `string`, a `bool` or a `double` (every number type arrives as a double); a producer that states no type gives the text as a string;
- `GetProxy(element, tag)` (optional): the typed proxy for the element, see below;
- `AddListener(element, type)` and `RemoveListener(element, type)`: the event types the component listens for.

The element, its attributes and its DOM-event listeners are made as for any element; the hook adds to that. `Attach(IElementEvents)` is called once before the first component is mounted. `IElementEvents.Raise(e, bubbles)` delivers an event to the handlers the components registered on the node `e.Target` names (get the id with `TargetOf(element)`) and, if it bubbles, on its ancestors, through the same dispatch as page events. Hook members run while a change set is applied: do not edit the component tree from them, and queue work for `BeforeFrame`.

Without a hook nothing changes: a property other than `value` and `checked` is an error, and its message names the hook.

## Typed script elements: proxies

A template can use a script-defined element through a typed proxy class (marked `JsElement`, see OneUI's `docs/markup.md`): attributes are checked at build time, `ref="{Player}"` gives an `ElementRef<PlayerProxy>` and `on:time-changed` subscribes to the proxy's C# event. The host is the window's `UiContext.ElementProxies` before the first component is built, and answers from the hook: `IElementInterop.GetProxy(MutableNode element, string tag)` returns the proxy for an element OneApp has created, so the proxy can be the document's element itself (one C# object per node, wrapping the `MutableNode`). The host asks only once the commit that created the element has been applied (OneUI 1.2 preview with that ordering), keeps the object and returns it for every later request for the node until the element is removed from the document; then it drops the object and disposes it if it is `IDisposable`. The proxy raises its events itself, on the UI thread. A hook that leaves `GetProxy` at its default makes typed elements fail with "the hook provides no element proxies".

```csharp
sealed class Runtime : IElementInterop
{
    public object GetProxy(MutableNode element, string tag) => new PlayerProxy(element);
    public void SetProperty(MutableNode element, string name, object? value) { /* null, string, bool or double */ }
    // Attach, AddListener, RemoveListener ...
}
```
