Skip to Content
Documentation

Views in the app

Your own HTML and CSS drawn inside Songstress in Chromium browsers on a computer, its code in a worker beside it, and everything such a view may not do.

A view is your own HTML, for what Songstress's components can't draw: a game, a visualiser, an editor of your own. There are two kinds:

  • A view for the browser (any view): the app opens it in a browser tab, the system's browser or a phone's in-app browser, apart from Songstress. It gets no songstress API and nothing of the app's; it is served by the member's own server at an address that expires, with the params it was opened with as its query, and fetches what it needs itself.
  • An in-app view ("inApp": true, API 0.1.13): the app draws its HTML and CSS inside Songstress, as a page or inside an overlay, and runs its code in a worker beside it. This page is about these.

Before you write one, see whether a page, components or a canvas will do: they draw on every app, phones included, and need none of the rules below.

An in-app view inside Songstress: its own markup in the app's colours, a field and a Count button, and what its code was told of the theme and its sizeAn in-app view inside Songstress: its own markup in the app's colours, a field and a Count button, and what its code was told of the theme and its size

Where it is drawn

An in-app view is drawn inside the app in Chromium browsers on a computer (Chrome, Edge and the others built on Chromium), whose frames run in a process of their own there, so a view can never hold up the music. Everywhere else it opens in the browser as any view does: a phone's or a tablet's browser, Safari, Firefox, the desktop apps (the macOS one included) and the phone apps. client.capabilities.inAppViews says which, and songstress check tells you, for a view marked inApp, where it will open.

Write one

The view template starts with this one. Its markup, its style and its code sit side by side:

src/views/scope.html
<main id="scope">
  <p id="title" class="title">Nothing playing</p>
  <p id="artist" class="muted"></p>
  <div class="row">
    <button id="add" type="button">Press</button>
    <span id="count">0</span>
    <button id="reset" type="button" hidden>Start again</button>
  </div>
</main>
src/views/scope.ts
import { on, update } from 'songstress/view'

let count = 0

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

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

A view's code hears no hooks. Your client code hears them and posts to the view while it is open:

src/scope.ts
import { songstress } from 'songstress/client'

songstress.hooks.onTrackChange(async (song) => {
  await songstress.ui.views.post('scope', { title: song?.title ?? null, artist: song?.artist ?? null })
})
songstress.json
"engines": { "songstress": "^0.1.13" },
"entry": { "client": "dist/client.js" },
"hooks": { "client": ["onTrackChange"] },
"permissions": { "listening.read": { "reason": "Show the song that plays in the view." } },
"contributes": {
  "views": [{ "id": "scope", "title": "Scope", "path": "dist/views/scope.html", "icon": "music", "inApp": true }],
  "sidebar": [{ "id": "scope", "title": "Scope", "icon": "music", "view": "scope" }]
}

songstress build makes the three one file, dist/views/scope.html from src/views/scope.html, .css and .ts, of 2 MiB at most: the markup and the style as written, the code in a <script type="songstress/view"> the app runs in a worker, and a small runner of the SDK's, so the same file opened in a browser works too, where every call answers not-available.

What its code can do

The code runs in a worker, with songstress/view:

  • update(id, change) changes an element by its id: its text, class, hidden, value, or image: a cover of the library ({ cover: 'album', id }) or one of your package's images ({ asset }), which the app finds. Never an address.
  • on(event, listener) hears clicks, keys, form input, the view's size, the theme and messages from your client code (ui.views.post).
  • drawCanvas draws on the document's <canvas> elements, up to four.
  • songstress has the few calls a view makes: commands.execute, settings.get, player.getState (with listening.read), ui.notify and host.info.
  • The theme arrives as CSS variables, var(--songstress-text), var(--songstress-accent) and the rest of VIEW_TOKENS, a dark or light class on the document, and a small base sheet, so plain HTML already looks like Songstress. Under reduced motion the document has a reduced-motion class, and the frame itself, whatever your sheet says, finishes at once every animation that has an end and holds every endless one where it stands, until the person no longer asks for less motion.

What a view may not do

By design, and enforced by the app, not by trust:

  • No script of yours runs in the view's document. Only the app's own script does. Your code is in the worker, which has no network (no fetch, WebSocket, WebRTC or the rest) and no storage of the page's.
  • The markup is kept to what VIEW_MARKUP allows: no script, link, meta, base or frame, no on… attribute, no address (href, src, action, ping), no autofocus; every field without autofill. A picture in the markup is a data: address of a still PNG, JPEG or WebP whose bytes are what they say, 4,096 by 4,096 pixels in all (an img past them loses its picture). A url(data:…) in the view's style is held to the same, within the same pixels, or the view is not drawn. songstress check names what the app would take out (VIEW_STRIPPED).
  • No field dressed as a password box. A view's style may not hold text-security (prefixed, escaped or not), which draws what is typed as a password field's dots: songstress check warns of it (VIEW_TEXT_SECURITY), songstress build refuses the view, and an app draws no view whose style holds it, saying in its place "This view disguises a text field as a password field, so the app won't show it." A secret is a setting, never a field of yours.
  • No navigation: the frame never leaves its document, and nothing loads from anywhere.
  • Esc is the app's, and so is Space outside a field or a button: a view never hears them.
  • It is held to caps (VIEW_CAPS): 4,000 elements and 128 Ki characters of style, 200 changes in one patch and 120 patches a second, twelve pictures at once and four found a second, four canvases, thirty messages a second from client code. Over a cap, the change is dropped; a flood ends the view.
  • It must stay quick. A view that keeps its frame busy (more than a third of the last two seconds in frames over 50 ms) is ended as slow, and only the person opens it again. A view opens four times a minute at most.
  • Four views are drawn at once in an app. When none is free, the app says which overlays hold them, and opens the view once one is free.

songstress check warns of a view with nothing to build it from (VIEW_MISSING), over 2 MiB (VIEW_TOO_LARGE), over the caps (VIEW_TOO_HEAVY), with markup the app would take out (VIEW_STRIPPED) or style that dresses a field as a password box (VIEW_TEXT_SECURITY); songstress build refuses one over the element or the style cap, or with that style. The app, check and build weigh a view by one count (viewWeight).

Test it

openView({ html }) from songstress/testing holds a view as the frame does, for a test that imports the view's code after it: click, type, send and draw tell the code what the person did, and element(id) is what its patches left. The view template has one. See Test and debug.

The API is in the reference: songstress/view.

On this page