{
// The shadow's and the controls' fades end here too.
if (event.target === event.currentTarget) {
switch (motion?.name) {
case "growing": {
setMotion(undefined);
break;
}
case "returning": {
act(motion.ending);
break;
}
case "leaving": {
onGone(id);
break;
}
case "waiting":
case undefined: {
break;
}
}
}
}}
onPointerEnter={() => {
setHovered(true);
}}
onPointerLeave={() => {
setHovered(false);
}}
// Inline because the box is a runtime value Panda cannot extract.
style={{
...motionStyle(offsetOf(motion)),
blockSize: `${box.height.toString()}px`,
inlineSize: `${box.width.toString()}px`,
insetBlockStart: `${box.y.toString()}px`,
insetInlineStart: `${box.x.toString()}px`,
}}
>
{
follow(event, (dx, dy) => movedBox(box, workspace, dx, dy));
}}
>
{
end("back-to-page");
}}
>
{
end("close");
}}
>
{video.canPreviousTrack && (
{
act("previous-track");
}}
>
)}
{video.canPlay &&
(video.playing ? (
{
act("pause");
}}
>
) : (
{
act("play");
}}
>
))}
{video.canNextTrack && (
{
act("next-track");
}}
>
)}
{video.duration > 0 && (
{GRIPS.map(({ grip, name }) => (
{
follow(event, (dx, dy) =>
resizedBox(box, workspace, video, grip, dx, dy),
);
}}
/>
))}
);
};
/**
* Whether a press on `target` landed on one of the float's buttons or its
* progress bar.
*/
const onControl = (target: EventTarget): boolean =>
target instanceof Element &&
target.closest("button, [data-picture-in-picture-progress]") !== null;
/** How often the time moves on while it shows. */
const TICK_MS = 1000;
/**
* The video's time and how far it is, moved on by the shell's clock. Moving
* the bar moves the video once it is let go.
*/
const Progress = ({
onSeek,
ticking,
video,
}: {
onSeek: (seconds: number) => void;
ticking: boolean;
video: DomicilePictureInPictureVideo;
}) => {
const { duration, playbackRate, playing, position } = video;
// When the page last said where it is.
const heard = useMemo(
() => ({
at: performance.now(),
duration,
playing,
position,
rate: playbackRate,
}),
[duration, playbackRate, playing, position],
);
const [now, setNow] = useState(() => performance.now());
useEffect(() => {
if (ticking && playing) {
const tick = setInterval(() => {
setNow(performance.now());
}, TICK_MS);
return () => {
clearInterval(tick);
};
} else {
return undefined;
}
}, [playing, ticking]);
// Where the bar is held, while it is.
const [held, setHeld] = useState
(undefined);
const at = held ?? playheadAt(heard, Math.max(now, heard.at));
return (
<>
{`${clockOf(at)} / ${clockOf(duration)}`}
{
setHeld(undefined);
onSeek(seconds);
}}
value={at}
/>
>
);
};
// Over every window, as Chrome's picture-in-picture window is, and under the
// panels with the toasts, which come later in the document. The shell's float
// border and shadow, with no title bar: the video is the window.
//
// Hidden until its video is drawn. It grows out of its place in its page and
// shrinks back into it, about its top left, by the offset `motionStyle` sets.
// Long moves, so `emphasized`. Returning holds its last frame until the page
// takes the video back; then it fades there over the page's own video, which
// can take a frame or two to draw again.
const floatStyles = css({
"&[data-motion=growing]": {
animationDuration: "token(durations.slow)",
animationName: "pictureInPictureGrowing",
animationTimingFunction: "token(easings.emphasized)",
},
"&[data-motion=leaving], &[data-motion=leaving-page]": {
animationDuration: "token(durations.fast)",
animationFillMode: "forwards",
animationName: "pictureInPictureLeaving",
animationTimingFunction: "token(easings.out)",
pointerEvents: "none",
},
"&[data-motion=returning]": {
animationDuration: "token(durations.slow)",
animationFillMode: "forwards",
animationName: "pictureInPictureReturning",
animationTimingFunction: "token(easings.emphasized)",
},
"&[data-motion=waiting]": { opacity: 0 },
position: "fixed",
transformOrigin: "top left",
zIndex: "toast",
});
// A layer of its own, outside the clip, so it fades in as the video grows out
// of its page and out as it shrinks back.
const shadowStyles = css({
"[data-motion=growing] > &": {
animationDuration: "token(durations.slow)",
animationName: "pictureInPictureShadowIn",
animationTimingFunction: "token(easings.emphasized)",
},
// Faded out as it shrank back into its page.
"[data-motion=leaving-page] > &": { opacity: 0 },
"[data-motion=returning] > &": {
animationDuration: "token(durations.slow)",
animationFillMode: "forwards",
animationName: "pictureInPictureShadowOut",
animationTimingFunction: "token(easings.emphasized)",
},
borderRadius: "lg",
boxShadow: "lifted",
inset: 0,
position: "absolute",
});
const clipStyles = css({
border: "1px solid {colors.border}",
borderRadius: "lg",
inset: 0,
overflow: "hidden",
position: "absolute",
});
const videoStyles = css({
blockSize: "100%",
display: "block",
inlineSize: "100%",
});
// Hidden until the pointer is over the video. Light lettering, as on the
// wallpaper, since a video can be any color.
const controlsStyles = cva({
base: {
color: "onPhoto",
cursor: "move",
inset: 0,
position: "absolute",
textShadow: "textOverPhoto",
transition: "opacity {durations.fast} {easings.default}",
},
variants: {
shown: {
false: { opacity: 0 },
true: { opacity: 1 },
},
},
});
// A band darkening the top and the bottom of the video, under the controls.
const scrimStyles = cva({
base: {
backgroundImage: "{gradients.scrimOverPhoto}",
blockSize: 12,
insetInline: 0,
position: "absolute",
},
variants: {
edge: {
bottom: { insetBlockEnd: 0, transform: "scaleY(-1)" },
top: { insetBlockStart: 0 },
},
},
});
const cornerStyles = cva({
base: { insetBlockStart: 2, position: "absolute" },
variants: {
side: {
end: { insetInlineEnd: 2 },
start: { insetInlineStart: 2 },
},
},
});
// Only as big as its buttons, so it covers neither the corners' buttons nor
// the progress bar.
const middleStyles = css({
alignItems: "center",
display: "flex",
gap: 2.5,
insetBlockStart: "50%",
insetInlineStart: "50%",
position: "absolute",
translate: "-50% -50%",
});
const clockStyles = css({
fontSize: "xs",
fontVariantNumeric: "tabular-nums",
insetBlockEnd: 5,
insetInlineStart: 2,
position: "absolute",
});
// Clear of the bottom edge's resize grip.
const progressStyles = css({
display: "flex",
insetBlockEnd: 1.5,
insetInline: 2,
position: "absolute",
});
// Edges grab 1.5 (6px) into the float, and corners 2 (8px) each way: short
// of the corner buttons, which start 2 in.
const gripStyles = cva({
base: { position: "absolute" },
variants: {
edge: {
bottom: {
blockSize: 1.5,
cursor: "ns-resize",
insetBlockEnd: 0,
insetInline: 0,
},
"bottom-left": {
blockSize: 2,
cursor: "nesw-resize",
inlineSize: 2,
insetBlockEnd: 0,
insetInlineStart: 0,
},
"bottom-right": {
blockSize: 2,
cursor: "nwse-resize",
inlineSize: 2,
insetBlockEnd: 0,
insetInlineEnd: 0,
},
left: {
cursor: "ew-resize",
inlineSize: 1.5,
insetBlock: 0,
insetInlineStart: 0,
},
right: {
cursor: "ew-resize",
inlineSize: 1.5,
insetBlock: 0,
insetInlineEnd: 0,
},
top: {
blockSize: 1.5,
cursor: "ns-resize",
insetBlockStart: 0,
insetInline: 0,
},
"top-left": {
blockSize: 2,
cursor: "nwse-resize",
inlineSize: 2,
insetBlockStart: 0,
insetInlineStart: 0,
},
"top-right": {
blockSize: 2,
cursor: "nesw-resize",
inlineSize: 2,
insetBlockStart: 0,
insetInlineEnd: 0,
},
},
},
});