Skip to content

Commit

Permalink
[AudioPlayer] Catch 2-finger mousepad tap as right click (#2943)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Feb 12, 2024
1 parent c2635ba commit 0ce9cd0
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/components/Pronunciations/AudioPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
} from "@mui/material";
import {
CSSProperties,
MouseEvent,
ReactElement,
TouchEvent,
useCallback,
useEffect,
useState,
Expand Down Expand Up @@ -124,12 +126,14 @@ export default function AudioPlayer(props: PlayerProps): ReactElement {
document.removeEventListener("contextmenu", preventEventOnce, false);
}

function handleTouch(event: any): void {
/** If audio can be deleted or speaker changed, a touchscreen press should open an
* options menu instead of the context menu. */
function handleTouch(e: TouchEvent<HTMLButtonElement>): void {
if (canChangeSpeaker || canDeleteAudio) {
// Temporarily disable context menu since some browsers
// interpret a long-press touch as a right-click.
disableContextMenu();
setAnchor(event.currentTarget);
setAnchor(e.currentTarget);
}
}

Expand All @@ -140,14 +144,22 @@ export default function AudioPlayer(props: PlayerProps): ReactElement {
setSpeakerDialog(false);
}

/** If speaker can be changed, a right click should open the speaker menu instead of
* the context menu. */
function handleOnAuxClick(): void {
if (canChangeSpeaker) {
// Temporarily disable context menu triggered by right-click.
disableContextMenu();
setSpeakerDialog(true);
}
}

/** Catch a multi-finger mousepad tap as a right click. */
function handleOnMouseDown(e: MouseEvent<HTMLButtonElement>): void {
if (e.buttons > 1) {
handleOnAuxClick();
}
}

const tooltipTexts = [t("pronunciations.playTooltip")];
if (canDeleteAudio) {
tooltipTexts.push(t("pronunciations.deleteTooltip"));
Expand Down Expand Up @@ -176,6 +188,7 @@ export default function AudioPlayer(props: PlayerProps): ReactElement {
tabIndex={-1}
onAuxClick={handleOnAuxClick}
onClick={deleteOrTogglePlay}
onMouseDown={handleOnMouseDown}
onTouchStart={handleTouch}
onTouchEnd={enableContextMenu}
aria-label="play"
Expand Down

0 comments on commit 0ce9cd0

Please sign in to comment.