bug(hotkey): apply and clear shortcuts when nav

This commit is contained in:
AkiChase 2024-04-18 11:03:19 +08:00
parent fde4f7ef56
commit 6eb2589f48
2 changed files with 31 additions and 30 deletions

View File

@ -4,7 +4,8 @@ import { NDialog } from "naive-ui";
import { useGlobalStore } from "../store/global";
import { onBeforeRouteLeave, useRouter } from "vue-router";
import {
initShortcuts,
applyShortcuts,
clearShortcuts,
listenToKeyEvent,
unlistenToKeyEvent,
updateScreenSizeAndMaskArea,
@ -16,39 +17,29 @@ const maskRef = ref<HTMLElement | null>(null);
const store = useGlobalStore();
const router = useRouter();
let isShortcutInited = false;
onBeforeRouteLeave(() => {
if (isShortcutInited) {
if (maskRef.value) {
unlistenToKeyEvent();
}
if (maskRef.value && store.controledDevice) {
unlistenToKeyEvent();
clearShortcuts();
}
});
onActivated(async () => {
if (isShortcutInited) {
if (maskRef.value) {
listenToKeyEvent();
}
return;
}
if (store.controledDevice) {
if (maskRef.value) {
const mt = 30;
const ml = 70;
const appWindow = getCurrent();
const size = (await appWindow.outerSize()).toLogical(
await appWindow.scaleFactor()
);
updateScreenSizeAndMaskArea(
[store.screenSizeW, store.screenSizeH],
[size.width - ml, size.height - mt]
);
initShortcuts(maskRef.value);
listenToKeyEvent();
isShortcutInited = true;
}
// TODO storeio
if (maskRef.value && store.controledDevice) {
const mt = 30;
const ml = 70;
const appWindow = getCurrent();
const size = (await appWindow.outerSize()).toLogical(
await appWindow.scaleFactor()
);
updateScreenSizeAndMaskArea(
[store.screenSizeW, store.screenSizeH],
[size.width - ml, size.height - mt]
);
applyShortcuts(maskRef.value);
listenToKeyEvent();
}
});

View File

@ -857,6 +857,14 @@ export function unlistenToKeyEvent() {
loopFlag = false;
}
export function clearShortcuts() {
downKeyMap.clear();
downKeyCBMap.clear();
loopDownKeyCBMap.clear();
upKeyCBMap.clear();
cancelAbleKeyList.length = 0;
}
export function updateScreenSizeAndMaskArea(
screenSize: [number, number],
maskArea: [number, number]
@ -867,10 +875,12 @@ export function updateScreenSizeAndMaskArea(
maskSizeH = maskArea[1];
}
export function initShortcuts(element: HTMLElement) {
export function applyShortcuts(element: HTMLElement) {
element.addEventListener("mousedown", handleMouseDown);
element.addEventListener("mousemove", handleMouseMove);
element.addEventListener("mouseup", handleMouseUp);
// TODO 使用setCursorGrab相关来限制移出而不是使用下面的方法
// TODO 任何down的时候都要限制移出
element.addEventListener("mouseout", handleMouseUp); // mouse out of the element as mouse up
// 读取按键配置文件时获取