Open source CapCut alternative.
TypeScript
feat(site): the tab wears a badge
David Lea3c087fbbc28
diff --git a/site/src/cut/components/TabStatus.tsx b/site/src/cut/components/TabStatus.tsxindex e49c2316..746b05ea 100644--- a/site/src/cut/components/TabStatus.tsx+++ b/site/src/cut/components/TabStatus.tsx@@ -4,27 +4,33 @@ import { useEffect, useState } from "react";import { useExports } from "@/cut/lib/exportStore";import { useGenerate } from "@/cut/lib/generate";import { useEditor } from "@/cut/lib/store";-import { busyIn, useTabActivity } from "@/cut/lib/tabActivity";+import { useTabActivity } from "@/cut/lib/tabActivity";/*** The tab says what it is doing.** Work here runs long and the user goes elsewhere while it does, so the two* things visible from another tab carry the state: the favicon and the title.- * A ring turns while anything is running, and when the last of it settles a- * green check takes its place and waits — until the user looks at the tab, or- * a few seconds if they were already looking. Then the tab goes back to being- * the project's name and the donkey.+ * The icon stays the donkey and takes a badge in its corner — amber, carrying+ * how many jobs are under way, then green with a check once the last of them+ * settles. The check waits for the user: until they look at the tab, or a few+ * seconds if they were already looking. Then the tab goes back to being the+ * project's name and the plain donkey.*/-const BUSY_MARK = "● ";const DONE_MARK = "✓ ";/** How long the finished mark holds when the user is already on the tab. */const DONE_MS = 6000;-const SPIN_MS = 90;+/** The art already in the tab; the badge is painted over a copy of it. */+const BASE_ICON = "/favicon.ico";+const BUSY_COLOR = "#ff9500";+const DONE_COLOR = "#30d158";-const strip = (title: string) =>- title.startsWith(BUSY_MARK) || title.startsWith(DONE_MARK) ? title.slice(2) : title;+/** Take back a mark this component put in front of the title, and nothing else. */+const strip = (title: string) => title.replace(/^(?:[✓●]|\(\d+\+?\)) /, "");++/** What the badge and the title say when several jobs run at once. */+const tally = (count: number) => (count > 9 ? "9+" : String(count));/*** The link this component paints on — its own, never the page's.@@ -51,35 +57,63 @@ function dropIconLink(): void {document.querySelector("link[data-tab-status]")?.remove();}-/** The status icon: a turning ring, or a check on a green disc. */-function paint(state: "busy" | "done", turn: number): string {+let baseIcon: HTMLImageElement | null = null;+let baseLoad: Promise<void> | null = null;++/** The tab's own art, decoded once, so the badge has something to sit on. */+function loadBase(): Promise<void> {+ baseLoad ??= new Promise<void>((resolve) => {+ const img = new Image();+ img.onload = () => {+ baseIcon = img;+ resolve();+ };+ // The badge alone still reads if the art never arrives.+ img.onerror = () => resolve();+ img.src = BASE_ICON;+ });+ return baseLoad;+}++/** The icon with its badge: a count while work runs, a check once it is done. */+function paint(state: "busy" | "done", count: number): string {const canvas = document.createElement("canvas");- canvas.width = canvas.height = 32;+ canvas.width = canvas.height = 64;const ctx = canvas.getContext("2d");if (!ctx) return "";- ctx.lineCap = "round";+ // Drawn in 32 units at twice the size, which is what a retina tab asks for.+ ctx.scale(2, 2);+ if (baseIcon) ctx.drawImage(baseIcon, 0, 0, 32, 32);++ const x = 22.5;+ const y = 22.5;+ // A ring in the icon's own white sets the badge off from the art beneath it.+ ctx.fillStyle = "#ffffff";+ ctx.beginPath();+ ctx.arc(x, y, 9.5, 0, Math.PI * 2);+ ctx.fill();+ ctx.fillStyle = state === "busy" ? BUSY_COLOR : DONE_COLOR;+ ctx.beginPath();+ ctx.arc(x, y, 7.5, 0, Math.PI * 2);+ ctx.fill();+if (state === "busy") {- ctx.strokeStyle = "rgba(10,132,255,0.2)";- ctx.lineWidth = 5;- ctx.beginPath();- ctx.arc(16, 16, 12, 0, Math.PI * 2);- ctx.stroke();- ctx.strokeStyle = "#0a84ff";- ctx.beginPath();- ctx.arc(16, 16, 12, turn, turn + Math.PI * 1.3);- ctx.stroke();+ const label = tally(count);+ ctx.fillStyle = "#ffffff";+ // Two glyphs have to fit the same circle one does.+ ctx.font = `bold ${label.length > 1 ? 9 : 11}px ui-sans-serif, -apple-system, system-ui, sans-serif`;+ ctx.textAlign = "center";+ ctx.textBaseline = "middle";+ ctx.fillText(label, x, y + 0.5);} else {- ctx.fillStyle = "#30d158";- ctx.beginPath();- ctx.arc(16, 16, 15, 0, Math.PI * 2);- ctx.fill();ctx.strokeStyle = "#ffffff";- ctx.lineWidth = 4;+ ctx.lineWidth = 2.4;+ ctx.lineCap = "round";ctx.lineJoin = "round";ctx.beginPath();- ctx.moveTo(9, 16.5);- ctx.lineTo(14, 21.5);- ctx.lineTo(23, 11);+ ctx.moveTo(x - 3.6, y + 0.2);+ ctx.lineTo(x - 1.1, y + 2.7);+ ctx.lineTo(x + 3.8, y - 2.9);ctx.stroke();}return canvas.toDataURL("image/png");@@ -89,27 +123,35 @@ export function TabStatus() {// The title is repainted when the project is renamed, which takes the mark// with it, so the name is a dependency of the paint.const projectName = useEditor((s) => s.projectName);- const [busy, setBusy] = useState(false);+ const [count, setCount] = useState(0);const [done, setDone] = useState(false);+ const busy = count > 0;// Four stores answer for the work, so the state is read off them together- // whenever any one moves: what matters is the moment the last of it stops.+ // whenever any one moves: the badge carries how much is running, and what+ // matters after that is the moment the last of it stops.useEffect(() => {- let was = false;- const read = () =>- busyIn(useTabActivity.getState().running) ||- useGenerate.getState().jobs.some((j) => j.status === "running") ||- useEditor.getState().renders.some((r) => r.status === "running") ||- useEditor.getState().subtitleStatus === "running" ||- useExports.getState().local.length > 0 ||- useExports.getState().jobs.some((j) => j.status === "queued" || j.status === "running");+ let was = 0;+ const read = () => {+ const activity = useTabActivity.getState().running;+ const editor = useEditor.getState();+ const exports = useExports.getState();+ return (+ Object.values(activity).filter(Boolean).length ++ useGenerate.getState().jobs.filter((j) => j.status === "running").length ++ editor.renders.filter((r) => r.status === "running").length ++ (editor.subtitleStatus === "running" ? 1 : 0) ++ exports.local.filter((r) => r.status !== "error").length ++ exports.jobs.filter((j) => j.status === "queued" || j.status === "running").length+ );+ };const apply = () => {const now = read();if (now === was) return;was = now;- setBusy(now);+ setCount(now);// Starting clears the last finish; stopping is the finish.- setDone(!now);+ setDone(now === 0);};const off = [useTabActivity.subscribe(apply),@@ -148,20 +190,18 @@ export function TabStatus() {return;}const link = iconLink();- document.title = (busy ? BUSY_MARK : DONE_MARK) + base;- if (!busy) {- link.href = paint("done", 0);- return;- }- let turn = 0;- const step = () => {- link.href = paint("busy", turn);- turn += Math.PI / 5;+ document.title = (busy ? `(${tally(count)}) ` : DONE_MARK) + base;+ let live = true;+ const render = () => {+ if (live) link.href = paint(busy ? "busy" : "done", count);+ };+ render();+ // The art decodes once; the badge painted before it lands is repainted on it.+ if (!baseIcon) void loadBase().then(render);+ return () => {+ live = false;};- step();- const timer = setInterval(step, SPIN_MS);- return () => clearInterval(timer);- }, [busy, done, projectName]);+ }, [busy, done, count, projectName]);// Leaving the editor leaves the tab as it was found.useEffect(() => dropIconLink, []);