Skip to Content
Documentation

songstress/view

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

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.

ViewCanvasOptions · ViewChange · ViewDraw · ViewEvent · ViewImage · ViewState · ViewTheme · drawCanvas · on · songstress · update · viewState

It also exports, as the modules that declare them document them: API_VERSION, ViewApi.

ViewCanvasOptions

How a canvas of the view's is drawn.

Prop

Type

Refers to CanvasPointer.

ViewChange

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

Prop

Type

Refers to 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.

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

Refers to CanvasFrame.

ViewEvent

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

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, 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.

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

Refers to LibraryType.

ViewState

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

Prop

Type

Refers to ViewTheme.

ViewTheme

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

Prop

Type

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.

export declare function drawCanvas<Context>(id: string, draw: (context: Context, frame: CanvasFrame) => void, options?: ViewCanvasOptions): Disposable;
ParameterTypeDescription
idstringThe canvas element's id.
draw(context: Context, frame: CanvasFrame) => voidDraws one frame.
options?ViewCanvasOptionsIts context, its motion, and a pointer listener.

Returns Disposable: What stops the drawing.

Example
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, ViewCanvasOptions, 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).

export declare function on<Type extends ViewEvent['type']>(type: Type, listener: (event: EventOf<Type>) => void): Disposable;
ParameterTypeDescription
typeTypeThe kind of event.
listener(event: EventOf<Type>) => voidTold each one.

Returns Disposable: What takes the listener away.

Example
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, Disposable.

songstress

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

export declare const viewApi: ViewApi;

Refers to 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.

export declare function update(id: string, change: ViewChange): void;
ParameterTypeDescription
idstringThe element's id.
changeViewChangeWhat of it changes.

Returns void.

Example
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.

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.

export declare function viewState(): ViewState;

Returns ViewState.

Example
import { update, viewState } from 'songstress/view'

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

Refers to ViewState.