Skip to Content
Documentation

Status and context keys

Set what a status item says from your code at any time, and declare keys your own conditions read, so menus, entries, panels and overlays follow your extension's state.

Two calls let your code say what your interface shows, without waiting for a render: ui.status.set for what a status item says, and ui.context.set for keys your when conditions read. Both came with API 0.1.13: ask for "songstress": "^0.1.13".

Status from code

A status item is a few words and an icon in the player bar (the full player on a phone). Its renderer answers what it shows when the player draws it; ui.status.set says it from anywhere, a hook, a job or a press, until you hand it back with null:

The player bar with Tunelog's status item, Sent with a check, beside the player's own buttonsThe player bar with Tunelog's status item, Sent with a check, beside the player's own buttons
src/sync.ts
import { songstress } from 'songstress/server'

songstress.jobs.register('sync', async (run) => {
  if (!run.userId) return
  const member = { userId: run.userId }
  await songstress.ui.status.set('sync', { text: 'Syncing', busy: true }, member)
  // … the work …
  await songstress.ui.status.set('sync', { text: 'Synced', icon: 'check', tone: 'success' }, member)
})

// Back to its renderer once the member has seen it.
songstress.commands.register('dismiss', async (_input, context) => {
  await songstress.ui.status.set('sync', null, { userId: context.userId })
})

A value is what a renderer would answer (StatusContent, without functions): text (24 characters), icon, a sprite of your package's own drawn in the icon's place, tone, tooltip, progress, busy and action. It shows where the item shows, even in an app that cannot run its renderer, a store build of the phones say. A status item set only from code may answer null from its renderer.

A status sprite may give each of its frames a length of its own (durations), for an icon that breathes rather than ticks.

Context keys

A condition reads what the app knows (platform, signedIn, a setting). Your own keys let it read your extension's state too. Declare them, up to 20, each a key, a type (boolean, number or string, a text of at most 100 characters) and optionally a default:

songstress.json
"engines": { "songstress": "^0.1.13" },
"contributes": {
  "context": [{ "key": "asleep", "type": "boolean" }, { "key": "mood", "type": "string", "default": "calm" }],
  "commands": [{ "id": "wake", "title": "Wake it", "run": "client" }],
  "menus": { "player/actions": [{ "command": "wake", "when": "ctx.asleep" }] },
  "overlays": [{ "id": "pet", "title": "Pet", "render": "client", "size": { "width": 160, "height": 96 }, "when": "!ctx.asleep" }]
}

Prop

Type

Then set them from code, and every menu item, page entry, panel, status item, sidebar entry and overlay under a condition that reads them follows at once:

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

songstress.hooks.onPause(async () => {
  await songstress.ui.context.set('asleep', true)
})

songstress.commands.register('wake', async () => {
  await songstress.ui.context.set('asleep', null) // back to its default
})

The validator refuses a condition that reads a key the manifest does not declare (WHEN_KEY_UNKNOWN).

Who sees what

Who sets a state decides where it lives, for both calls, and for badges and overlays too:

  • The server entry sets it for one member ({ userId }) or for every member, said in words ({ everyone: true }): a call with neither answers invalid, so one that forgets its member never shows one member's state to everyone. The server keeps it across a relaunch and tells each of the member's open apps at once.
  • Client code sets it on its own device, in memory, until its code stops running there.

An app lays this device's state over the member's, and the member's over every member's, key by key. A state is 16 KiB at most. The server entry writes ten a second for one member and one a second for every member, client code ten a second (SURFACE_CAPS); more answer limit-exceeded.

Test it

host.surface(userId) answers what your code set, as an app shows it to that member: badges, context, status and overlays. See Test and debug.

On this page