chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
<script lang="ts" module>
import type { Opener } from './open';
import type { Source } from './types';
/** A track as the player takes it: its audio (null when nothing HUD may
* play: "not playable"), its words, its length in seconds and artwork;
* `openSubtitle` what the subtitle opens as a link (an artist's account). */
export type Track = {
src: string | null;
title: string | null;
subtitle: string | null;
openSubtitle?: Opener | null;
duration: number | null;
art: Source | null;
};
</script>
<script lang="ts">
import { untrack } from 'svelte';
import LinkButton from './LinkButton.svelte';
import Picture from './Picture.svelte';
import { formatDuration } from './format';
import { AudioHost, startsWith, type PlayerHost } from './player-host.svelte';
// Tracks as a player: a bar and the tracks in order. One <audio> plays them
// all: a click on a track plays it, the bar plays and pauses, goes back and
// on, and seeks, and the next playable track follows when one ends. With
// focus in the bar, space plays and pauses, left and right seek five
// seconds, up and down go to the previous and next track. Tracks added at
// the end (a page more) join the queue; what plays goes on playing.
// `onpopout`, when given, is a button that asks to show it apart.
//
// With a `host` (the app's, see ./player-host.svelte.ts), its <audio>
// plays, apart from this player, as `origin`'s: what plays goes on as the
// player goes away, and the player, drawn again, shows it as its own while
// the host's queue is its tracks from the same origin. With none, the
// player keeps an <audio> of its own, which stops as it goes away.
let {
tracks,
onpopout,
host = null,
origin = null
}: { tracks: Track[]; onpopout?: () => void; host?: PlayerHost | null; origin?: unknown } = $props();
/** How far left and right seek, in seconds. */
const STEP = 5;
/** Its own host, when the app gives none (an app's host is its for good): its <audio> is drawn here. */
const own = untrack(() => host) ? null : new AudioHost({ session: false });
const player: PlayerHost = $derived(host ?? own!);
const me = $derived(host ? origin : own);
/** Whether what the host plays is this player's tracks: the same origin, and one list starting with the other (a page more on either side: loaded here, or loaded before the tile drew again). */
const mine = $derived(player.origin === me && (startsWith(tracks, player.queue) || startsWith(player.queue, tracks)));
const index = $derived(mine ? player.index : null);
/** The track playing: past the tracks drawn here, the host's. */
const current: Track | null = $derived(index === null ? null : (tracks[index] ?? player.track));
const time = $derived(mine ? player.time : 0);
const total = $derived(mine ? player.duration : 0);
const paused = $derived(mine ? player.paused : true);
const error = $derived(mine ? player.error : null);
const playableCount = $derived(tracks.filter((t) => t.src !== null).length);
// A page more: the host's queue, this player's, grows with it.
$effect(() => {
if (mine && tracks.length > player.queue.length) player.extend(tracks, me);
});
/** The first playable track after `from`, or before it going back. */
function step(from: number, by: 1 | -1): number | null {
for (let i = from + by; i >= 0 && i < tracks.length; i += by) if (tracks[i].src !== null) return i;
return null;
}
function play(i: number) {
player.play(tracks, i, me);
}
function toggle() {
if (mine) return player.toggle();
const first = step(-1, 1);
if (first !== null) play(first);
}
function next() {
if (mine) return player.next();
const i = step(-1, 1);
if (i !== null) play(i);
}
function previous() {
if (mine) return player.previous();
const i = step(tracks.length, -1);
if (i !== null) play(i);
}
function seek(to: number) {
if (mine) player.seek(to);
}
function keydown(event: KeyboardEvent) {
const target = event.target as HTMLElement;
if (target instanceof HTMLInputElement) return;
if (event.key === ' ' && target instanceof HTMLButtonElement) return;
const actions: Record<string, () => void> = {
' ': toggle,
ArrowLeft: () => seek(time - STEP),
ArrowRight: () => seek(time + STEP),
ArrowUp: previous,
ArrowDown: next
};
const action = actions[event.key];
if (!action) return;
event.preventDefault();
action();
}
</script>
{#snippet icon(path: string)}
<svg viewBox="0 0 24 24" aria-hidden="true"><path d={path} fill-rule="evenodd" /></svg>
{/snippet}
{#snippet subtitle(track: Track, cls: string)}
{#if track.subtitle}
<span class={cls}>
{#if track.openSubtitle}
<LinkButton onclick={track.openSubtitle.run} href={track.openSubtitle.href}>{track.subtitle}</LinkButton>
{:else}
{track.subtitle}
{/if}
</span>
{/if}
{/snippet}
<div class="player">
{#if tracks.length === 0}
<p class="empty">Nothing to play.</p>
{:else}
<div class="bar" role="toolbar" aria-label="Player" tabindex="0" onkeydown={keydown}>
<span class="now-art"><Picture src={current?.art ?? null} alt="" placeholder="♪" /></span>
<div class="now">
{#if current}
<span class="now-title">{current.title || 'Untitled'}</span>
{@render subtitle(current, 'now-subtitle')}
{:else}
<span class="now-title idle">{playableCount > 0 ? 'Press play to start' : 'Nothing playable'}</span>
{/if}
</div>
<div class="controls">
<button type="button" class="control" onclick={previous} aria-label="Previous" disabled={playableCount === 0}>
{@render icon('M6 5h2v14H6zM20 5v14L9 12z')}
</button>
<button type="button" class="control play" onclick={toggle} aria-label={paused ? 'Play' : 'Pause'} disabled={playableCount === 0}>
{@render icon(paused ? 'M7 4v16l13-8z' : 'M6 4h4v16H6zM14 4h4v16h-4z')}
</button>
<button type="button" class="control" onclick={next} aria-label="Next" disabled={playableCount === 0}>
{@render icon('M16 5h2v14h-2zM4 5v14l11-7z')}
</button>
{#if onpopout}
<button type="button" class="control" onclick={onpopout} aria-label="Pop out" title="Pop out">
{@render icon('M2 4h20v16H2zM4 6v12h16V6zM11 11h7v5h-7z')}
</button>
{/if}
</div>
<div class="seek">
<span class="clock">{formatDuration(time)}</span>
<input
type="range"
min="0"
max={total}
step="any"
value={time}
disabled={!current}
aria-label="Seek"
aria-valuetext={`${formatDuration(time)} of ${formatDuration(total)}`}
oninput={(event) => seek(Number(event.currentTarget.value))}
/>
<span class="clock">{formatDuration(total)}</span>
</div>
{#if error}<p class="error" role="alert">{error}</p>{/if}
{#if own}
<audio preload="none" {@attach own.attach}></audio>
{/if}
</div>
<ol class="tracks">
{#each tracks as track, i (i)}
<li class="track" class:current={i === index} class:disabled={track.src === null}>
<!-- The whole row plays the track: a button behind its words, so the
artist's link in them stays a link of its own. -->
<button
type="button"
class="hit"
aria-current={i === index ? 'true' : undefined}
aria-label={`Play ${track.title || 'Untitled'}`}
disabled={track.src === null}
onclick={() => (i === index ? toggle() : play(i))}
></button>
<span class="number" aria-hidden="true">{i === index && !paused ? '♪' : i + 1}</span>
<span class="thumb"><Picture src={track.art} alt="" placeholder="♪" /></span>
<span class="words">
<span class="track-title">{track.title || 'Untitled'}</span>
{@render subtitle(track, 'track-subtitle')}
</span>
<span class="length">{track.src === null ? 'not playable' : track.duration !== null ? formatDuration(track.duration) : ''}</span>
</li>
{/each}
</ol>
{/if}
</div>
<style>
.player {
display: flex;
flex-direction: column;
gap: var(--hud-space-3);
}
p {
margin: 0;
}
.empty {
padding: var(--hud-space-5) 0;
color: var(--color-text-secondary);
text-align: center;
}
.bar {
display: grid;
grid-template-columns: auto minmax(0, 1fr) auto;
grid-template-areas:
'art now controls'
'seek seek seek'
'error error error';
align-items: center;
gap: var(--hud-space-2) var(--hud-space-3);
padding: var(--hud-space-3);
border: var(--border-width-regular) solid var(--color-border-secondary);
border-radius: var(--border-radius-lg);
background: var(--color-background-secondary);
}
.now-art {
grid-area: art;
width: 2.75rem;
height: 2.75rem;
border-radius: var(--border-radius-sm);
overflow: hidden;
font-size: 1.25rem;
}
.now {
grid-area: now;
display: flex;
flex-direction: column;
min-width: 0;
}
.now-title,
.now-subtitle,
.track-title,
.track-subtitle {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.now-title {
font-size: var(--font-text-sm-size);
font-weight: var(--font-weight-semibold);
}
.idle {
color: var(--color-text-secondary);
font-weight: var(--font-weight-normal);
}
.now-subtitle {
color: var(--color-text-secondary);
font-size: var(--font-text-xs-size);
}
.controls {
grid-area: controls;
display: flex;
align-items: center;
gap: var(--hud-space-1);
}
.control {
display: grid;
place-items: center;
width: 2rem;
height: 2rem;
padding: 0;
border: 0;
border-radius: var(--border-radius-full);
background: none;
color: var(--color-text-primary);
cursor: pointer;
}
.control:hover:not(:disabled) {
background: var(--color-background-tertiary);
}
.control:disabled {
color: var(--color-text-disabled, var(--color-text-tertiary));
cursor: default;
}
.play {
width: 2.5rem;
height: 2.5rem;
background: var(--color-background-inverse);
color: var(--color-text-inverse);
}
.play:hover:not(:disabled) {
background: var(--color-background-inverse);
opacity: 0.9;
}
svg {
width: 1.125rem;
height: 1.125rem;
fill: currentColor;
}
.seek {
grid-area: seek;
display: flex;
align-items: center;
gap: var(--hud-space-2);
}
.seek input {
flex: 1;
min-width: 0;
margin: 0;
accent-color: var(--color-text-primary);
}
.clock {
min-width: 2.75rem;
color: var(--color-text-secondary);
font-family: var(--font-mono);
font-size: var(--font-text-xs-size);
font-variant-numeric: tabular-nums;
}
.clock:last-child {
text-align: end;
}
.error {
grid-area: error;
color: var(--color-text-danger);
font-size: var(--font-text-xs-size);
}
.tracks {
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
list-style: none;
}
.track {
position: relative;
display: grid;
grid-template-columns: 1.5rem 2.5rem minmax(0, 1fr) auto;
align-items: center;
gap: var(--hud-space-3);
padding: var(--hud-space-2);
border-radius: var(--border-radius-md);
color: var(--color-text-primary);
}
/* The row's play button, behind its words: clicks fall through the
words to it, except on the artist's link. */
.hit {
position: absolute;
inset: 0;
padding: 0;
border: 0;
border-radius: inherit;
background: none;
cursor: pointer;
}
.hit:disabled {
cursor: default;
}
.hit:focus-visible {
outline: 2px solid var(--color-ring-primary);
outline-offset: -2px;
}
.track > :not(.hit) {
position: relative;
pointer-events: none;
}
.track :global(.link) {
pointer-events: auto;
}
.track:hover:not(.disabled) {
background: var(--color-background-secondary);
}
.disabled .words,
.disabled .thumb {
opacity: 0.55;
}
.current {
background: var(--color-background-secondary);
}
.current .track-title,
.current .number {
color: var(--color-text-info);
}
.number {
color: var(--color-text-tertiary);
font-size: var(--font-text-xs-size);
font-variant-numeric: tabular-nums;
text-align: center;
}
.thumb {
width: 2.5rem;
height: 2.5rem;
border-radius: var(--border-radius-sm);
overflow: hidden;
font-size: 1rem;
}
.words {
display: flex;
flex-direction: column;
min-width: 0;
}
.track-title {
font-size: var(--font-text-sm-size);
font-weight: var(--font-weight-medium);
}
.track-subtitle {
color: var(--color-text-secondary);
font-size: var(--font-text-xs-size);
}
.length {
color: var(--color-text-secondary);
font-family: var(--font-mono);
font-size: var(--font-text-xs-size);
font-variant-numeric: tabular-nums;
}
.disabled .length {
font-family: inherit;
font-style: italic;
}
</style>