Refactor to remove some duplicated functiosn

This commit is contained in:
James Babcock committed 2026-07-26 11:33:16 -07:00
1 parent 797682a386
commit 205d393415
17 files changed
+57 -223

No files matched your search

+2 -10
View File
@@ -1,4 +1,5 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
@@ -119,7 +120,7 @@ class LauncherGridLayer implements Layer {
}
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
if (labelY + font.lineHeight <= gridBottom) {
const label = truncate(font, app.label, colW - 8);
const label = truncateText(font, app.label, colW - 8);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, 210);
}
}
@@ -196,12 +197,3 @@ export function createLauncherWindow(options: LauncherOptions): ShellWindow {
setSurfaceVisible: options.setSurfaceVisible,
}).window;
}
function truncate(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}...`;
}
+5 -14
View File
@@ -1,6 +1,6 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import {
GESTURE_CLICK,
@@ -109,9 +109,9 @@ class MusicAppLayer implements Layer {
}
image.drawText(font, META_X, ART_Y + 36, media.artist || "Unknown artist", 180);
if (media.album) {
image.drawText(font, META_X, ART_Y + 52, truncate(font, media.album, metaWidth), 150);
image.drawText(font, META_X, ART_Y + 52, truncateText(font, media.album, metaWidth), 150);
}
image.drawText(font, META_X, ART_Y + 68, truncate(font, media.appName || media.packageName, metaWidth), 110);
image.drawText(font, META_X, ART_Y + 68, truncateText(font, media.appName || media.packageName, metaWidth), 110);
this.drawProgress(image, media, metaWidth);
const queue = mediaControllerBridge.getQueue();
@@ -146,7 +146,7 @@ class MusicAppLayer implements Layer {
);
}
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
image.drawText(font, ACTION_X, y + 1, truncate(font, action.label, ACTION_WIDTH - 16), value);
image.drawText(font, ACTION_X, y + 1, truncateText(font, action.label, ACTION_WIDTH - 16), value);
if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) {
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, value);
}
@@ -174,7 +174,7 @@ class MusicAppLayer implements Layer {
);
}
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
image.drawText(font, QUEUE_X, y + 1, truncate(font, label, queueWidth - 4), selected ? 255 : 200);
image.drawText(font, QUEUE_X, y + 1, truncateText(font, label, queueWidth - 4), selected ? 255 : 200);
}
if (queue.length > visibleRows) {
const trackHeight = visibleRows * ROW_HEIGHT - 4;
@@ -492,12 +492,3 @@ function formatMediaTime(milliseconds: number): string {
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
: `${minutes}:${String(seconds).padStart(2, "0")}`;
}
function truncate(font: import("../../graphics/bdffont").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}...`;
}