Fix click+dblclick trigger on 1 touch

This commit is contained in:
Dustin Brett 2023-02-28 12:16:13 -08:00
parent 4aad14f294
commit 41b4f1dd37
3 changed files with 13 additions and 18 deletions

View File

@ -12,7 +12,7 @@ import { useSession } from "contexts/session";
import { dirname, join } from "path";
import { useCallback, useEffect } from "react";
import { DESKTOP_PATH, PREVENT_SCROLL } from "utils/constants";
import { haltEvent } from "utils/functions";
import { haltEvent, sendMouseClick } from "utils/functions";
type KeyboardShortcutEntry = (file?: string) => React.KeyboardEventHandler;
@ -122,9 +122,7 @@ const useFileKeyboardShortcuts = (
case "Enter":
if (target instanceof HTMLButtonElement) {
haltEvent(event);
target.dispatchEvent(
new MouseEvent("dblclick", { bubbles: true })
);
sendMouseClick(target, 2);
}
break;
default:

View File

@ -6,19 +6,11 @@ const MAX_MOVES = 5;
const useDoubleClick = (
handler: React.MouseEventHandler,
singleClick = false
): {
onClick: React.MouseEventHandler;
onDoubleClick: React.MouseEventHandler;
} => {
): { onClick: React.MouseEventHandler } => {
const timer = useRef<number | undefined>();
const moveCount = useRef(0);
const onClick: React.MouseEventHandler = useCallback(
(event) => {
const mouseEvent = event.clientX || event.clientY;
const doubleClickEvent = event.type === "dblclick";
if (!mouseEvent && !doubleClickEvent) return;
const runHandler = (): void => {
event.stopPropagation();
handler(event);
@ -45,10 +37,7 @@ const useDoubleClick = (
}
};
if (
(singleClick && !doubleClickEvent) ||
(!mouseEvent && doubleClickEvent)
) {
if (singleClick) {
runHandler();
} else if (timer.current === undefined) {
timer.current = window.setTimeout(
@ -66,7 +55,7 @@ const useDoubleClick = (
[handler, singleClick]
);
return { onClick, onDoubleClick: onClick };
return { onClick };
};
export default useDoubleClick;

View File

@ -36,6 +36,14 @@ export const getDpi = (): number => {
return dpi;
};
export const sendMouseClick = (target: HTMLElement, count = 1): void => {
if (count === 0) return;
target.dispatchEvent(new MouseEvent("click", { bubbles: true }));
sendMouseClick(target, count - 1);
};
export const toggleFullScreen = async (): Promise<void> => {
try {
await (document.fullscreenElement