mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-29 09:03:12 +00:00
First iteration of glanceboard
This commit is contained in:
1 parent
a3660fac61
commit
8a340fc4ad
36 files changed
+2874
-357
No files matched your search
@@ -0,0 +1,127 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { mediaControllerBridge, type MediaControllerState } from "../../../native/media-controller";
|
||||
import { clamp } from "../../../util/numeric-util";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
const ART_SIZE = 96;
|
||||
const ART_X = PAD;
|
||||
const PROGRESS_BAR_HEIGHT = 5;
|
||||
|
||||
/**
|
||||
* Now playing: album art, title/artist/album and a progress bar for the
|
||||
* active Android media session, the Music app's header at card size. 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 progressTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private art: GrayImage | null = null;
|
||||
private artKey = "";
|
||||
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.stopProgressTimer();
|
||||
}
|
||||
|
||||
private stopProgressTimer(): void {
|
||||
if (this.progressTimer !== null) clearInterval(this.progressTimer);
|
||||
this.progressTimer = null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const medium = getDefaultMediumFont();
|
||||
const small = getDefaultSmallFont();
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
this.media = media;
|
||||
if (!media.available || (!media.title && !media.artist)) {
|
||||
image.drawText(small, PAD, PAD, "Music", 150);
|
||||
const text = media.accessEnabled ? "Nothing playing" : "Notification access needed";
|
||||
image.drawText(small, PAD, PAD + small.lineHeight + 6, text, 140);
|
||||
return;
|
||||
}
|
||||
|
||||
const artY = Math.max(PAD, ((image.height - ART_SIZE) / 2) | 0);
|
||||
this.drawArt(image, media, artY);
|
||||
|
||||
const textX = ART_X + ART_SIZE + 12;
|
||||
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 {
|
||||
const key = `${media.packageName}|${media.title}|${media.album}`;
|
||||
// Retry while null: players often publish metadata before the bitmap.
|
||||
if (key !== this.artKey || this.art === null) {
|
||||
this.artKey = key;
|
||||
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 = getDefaultSmallFont();
|
||||
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")}`;
|
||||
}
|
||||
Reference in new issue
Block a user