---
title: View
summary: "`host.view`: the extension's own view in the app: notification targets, visibility, window focus, the route that puts the view's page in the address bar, and full screen."
audience: both
stability: stable
since: protocol 1
---

# View

`host.view` is the extension's own view: whether it is on screen, whether the window has the user's attention,
which page of the view the user is on, whether the view has the whole window, and the notification targets handed to it.

```ts
interface SpacemixHost {
  view: ViewApi; // always present
}

interface ViewApi {
  onTarget(fn: (target: string) => void): () => void;
  onVisible(fn: (visible: boolean) => void): () => void;
  visible(): boolean;
  focused(): boolean;
  onFocus(fn: (focused: boolean) => void): () => void;
  route(): string;
  navigate(route: string, options?: { replace?: boolean }): void;
  onRoute(fn: (route: string) => void): () => void;
  setFullscreen(on: boolean): void;
  fullscreen(): boolean;
}
```

**Permission:** none. In React, `useRoute`, `useVisible` and `useLinkTarget` from `@spacemix/sdk/react` wrap these
(see [react.md](../react.md)).

All of this is per frame: each window or browser has its own visibility, focus, route and full screen.

## `host.view.onTarget`

```ts
onTarget(fn: (target: string) => void): () => void;
```

Called with the `target` of a notification the user opened ([notifications.md](notifications.md)), **once**, after
the view is showing. A target that arrives while the view is hidden, or before anything listens, is held and
delivered when both are true; only the latest one is kept. Every listener registered at that moment gets it.
Returns the unsubscribe function.

## `host.view.visible`, `host.view.onVisible`

```ts
visible(): boolean;
onVisible(fn: (visible: boolean) => void): () => void;
```

Whether the view is on screen (the user has this extension open). The frame keeps running while hidden, so pause
timers and animations there. `onVisible` is called on each change, not with the current value.

## `host.view.focused`, `host.view.onFocus`

```ts
focused(): boolean;
onFocus(fn: (focused: boolean) => void): () => void;
```

Whether SpaceMIX's window has the user's attention: focused (anywhere in it, this frame included) and not
minimized or in a background tab. Use it to hold off "unread" marks and sounds while the user is elsewhere. It
changes for every frame, shown or not. `onFocus` is called on each change.

## Routes

```ts
route(): string;
navigate(route: string, options?: { replace?: boolean }): void;
onRoute(fn: (route: string) => void): () => void;
```

The route is where the user is inside the view, as a path the extension makes up: `""` for its start page, then
for example `notes/42` or `repo/abc/commit/9f3e`. It is part of the app's address (`#/@<server>/<extension id>/<route>`), so
Back and Forward step through it and a copied link reopens the same place. `@<server>` is the active server's shortcode: the first 16 characters of its id (lowercase hex), the same for every member. So a link opens on the same server for everyone; an address without it still opens on the active server. An 8- to 15-character shortcode (links made before the shortcode grew to 16 had 8) or one from the server's id before it moved to key-based identity still finds the server, but asks **Open this link on <name>?** before switching. A server's id, and so its shortcode, changes once, when the server moves to key-based identity (its old id stays known as `previousId`), and stays the same after that, also when the server is renamed (`HANDLE_LEN` and `matchHandle` in `apps/desktop/src/lib/servers.ts`). The host writes the shortcode; `route()`, `navigate` and `onRoute` see only your route.

- **`route()`**: the current route. On start it is the route the address names, or `""`.
- **`navigate(route, { replace })`**: the user moved to another page of the view. Without `replace` it adds a
  history step; with `replace: true` it rewrites the current one (a redirect, a default tab filled in, a search box
  updating as they type). Listeners in this frame hear it at once. Nothing happens when it's the route already
  showing. It returns nothing; a failure to update the address is logged. Bridge method: `view.navigate`.
- **`onRoute(fn)`**: the route changed: Back/Forward, a link, or `navigate` from this frame. The host never echoes
  the frame's own `navigate` back.

Treat the route as the source of truth for the view's page: read it on start, call `navigate` when the user moves
to another page, follow `onRoute`. Keep it to places worth returning to (an open record, a folder, a tab), not every
keystroke or scroll. A route naming something that's gone should show the nearest sensible page and fix the address
with `navigate(…, { replace: true })`.

## Full screen

```ts
setFullscreen(on: boolean): void;
fullscreen(): boolean;
```

**Stability:** experimental: the shell's ways out of full screen may still change. The two methods and what they
mean won't.

`setFullscreen(true)` asks the app to hide its own navigation (the sidebar, or the rail with **Collapse until hover**)
while this view is showing, so the view fills the window's width: an editor for one record, a canvas, a board.
`setFullscreen(false)` gives it back. The sidebar slides out and back in the way it does for **Collapse until hover**.
Bridge method: `view.setFullscreen`.

- **`fullscreen()`**: what this frame last asked for; `false` until it asks.
- **Per frame, while the view is showing.** Opening another view brings the navigation back. Coming back to this view
  while the frame still holds `true` hides it again, unless the user ended it (below). Keep the request tied to the
  page that wants it: ask on the route that needs it (`setFullscreen(route.startsWith("edit/"))` on every route
  change), not once at start.
- **A reload starts without it.** When the frame is reloaded or unloaded (an update, **Reload** in the store,
  uninstalling), the request is dropped and `fullscreen()` is `false` in the new document.
- **The app keeps its own bar.** On desktop widths a slim bar of the app's runs across the top of the view while it
  is full screen: the extension's icon and name, and a **Show navigation** button. The frame is laid out below it and
  can't draw over it, so the button is the way out the extension can't block. On macOS it leaves room for the window
  buttons and is the window's drag area.
- **Other ways out.** Escape pressed on the app itself (not inside the frame, nor in a text field, the search palette,
  the chat window or an open menu) ends full screen; pointing at the window's left edge brings the sidebar out over
  the view. With the sidebar out that way, Escape both puts it away and ends full screen. Back and Forward still work.
  Escape inside the frame stays the extension's own.
- **After the user ends it,** the frame isn't told: `fullscreen()` still returns what it asked for. Its further
  `setFullscreen(true)` calls are ignored while the view stays open, until the user leaves it (or the extension is
  reloaded from the store or updated; the frame reloading its own document doesn't clear it), even when the user
  opens another record. Coming back doesn't bring full screen back by itself either: after that, the frame must ask
  again on a user action, for example the next time the user opens a record, not on its own when the view shows
  again.
- **Narrow screens.** The navigation there is the tab bar at the bottom of the window and the account menu at the top
  right of the top bar, which both stay, so full screen changes nothing on a phone.
- **Older hosts** don't have it: `host.view.setFullscreen` is `undefined` there and calling it throws. Guard with
  `typeof host.view.setFullscreen === "function"`. It returns nothing. A value that isn't a boolean is refused
  (`setFullscreen takes true or false.`) and the frame's console logs
  `[spacemix] couldn't change full screen: <reason>`.

## Example

```ts
import { host } from "@spacemix/sdk";

export function followRoute(show: (noteId: string | null) => void): () => void {
  const apply = (route: string) => show(route.startsWith("notes/") ? route.slice("notes/".length) : null);
  apply(host.view.route());
  return host.view.onRoute(apply);
}

export function openNote(id: string): void {
  host.view.navigate(`notes/${id}`);
}
```

## Stability

Stable.
