---
title: "OneUI components in a native window"
url: https://recursiveai.org/oneapp/docs/components/
description: "An app window can show a OneUI component tree instead of an HTML page. The window mounts the root component; every state change the components make is one…"
version: "1.0.0"
source: https://github.com/recursive-ai-oss/oneapp-csharp/blob/64b0f3162f8c1b7bb766ceae32357442feaa5960/docs/components.md
---

# OneUI components in a native window

An app window can show a OneUI component tree instead of an HTML page. The window mounts the root component; every state change the
components make is one change-set commit (see OneUI's `docs/change-sets.md`, protocol versions 1 and 2), which OneApp applies to a
OneHtml `MutableDocument` that a OneRender `InteractivePage` follows. The page restyles and lays out only what the edit reaches, and the
window draws on damage only: an idle component window uses no CPU and draws nothing.

```csharp
using var host = AppHost.Create(new AppOptions(new TodoApp()) { Title = "Todo" });
host.Run();
```

`AppWindowOptions(Component root)` does the same for further windows of `AppHost.OpenWindow`. HTML windows are unchanged.

## API

| | |
|---|---|
| `new AppOptions(Component root)`, `new AppWindowOptions(Component root)` | Show a component tree. `Html` is empty then. |
| `AppWindow.Root`, `AppWindow.Components` | The mounted component and its `UiContext` (to batch state changes or read state by path); null for an HTML window. |
| `AppWindow.Page` | The `InteractivePage`; it shows the components' pending edits before you read it. `Page.MutableDocument` is the document. |

Everything runs on the UI thread. A state change made from another thread goes through `AppHost.Post`.

## What the sink does

- **Document.** Node 1 is the body. A `style` element in the head holds `html,body{margin:0}`; each `AddStyleSheet` adds a `<style>` once, which
  makes OneRender build the page again (state is kept). Inline SVG (`CreateElementNS`) is created in the SVG namespace; any other namespace is a `ChangeSetException`.
- **Properties.** `SetProperty value` and `checked` set the live state of a control (script semantics: no event is raised, and a value equal to the
  current one is left alone so the caret does not move). Before the first frame they are written as attributes.
- **Events.** For each type a component listens to, one capturing listener on the page's window finds the nodes on the event's path that have a
  component listener and calls them target first, so `StopPropagation` and `PreventDefault` act on the page's event. Events that do not bubble
  (`focus`, `blur`) go to the target only. `input` and `change` carry the control's value and `checked`.
- **Focus.** A `Focus` change runs the HTML focusing steps after the whole commit is applied. The `focus`/`blur`/`focusin`/`focusout` events it raises
  (and any the page raises while a commit is being applied, such as focus leaving a removed element) are delivered after the commit, never from inside it.
- **Removal.** Removed subtrees are forgotten by id at once and freed in the document after the page has shown the removal.
- A change that names a missing node, or an `Insert` that breaks the DOM's rules, is a `ChangeSetException`; the commit is not retried.

`page.LastMutationRebuildReason` names why OneRender built the page again instead of patching it (a style sheet, an edit inside a form control, a
table or an SVG, `value`/`type`/`src` attributes, generated content).

## Scrollbars

A box with `overflow: scroll`, or `overflow: auto` and content beyond its box, gets a classic 15 px scrollbar (10 px with `scrollbar-width: thin`) that narrows its content, and so does the document when it is taller or wider than the window. Presses on the arrows, the track and the thumb (and dragging it) work like any window's. A box whose content fits keeps its full width. That is the layout Chrome gives a page with classic scrollbars (Linux, Windows), so the same component lays out the same natively and as a custom element in a browser there. We checked clientWidth of an overflowing `auto` box, a fitting one, an `overflow: scroll` box, a `scrollbar-gutter: stable` box and the document against headless Chrome: 185, 200, 185, 185 and 785 in both.

`AppOptions.Scrollbars` (and `AppWindowOptions.Scrollbars`) chooses: `ScrollbarMode.Classic` (the default) or `ScrollbarMode.Hidden`, which reserves no space and draws no bars, as a browser with overlay scrollbars (macOS) lays out. An element can opt out with `scrollbar-width: none`. `scrollbar-color` and `scrollbar-gutter` are supported.

## Resizing

A window resize or scale change calls `InteractivePage.SetViewport`: the page, its listeners, focus, typed values, scroll positions and running
transitions stay. `AppOptions.Setup` runs once when the page is created. This holds for HTML windows too.

## Harness

`component` (no arguments) returns the mounted component tree with each component's state as text; with `path` (`TodoApp/Items`,
`TodoApp/TodoRow[1]/Title`) it returns one value, and a list gives its count and first items. HTML windows answer `unavailable`.

## Sample

`samples/Native` opens the browser sample's todo list (`Native todo`), its roving tab strip (`Native tabs`) or the kit gallery (`Native gallery`)
in a native window. The components are the other samples' source files, compiled again; the kit is a reference to `RecursiveAI.OneApp.Kit`.
