Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

2.0 KB
Raw Blame
import { leavePictureInPicture } from "./use-picture-in-picture";

export type FullscreenVideo = HTMLVideoElement & {
  webkitEnterFullscreen?: () => void;
  webkitExitFullscreen?: () => void;
  webkitEnterFullScreen?: () => void;
  webkitExitFullScreen?: () => void;
  webkitSupportsFullscreen?: boolean;
  webkitDisplayingFullscreen?: boolean;
  webkitPresentationMode?: string;
};

export function videoFullscreenState(video: FullscreenVideo) {
  const screen = video.parentElement;
  const pageSupported = Boolean(document.fullscreenEnabled && screen?.requestFullscreen);
  const nativeSupported = typeof (video.webkitEnterFullscreen ?? video.webkitEnterFullScreen) === "function";
  const nativeActive = Boolean(video.webkitDisplayingFullscreen || video.webkitPresentationMode === "fullscreen");
  return {
    supported: pageSupported || nativeSupported,
    // Native players need not expose Safari's optional readiness property.
    ready: pageSupported || (nativeSupported && video.readyState >= 1 && video.webkitSupportsFullscreen !== false),
    active: Boolean(screen && document.fullscreenElement === screen) || nativeActive,
    nativeActive,
  };
}

// Request in the original click stack: fullscreen consumes user activation.
// Native events, rather than a successful method call, confirm Safari entry.
export async function toggleVideoFullscreen(video: FullscreenVideo): Promise<void> {
  const screen = video.parentElement;
  const state = videoFullscreenState(video);
  if (screen && document.fullscreenElement === screen) {
    await document.exitFullscreen();
  } else if (state.nativeActive) {
    (video.webkitExitFullscreen ?? video.webkitExitFullScreen)?.call(video);
  } else if (state.ready) {
    if (document.fullscreenEnabled && screen?.requestFullscreen) {
      await screen.requestFullscreen();
      await leavePictureInPicture(video).catch(() => undefined);
    } else {
      (video.webkitEnterFullscreen ?? video.webkitEnterFullScreen)?.call(video);
    }
  }
}