# songstress/view

> The API of an in-app view's code, in the worker beside its HTML.

Page: https://songstress.app/docs/develop/reference/view/

Import from `songstress/view` in an in-app view's code (`views[].inApp`): the code `songstress build` puts beside the
view's own HTML and CSS, which the app runs in a worker, never in the view's document. `update` changes an element by its
id, `on` hears the person and the frame, `drawCanvas` draws on a `<canvas>`, `viewState` says where the view is, and
`songstress` has the few calls a view makes (`commands.execute`, `settings.get`, `player.getState`, `ui.notify`,
`host.info`). Opened in a browser, apart from Songstress, every call answers `not-available`.

What it exports is the SDK's own record of its public surface, which the SDK's tests hold to the code. Each type's
fields, every description and every function's usage example are read from the SDK's source when the site is built, and
each example is compiled against the SDK by its tests.

It also exports, as the modules that declare them document them: [API_VERSION](/docs/develop/reference/songstress/#type-API_VERSION), [ViewApi](/docs/develop/reference/songstress/#type-ViewApi).

### ViewCanvasOptions

How a canvas of the view's is drawn.

| Field | Type | Description |
|---|---|---|
| `context?` | `"2d" \| "webgl2" \| undefined` | `'2d'` (left out) or `'webgl2'`. |
| `motion?` | `"decorative" \| "essential" \| undefined` | `'decorative'` (left out): under reduced motion it draws only when it appears, after a patch and when its size changes. `'essential'`: as if motion were not reduced. |
| `onPointer?` | `((pointer: CanvasPointer) => void) \| undefined` | Told the pointer over the canvas. |

Refers to [CanvasPointer](/docs/develop/reference/ui/#type-CanvasPointer).

### ViewChange

One element's change: what of it becomes what. Left out, a part stays.

| Field | Type | Description |
|---|---|---|
| `text?` | `string \| undefined` | Its text (not on a field, a canvas or a style). |
| `class?` | `string \| undefined` | Its whole `class`. |
| `hidden?` | `boolean \| undefined` | Whether it is hidden. |
| `value?` | `string \| number \| boolean \| undefined` | A field's value; true or false checks a checkbox or a radio. |
| `image?` | `ViewImage \| null \| undefined` | An `img`'s picture: a library cover or one of the package's own images, which the app fetches; null for none. Never an address. |

Refers to [ViewImage](/docs/develop/reference/view/#type-ViewImage).

### ViewDraw

A view's canvas drawing function: told a context of the kind asked, already scaled to the frame's pixel ratio
for 2D, and the frame. The canvas is clear each time.

```ts
export type ViewDraw = (context: never, frame: CanvasFrame) => void;
```

Refers to [CanvasFrame](/docs/develop/reference/ui/#type-CanvasFrame).

### ViewEvent

What the person did, or what changed around the view, as its code hears it.

```ts
export type ViewEvent = {
    type: 'click';
    id: string;
    x: number;
    y: number;
} | {
    type: 'input';
    id: string;
    value: string;
    checked?: boolean;
} | {
    type: 'change';
    id: string;
    value: string;
    checked?: boolean;
} | {
    type: 'submit';
    id: string;
} | {
    type: 'key';
    kind: 'down' | 'up';
    id?: string;
    key: string;
    code: string;
    alt: boolean;
    ctrl: boolean;
    meta: boolean;
    shift: boolean;
    repeat: boolean;
} | ({
    type: 'pointer';
    id: string;
} & CanvasPointer) | {
    type: 'resize';
    width: number;
    height: number;
} | {
    type: 'theme';
    theme: ViewTheme;
} | {
    type: 'motion';
    reduced: boolean;
} | {
    type: 'message';
    data: unknown;
};
```

Refers to [CanvasPointer](/docs/develop/reference/ui/#type-CanvasPointer), [ViewTheme](/docs/develop/reference/view/#type-ViewTheme).

### ViewImage

A picture an `img` of a view shows: a library cover or one of the
package's own images, as the `Image` component names them. The app finds
and fetches it; a view never names an address.

```ts
export type ViewImage = {
    cover: LibraryType;
    id: string;
} | {
    asset: string;
};
```

Refers to [LibraryType](/docs/develop/reference/songstress/#type-LibraryType).

### ViewState

Where the view is, as its code knows it: kept current by the frame's events.

| Field | Type | Description |
|---|---|---|
| `view` | `string` | The view's id, as the manifest declares it. |
| `params` | `Readonly<Record<string, string>>` | What it was opened with (`navigate`'s params). |
| `size` | `{ width: number; height: number; }` | Its size, in points. |
| `theme` | `ViewTheme` | The theme it is drawn in. |
| `reducedMotion` | `boolean` | Whether the person asks for less motion. |
| `canvas` | `boolean` | Whether it may draw on a canvas here (the web, and the macOS desktop from 13.3). |

Refers to [ViewTheme](/docs/develop/reference/view/#type-ViewTheme).

### ViewTheme

The theme handed to a view: dark or light, and the values of `VIEW_TOKENS`.

| Field | Type | Description |
|---|---|---|
| `mode` | `"dark" \| "light"` |  |
| `tokens` | `Record<string, string>` |  |

### drawCanvas

Draws on one of the view's `<canvas>` elements, by its id: `draw` is called for each frame the frame asks
(at most 60 a second, none while the view is out of sight), and what it draws goes back as a bitmap. The
canvas is clear each time: draw the whole picture. Drawing a canvas again replaces its drawing; naming canvases
and letting them go is paced for the frame (the newest of each standing), never refused for its pace.

```ts
export declare function drawCanvas<Context>(id: string, draw: (context: Context, frame: CanvasFrame) => void, options?: ViewCanvasOptions): Disposable;
```

| Parameter | Type | Description |
|---|---|---|
| `id` | `string` | The canvas element's `id`. |
| `draw` | `(context: Context, frame: CanvasFrame) => void` | Draws one frame. |
| `options?` | `ViewCanvasOptions` | Its context, its motion, and a pointer listener. |

Returns `Disposable`: What stops the drawing.

Example:

```ts
import { drawCanvas } from 'songstress/view'

drawCanvas('scope', (context: { fillStyle: string; fillRect(x: number, y: number, w: number, h: number): void }, frame) => {
  context.fillStyle = '#e50043'
  context.fillRect(0, frame.height / 2, (frame.time / 10) % frame.width, 4)
})
```

Refers to [CanvasFrame](/docs/develop/reference/ui/#type-CanvasFrame), [ViewCanvasOptions](/docs/develop/reference/view/#type-ViewCanvasOptions), [Disposable](/docs/develop/reference/songstress/#type-Disposable).

### on

Listens to what the person does in the view, and to what changes around it: `click`, `input`, `change`,
`submit`, `key`, `pointer` (on a canvas drawn with `onPointer`), `resize`, `theme`, `motion`, and `message`
(what the extension's client code posts with `ui.views.post`).

```ts
export declare function on<Type extends ViewEvent['type']>(type: Type, listener: (event: EventOf<Type>) => void): Disposable;
```

| Parameter | Type | Description |
|---|---|---|
| `type` | `Type` | The kind of event. |
| `listener` | `(event: EventOf<Type>) => void` | Told each one. |

Returns `Disposable`: What takes the listener away.

Example:

```ts
import { on, update } from 'songstress/view'

on('message', (event) => {
  const { title } = event.data as { title: string | null }
  update('title', { text: title ?? 'Nothing playing' })
})
```

Refers to [ViewEvent](/docs/develop/reference/view/#type-ViewEvent), [Disposable](/docs/develop/reference/songstress/#type-Disposable).

### songstress

An in-app view's code's API: `import { songstress } from 'songstress/view'`.

```ts
export declare const viewApi: ViewApi;
```

Refers to [ViewApi](/docs/develop/reference/songstress/#type-ViewApi).

### update

Changes one element of the view's document, by its id: its text, its class, whether it is hidden, a field's
value, or an `img`'s picture. Changes are sent together once the code is done for now, the newest of each
part standing; the frame leaves alone an element it does not have.

```ts
export declare function update(id: string, change: ViewChange): void;
```

| Parameter | Type | Description |
|---|---|---|
| `id` | `string` | The element's `id`. |
| `change` | `ViewChange` | What of it changes. |

Returns `void`.

Example:

```ts
import { on, update } from 'songstress/view'

let count = 0
on('click', (event) => {
  if (event.id !== 'add') return
  count += 1
  update('count', { text: String(count) })
  update('reset', { hidden: false })
})
```

Refers to [ViewChange](/docs/develop/reference/view/#type-ViewChange).

### viewState

Where the view is: its id, what it was opened with, its size, its theme, whether the person asks for less
motion and whether it may draw on a canvas here, as the frame last said.

```ts
export declare function viewState(): ViewState;
```

Returns `ViewState`.

Example:

```ts
import { update, viewState } from 'songstress/view'

const { size, reducedMotion } = viewState()
update('note', { text: `${size.width} by ${size.height}${reducedMotion ? ', still' : ''}` })
```

Refers to [ViewState](/docs/develop/reference/view/#type-ViewState).
