Add centered/popup version of app switcher; plus various UI tweaks

This commit is contained in:
James Babcock committed 2026-10-02 23:36:51 -07:00
1 parent 295922effd
commit 96e155ebb0
12 files changed
+810 -190

No files matched your search

+4 -1
View File
@@ -38,6 +38,9 @@ const TOP_PAD = 2;
const BOTTOM_PAD = 4;
/** Clearance between the rose's widest point and the viewport edge. */
const EDGE_PAD = 8;
/** Inset of the top-right magnetometer calibration readout from the window edges. */
const ACCURACY_TOP_PAD = TOP_PAD + 4;
const ACCURACY_RIGHT_PAD = EDGE_PAD + 4;
/** Cap on the rose's radius, so a tall window doesn't get a comical one. */
const MAX_ROSE_RADIUS = 98;
@@ -197,7 +200,7 @@ class CompassLayer implements Layer {
y += smallStep;
}
const accuracyText = magneticAccuracyText(this.diagnostics);
image.drawText(small, width - EDGE_PAD - small.measureText(accuracyText), TOP_PAD, accuracyText, 175);
image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyText), ACCURACY_TOP_PAD, accuracyText, 175);
return image;
}
+80 -26
View File
@@ -1,5 +1,5 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { GrayImage, type UiFont } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL_DOWN, GESTURE_SCROLL_UP, type InputEvent, isDirectionalInput } from "../../ui/gestures";
@@ -20,8 +20,9 @@ const ART_SIZE = 112;
const ART_X = 8; // left margin matches the top margin
const ART_Y = 8;
const META_X = ART_X + ART_SIZE + 14;
const PROGRESS_TIME_Y = ART_Y + 100;
const PROGRESS_BAR_Y = ART_Y + 116;
/** Clear rows between the time labels' baseline (digits have no descenders) and the bar. */
const PROGRESS_TIME_GAP = 3;
const LIST_TOP = 136;
/** Gap between the menus' last row band and the viewport bottom. */
const LIST_BOTTOM_MARGIN = 4;
@@ -47,6 +48,8 @@ type FocusColumn = "actions" | "playlist";
* Music controller app: metadata + album art + transport controls for the
* active Android media session (any player that publishes one), 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).
*/
class MusicAppLayer implements Layer {
// Watch swipes skip tracks: right = next, left = previous.
@@ -66,6 +69,19 @@ class MusicAppLayer implements Layer {
getHeight: () => tightRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawQueueItem(args),
});
/** The no-session front page's picker of apps whose library can be browsed. */
private readonly libraryMenu = new Menu<MediaBrowserApp>({
wrap: false,
rowGap: 1,
highlight: { radius: 4 },
getHeight: () => tightRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawLibraryApp(args),
});
/**
* Whether the browsable-app list has been re-queried since the no-session
* page last appeared (apps installed since the cached query then show up).
*/
private libraryAppsFresh = false;
private art: GrayImage | null = null;
private artKey = "";
@@ -93,16 +109,31 @@ class MusicAppLayer implements Layer {
}
if (!media.available) {
const apps = mediaBrowserBridge.listBrowsableApps(!this.libraryAppsFresh);
this.libraryAppsFresh = true;
this.libraryMenu.setItems(apps);
image.drawText(font, 24, 16, "No active media session.", 180);
if (mediaBrowserBridge.listBrowsableApps().length) {
image.drawText(font, 24, 22 + lineStep(font), "Click to browse a music app's library,", 150);
image.drawText(font, 24, 22 + 2 * lineStep(font), "or start playback on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} browse`, 110);
} else {
image.drawText(font, 24, 22 + lineStep(font), "Start playback in another app on the phone.", 150);
const prompt = apps.length
? "Browse a music app's library, or start playback on the phone."
: "Start playback in another app on the phone.";
let y = 22 + lineStep(font);
for (const line of wrapText(font, prompt, width - 48)) {
image.drawText(font, 24, y, line, 150);
y += lineStep(font);
}
if (apps.length) {
const listTop = y + 6;
const listHeight = height - listTop - LIST_BOTTOM_MARGIN;
this.libraryMenu.paint(
image,
{ x: 24 - LIST_TEXT_INSET, y: listTop - 1, width: width - 48 + 2 * LIST_TEXT_INSET - 4, height: listHeight },
ctx.stack.isFocused(),
);
this.libraryMenu.drawScrollbar(image, width - 12, listTop, listHeight - 4);
}
return image;
}
this.libraryAppsFresh = false;
this.drawArt(image, media);
@@ -113,7 +144,7 @@ class MusicAppLayer implements Layer {
// (title, second title line, artist, album, player app); when the font is
// too large for all five, the title gets one truncated line instead of
// wrapping to two, and the rest shift up a slot.
const maxLines = Math.floor((PROGRESS_TIME_Y - metaTop - 4 - font.lineHeight) / metaStep) + 1;
const maxLines = Math.floor((progressTimeY(font) - metaTop - 4 - font.lineHeight) / metaStep) + 1;
const titleSlots = maxLines >= 5 ? 2 : 1;
const title = media.title || "Unknown title";
const titleLines =
@@ -171,6 +202,13 @@ class MusicAppLayer implements Layer {
}
}
private drawLibraryApp({ image, item: app, x, y, width, height, selected }: MenuDrawArgs<MediaBrowserApp>): void {
const font = getDefaultSmallFont();
const value = selected ? 255 : 200;
image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, app.appName, width - 2 * LIST_TEXT_INSET - 16), value);
drawSubmenuIndicator(image, font, x, y, width, height, value);
}
private drawQueueItem({ image, item, x, y, width, height, selected }: MenuDrawArgs<MediaQueueItem>): void {
const font = getDefaultSmallFont();
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
@@ -197,8 +235,12 @@ class MusicAppLayer implements Layer {
return;
}
if (!media.available) {
if (event.type === "click") {
this.openBrowse(ctx);
this.libraryMenu.setItems(mediaBrowserBridge.listBrowsableApps());
if (event.type === "scroll-up" || event.type === "scroll-down") {
await this.libraryMenu.handleInput(event);
} else if (event.type === "click") {
const app = this.libraryMenu.selectedItem;
if (app) this.pushLibraryBrowser(ctx, app);
}
return;
}
@@ -252,23 +294,15 @@ class MusicAppLayer implements Layer {
/**
* Open the media-library browser (the Android Auto MediaBrowserService
* path): with one browsable player installed go straight to its library,
* otherwise offer a picker first.
* path) from the now-playing page: with one browsable player installed go
* straight to its library, otherwise offer a picker first. (With no session
* the picker is the front page itself.)
*/
private openBrowse(ctx: LayerContext): void {
const apps = mediaBrowserBridge.listBrowsableApps(true);
if (!apps.length) return;
const pushBrowser = (target: LayerContext, app: MediaBrowserApp) => {
target.stack.push(
new MediaBrowseLayer({
app,
onPlayed: (browseCtx) => browseCtx.stack.pop(),
onLeave: (browseCtx) => browseCtx.stack.pop(),
}),
);
};
if (apps.length === 1) {
pushBrowser(ctx, apps[0]!);
this.pushLibraryBrowser(ctx, apps[0]!);
return;
}
ctx.stack.push(
@@ -278,13 +312,23 @@ class MusicAppLayer implements Layer {
label: app.appName,
onSelect: (menuCtx: LayerContext) => {
menuCtx.stack.pop();
pushBrowser(menuCtx, app);
this.pushLibraryBrowser(menuCtx, app);
},
})),
),
);
}
private pushLibraryBrowser(ctx: LayerContext, app: MediaBrowserApp): void {
ctx.stack.push(
new MediaBrowseLayer({
app,
onPlayed: (browseCtx) => browseCtx.stack.pop(),
onLeave: (browseCtx) => browseCtx.stack.pop(),
}),
);
}
private buildActions(media: MediaControllerState, queue: MediaQueueItem[]): MusicAction[] {
const volume = mediaControllerBridge.getMediaVolumePercent();
return [
@@ -316,8 +360,9 @@ class MusicAppLayer implements Layer {
const font = getDefaultSmallFont();
const elapsed = formatMediaTime(media.positionMs);
const duration = formatMediaTime(media.durationMs);
image.drawText(font, META_X, PROGRESS_TIME_Y, elapsed, 140);
image.drawText(font, META_X + width - font.measureText(duration), PROGRESS_TIME_Y, duration, 140);
const timeY = progressTimeY(font);
image.drawText(font, META_X, timeY, elapsed, 140);
image.drawText(font, META_X + width - font.measureText(duration), timeY, duration, 140);
const barY = PROGRESS_BAR_Y;
image.drawRect(META_X, barY, width, 5, 55);
@@ -481,6 +526,15 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
return app;
}
/**
* Line top of the elapsed/duration labels: their baseline sits a fixed gap
* above the progress bar, so the labels hug the bar whatever the font's
* height (a taller face grows upward into the metadata's line budget).
*/
function progressTimeY(font: UiFont): number {
return PROGRESS_BAR_Y - PROGRESS_TIME_GAP - font.ascent;
}
function formatMediaTime(milliseconds: number): string {
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
const seconds = totalSeconds % 60;
+35 -28
View File
@@ -1,9 +1,10 @@
/**
* Terminal app, hosted in its own worker thread. Window model:
* - "terminal:hub": the window opened from the launcher; shows the list of
* live g2mirror sessions across every connected host (grouped by host when
* more than one is connected; selecting a host's heading launches a shell
* there), and hosts the Manage Connections section
* live g2mirror sessions grouped under a heading per configured host
* (unconnected hosts show their state and a Connect row; selecting a
* connected host's heading launches a shell there), and hosts the Manage
* Connections section
* where g2mirror:// connections are added, removed, and toggled.
* - "terminal:view:N": opened by selecting a session in the hub; each has
* its own websocket connection (the protocol allows one attached session
@@ -1042,12 +1043,15 @@ function windowMenu(window: TerminalWindow): WindowMenu {
return window.menu;
}
/** Whether a control's handshake has been accepted (it can list and launch sessions). */
function isControlConnected(control: ControlConnection): boolean {
const phase = control.state?.phase;
return phase === "connected" || phase === "attached";
}
/** Controls currently connected (handshake accepted), in stored order. */
function connectedControls(): ControlConnection[] {
return [...controls.values()].filter((control) => {
const phase = control.state?.phase;
return phase === "connected" || phase === "attached";
});
return [...controls.values()].filter(isControlConnected);
}
function windowMenuItems(window: TerminalWindow): MenuItem[] {
@@ -1220,7 +1224,7 @@ type HubItem = {
*/
heading?: boolean;
/**
* Top-level action (Manage Connections, Settings, Connect <host>): drawn
* Top-level action (Manage Connections, Settings): drawn
* unindented like a heading but at full brightness, rather than as an
* indented session row.
*/
@@ -1338,17 +1342,31 @@ function hubSessionItems(window: HubWindow): HubItem[] {
onSelect: () => windowMenu(window).open(settingsMenuItems(), "Terminal settings"),
},
];
const connected = connectedControls();
const multiHost = connected.length > 1;
const multiHost = controls.size > 1;
const canLaunch = launchPresetNames().length > 0;
for (const control of connected) {
if (multiHost) {
items.push({
label: connectionDisplayName(control.config),
heading: true,
onSelect: canLaunch ? () => launchOnHost(window, control) : undefined,
});
// Every configured host is listed under its own heading, in stored order,
// whatever its connection state: a connected host lists its sessions, any
// other shows its state on the heading plus a Connect row (unless it is
// already connecting). Full management (remove, add) lives in Manage
// Connections.
for (const control of controls.values()) {
const name = connectionDisplayName(control.config);
if (!isControlConnected(control)) {
items.push({ label: `${name} (${controlStatusWord(control)})`, heading: true });
const connecting = control.config.enabled && control.state?.phase === "connecting";
if (!connecting && clientOptionsFor(control.config)) {
items.push({
label: control.reconnectTimer ? "Connect now" : "Connect",
onSelect: () => connectControl(control),
});
}
continue;
}
items.push({
label: name,
heading: true,
onSelect: canLaunch ? () => launchOnHost(window, control) : undefined,
});
const sessions = orderedSessions(window, control);
for (const session of sessions) {
items.push({
@@ -1372,17 +1390,6 @@ function hubSessionItems(window: HubWindow): HubItem[] {
});
}
}
// Disconnected/failed connections get a one-click Connect shortcut here;
// full management (remove, add) lives in Manage Connections.
for (const control of controls.values()) {
if (connected.includes(control)) continue;
if (control.config.enabled && control.state?.phase === "connecting") continue;
items.push({
label: `Connect ${connectionDisplayName(control.config)}`,
topLevel: true,
onSelect: () => connectControl(control),
});
}
return items;
}