forked from Blendman974/faceclaw
Merge branch 'ios-spotify'
This commit is contained in:
commit
48e0bc867b
32 files changed
+3176
-51
No files matched your search
+49
-14
@@ -8,8 +8,10 @@ import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller";
|
||||
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
|
||||
import { spotifyService } from "../../native/spotify";
|
||||
import { MediaBrowseLayer } from "./media-browse";
|
||||
import { MusicSettingsLayer } from "./music-settings";
|
||||
import { spotifyMenuItem } from "./spotify-menu";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
|
||||
@@ -33,6 +35,8 @@ const QUEUE_X = 204;
|
||||
/** Horizontal inset of row text from its selection box. */
|
||||
const LIST_TEXT_INSET = 6;
|
||||
const PLAYLIST_ACTION_INDEX = 1;
|
||||
/** Minimum room for the dim artist after a queue row's title; less and it is left off. */
|
||||
const QUEUE_SUBTITLE_MIN_WIDTH = 40;
|
||||
|
||||
type MusicAction =
|
||||
| { kind: "play-pause"; label: string; enabled: boolean }
|
||||
@@ -40,14 +44,17 @@ type MusicAction =
|
||||
| { kind: "next"; label: string; enabled: boolean }
|
||||
| { kind: "volume"; label: string; enabled: boolean }
|
||||
| { kind: "playlist"; label: string; enabled: boolean }
|
||||
| { kind: "browse"; label: string; enabled: boolean };
|
||||
| { kind: "browse"; label: string; enabled: boolean }
|
||||
/** A player-specific action from the state's customActions (Spotify's shuffle / repeat / like). */
|
||||
| { kind: "custom"; label: string; enabled: boolean; action: string };
|
||||
|
||||
type FocusColumn = "actions" | "playlist";
|
||||
|
||||
/**
|
||||
* Music controller app: metadata + album art + transport controls for the
|
||||
* active Android media session (any player that publishes one) or, on iOS,
|
||||
* the Apple Music system player, plus the player's queue when it exposes one
|
||||
* the Apple Music system player or Spotify (signed in from the window menu),
|
||||
* plus the player's queue when it exposes one
|
||||
* (scroll to a track, click to jump). With no session, the front page lists
|
||||
* the apps whose library can be browsed (click one to open its
|
||||
* MediaBrowseLayer).
|
||||
@@ -110,7 +117,7 @@ class MusicAppLayer implements Layer {
|
||||
const apps = mediaBrowserBridge.listBrowsableApps(!this.libraryAppsFresh);
|
||||
this.libraryAppsFresh = true;
|
||||
this.libraryMenu.setItems(apps);
|
||||
image.drawText(font, 24, 16, global.isIOS ? "Nothing is playing in Apple Music." : "No active media session.", 180);
|
||||
image.drawText(font, 24, 16, global.isIOS ? media.status || "Nothing is playing." : "No active media session.", 180);
|
||||
const prompt = global.isIOS
|
||||
? "Browse your library, or start playback on the phone."
|
||||
: apps.length
|
||||
@@ -121,6 +128,11 @@ class MusicAppLayer implements Layer {
|
||||
image.drawText(font, 24, y, line, 150);
|
||||
y += lineStep(font);
|
||||
}
|
||||
// Why the last command failed (e.g. playing from the Spotify library with no device open).
|
||||
for (const line of media.notice ? wrapText(font, media.notice, width - 48) : []) {
|
||||
image.drawText(font, 24, y, line, 200);
|
||||
y += lineStep(font);
|
||||
}
|
||||
if (apps.length) {
|
||||
const listTop = y + 6;
|
||||
const listHeight = height - listTop - LIST_BOTTOM_MARGIN;
|
||||
@@ -156,12 +168,13 @@ class MusicAppLayer implements Layer {
|
||||
if (media.album) {
|
||||
image.drawText(font, META_X, metaTop + 4 + (titleSlots + 1) * metaStep, truncateText(font, media.album, metaWidth), 150);
|
||||
}
|
||||
// The player line, or for a few seconds why a command failed.
|
||||
image.drawText(
|
||||
font,
|
||||
META_X,
|
||||
metaTop + 4 + (titleSlots + 2) * metaStep,
|
||||
truncateText(font, media.appName || media.packageName, metaWidth),
|
||||
110,
|
||||
truncateText(font, media.notice || media.appName || media.packageName, metaWidth),
|
||||
media.notice ? 200 : 110,
|
||||
);
|
||||
this.drawProgress(image, media, metaWidth);
|
||||
|
||||
@@ -212,7 +225,16 @@ class MusicAppLayer implements Layer {
|
||||
private drawQueueItem({ image, item, x, y, width, height, selected }: MenuDrawArgs<MediaQueueItem>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
|
||||
image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, label, width - 2 * LIST_TEXT_INSET), selected ? 255 : 200);
|
||||
const textWidth = width - 2 * LIST_TEXT_INSET;
|
||||
const textY = centeredTextY(font, y, height);
|
||||
const title = truncateText(font, label, textWidth);
|
||||
image.drawText(font, x + LIST_TEXT_INSET, textY, title, selected ? 255 : 200);
|
||||
// The artist, dimmer, in whatever room the title leaves.
|
||||
const subtitleX = font.measureText(`${label} `);
|
||||
if (item.subtitle && title === label && textWidth - subtitleX >= QUEUE_SUBTITLE_MIN_WIDTH) {
|
||||
image.drawText(font, x + LIST_TEXT_INSET + subtitleX, textY,
|
||||
truncateText(font, item.subtitle, textWidth - subtitleX), selected ? 150 : 110);
|
||||
}
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
@@ -278,6 +300,7 @@ class MusicAppLayer implements Layer {
|
||||
else if (action.kind === "previous") await mediaControllerBridge.skipPrevious();
|
||||
else if (action.kind === "next") await mediaControllerBridge.skipNext();
|
||||
else if (action.kind === "browse") this.openBrowse(ctx);
|
||||
else if (action.kind === "custom") await mediaControllerBridge.sendCustomAction?.(action.action);
|
||||
else if (action.kind === "volume") {
|
||||
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
|
||||
} else if (action.kind === "playlist") {
|
||||
@@ -342,6 +365,12 @@ class MusicAppLayer implements Layer {
|
||||
{ kind: "volume", label: volume >= 0 ? `Volume (${volume})` : "Volume", enabled: volume >= 0 },
|
||||
{ kind: "next", label: "Next track", enabled: media.canSkipNext },
|
||||
{ kind: "previous", label: "Previous track", enabled: media.canSkipPrevious },
|
||||
...(mediaControllerBridge.sendCustomAction ? media.customActions ?? [] : []).map((custom): MusicAction => ({
|
||||
kind: "custom",
|
||||
label: custom.name,
|
||||
enabled: true,
|
||||
action: custom.action,
|
||||
})),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -496,14 +525,20 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
|
||||
iconLetter: "M",
|
||||
icon: "music",
|
||||
closeable: true,
|
||||
// Music settings choose among Android media apps; iOS has only Apple Music.
|
||||
menuItems: () => global.isIOS ? [] : [{
|
||||
label: "Music settings",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new MusicSettingsLayer());
|
||||
},
|
||||
}],
|
||||
// Music settings choose among Android media apps; iOS has Apple Music, plus Spotify's sign-in here.
|
||||
menuItems: () => {
|
||||
if (!global.isIOS) {
|
||||
return [{
|
||||
label: "Music settings",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new MusicSettingsLayer());
|
||||
},
|
||||
}];
|
||||
}
|
||||
const spotify = spotifyService();
|
||||
return spotify ? [spotifyMenuItem(spotify)] : [];
|
||||
},
|
||||
actions: options.actions,
|
||||
baseLayer: new MusicRootLayer(musicLayer),
|
||||
submitFrame: options.submitFrame,
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import type { GrayImage } from "../../graphics/image";
|
||||
import { isValidSpotifyClientId, SPOTIFY_DASHBOARD_URL, SPOTIFY_REDIRECT_URI } from "../../native/spotify-auth";
|
||||
import type { SpotifyService } from "../../native/spotify-service";
|
||||
import { spotifyClientIdSetting, textSettingMenuItem } from "../../ui/dashboard-settings";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { submenuItem, TextPageLayer, type MenuItem } from "../../ui/menu";
|
||||
import { WindowMenuLayer } from "../../ui/window-menu";
|
||||
import { TextViewerLayer } from "../files/text-viewer";
|
||||
|
||||
const SETUP_HELP = [
|
||||
"Faceclaw controls Spotify through Spotify's Web API, which needs a Spotify developer app of your own. Creating one is free; Spotify requires its owner to have Premium, and controlling playback needs Premium too.",
|
||||
"",
|
||||
`1. On a computer, open ${SPOTIFY_DASHBOARD_URL.replace("https://", "")} and sign in with your Spotify account.`,
|
||||
"2. Create an app. Any name and description will do.",
|
||||
`3. Add this Redirect URI exactly: ${SPOTIFY_REDIRECT_URI}`,
|
||||
"4. Under \"Which API/SDKs are you planning to use?\", tick Web API, then save.",
|
||||
"5. Open the app's settings, copy its Client ID, and enter it as Client ID here.",
|
||||
"6. Choose Sign in, then finish on your phone: Faceclaw opens Spotify's sign-in page there.",
|
||||
"",
|
||||
"Spotify plays on whichever of your devices is open (this phone, a computer, a speaker). To start music when nothing is playing, Spotify must be open on one of them.",
|
||||
"",
|
||||
"Others can sign in with your client ID once you add their Spotify email under User Management in the dashboard (up to five people).",
|
||||
].join("\n");
|
||||
|
||||
/** The Music app menu's Spotify row (iOS): sign in, sign out, client ID, setup help. */
|
||||
export function spotifyMenuItem(service: SpotifyService): MenuItem {
|
||||
return submenuItem("Spotify", (ctx) => ctx.stack.push(new SpotifyMenuLayer(service)), {
|
||||
description: "Sign in to Spotify to show and control it from the glasses.",
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Rows rebuilt every paint and input from the account and sign-in progress,
|
||||
* which change asynchronously. A WindowMenuLayer, so the shell's system menu
|
||||
* closes it like the window menu it opens from.
|
||||
*/
|
||||
class SpotifyMenuLayer extends WindowMenuLayer {
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
|
||||
constructor(private readonly service: SpotifyService) {
|
||||
super("Spotify", []);
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
this.unsubscribe ??= this.service.onChange(() => ctx.actions.requestRender());
|
||||
this.setItems(this.rows());
|
||||
return super.paint(ctx, paintBelow);
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
this.setItems(this.rows());
|
||||
await super.handleInput(event, ctx);
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
}
|
||||
|
||||
private rows(): MenuItem[] {
|
||||
const service = this.service;
|
||||
const account = service.account();
|
||||
const status = service.signInStatus();
|
||||
const help: MenuItem = {
|
||||
label: "Setup help",
|
||||
onSelect: (ctx) => ctx.stack.push(new TextViewerLayer(SETUP_HELP, "Spotify setup")),
|
||||
};
|
||||
if (account) {
|
||||
return [
|
||||
info(`Signed in as ${account.displayName || account.userId}`),
|
||||
...(account.product && account.product !== "premium" ? [info("Playback control needs Premium")] : []),
|
||||
{ label: "Sign out", onSelect: () => service.signOut() },
|
||||
textSettingMenuItem(spotifyClientIdSetting),
|
||||
help,
|
||||
];
|
||||
}
|
||||
if (status.phase === "waiting") {
|
||||
return [
|
||||
info("Finish signing in on the phone"),
|
||||
{ label: "Cancel sign-in", onSelect: () => service.cancelSignIn() },
|
||||
help,
|
||||
];
|
||||
}
|
||||
const failure: MenuItem[] = status.error
|
||||
? [{
|
||||
label: "Sign-in failed (details)",
|
||||
onSelect: (ctx) => ctx.stack.push(new TextPageLayer("Spotify sign-in failed", status.error)),
|
||||
}]
|
||||
: [];
|
||||
return [
|
||||
{
|
||||
label: "Sign in",
|
||||
description: "Opens Spotify's sign-in page on the phone.",
|
||||
disabled: () => !isValidSpotifyClientId(spotifyClientIdSetting.get()),
|
||||
onSelect: () => void service.signIn(),
|
||||
},
|
||||
textSettingMenuItem(spotifyClientIdSetting),
|
||||
help,
|
||||
...failure,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
function info(label: string): MenuItem {
|
||||
return { label, disabled: true, onSelect: () => {} };
|
||||
}
|
||||
Reference in new issue
Block a user