# Embedded application windows

A tab of kind `window` holds a window of another application. Each platform does it the way its window system allows; the tab, the worker requests and giving the window back are the same everywhere. A small chat preview can show the same window at the same time without moving it.

- **Page**: the tab's rectangle and whether it shows; on macOS also pointer, keys and the capture video
- **App**: its window handle, zoom and display scale, and a window's capture source
- **Worker**: child window on Windows and X11; on macOS places the window behind the tab and posts input

## Per platform

| Platform | Mode | How |
| --- | --- | --- |
| Windows | `native` | `SetParent` into the App's top-level window; frame styles swapped for `WS_CHILD` and restored on release. Rectangle in device pixels of the client area. |
| Linux, X11 | `native` | `XReparentWindow` into the App's X window, as a window manager does with its frame, after withdrawing it so the window manager lets go (ICCCM 4.1.4); back to the root at the old place on release, where the window manager takes it again. |
| macOS | `stream` | A window cannot leave its process. The page shows a ScreenCaptureKit capture (Chromium window capture, `chromeMediaSource: "desktop"`). The worker moves and sizes the window through Accessibility to the tab's screen rectangle and posts the tab's events with SkyLight. |
| Linux, Wayland | none | No client may place another's surface; reported as unavailable. |

## Tabs and views

Each page that shows a window is a *holder* with an id it chooses. The worker keeps, per window, which holders hold it and whether each places it.

| Holders | The window |
| --- | --- |
| Any tab | Placed, as above. Layout requests come from a placing holder only; a view's are answered with the window's state. |
| Views only | Watched: recorded for input and state, left where it is, at its size, in its own parent, not unhidden. |
| Last tab closes, views remain | Given back where it was, then watched where it now is. |
| Last holder lets go | Given back if it was placed; forgotten otherwise. |

| Platform | Tab forwards input | View forwards input |
| --- | --- | --- |
| macOS | yes, SkyLight | yes, SkyLight; a point maps to the screen through the window's current frame |
| Windows | no, the child window takes it | yes, posted with `PostMessage` |
| X11 | no, the child window takes it | no, the view only shows it |

## Decisions

| Decision | Why |
| --- | --- |
| The window system's own embedding on Windows and X11 | The window stays the application's real window: no latency, its own input, menus and accessibility. Window managers embed windows this way. |
| Capture plus posted input on macOS | The only way there; Codex Computer Use keeps the window it operates under a live capture the same way, and the input route is the one the computer_use backend already uses (after cua-driver). |
| On macOS the window sits behind the tab, at its size | Menus, pop-ups and input-method panels are separate windows the application opens next to its window, so they appear over the tab where it shows the window. If the application raises the window, it covers the tab exactly. |
| Capture drawn at the window's size from the tab's top-left | What is shown lines up with the window behind it; a point in the tab maps to the window by the page zoom alone. |
| Input through one queue on one thread | Events reach the application in the order the user made them; moves and wheel turns are folded once a frame in the page, so the socket never backs up. |
| Keys go to a hidden text field first | An input method can compose there; its finished text goes as text, every other key as a key with its physical code, so shortcuts keep working on any layout. Text that arrives without a key, from dictation or the emoji picker, goes as text too. |
| Capture source through `getUserMedia` | Needs no click, so a tab restored after a reload shows its window again by itself. |
| A view never moves the window | The preview follows what the Agent or the user is doing; taking the window out of its place for a thumbnail would change what it shows. The reply's frame gives the view the window's aspect. |
| Windows view input posted in the background | The route the computer_use backend takes for background clicks: mouse messages to the deepest child under the point in its client coordinates, the wheel in screen coordinates, keys to the thread's focus window, through one queue. Posted keys leave the keyboard state alone, so a printable key goes as its character and Ctrl with a letter as its control character, what `TranslateMessage` would make; shortcuts that read the keyboard state need the window in front. |
| computer_use finds embedded windows from the window tree | An embedded window is a child of the App window, so top-level walks miss it. The backends look for children of OpenProgram App windows owned by another process (Windows: process image `openprogram*`; X11: `WM_CLASS` `openprogram*`, `XQueryTree`) and list them under their application. No worker records are read, so a backend in an agentic subprocess sees them too; screen rectangles come from the window itself. |
| The App holds its close until windows are given back | A child window would close with its parent. The page detaches them and answers; after 2 s the App closes anyway. |

