From f0b116714ba4144c9e0943cc1e6e5ce5162e4759 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 4 May 2022 16:52:45 +0100 Subject: [PATCH 1/3] Fix mouseup/down behaviour of PTT button Handle mouseup events anywhere so the button releases if you move the cursor out of the button & release. Likewsie for window losing focus. --- src/room/PTTButton.jsx | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/room/PTTButton.jsx b/src/room/PTTButton.jsx index 6da9b863..02f90571 100644 --- a/src/room/PTTButton.jsx +++ b/src/room/PTTButton.jsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useCallback, useEffect, useState } from "react"; import classNames from "classnames"; import styles from "./PTTButton.module.css"; import { ReactComponent as MicIcon } from "../icons/Mic.svg"; @@ -14,14 +14,39 @@ export function PTTButton({ startTalking, stopTalking, }) { + const [isHeld, setHeld] = useState(false); + const onDocumentMouseUp = useCallback(() => { + if (isHeld) stopTalking(); + setHeld(false); + }, [isHeld, setHeld]); + + const onWindowBlur = useCallback(() => { + if (isHeld) stopTalking(); + setHeld(false); + }, [isHeld, setHeld]); + + const onButtonMouseDown = useCallback(() => { + setHeld(true); + startTalking(); + }, [setHeld]); + + useEffect(() => { + window.addEventListener("mouseup", onDocumentMouseUp); + window.addEventListener("blur", onWindowBlur); + + return () => { + window.removeEventListener("mouseup", onDocumentMouseUp); + window.removeEventListener("blur", onWindowBlur); + }; + }, [onDocumentMouseUp, onWindowBlur]); + return (