Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Bar menus: fewer clips on modest devices, none on a slow connection

Adarsh666be2c2a6e2
diff --git a/src/components/nav/cover-thumb.tsx b/src/components/nav/cover-thumb.tsx
index 11a989cc..3a18cc3e 100644
--- a/src/components/nav/cover-thumb.tsx
+++ b/src/components/nav/cover-thumb.tsx
@@ -20,6 +20,9 @@ const ALIVE_AFTER = 120;
/** The one cover clip playing on hover (a card under the pointer: one decoder at a time). A menu's player runs its own (shared). */
let playingClip: HTMLVideoElement | null = null;
+/** Sent on window when the browser refuses to start a muted clip (iOS Low Power Mode, an autoplay policy). */
+export const AUTOPLAY_BLOCKED = "fuobe:autoplay-blocked";
+
/** Clips play unless the viewer asks for less motion or to save data. */
export function clipsAllowed(): boolean {
if (reducedMotion()) return false;
@@ -94,7 +97,10 @@ function CoverClip({ src, at, leaving, focus, shared }: { src: string; at?: numb
v.addEventListener("playing", playing, { once: true });
v.addEventListener("timeupdate", tail);
v.src = src;
- void v.play().catch(() => {});
+ void v.play().catch((e: unknown) => {
+ // Refused (not just interrupted by a pause): a menu's player stops asking for the rest of the visit.
+ if (shared && e instanceof DOMException && e.name === "NotAllowedError") window.dispatchEvent(new Event(AUTOPLAY_BLOCKED));
+ });
return () => {
live = false;
v.removeEventListener("timeupdate", tail);
diff --git a/src/components/nav/mega-panel.tsx b/src/components/nav/mega-panel.tsx
index 7a888308..42f42e88 100644
--- a/src/components/nav/mega-panel.tsx
+++ b/src/components/nav/mega-panel.tsx
@@ -11,7 +11,7 @@ import { NEW_CREATION_HREF } from "@/lib/nav";
import { bundledMenu, MEGA_AREAS, type MegaColumn, type MegaId, type MegaItem, type MegaModel } from "@/lib/mega-menus";
import { isNew, subParent, type ViewerKind } from "@/lib/nav-model";
import { cn } from "@/lib/utils";
-import { clipsAllowed, CoverThumb, useAliveCard, useHasClip } from "./cover-thumb";
+import { AUTOPLAY_BLOCKED, clipsAllowed, CoverThumb, useAliveCard, useHasClip } from "./cover-thumb";
import { useMegaMenu } from "./mega-data";
import { megaCurrent, MegaSkeleton } from "./mega-launcher";
import { useViewerKind } from "./nav-ui";
@@ -37,6 +37,24 @@ export const TILE_SIZES = "(min-width: 1680px) 316px, (min-width: 1280px) calc((
/** How many tiles play their clips at once: enough to bring the menu to life, few enough for any laptop. */
export const PLAY_AT_ONCE = 8;
+/** On a modest device (4 GB of memory or less, or 4 cores or fewer): fewer at once, so the menu stays smooth. */
+export const PLAY_AT_ONCE_MODEST = 3;
+
+/**
+ * How many clips this device plays at once: none on a slow connection (2G or 3G) or when saving data, a few on a modest
+ * device, else PLAY_AT_ONCE. Browsers that don't say (Safari) count as capable; Safari's Low Power Mode refuses autoplay
+ * by itself (AUTOPLAY_BLOCKED), and the stills stay.
+ */
+export function playCap(nav: Navigator | undefined = typeof navigator === "undefined" ? undefined : navigator): number {
+ if (!nav) return 0;
+ const n = nav as Navigator & { connection?: { saveData?: boolean; effectiveType?: string }; deviceMemory?: number };
+ if (n.connection?.saveData || /(^|-)2g$|^3g$/.test(n.connection?.effectiveType ?? "")) return 0;
+ if ((n.deviceMemory ?? 8) <= 4 || (n.hardwareConcurrency || 8) <= 4) return PLAY_AT_ONCE_MODEST;
+ return PLAY_AT_ONCE;
+}
+
+/** The browser refused a clip this visit: no tile asks again until the next page load. */
+let autoplayBlocked = false;
function NewBadge() {
return (
@@ -146,17 +164,27 @@ function usePlaceKeys(root: React.RefObject<HTMLElement | null>, key: unknown) {
}
/**
- * The menu's player: the tiles on screen with a clip play it, at most PLAY_AT_ONCE, the first ones first. Nothing plays
- * while the panel is closed or scrolled past, in a hidden tab, with previews paused (Pause previews), under reduced
- * motion or when saving data. Returns the ids playing.
+ * The menu's player: the tiles on screen with a clip play it, at most playCap() (eight; three on a modest device; none on
+ * a slow connection), the first ones first. Nothing plays while the panel is closed or scrolled past, in a hidden tab,
+ * with previews paused (Pause previews), under reduced motion, when saving data, or once the browser has refused a clip
+ * (iOS Low Power Mode). Returns the ids playing.
*/
const NONE: ReadonlySet<string> = new Set();
function useMenuPlayer(root: React.RefObject<HTMLElement | null>, key: unknown): ReadonlySet<string> {
const paused = usePreviewsPaused();
- const [allowed] = useState(() => typeof window !== "undefined" && clipsAllowed());
+ const [cap] = useState(() => (typeof window !== "undefined" && clipsAllowed() ? playCap() : 0));
+ const [blocked, setBlocked] = useState(() => autoplayBlocked);
const [ids, setIds] = useState<ReadonlySet<string>>(NONE);
- const off = paused || !allowed;
+ const off = paused || !cap || blocked;
+ useEffect(() => {
+ const stop = () => {
+ autoplayBlocked = true;
+ setBlocked(true);
+ };
+ window.addEventListener(AUTOPLAY_BLOCKED, stop);
+ return () => window.removeEventListener(AUTOPLAY_BLOCKED, stop);
+ }, []);
useEffect(() => {
const el = root.current;
if (!el || off) return;
@@ -164,7 +192,7 @@ function useMenuPlayer(root: React.RefObject<HTMLElement | null>, key: unknown):
const order = new Map(tiles.map((t, i) => [t, i]));
const seen = new Set<HTMLElement>();
const update = () => {
- const next = document.hidden ? [] : [...seen].sort((a, b) => order.get(a)! - order.get(b)!).slice(0, PLAY_AT_ONCE).map((t) => t.dataset.tile!);
+ const next = document.hidden ? [] : [...seen].sort((a, b) => order.get(a)! - order.get(b)!).slice(0, cap).map((t) => t.dataset.tile!);
setIds((prev) => (prev.size === next.length && next.every((id) => prev.has(id)) ? prev : new Set(next)));
};
// The panel scrolls inside itself: its section is the box the tiles must be in to count as on screen.
@@ -184,7 +212,7 @@ function useMenuPlayer(root: React.RefObject<HTMLElement | null>, key: unknown):
io.disconnect();
document.removeEventListener("visibilitychange", update);
};
- }, [root, key, off]);
+ }, [root, key, off, cap]);
return off ? NONE : ids;
}
diff --git a/tests/nav-bar.test.ts b/tests/nav-bar.test.ts
index fe3200b9..d6c3c1e2 100644
--- a/tests/nav-bar.test.ts
+++ b/tests/nav-bar.test.ts
@@ -538,3 +538,18 @@ describe("create and prefetch", () => {
expect(list.every((h) => entriesFor("guest").some((e) => e.href === h))).toBe(true);
});
});
+
+describe("the menus' clips on any device", () => {
+ it("play eight at once on a capable device, three on a modest one, none on a slow connection or when saving data", async () => {
+ const { playCap, PLAY_AT_ONCE, PLAY_AT_ONCE_MODEST } = await import("@/components/nav/mega-panel");
+ const dev = (o: object) => o as Navigator;
+ expect(PLAY_AT_ONCE).toBe(8);
+ expect(playCap(dev({ hardwareConcurrency: 10, deviceMemory: 16 }))).toBe(PLAY_AT_ONCE);
+ // Safari says neither: it counts as capable (its Low Power Mode refuses autoplay by itself).
+ expect(playCap(dev({}))).toBe(PLAY_AT_ONCE);
+ expect(playCap(dev({ hardwareConcurrency: 4, deviceMemory: 8 }))).toBe(PLAY_AT_ONCE_MODEST);
+ expect(playCap(dev({ hardwareConcurrency: 8, deviceMemory: 4 }))).toBe(PLAY_AT_ONCE_MODEST);
+ expect(playCap(dev({ hardwareConcurrency: 8, connection: { effectiveType: "3g" } }))).toBe(0);
+ expect(playCap(dev({ hardwareConcurrency: 8, connection: { effectiveType: "4g", saveData: true } }))).toBe(0);
+ });
+});