From 8da161d0b4773a5eec1f756ced9f2567262cf88f Mon Sep 17 00:00:00 2001 From: AkiChase <1003019131@qq.com> Date: Tue, 21 May 2024 11:50:12 +0800 Subject: [PATCH] feat(hotkey): add resize listener --- .github/workflows/publish.yml | 3 +- package.json | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/src/adb.rs | 2 +- src-tauri/tauri.conf.json | 2 +- src/components/Mask.vue | 16 +--- src/hotkey.ts | 140 ++++++++++++++++++---------------- 7 files changed, 80 insertions(+), 87 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 84739e8..159feef 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,5 +1,4 @@ -name: "publish" - +name: "Multi platform compile" on: push: # 匹配特定标签 (refs/tags) diff --git a/package.json b/package.json index f3e9c6b..5834b4a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "scrcpy-mask", "private": true, - "version": "0.3.1", + "version": "0.4.0-rc5", "type": "module", "scripts": { "dev": "vite", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index cf59adf..e074988 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scrcpy-mask" -version = "0.3.1" +version = "0.4.0-rc5" description = "A Tauri App" authors = ["AkiChase"] edition = "2021" diff --git a/src-tauri/src/adb.rs b/src-tauri/src/adb.rs index 6c00e4d..c800913 100644 --- a/src-tauri/src/adb.rs +++ b/src-tauri/src/adb.rs @@ -89,7 +89,7 @@ impl Adb { { let mut cmd = Command::new("adb"); cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW - cmd + return cmd; } Command::new("adb") } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 3f65b13..b38b1e5 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,6 +1,6 @@ { "productName": "scrcpy-mask", - "version": "0.3.1", + "version": "0.4.0-rc5", "identifier": "com.akichase.mask", "build": { "beforeDevCommand": "pnpm dev", diff --git a/src/components/Mask.vue b/src/components/Mask.vue index e46b940..ca9be6f 100644 --- a/src/components/Mask.vue +++ b/src/components/Mask.vue @@ -8,7 +8,6 @@ import { clearShortcuts, listenToEvent, unlistenToEvent, - updateScreenSizeAndMaskArea, } from "../hotkey"; import { KeyMappingConfig, KeySteeringWheel } from "../keyMappingConfig"; import { getVersion } from "@tauri-apps/api/app"; @@ -44,15 +43,11 @@ onActivated(async () => { const maskElement = document.getElementById("maskElement") as HTMLElement; if (store.controledDevice) { - updateScreenSizeAndMaskArea( - [store.screenSizeW, store.screenSizeH], - [maskElement.clientWidth, maskElement.clientHeight] - ); - if ( applyShortcuts( maskElement, store.keyMappingConfigList[store.curKeyMappingIndex], + store, message, t ) @@ -84,15 +79,6 @@ async function checkAdb() { async function loadLocalStore() { const localStore = new Store("store.bin"); - // loading screenSize from local store - const screenSize = await localStore.get<{ sizeW: number; sizeH: number }>( - "screenSize" - ); - if (screenSize !== null) { - store.screenSizeW = screenSize.sizeW; - store.screenSizeH = screenSize.sizeH; - } - // loading keyMappingConfigList from local store let keyMappingConfigList = await localStore.get( "keyMappingConfigList" diff --git a/src/hotkey.ts b/src/hotkey.ts index db7e4c8..2387c0d 100644 --- a/src/hotkey.ts +++ b/src/hotkey.ts @@ -24,17 +24,18 @@ import { import { useGlobalStore } from "./store/global"; import { LogicalPosition, getCurrent } from "@tauri-apps/api/window"; import { useI18n } from "vue-i18n"; +import { UnlistenFn } from "@tauri-apps/api/event"; function clientxToPosx(clientx: number) { return clientx < 70 ? 0 - : Math.floor((clientx - 70) * (screenSizeW / maskSizeW)); + : Math.floor((clientx - 70) * (store.screenSizeW / maskSizeW)); } function clientyToPosy(clienty: number) { return clienty < 30 ? 0 - : Math.floor((clienty - 30) * (screenSizeH / maskSizeH)); + : Math.floor((clienty - 30) * (store.screenSizeH / maskSizeH)); } function clientxToPosOffsetx(clientx: number, posx: number, scale = 1) { @@ -51,7 +52,7 @@ function clientPosToSkillOffset( clientPos: { x: number; y: number }, range: number ): { offsetX: number; offsetY: number } { - const maxLength = (120 / maskSizeH) * screenSizeH; + const maxLength = (120 / maskSizeH) * store.screenSizeH; const centerX = maskSizeW * 0.5; const centerY = maskSizeH * 0.5; @@ -95,7 +96,7 @@ function calculateMacroPosX( relativeSizeW: number ): number { if (typeof posX === "number") { - return Math.round(posX * (screenSizeW / relativeSizeW)); + return Math.round(posX * (store.screenSizeW / relativeSizeW)); } if (typeof posX === "string") { return clientxToPosx(mouseX); @@ -103,7 +104,7 @@ function calculateMacroPosX( if (posX[0] === "mouse") { return ( clientxToPosx(mouseX) + - Math.round(posX[1] * (screenSizeW / relativeSizeW)) + Math.round(posX[1] * (store.screenSizeW / relativeSizeW)) ); } else { throw new Error("Invalid pos"); @@ -117,7 +118,7 @@ function calculateMacroPosY( relativeSizeH: number ): number { if (typeof posY === "number") { - return Math.round(posY * (screenSizeH / relativeSizeH)); + return Math.round(posY * (store.screenSizeH / relativeSizeH)); } if (typeof posY === "string") { return clientyToPosy(mouseY); @@ -125,7 +126,7 @@ function calculateMacroPosY( if (posY[0] === "mouse") { return ( clientyToPosy(mouseY) + - Math.round(posY[1] * (screenSizeH / relativeSizeH)) + Math.round(posY[1] * (store.screenSizeH / relativeSizeH)) ); } else { throw new Error("Invalid pos"); @@ -157,8 +158,8 @@ function addObservationShortcuts( ) { let observationMouseX = 0; let observationMouseY = 0; - posX = Math.round((posX / relativeSize.w) * screenSizeW); - posY = Math.round((posY / relativeSize.h) * screenSizeH); + posX = Math.round((posX / relativeSize.w) * store.screenSizeW); + posY = Math.round((posY / relativeSize.h) * store.screenSizeH); addShortcut( key, async () => { @@ -195,8 +196,8 @@ function addTapShortcuts( posY: number, pointerId: number ) { - posX = Math.round((posX / relativeSize.w) * screenSizeW); - posY = Math.round((posY / relativeSize.h) * screenSizeH); + posX = Math.round((posX / relativeSize.w) * store.screenSizeW); + posY = Math.round((posY / relativeSize.h) * store.screenSizeH); addShortcut( key, async () => { @@ -216,8 +217,8 @@ function addCancelSkillShortcuts( posY: number, pointerId: number ) { - posX = Math.round((posX / relativeSize.w) * screenSizeW); - posY = Math.round((posY / relativeSize.h) * screenSizeH); + posX = Math.round((posX / relativeSize.w) * store.screenSizeW); + posY = Math.round((posY / relativeSize.h) * store.screenSizeH); addShortcut( key, async () => { @@ -259,8 +260,8 @@ function addTriggerWhenPressedSkillShortcuts( rangeOrTime: number, pointerId: number ) { - posX = Math.round((posX / relativeSize.w) * screenSizeW); - posY = Math.round((posY / relativeSize.h) * screenSizeH); + posX = Math.round((posX / relativeSize.w) * store.screenSizeW); + posY = Math.round((posY / relativeSize.h) * store.screenSizeH); if (directional) { addShortcut( key, @@ -276,8 +277,8 @@ function addTriggerWhenPressedSkillShortcuts( action: SwipeAction.Default, pointerId, screen: { - w: screenSizeW, - h: screenSizeH, + w: store.screenSizeW, + h: store.screenSizeH, }, pos: [ { x: posX, y: posY }, @@ -316,8 +317,8 @@ function addTriggerWhenDoublePressedSkillShortcuts( range: number, pointerId: number ) { - posX = Math.round((posX / relativeSize.w) * screenSizeW); - posY = Math.round((posY / relativeSize.h) * screenSizeH); + posX = Math.round((posX / relativeSize.w) * store.screenSizeW); + posY = Math.round((posY / relativeSize.h) * store.screenSizeH); doublePressedDownKey.set(key, false); addShortcut( key, @@ -333,8 +334,8 @@ function addTriggerWhenDoublePressedSkillShortcuts( action: SwipeAction.NoUp, pointerId, screen: { - w: screenSizeW, - h: screenSizeH, + w: store.screenSizeW, + h: store.screenSizeH, }, pos: [ { x: posX, y: posY }, @@ -392,8 +393,8 @@ function addDirectionlessSkillShortcuts( posY: number, pointerId: number ) { - posX = Math.round((posX / relativeSize.w) * screenSizeW); - posY = Math.round((posY / relativeSize.h) * screenSizeH); + posX = Math.round((posX / relativeSize.w) * store.screenSizeW); + posY = Math.round((posY / relativeSize.h) * store.screenSizeH); addShortcut( key, // down @@ -410,8 +411,8 @@ function addDirectionlessSkillShortcuts( action: TouchAction.Up, pointerId, screen: { - w: screenSizeW, - h: screenSizeH, + w: store.screenSizeW, + h: store.screenSizeH, }, pos: { x: posX, @@ -442,8 +443,8 @@ function addDirectionalSkillShortcuts( range: number, pointerId: number ) { - posX = Math.round((posX / relativeSize.w) * screenSizeW); - posY = Math.round((posY / relativeSize.h) * screenSizeH); + posX = Math.round((posX / relativeSize.w) * store.screenSizeW); + posY = Math.round((posY / relativeSize.h) * store.screenSizeH); addShortcut( key, // down @@ -458,8 +459,8 @@ function addDirectionalSkillShortcuts( action: SwipeAction.NoUp, pointerId, screen: { - w: screenSizeW, - h: screenSizeH, + w: store.screenSizeW, + h: store.screenSizeH, }, pos: [ { x: posX, y: posY }, @@ -514,8 +515,8 @@ function addSteeringWheelKeyboardShortcuts( let loopFlag = false; let curPosX = 0; let curPosY = 0; - posX = Math.round((posX / relativeSize.w) * screenSizeW); - posY = Math.round((posY / relativeSize.h) * screenSizeH); + posX = Math.round((posX / relativeSize.w) * store.screenSizeW); + posY = Math.round((posY / relativeSize.h) * store.screenSizeH); // calculate the end coordinates of the eight directions of the direction wheel let offsetHalf = Math.round(offset / 1.414); @@ -657,18 +658,18 @@ function addSightShortcuts( const sightClientX = 70 + sightKeyMapping.posX; const sightClientY = 30 + sightKeyMapping.posY; const sightDeviceX = Math.round( - (sightKeyMapping.posX / relativeSize.w) * screenSizeW + (sightKeyMapping.posX / relativeSize.w) * store.screenSizeW ); const sightDeviceY = Math.round( - (sightKeyMapping.posY / relativeSize.h) * screenSizeH + (sightKeyMapping.posY / relativeSize.h) * store.screenSizeH ); const fireDeviceX = fireKeyMapping - ? Math.round((fireKeyMapping.posX / relativeSize.w) * screenSizeW) + ? Math.round((fireKeyMapping.posX / relativeSize.w) * store.screenSizeW) : 0; const fireDeviceY = fireKeyMapping - ? Math.round((fireKeyMapping.posY / relativeSize.h) * screenSizeH) + ? Math.round((fireKeyMapping.posY / relativeSize.h) * store.screenSizeH) : 0; const removeShortcut = (key: string) => { @@ -715,7 +716,7 @@ function addSightShortcuts( fireDeviceX + accOffsetX + clientxToPosOffsetx(mouseX, sightDeviceX, fireKeyMapping.scaleX), - fireDeviceY + + fireDeviceY + accOffsetY + clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY) ); @@ -892,7 +893,7 @@ function addSightShortcuts( sightDeviceX, fireKeyMapping.scaleX ), - fireDeviceY + + fireDeviceY + clientyToPosOffsety(mouseY, sightDeviceY, fireKeyMapping.scaleY) ); // touch down sight @@ -917,22 +918,6 @@ function addSightShortcuts( }); } -let screenSizeW: number; -let screenSizeH: number; -let maskSizeW: number; -let maskSizeH: number; -let mouseX = 0; -let mouseY = 0; -let maskElement: HTMLElement; -let message: ReturnType; -let t: ReturnType["t"]; - -const downKeyMap: Map = new Map(); -const downKeyCBMap: Map Promise> = new Map(); -const loopDownKeyCBMap: Map Promise> = new Map(); -const upKeyCBMap: Map Promise> = new Map(); -const cancelAbleKeyList: string[] = []; - function handleKeydown(event: KeyboardEvent) { event.preventDefault(); if (event.repeat) return; @@ -1166,8 +1151,8 @@ async function execMacro( action: swipeAction, pointerId: cmd.args[1], screen: { - w: screenSizeW, - h: screenSizeH, + w: store.screenSizeW, + h: store.screenSizeH, }, pos: calculateMacroPosList(cmd.args[2], relativeSize), intervalBetweenPos: cmd.args[3], @@ -1356,8 +1341,8 @@ async function touchX( action, pointerId, screen: { - w: screenSizeW, - h: screenSizeH, + w: store.screenSizeW, + h: store.screenSizeH, }, pos: { x: posX, @@ -1395,28 +1380,51 @@ export function clearShortcuts() { loopDownKeyCBMap.clear(); upKeyCBMap.clear(); cancelAbleKeyList.length = 0; -} -export function updateScreenSizeAndMaskArea( - screenSize: [number, number], - maskArea: [number, number] -) { - screenSizeW = screenSize[0]; - screenSizeH = screenSize[1]; - maskSizeW = maskArea[0]; - maskSizeH = maskArea[1]; + // unlisten to resize + unlistenResize(); } export function applyShortcuts( element: HTMLElement, keyMappingConfig: KeyMappingConfig, + globalStore: ReturnType, messageAPI: ReturnType, i18nT: ReturnType["t"] ) { + store = globalStore; maskElement = element; message = messageAPI; t = i18nT; + + maskSizeW = maskElement.clientWidth; + maskSizeH = maskElement.clientHeight; + // listen to resize to update mask size + getCurrent() + .onResized(() => { + maskSizeW = maskElement.clientWidth; + maskSizeH = maskElement.clientHeight; + }) + .then((f) => (unlistenResize = f)); + addClickShortcuts("M0", 0); return applyKeyMappingConfigShortcuts(keyMappingConfig); } + +let maskSizeW: number; +let maskSizeH: number; +let mouseX = 0; +let mouseY = 0; +let store: ReturnType; +let maskElement: HTMLElement; +let message: ReturnType; +let t: ReturnType["t"]; + +let unlistenResize: UnlistenFn; + +const downKeyMap: Map = new Map(); +const downKeyCBMap: Map Promise> = new Map(); +const loopDownKeyCBMap: Map Promise> = new Map(); +const upKeyCBMap: Map Promise> = new Map(); +const cancelAbleKeyList: string[] = [];