Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/web/public/illustrations/launch-hand.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions apps/web/src/api/startup-animation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getDaemonHealth } from "./control-plane.js";
import { daemonBaseUrl } from "./client.js";
import { readActiveWorkspaceCwd, readInitialWorkspaceCwd } from "../lib/local-storage.js";

export async function readStartupAnimationVideo(): Promise<Blob | undefined> {
let cwd = readActiveWorkspaceCwd() || readInitialWorkspaceCwd();
if (!cwd) {
const health = await Promise.race([
getDaemonHealth(),
new Promise<undefined>((resolve) => window.setTimeout(() => resolve(undefined), 500)),
]);
cwd = health?.defaultWorkspaceCwd?.trim() ?? "";
}
if (!cwd) return undefined;
const url = new URL("/plugins/startup-animation/media", daemonBaseUrl);
url.searchParams.set("cwd", cwd);
const response = await fetch(url, { signal: AbortSignal.timeout(1200), cache: "no-store" });
if (response.status === 204) return undefined;
if (!response.ok || response.headers.get("content-type") !== "video/mp4") throw new Error("Startup animation video is unavailable.");
return await response.blob();
}
117 changes: 101 additions & 16 deletions apps/web/src/features/launch/LaunchTransition.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createPortal } from "react-dom";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import { readStartupAnimationVideo } from "../../api/startup-animation.js";
import {
createLaunchAnimation,
LAUNCH_ANIMATION_FADE_DURATION,
Expand All @@ -21,13 +22,63 @@ export function LaunchTransition({
const controllerRef = useRef<LaunchAnimationController | undefined>(undefined);
const objectUrlRef = useRef<string | undefined>(undefined);
const onFinishedRef = useRef(onFinished);
const finishedRef = useRef(false);
const fadeTimerRef = useRef<number | undefined>(undefined);
const [exiting, setExiting] = useState(false);
const [visible, setVisible] = useState(true);
const [media, setMedia] = useState<Blob | null | undefined>(undefined);
const [videoUrl, setVideoUrl] = useState<string | undefined>(undefined);
const [introComplete, setIntroComplete] = useState(false);
const [handoff, setHandoff] = useState(false);

const complete = (): void => {
if (finishedRef.current) return;
finishedRef.current = true;
setExiting(true);
fadeTimerRef.current = window.setTimeout(() => {
setVisible(false);
onFinishedRef.current?.();
}, LAUNCH_ANIMATION_FADE_DURATION);
};

useEffect(() => {
onFinishedRef.current = onFinished;
}, [onFinished]);

useEffect(() => {
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
if (finishedRef.current) return undefined;
finishedRef.current = true;
setVisible(false);
onFinishedRef.current?.();
return undefined;
}
const timeout = window.setTimeout(complete, 5000);
const introTimer = window.setTimeout(() => setIntroComplete(true), 720);
let cancelled = false;
void readStartupAnimationVideo()
.then((video) => { if (!cancelled) setMedia(video ?? null); })
.catch(() => { if (!cancelled) setMedia(null); });
return () => {
cancelled = true;
window.clearTimeout(timeout);
window.clearTimeout(introTimer);
};
}, []);

useEffect(() => {
if (!media) return undefined;
const url = URL.createObjectURL(media);
setVideoUrl(url);
return () => {
URL.revokeObjectURL(url);
};
}, [media]);

useEffect(() => () => {
if (fadeTimerRef.current !== undefined) window.clearTimeout(fadeTimerRef.current);
}, []);

useLayoutEffect(() => {
if (!visible) return undefined;
const roots = ["root", "ling-ui-overlay-root"]
Expand All @@ -36,6 +87,7 @@ export function LaunchTransition({
.map((root) => ({ root, previousInert: root.inert }));
for (const { root } of roots) root.inert = true;
const blockKeyboard = (event: KeyboardEvent): void => {
if (event.key === "Escape") complete();
event.stopImmediatePropagation();
if (!event.metaKey && !event.ctrlKey) event.preventDefault();
};
Expand All @@ -44,27 +96,21 @@ export function LaunchTransition({
window.removeEventListener("keydown", blockKeyboard, true);
for (const { root, previousInert } of roots) root.inert = previousInert;
};
}, [visible]);
}, [visible, media]);

useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return undefined;
if (!canvas || media !== null) return undefined;

let fadeTimer: number | undefined;
let controller: LaunchAnimationController;
try {
controller = createLaunchAnimation(canvas, {
...(referenceImageUrl !== undefined ? { referenceImageUrl } : {}),
onComplete: () => {
setExiting(true);
fadeTimer = window.setTimeout(() => {
setVisible(false);
onFinishedRef.current?.();
}, LAUNCH_ANIMATION_FADE_DURATION);
},
...((objectUrlRef.current ?? referenceImageUrl) !== undefined ? { referenceImageUrl: objectUrlRef.current ?? referenceImageUrl } : {}),
onComplete: complete,
});
} catch (error) {
console.warn("Launch animation could not start.", error);
finishedRef.current = true;
setVisible(false);
onFinishedRef.current?.();
return undefined;
Expand All @@ -74,9 +120,8 @@ export function LaunchTransition({
return () => {
controller?.destroy();
controllerRef.current = undefined;
if (fadeTimer !== undefined) window.clearTimeout(fadeTimer);
};
}, [referenceImageUrl]);
}, [referenceImageUrl, media]);

useEffect(() => {
const referenceImage = resolved.manifest.launchAnimation?.referenceImage;
Expand All @@ -103,14 +148,54 @@ export function LaunchTransition({

if (!visible) return null;

const showVideo = Boolean(media && videoUrl && introComplete);

const transition = (
<div
aria-hidden="true"
className="ling-launch-transition"
data-state={exiting ? "exiting" : "playing"}
data-mode={showVideo ? "video" : media === null ? "canvas" : "loading"}
data-state={exiting ? "exiting" : handoff ? "handoff" : "playing"}
data-handoff={handoff}
data-testid="ling-launch-transition"
onClick={complete}
>
<canvas ref={canvasRef} aria-label="Ling launch animation" />
{showVideo ? (
<div className="ling-launch-video-stage">
<video
aria-hidden="true"
src={videoUrl}
autoPlay
muted
playsInline
onLoadedMetadata={(event) => {
const video = event.currentTarget;
if (!Number.isFinite(video.duration) || video.duration > 5) setMedia(null);
}}
onTimeUpdate={(event) => {
const video = event.currentTarget;
if (Number.isFinite(video.duration) && video.currentTime >= video.duration - 0.55) {
setHandoff(true);
}
}}
onEnded={complete}
onError={() => { setHandoff(false); setMedia(null); }}
/>
</div>
) : media === null ? <canvas ref={canvasRef} aria-hidden="true" /> : null}
{media !== null ? (
<div className="ling-launch-brand" aria-hidden="true">
<span className="ling-launch-brand-emblem">
<img src={`${import.meta.env.BASE_URL}icons/ling-mark.svg`} alt="" />
</span>
<img className="ling-launch-hand" src={`${import.meta.env.BASE_URL}illustrations/launch-hand.svg`} alt="" />
<span className="ling-launch-brand-ripple" />
</div>
) : null}
{media !== null ? (
<button className="ling-launch-skip" type="button" onClick={complete}>
跳过 <kbd>Esc</kbd>
</button>
) : null}
</div>
);

Expand Down
156 changes: 156 additions & 0 deletions apps/web/src/styles/features/launch-transition.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,162 @@
height: 100%;
}

.ling-launch-transition[data-mode="video"],
.ling-launch-transition[data-mode="loading"] {
background: #fff;
}

.ling-launch-transition[data-mode="video"] {
transition: background calc(var(--ling-motion-attention) * 0.7) var(--ling-ease-standard), opacity 220ms ease;
}

@property --ling-launch-reveal {
syntax: "<percentage>";
inherits: false;
initial-value: -15%;
}

.ling-launch-video-stage {
--ling-launch-reveal: -15%;
position: absolute;
inset: 0;
overflow: hidden;
mask-image: radial-gradient(circle at center, transparent var(--ling-launch-reveal), #000 calc(var(--ling-launch-reveal) + 15%));
transition: --ling-launch-reveal calc(var(--ling-motion-attention) * 0.8) var(--ling-ease-enter);
}

.ling-launch-transition video {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}

.ling-launch-transition[data-handoff="true"] {
background: transparent;
}

.ling-launch-transition[data-handoff="true"] .ling-launch-video-stage {
--ling-launch-reveal: 100%;
}

.ling-launch-brand {
position: absolute;
inset: 0;
background: #fff;
pointer-events: none;
}

.ling-launch-brand-emblem {
position: absolute;
top: 47%;
left: 50%;
display: grid;
place-items: center;
width: clamp(76px, 8vw, 100px);
aspect-ratio: 1;
border-radius: 22%;
background: #1c2030;
box-shadow: 0 18px 40px rgb(37 34 70 / 14%);
transform: translate(-50%, -50%);
animation: ling-launch-tap calc(var(--ling-motion-slow) * 3) var(--ling-ease-standard) both;
}

.ling-launch-brand-emblem img {
width: 78%;
height: 78%;
}

.ling-launch-hand {
position: absolute;
top: calc(47% + 8px);
left: calc(50% - 28px);
width: clamp(210px, 22vw, 300px);
height: auto;
transform-origin: 25% 18%;
animation: ling-launch-hand-tap calc(var(--ling-motion-slow) * 3) var(--ling-ease-standard) both;
}

.ling-launch-brand-ripple {
position: absolute;
top: 47%;
left: 50%;
width: 110px;
aspect-ratio: 1;
border: 2px solid #a48af4;
border-radius: 50%;
transform: translate(-50%, -50%) scale(0.4);
animation: ling-launch-ripple calc(var(--ling-motion-slow) * 3) var(--ling-ease-exit) both;
}

.ling-launch-transition[data-mode="video"] .ling-launch-brand {
animation: ling-launch-reveal calc(var(--ling-motion-attention) / 2) var(--ling-ease-enter) forwards;
}

.ling-launch-skip {
position: absolute;
top: 24px;
right: 28px;
display: inline-flex;
align-items: center;
gap: 10px;
padding: 8px 12px;
border: 1px solid rgb(32 36 54 / 18%);
border-radius: 8px;
background: rgb(255 255 255 / 78%);
color: #24283a;
font: inherit;
font-size: 13px;
font-weight: 500;
cursor: pointer;
backdrop-filter: blur(8px);
}

.ling-launch-skip kbd {
font: inherit;
opacity: 0.72;
}

.ling-launch-skip:hover,
.ling-launch-skip:focus-visible {
background: #fff;
}

.ling-launch-transition[data-mode="video"] .ling-launch-skip {
border-color: rgb(255 255 255 / 32%);
background: rgb(16 20 30 / 50%);
color: #fff;
}

.ling-launch-transition[data-mode="video"] .ling-launch-skip:hover,
.ling-launch-transition[data-mode="video"] .ling-launch-skip:focus-visible {
background: rgb(16 20 30 / 76%);
}

@keyframes ling-launch-reveal {
to { opacity: 0; visibility: hidden; }
}

@keyframes ling-launch-hand-tap {
0% { transform: translate(110px, 105px) rotate(9deg); opacity: 0; }
25% { opacity: 1; }
56% { transform: translate(2px, 2px) rotate(0); }
68% { transform: translate(-7px, -5px) rotate(-3deg); }
100% { transform: translate(0, 0) rotate(0); }
}

@keyframes ling-launch-tap {
0%, 56% { transform: translate(-50%, -50%) scale(1); }
68% { transform: translate(-50%, -50%) scale(0.9); }
100% { transform: translate(-50%, -50%) scale(1); }
}

@keyframes ling-launch-ripple {
0%, 60% { opacity: 0; transform: translate(-50%, -50%) scale(0.4); }
68% { opacity: 0.65; }
100% { opacity: 0; transform: translate(-50%, -50%) scale(2.3); }
}

@media (prefers-reduced-motion: reduce) {
.ling-launch-transition {
transition-duration: 180ms;
Expand Down
Loading
Loading