Skip to content

Commit

Permalink
Add play controls to Ruffle taskbar peek
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinBrett committed Dec 9, 2024
1 parent 80d4241 commit f0f57cf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions components/apps/Ruffle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ type BaseLoadOptions = {
autoplay?: "auto" | "off" | "on";
backgroundColor?: string | null;
letterbox?: "fullscreen" | "off" | "on";
menu?: boolean;
unmuteOverlay?: "hidden";
};

Expand All @@ -16,7 +17,10 @@ type DataLoadOptions = {
};

export type RufflePlayer = HTMLElement & {
isPlaying: () => boolean;
load: (options: BaseLoadOptions & DataLoadOptions) => Promise<void>;
pause: () => void;
play: () => void;
};

type SourceAPI = {
Expand Down
29 changes: 25 additions & 4 deletions components/apps/Ruffle/useRuffle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { basename, extname } from "path";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { type RufflePlayer } from "components/apps/Ruffle/types";
import { type ContainerHookProps } from "components/system/Apps/AppContainer";
import useTitle from "components/system/Window/useTitle";
Expand All @@ -13,9 +13,13 @@ const useRuffle = ({
setLoading,
url,
}: ContainerHookProps): void => {
const { linkElement, processes: { [id]: { libs = [] } = {} } = {} } =
useProcesses();
const {
argument,
linkElement,
processes: { [id]: { libs = [] } = {} } = {},
} = useProcesses();
const [player, setPlayer] = useState<RufflePlayer>();
const eventTarget = useRef(new EventTarget());
const { appendFileToTitle } = useTitle(id);
const { readFile } = useFileSystem();
const loadFlash = useCallback(async () => {
Expand All @@ -40,6 +44,7 @@ const useRuffle = ({
autoplay: "on",
backgroundColor: "#000000",
letterbox: "on",
menu: false,
polyfills: false,
preloader: false,
unmuteOverlay: "hidden",
Expand All @@ -59,10 +64,26 @@ const useRuffle = ({
if (containerRef.current && player) {
containerRef.current.append(player);
linkElement(id, "peekElement", player);
argument(id, "play", () => {
player.play();
eventTarget.current.dispatchEvent(new Event("play"));
});
argument(id, "pause", () => {
player.pause();
eventTarget.current.dispatchEvent(new Event("pause"));
});
argument(id, "paused", (callback?: (paused: boolean) => void) => {
if (callback) {
eventTarget.current.addEventListener("pause", () => callback(true));
eventTarget.current.addEventListener("play", () => callback(false));
}

return !player.isPlaying;
});
}

return () => player?.remove();
}, [containerRef, id, linkElement, player]);
}, [argument, containerRef, id, linkElement, player]);

useEffect(() => {
if (containerRef.current && player && url) loadFlash();
Expand Down

0 comments on commit f0f57cf

Please sign in to comment.