Files
faceclaw/app/apps/glanceboard/widgets/music-widget.ts
T

194 lines
8.1 KiB
TypeScript

import { GrayImage } from "../../../graphics/image";
import { renderIcon } from "../../../graphics/icons";
import { truncateText } from "../../../graphics/textwrap";
import { mediaControllerBridge, type MediaControllerState } from "../../../native/media-controller";
import { ConfigSettingBoolean, onAnySettingChanged, toggleSettingMenuItem } from "../../../ui/dashboard-settings";
import { type MenuItem } from "../../../ui/menu";
import { clamp } from "../../../util/numeric-util";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
const ART_SIZE = 96;
/** The idle note: the Music app's icon, at this size and the dimmest shade. */
const IDLE_NOTE_SIZE = 64;
const IDLE_NOTE_VALUE = 20;
let idleNote: GrayImage | null | undefined;
/**
* The music icon reduced to a single dim shade: every pixel the renderer
* lit at least half becomes IDLE_NOTE_VALUE, the rest stay transparent
* (scaling the antialiased original down would quantize its edges away).
* Built once and kept, so drawImage's content-addressed cache holds.
*/
function idleNoteImage(): GrayImage | null {
if (idleNote !== undefined) return idleNote;
const icon = renderIcon("music", IDLE_NOTE_SIZE);
if (!icon) {
idleNote = null;
return null;
}
const note = new GrayImage(icon.width, icon.height, 0);
for (let y = 0; y < icon.height; y++) {
for (let x = 0; x < icon.width; x++) {
if (icon.getPixel(x, y) >= 128) note.setPixel(x, y, IDLE_NOTE_VALUE);
}
}
idleNote = note;
return note;
}
const ART_X = PAD;
const ART_TEXT_GAP = 12;
const PROGRESS_BAR_HEIGHT = 5;
export const musicShowAlbumArtSetting = new ConfigSettingBoolean({
id: "glanceboard-music-show-album-art",
label: "Show album art",
storageKey: "glanceboard.music.showAlbumArt",
defaultValue: true,
description:
"Show the album art beside the track details. When off, the title, artist, album and progress bar use the full width of the card.",
});
export const musicShowIdleNoteSetting = new ConfigSettingBoolean({
id: "glanceboard-music-show-idle-note",
label: "Show note when idle",
storageKey: "glanceboard.music.showIdleNote",
defaultValue: true,
description: "Show a dim music note on the card while nothing is playing. When off, the card is left blank.",
});
/** The music card's submenu in Glanceboard > Settings. */
export function musicSettingsMenuItems(): MenuItem[] {
return [toggleSettingMenuItem(musicShowAlbumArtSetting), toggleSettingMenuItem(musicShowIdleNoteSetting)];
}
/**
* Now playing: album art (unless turned off), title/artist/album and a
* progress bar for the active media session (Apple Music on iOS), the Music
* app's header at card size. With nothing playing the card holds a dim note,
* or nothing with that turned off. The media bridge runs for the whole
* session; the widget subscribes and ticks once a second only while
* something is playing.
*/
export class MusicWidget implements GlanceWidget {
private media: MediaControllerState = mediaControllerBridge.snapshot();
private unsubscribe: (() => void) | null = null;
private unsubscribeSettings: (() => void) | null = null;
private progressTimer: ReturnType<typeof setInterval> | null = null;
private art: GrayImage | null = null;
/** The artVersion `art` was fetched for; -1 = not fetched yet. */
private artVersion = -1;
start(requestRender: () => void): void {
this.unsubscribe = mediaControllerBridge.onStateChange((state) => {
this.media = state;
if (state.playbackState === "playing" && state.durationMs > 0 && state.positionMs >= 0) {
this.progressTimer ??= setInterval(requestRender, 1_000);
} else {
this.stopProgressTimer();
}
requestRender();
});
this.unsubscribeSettings = onAnySettingChanged(requestRender);
}
stop(): void {
this.unsubscribe?.();
this.unsubscribe = null;
this.unsubscribeSettings?.();
this.unsubscribeSettings = null;
this.stopProgressTimer();
}
private stopProgressTimer(): void {
if (this.progressTimer !== null) clearInterval(this.progressTimer);
this.progressTimer = null;
}
paint(image: GrayImage): void {
const medium = glanceFont.medium();
const small = glanceFont.small();
const media = mediaControllerBridge.snapshot();
this.media = media;
if (!media.available || (!media.title && !media.artist)) {
if (!media.accessEnabled) {
// Actionable, so it stays as text.
image.drawText(small, PAD, PAD, "Music", 150);
image.drawText(small, PAD, PAD + small.lineHeight + 6, mediaControllerBridge.accessLabel, 140);
return;
}
// Nothing playing: a dim note, centred, and nothing else; or a blank card.
const note = musicShowIdleNoteSetting.get() ? idleNoteImage() : null;
if (note) image.drawImage(note, ((image.width - note.width) / 2) | 0, ((image.height - note.height) / 2) | 0);
return;
}
// Without the art, the text and progress bar keep its rows and take its columns.
const artY = Math.max(PAD, ((image.height - ART_SIZE) / 2) | 0);
const showArt = musicShowAlbumArtSetting.get();
if (showArt) this.drawArt(image, media, artY);
const textX = showArt ? ART_X + ART_SIZE + ART_TEXT_GAP : PAD;
const textWidth = image.width - textX - PAD;
let y = artY;
image.drawText(medium, textX, y, truncateText(medium, media.title || "Unknown title", textWidth), 230);
y += medium.lineHeight + 2;
if (media.artist) {
image.drawText(small, textX, y, truncateText(small, media.artist, textWidth), 170);
y += small.lineHeight + 2;
}
if (media.album) {
image.drawText(small, textX, y, truncateText(small, media.album, textWidth), 120);
}
// Progress pinned to the art's bottom edge; the state label sits beside it.
const barY = artY + ART_SIZE - PROGRESS_BAR_HEIGHT;
const timeY = barY - small.lineHeight - 3;
const stateLabel = media.playbackState === "playing" ? "" : media.playbackState === "paused" ? "Paused" : media.playbackState;
if (media.durationMs > 0 && media.positionMs >= 0) {
const elapsed = formatMediaTime(media.positionMs);
const duration = formatMediaTime(media.durationMs);
image.drawText(small, textX, timeY, elapsed, 140);
image.drawText(small, textX + textWidth - small.measureText(duration), timeY, duration, 140);
if (stateLabel) {
image.drawText(small, textX + (((textWidth - small.measureText(stateLabel)) / 2) | 0), timeY, stateLabel, 140);
}
image.drawRect(textX, barY, textWidth, PROGRESS_BAR_HEIGHT, 55);
const progress = clamp(media.positionMs / media.durationMs, 0, 1);
image.fillRect(textX + 1, barY + 1, Math.round((textWidth - 2) * progress), PROGRESS_BAR_HEIGHT - 2, 170);
} else if (stateLabel) {
image.drawText(small, textX, timeY, stateLabel, 140);
}
}
private drawArt(image: GrayImage, media: MediaControllerState, artY: number): void {
if (media.artVersion !== this.artVersion) {
this.artVersion = media.artVersion;
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
}
if (this.art) {
const dx = ART_X + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0);
const dy = artY + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0);
image.bitBlt(this.art, dx, dy);
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
} else {
image.drawRect(ART_X, artY, ART_SIZE, ART_SIZE, 60);
const font = glanceFont.small();
image.drawText(font, ART_X + Math.round((ART_SIZE - font.measureText("no art")) / 2),
artY + Math.round((ART_SIZE - font.lineHeight) / 2), "no art", 90);
}
}
}
function formatMediaTime(milliseconds: number): string {
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
const seconds = totalSeconds % 60;
const totalMinutes = Math.floor(totalSeconds / 60);
const minutes = totalMinutes % 60;
const hours = Math.floor(totalMinutes / 60);
return hours > 0
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
: `${minutes}:${String(seconds).padStart(2, "0")}`;
}