Repository navigation
Expand file tree
/
Copy pathx-hud.ts
More file actions
321 lines (298 loc) · 11.8 KB
/
Copy pathx-hud.ts
File metadata and controls
321 lines (298 loc) · 11.8 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*! Sandevistan X HUD
MIT License
Copyright (c) 2026 hmmhmmhm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
export const COLOR = {
background: "#000000",
primary: "#ffffff",
secondary: "#d0d0d0",
dim: "#808080",
} as const;
export type FastCanvasColor =
typeof COLOR[keyof typeof COLOR];
export type FastCanvasPoint = readonly [number, number];
export function drawText(
context: CanvasRenderingContext2D,
value: string,
x: number,
y: number,
size: number,
color: FastCanvasColor = COLOR.primary,
weight: "normal" | "bold" = "normal",
) {
context.fillStyle = color;
context.font = `${weight} ${size}px "SFMono-Regular", Consolas, monospace`;
context.textAlign = "left";
context.textBaseline = "top";
context.fillText(value, x, y);
}
export function drawFrame(
context: CanvasRenderingContext2D,
x: number,
y: number,
width: number,
height: number,
color: FastCanvasColor = COLOR.dim,
) {
const corner = 18;
const right = x + width;
const bottom = y + height;
context.fillStyle = color;
context.fillRect(x, y, corner, 1);
context.fillRect(x, y, 1, corner);
context.fillRect(right - corner, y, corner, 1);
context.fillRect(right - 1, y, 1, corner);
context.fillRect(x, bottom - 1, corner, 1);
context.fillRect(x, bottom - corner, 1, corner);
context.fillRect(right - corner, bottom - 1, corner, 1);
context.fillRect(right - 1, bottom - corner, 1, corner);
}
function hudUnits(value: string): number {
return [...value].reduce(
(total, character) =>
total + (/^[\x00-\x7f]$/.test(character) ? 1 : 2),
0,
);
}
function prefixLength(value: string, maxUnits: number): number {
let used = 0;
let length = 0;
for (const character of value) {
const units = /^[\x00-\x7f]$/.test(character) ? 1 : 2;
if (used + units > maxUnits) break;
used += units;
length += 1;
}
return length;
}
function truncatedLine(value: string, maxUnits: number): string {
const ellipsisUnits = 2;
if (maxUnits < ellipsisUnits) return "";
const characters = [...value];
const length = prefixLength(value, maxUnits - ellipsisUnits);
return `${characters.slice(0, length).join("").trimEnd()}…`;
}
export function wrapHudText(
value: string,
maxUnits: number,
maxLines: number,
): readonly string[] {
let remaining = value.replace(/\s+/g, " ").trim();
if (!remaining || maxUnits <= 0 || maxLines <= 0) return [];
const lines: string[] = [];
while (remaining && lines.length < maxLines) {
if (hudUnits(remaining) <= maxUnits) {
lines.push(remaining);
break;
}
if (lines.length === maxLines - 1) {
const line = truncatedLine(remaining, maxUnits);
if (line) lines.push(line);
break;
}
const characters = [...remaining];
const length = prefixLength(remaining, maxUnits);
const candidate = characters.slice(0, length).join("");
const wordBoundary = candidate.lastIndexOf(" ");
const nextIsBoundary = characters[length] === " ";
const breakAt = nextIsBoundary
? length
: wordBoundary > 0
? [...candidate.slice(0, wordBoundary)].length
: length;
const line = characters.slice(0, breakAt).join("").trim();
if (line) lines.push(line);
remaining = characters.slice(
nextIsBoundary || wordBoundary > 0 ? breakAt + 1 : breakAt,
).join("").trimStart();
}
return lines;
}
export function drawHeader(
context: CanvasRenderingContext2D,
title: string,
counter?: string,
) {
context.fillStyle = COLOR.background;
context.fillRect(0, 0, WIDTH, 38);
drawText(context, title, 14, 10, 16, COLOR.primary, "bold");
if (counter) {
context.font = 'bold 13px "SFMono-Regular", Consolas, monospace';
const width = context.measureText(counter).width;
drawText(
context,
counter,
Math.max(300, 562 - width),
11,
13,
COLOR.secondary,
"bold",
);
}
context.fillStyle = COLOR.dim;
context.fillRect(14, 36, 548, 1);
}
export function drawEmptyState(
context: CanvasRenderingContext2D,
headline: string,
detail: string,
) {
drawText(context, headline, 36, 84, 30, COLOR.primary, "bold");
for (const [index, line] of wrapHudText(detail, 50, 3).entries()) {
drawText(context, line, 38, 136 + index * 25, 17, COLOR.secondary, "bold");
}
}
const WIDTH = 576;
function compactPosition(index: number, count: number): string {
const width = Math.max(2, String(Math.max(0, count)).length);
const current = count > 0 ? Math.min(index, count - 1) + 1 : 0;
return `${String(current).padStart(width, "0")}/${
String(count).padStart(width, "0")
}`;
}
const xImages = new Map<string, HTMLImageElement>();
const X_IMAGE_LIMIT = 48;
export function clearXImages() { xImages.clear(); }
function loadXImage(url: string): HTMLImageElement | undefined {
const cached = xImages.get(url);
if (cached) { xImages.delete(url); xImages.set(url, cached); return cached; }
const image = new Image();
image.crossOrigin = "anonymous";
image. => window.dispatchEvent(new Event("sandevistan-x-image-ready"));
image.src = import.meta.env.DEV && url.startsWith('/src/assets/') ? url : `https://sandevistan-x-relay.hmmhmmhm.workers.dev/media?url=${encodeURIComponent(url)}`;
xImages.set(url, image);
if (xImages.size > X_IMAGE_LIMIT) {
const oldest = xImages.keys().next().value;
if (oldest !== undefined) xImages.delete(oldest);
}
return image;
}
function drawXMetricIcon(
context: CanvasRenderingContext2D,
kind: "reply" | "repost" | "like" | "quote",
x: number,
y: number,
) {
context.fillStyle = COLOR.secondary;
if (kind === "reply") {
context.fillRect(x, y, 16, 3); context.fillRect(x, y, 3, 12);
context.fillRect(x + 13, y, 3, 12); context.fillRect(x + 4, y + 9, 9, 3);
context.fillRect(x + 3, y + 12, 4, 3);
} else if (kind === "repost") {
context.fillRect(x + 2, y + 2, 11, 3); context.fillRect(x + 10, y, 6, 7);
context.fillRect(x + 3, y + 11, 11, 3); context.fillRect(x, y + 9, 6, 7);
} else if (kind === "like") {
context.fillRect(x + 2, y, 5, 4); context.fillRect(x + 10, y, 5, 4);
context.fillRect(x, y + 3, 17, 6); context.fillRect(x + 3, y + 8, 11, 4);
context.fillRect(x + 6, y + 12, 5, 3);
} else {
context.fillRect(x, y, 5, 7); context.fillRect(x + 7, y, 5, 7);
context.fillRect(x + 3, y + 5, 5, 6); context.fillRect(x + 10, y + 5, 5, 6);
}
}
function drawXMetric(
context: CanvasRenderingContext2D,
kind: "reply" | "repost" | "like" | "quote",
value: number,
x: number,
y: number,
) {
drawXMetricIcon(context, kind, x, y);
drawText(context, String(value), x + 23, y - 1, 15, COLOR.secondary, "bold");
}
function paginateText(value: string, width: number, capacity: number) {
let remaining = value.replace(/\s+/g, " ").trim();
const pages: string[][] = [];
while (remaining) {
const lines = wrapHudText(remaining, width, capacity);
pages.push([...lines]);
for (const [index, line] of lines.entries()) {
const consumed = index === capacity - 1 && line.endsWith("…") && line !== remaining
? line.slice(0, -1) : line;
remaining = remaining.slice(consumed.length).trimStart();
}
}
return pages;
}
export type XHudPost = {
author: string; username: string; text: string; avatarUrl?: string; imageUrl?: string; createdAt?: string;
repostedFrom?: { name: string }; quotedPost?: { author: string; text: string };
metrics?: { replies: number; reposts: number; likes: number; quotes: number };
};
type XHudState = { status: string; value?: readonly XHudPost[]; offset?: number; message?: string };
export function drawX(context: CanvasRenderingContext2D, state: XHudState, index: number, locale: string, page = 0) {
context.imageSmoothingEnabled = false;
context.fillStyle = COLOR.background;
context.fillRect(0, 0, WIDTH, 288);
const posts = state.value ?? [];
const post = state.status === "fresh" || state.status === "stale" ? posts[Math.min(index, posts.length - 1)] : undefined;
const offset = state.offset ?? 0;
drawHeader(context, state.status === "fresh" ? "X // HOME" : "X // UNAVAILABLE", post ? compactPosition(index + offset, posts.length + offset) : "00/00");
drawFrame(context, 14, 44, 548, 204);
if (!post) { drawEmptyState(context, "X timeline unavailable", state.message || "Check your X token and plan."); return 1; }
const authorY = post.repostedFrom ? 72 : 52;
if (post.repostedFrom) drawText(context, `REPOSTED · ${post.repostedFrom.name}`, 30, 52, 11, COLOR.dim, "bold");
const avatar = post.avatarUrl ? loadXImage(post.avatarUrl) : undefined;
if (avatar?.complete && avatar.naturalWidth > 0) {
context.drawImage(avatar, 28, authorY, 28, 28);
} else {
context.fillStyle = COLOR.secondary;
context.fillRect(28, authorY, 28, 28);
drawText(context, post.author.slice(0, 1).toUpperCase(), 36, authorY + 5, 15, COLOR.background, "bold");
}
drawText(context, post.author, 66, authorY, 17, COLOR.primary, "bold");
drawText(context, `@${post.username}`, 66, authorY + 19, 12, COLOR.secondary, "bold");
const image = post.imageUrl ? loadXImage(post.imageUrl) : undefined;
const imageVisible = Boolean(image?.complete && image.naturalWidth > 0);
if (imageVisible && image) {
context.filter = "grayscale(1) contrast(2.4) brightness(1.18)";
context.drawImage(image, 346, 92, 194, 106);
context.filter = "none";
}
const textY = authorY + 36;
const quote = post.quotedPost;
const width = imageVisible ? 25 : 48;
const capacity = quote ? (imageVisible ? 2 : 3) : (imageVisible ? 4 : 6);
const bodyPages = paginateText(post.text, width, capacity);
const quotePages = quote ? paginateText(quote.text, imageVisible ? 24 : 43, 3) : [];
const pages = Math.max(1, bodyPages.length, quotePages.length);
const current = Math.max(0, Math.min(page, pages - 1));
const textLines = bodyPages[current] ?? [];
textLines.forEach((line, lineIndex) => drawText(context, line, 28, textY + lineIndex * 22, 19, COLOR.primary, "bold"));
if (quote && quotePages[current]) {
const quoteY = textY + textLines.length * 22 + 4;
context.fillStyle = COLOR.dim;
context.fillRect(28, quoteY, imageVisible ? 300 : 500, 1);
drawText(context, `QUOTE · ${quote.author}`, 28, quoteY + 6, 12, COLOR.secondary, "bold");
quotePages[current].forEach((line, lineIndex) => (
drawText(context, line, 28, quoteY + 21 + lineIndex * 16, 13, COLOR.primary, "bold")
));
}
const metrics = post.metrics;
const day = post.createdAt ? new Date(post.createdAt).toLocaleDateString(locale) : "";
context.fillStyle = COLOR.background;
context.fillRect(0, 254, WIDTH, 34);
context.fillStyle = COLOR.dim;
context.fillRect(14, 254, 548, 1);
drawText(context, day, 20, 267, 12, COLOR.secondary, "bold");
drawXMetric(context, "reply", metrics?.replies ?? 0, 150, 264);
drawXMetric(context, "repost", metrics?.reposts ?? 0, 238, 264);
drawXMetric(context, "like", metrics?.likes ?? 0, 326, 264);
drawXMetric(context, "quote", metrics?.quotes ?? 0, 414, 264);
return pages;
}