## Following the window

A view looks at nothing on a timer. The operating system says when the window moves, resizes, is minimized or restored, is renamed or closes; the Worker signals `embedded_windows` keyed by the window id, and the view reads the window's state once, in the next animation frame.

| Change | Source |
| --- | --- |
| The window, macOS | An Accessibility observer per process: `AXMoved`, `AXResized`, minimized and restored, `AXTitleChanged`, `AXUIElementDestroyed` on the window; focused window changed, hidden, shown and activated on its application. A kqueue `NOTE_EXIT` catches a process that quits without closing its windows. One CFRunLoop thread |
| The window, Windows | `SetWinEventHook` on the window's process: location, name, show, hide, destroy, minimize and foreground, for the window object itself. One thread pumps them |
| The window, X11 | `StructureNotifyMask` and `PropertyChangeMask` on the window, from a connection of its own: configure, map, unmap, reparent, destroy, its name and state. One thread waits in `select` |
| The App window | Electron's `move` and `resize`, sent as `embedded-window:host-changed`; a page zoom is the page's own `resize` |
| The tab within the page | A `ResizeObserver`, an intersection observer cropped to the tab's own rectangle for a shift without a resize, scrolling and the page showing or hiding |
| A lost picture | Taken again when the window changes or the page shows or gets focus again |

The Worker's thread starts with the first window a view shows and ends with the last. Where it cannot start (no Accessibility access), the attach reply has no `follows`, and that view reads the state every 1.5 s and retries a lost picture as often, only while it is open.

## Requests

| Socket action | Does |
| --- | --- |
| `embedded_windows` | Lists ordinary windows of other applications, without the App's own process |
| `embedded_window_attach` | `window_id`, `holder` (default `"tab"`), `place` (default true), `host` when placing. Places or watches the window; answers its mode, title, `frame` (screen rectangle), `placed`, `input`: whether this holder forwards input, and `follows` when the Worker follows the window by its events |
| `embedded_window_layout` | `holder`, `host`: the tab moved, resized, hid or showed; ignored for a view |
| `embedded_window_state` | Whether it is still open, its title and frame; with `holder`, that holder's `input` |
| `embedded_window_detach` | `holder` lets go; see the holder table |
| `embedded_window_input` | Pointer, wheel, key, text and focus events, x and y from the window's top-left; no reply. Taken where `input` is true |

Every reply is one `embedded_window` message carrying the request's `request_id`. When the worker stops it gives back every window it holds.

## Edges

| Case | Behavior |
| --- | --- |
| Window closed | The tab says so and offers the open windows again |
| Restored tab whose id now names another application's window | Released at once; the tab offers the list |
| Window of an elevated process (Windows) | Refused by the system; the reason is shown |
| Window on another Space (macOS) | No capture source; the tab says so |
| Tab hidden | Windows and X11 hide the child; macOS leaves the window behind the App |
| System shortcuts, protected video (macOS) | Stay with the system; protected video captures black |

Code: [`computer_use/desktop/embedding/`](https://github.com/Fzkuji/OpenProgram/tree/main/openprogram/programs/tools/computer_use/computer_use/desktop/embedding), [`ws_actions/embedded_window.py`](https://github.com/Fzkuji/OpenProgram/blob/main/openprogram/interfaces/server/ws_actions/embedded_window.py), [`apps/desktop/main/windows/embedded-windows.js`](https://github.com/Fzkuji/OpenProgram/blob/main/apps/desktop/main/windows/embedded-windows.js), [`apps/web/components/center-tabs/window/`](https://github.com/Fzkuji/OpenProgram/tree/main/apps/web/components/center-tabs/window).

## Implementation status

Placing is checked on a real desktop on all three: macOS in the App, Windows 11 (Arm64) with Notepad, and X11 under Openbox on Xvfb. Wayland stays unavailable. Holders, watching, Windows view input and computer_use finding embedded windows are covered by unit tests and not yet checked on a real Windows or X11 desktop.

Following by events is covered by unit tests on all three; its Windows and X11 sources, and the macOS observers' notifications, are not yet checked on a real desktop.
