Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

8.0 KB
Raw Blame
// Polyfill for HTML5 Fullscreen API where the webview has none of its own:
// Linux, and a Mac only if its engine reports none (Wry turns WebKit's on for
// every macOS it supports).
// Bridges the standard requestFullscreen / exitFullscreen DOM API to Tauri's
// native window fullscreen so a player's own button works.
//
// Split out from component.js so a future CLI flag (or custom.js override)
// can short-circuit the polyfill for apps that don't need video fullscreen.
(function () {
  if (window.__PAKE_FULLSCREEN_POLYFILL__) return;

  // WebKit on macOS has an element fullscreen of its own, which Wry turns on,
  // and a player needs the real one: YouTube asks for the whole document and
  // then lays its player out by what only the engine can report, so under the
  // polyfill the video stayed a box in a full-screen page and the button
  // could not leave. Read the engine's answer before anything here replaces it.
  function engineHasFullscreen() {
    if (!/^Mac/.test(navigator.platform || "")) return false;
    return (
      document.fullscreenEnabled === true ||
      document.webkitFullscreenEnabled === true
    );
  }
  if (engineHasFullscreen()) return;

  window.__PAKE_FULLSCREEN_POLYFILL__ = true;

  function initFullscreenPolyfill() {
    if (!window.__PAKE_NATIVE__ || !document.head) {
      setTimeout(initFullscreenPolyfill, 100);
      return;
    }

    const appWindow = window.__PAKE_NATIVE__.window.getCurrentWindow();
    let fullscreenElement = null;
    let actualFullscreenElement = null;
    let originalStyles = null;
    let originalParent = null;
    let originalNextSibling = null;
    let wasInBody = false;
    let monitorId = null;

    if (!document.getElementById("pake-fullscreen-style")) {
      const css = `
      body.pake-fullscreen-active {
        overflow: hidden !important;
      }
      .pake-fullscreen-element {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        max-width: 100vw !important;
        max-height: 100vh !important;
        margin: 0 !important;
        padding: 0 !important;
        z-index: 2147483647 !important;
        background: #000 !important;
        object-fit: contain !important;
      }
      .pake-fullscreen-element:not(html):not(body) video {
        width: 100% !important;
        height: 100% !important;
        object-fit: contain !important;
      }
    `;
      if (typeof window.__PAKE_INJECT_STYLE__ === "function") {
        window.__PAKE_INJECT_STYLE__(css, "pake-fullscreen-style");
      } else {
        const styleEl = document.createElement("style");
        styleEl.id = "pake-fullscreen-style";
        styleEl.textContent = css;
        document.head.appendChild(styleEl);
      }
    }

    function startFullscreenMonitor() {
      if (monitorId) return;
      monitorId = setInterval(() => {
        appWindow
          .isFullscreen()
          .then((isFullscreen) => {
            if (fullscreenElement && !isFullscreen) {
              exitFullscreen();
            }
          })
          .catch(() => {});
      }, 500);
    }

    function stopFullscreenMonitor() {
      if (!monitorId) return;
      clearInterval(monitorId);
      monitorId = null;
    }

    function enterFullscreen(element) {
      // Commit DOM changes only after native fullscreen succeeds. An ACL
      // rejection must leave the original player and its controls untouched.
      return appWindow.setFullscreen(true).then(() => {
        fullscreenElement = element;
        // Preserve the requested subtree. Extracting a video from a document
        // fullscreen request separates it from its controls and overlays.
        const targetElement = element;
        actualFullscreenElement = element;

        originalStyles = {
          position: targetElement.style.position,
          top: targetElement.style.top,
          left: targetElement.style.left,
          width: targetElement.style.width,
          height: targetElement.style.height,
          maxWidth: targetElement.style.maxWidth,
          maxHeight: targetElement.style.maxHeight,
          margin: targetElement.style.margin,
          padding: targetElement.style.padding,
          zIndex: targetElement.style.zIndex,
          background: targetElement.style.background,
          objectFit: targetElement.style.objectFit,
        };

        wasInBody =
          targetElement === document.documentElement ||
          targetElement === document.body ||
          targetElement.parentNode === document.body;
        if (!wasInBody) {
          originalParent = targetElement.parentNode;
          originalNextSibling = targetElement.nextSibling;
        }

        targetElement.classList.add("pake-fullscreen-element");
        document.body.classList.add("pake-fullscreen-active");

        if (!wasInBody) {
          document.body.appendChild(targetElement);
        }

        startFullscreenMonitor();
        const event = new Event("fullscreenchange", { bubbles: true });
        document.dispatchEvent(event);
        element.dispatchEvent(event);

        const webkitEvent = new Event("webkitfullscreenchange", {
          bubbles: true,
        });
        document.dispatchEvent(webkitEvent);
        element.dispatchEvent(webkitEvent);
      });
    }

    function exitFullscreen() {
      if (!fullscreenElement) {
        return Promise.resolve();
      }

      stopFullscreenMonitor();

      const exitingElement = fullscreenElement;
      const targetElement = actualFullscreenElement;

      targetElement.classList.remove("pake-fullscreen-element");
      document.body.classList.remove("pake-fullscreen-active");

      if (originalStyles) {
        Object.keys(originalStyles).forEach((key) => {
          targetElement.style[key] = originalStyles[key];
        });
      }

      if (!wasInBody && originalParent) {
        if (
          originalNextSibling &&
          originalNextSibling.parentNode === originalParent
        ) {
          originalParent.insertBefore(targetElement, originalNextSibling);
        } else if (originalParent.isConnected) {
          originalParent.appendChild(targetElement);
        }
      }

      fullscreenElement = null;
      actualFullscreenElement = null;
      originalStyles = null;
      originalParent = null;
      originalNextSibling = null;
      wasInBody = false;

      return appWindow.setFullscreen(false).then(() => {
        const event = new Event("fullscreenchange", { bubbles: true });
        document.dispatchEvent(event);
        exitingElement.dispatchEvent(event);

        const webkitEvent = new Event("webkitfullscreenchange", {
          bubbles: true,
        });
        document.dispatchEvent(webkitEvent);
        exitingElement.dispatchEvent(webkitEvent);
      });
    }

    Object.defineProperty(document, "fullscreenEnabled", {
      get: () => true,
      configurable: true,
    });
    Object.defineProperty(document, "webkitFullscreenEnabled", {
      get: () => true,
      configurable: true,
    });

    Object.defineProperty(document, "fullscreenElement", {
      get: () => fullscreenElement,
      configurable: true,
    });
    Object.defineProperty(document, "webkitFullscreenElement", {
      get: () => fullscreenElement,
      configurable: true,
    });
    Object.defineProperty(document, "webkitCurrentFullScreenElement", {
      get: () => fullscreenElement,
      configurable: true,
    });

    Element.prototype.requestFullscreen = function () {
      return enterFullscreen(this);
    };
    Element.prototype.webkitRequestFullscreen = function () {
      return enterFullscreen(this);
    };
    Element.prototype.webkitRequestFullScreen = function () {
      return enterFullscreen(this);
    };

    document.exitFullscreen = exitFullscreen;
    document.webkitExitFullscreen = exitFullscreen;
    document.webkitCancelFullScreen = exitFullscreen;

    document.addEventListener(
      "keydown",
      (e) => {
        if (e.key === "Escape" && fullscreenElement) {
          exitFullscreen();
        }
      },
      true,
    );
  }

  initFullscreenPolyfill();
})();