Repository navigation
Expand file tree
/
Copy pathmedia-browse.ts
More file actions
304 lines (279 loc) · 9.73 KB
/
Copy pathmedia-browse.ts
File metadata and controls
304 lines (279 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
import {
MenuLayer,
drawListScrollbar,
drawSelectionHighlight,
drawSubmenuIndicator,
scrollToKeepSelectionVisible,
} from "../menu";
import {
mediaBrowserBridge,
type MediaBrowseItem,
type MediaBrowserApp,
} from "../../native/media-browser";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
const HEADER_HEIGHT = 34;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
export type MediaBrowseOptions = {
app: MediaBrowserApp;
/** Called after playback of a picked item has been requested. */
onPlayed: (ctx: LayerContext) => void;
/** Double-click at the library root (leave the browser). */
onLeave: (ctx: LayerContext) => void;
};
type BrowseLevel = {
parentId: string;
title: string;
/** null while the listing is loading. */
items: MediaBrowseItem[] | null;
error: string;
selectedIndex: number;
scrollRow: number;
};
type BrowsePhase = "idle" | "connecting" | "ready" | "failed";
/**
* Library browser over another app's MediaBrowserService (the Android Auto
* mechanism): connecting starts the player's process if needed, so this works
* with no media session active and the phone locked. Scroll to select, click
* to open a folder or play an item, double-click to go up (leaving the browser
* when already at the root).
*/
export class MediaBrowseLayer implements Layer {
private phase: BrowsePhase = "idle";
private connectError = "";
private levels: BrowseLevel[] = [];
private removed = false;
constructor(private readonly options: MediaBrowseOptions) {}
paint(ctx: LayerContext): GrayImage {
if (this.phase === "idle") {
this.startConnect(ctx);
}
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, 20, 8, `Library: ${this.options.app.appName}`, 220);
if (this.phase === "connecting" || this.phase === "idle") {
image.drawText(font, 24, HEADER_HEIGHT + 8, `Starting ${this.options.app.appName}...`, 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
if (this.phase === "failed") {
image.drawText(font, 24, HEADER_HEIGHT + 8, "Could not connect:", 180);
image.drawText(font, 24, HEADER_HEIGHT + 24, truncateRight(font, this.connectError, width - 48), 140);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const level = this.currentLevel();
const path = this.levels.map((entry) => entry.title).join(" / ");
image.drawText(font, 20, 22, truncateLeft(font, path, width - 40), 130);
if (!level || level.items === null) {
image.drawText(font, 24, HEADER_HEIGHT + 8, level?.error ? level.error : "Loading...", level?.error ? 140 : 150);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
const items = level.items;
if (!items.length) {
image.drawText(font, 24, HEADER_HEIGHT + 8, "(empty)", 120);
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} up / back`, 110);
return image;
}
level.selectedIndex = clamp(level.selectedIndex, 0, items.length - 1);
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
level.scrollRow = scrollToKeepSelectionVisible(level.scrollRow, level.selectedIndex, visibleRows, items.length);
const rowWidth = width - 2 * LIST_X;
const lastVisible = Math.min(items.length, level.scrollRow + visibleRows);
for (let index = level.scrollRow; index < lastVisible; index++) {
const item = items[index]!;
const y = HEADER_HEIGHT + (index - level.scrollRow) * ROW_HEIGHT;
const selected = index === level.selectedIndex;
const highlightX = LIST_X - 6;
const highlightY = y - 1;
const highlightWidth = rowWidth + 12;
const highlightHeight = ROW_HEIGHT - 1;
if (selected) {
drawSelectionHighlight(image, highlightX, highlightY, highlightWidth, highlightHeight, ctx.stack.isFocused(), 4);
}
const value = selected ? 255 : 200;
const title = truncateRight(font, item.title || "(untitled)", rowWidth - 16);
image.drawText(font, LIST_X, y + 1, title, value);
if (item.subtitle) {
const titleWidth = font.measureText(title);
const subtitleWidth = font.measureText(item.subtitle);
if (titleWidth + subtitleWidth + 24 <= rowWidth - 16) {
image.drawText(font, LIST_X + rowWidth - 16 - subtitleWidth, y + 1, item.subtitle, 110);
}
}
if (item.browsable) {
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, selected ? value : 110);
}
}
if (items.length > visibleRows) {
drawListScrollbar(
image,
width - 12,
HEADER_HEIGHT,
visibleRows * ROW_HEIGHT - 4,
level.scrollRow,
visibleRows,
items.length,
);
}
image.drawText(
font,
20,
height - 16,
`${GESTURE_SCROLL} select ${GESTURE_CLICK} open/play ${GESTURE_DOUBLE_CLICK} up / back`,
110,
);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
if (this.phase === "failed") {
if (event.type === "click") {
this.phase = "idle";
this.connectError = "";
} else if (event.type === "double-click") {
this.leave(ctx);
}
return;
}
if (event.type === "double-click") {
if (this.levels.length > 1) {
this.levels.pop();
} else {
this.leave(ctx);
}
return;
}
const level = this.currentLevel();
const items = level?.items;
if (!level || !items || !items.length) return;
switch (event.type) {
case "scroll-up":
level.selectedIndex = Math.max(0, level.selectedIndex - 1);
return;
case "scroll-down":
level.selectedIndex = Math.min(items.length - 1, level.selectedIndex + 1);
return;
case "click": {
const item = items[clamp(level.selectedIndex, 0, items.length - 1)];
if (!item) return;
if (item.browsable && item.playable) {
ctx.stack.push(
new MenuLayer(truncateRight(getDefaultSmallFont(), item.title || "(untitled)", 230), [
{
label: "Play",
onSelect: (menuCtx) => {
menuCtx.stack.pop();
this.play(menuCtx, item);
},
},
{
label: "Open",
onSelect: (menuCtx) => {
menuCtx.stack.pop();
this.descendInto(menuCtx, item);
},
},
]),
);
} else if (item.browsable) {
this.descendInto(ctx, item);
} else if (item.playable) {
this.play(ctx, item);
}
return;
}
default:
return;
}
}
onRemoved(): void {
this.removed = true;
mediaBrowserBridge.disconnect();
}
private currentLevel(): BrowseLevel | undefined {
return this.levels[this.levels.length - 1];
}
private leave(ctx: LayerContext): void {
this.options.onLeave(ctx);
}
private play(ctx: LayerContext, item: MediaBrowseItem): void {
mediaBrowserBridge.playFromMediaId(item.mediaId);
this.options.onPlayed(ctx);
}
private descendInto(ctx: LayerContext, item: MediaBrowseItem): void {
const level: BrowseLevel = {
parentId: item.mediaId,
title: item.title || "(untitled)",
items: null,
error: "",
selectedIndex: 0,
scrollRow: 0,
};
this.levels.push(level);
this.loadLevel(ctx, level);
}
private startConnect(ctx: LayerContext): void {
this.phase = "connecting";
mediaBrowserBridge
.connect(this.options.app)
.then((rootId) => {
if (this.removed) return;
this.phase = "ready";
const root: BrowseLevel = {
parentId: rootId,
title: "Library",
items: null,
error: "",
selectedIndex: 0,
scrollRow: 0,
};
this.levels = [root];
this.loadLevel(ctx, root);
ctx.actions.requestRender();
})
.catch((error) => {
if (this.removed) return;
this.phase = "failed";
this.connectError = error instanceof Error ? error.message : String(error);
ctx.actions.requestRender();
});
}
private loadLevel(ctx: LayerContext, level: BrowseLevel): void {
mediaBrowserBridge
.browse(level.parentId)
.then((items) => {
if (this.removed) return;
level.items = items;
ctx.actions.requestRender();
})
.catch((error) => {
if (this.removed) return;
level.error = error instanceof Error ? error.message : String(error);
ctx.actions.requestRender();
});
}
}
function truncateRight(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
function truncateLeft(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) {
out = out.slice(1);
}
return `...${out}`;
}