Skip to content

Commit db251fd

Browse files
authored
refactor: 优化了获取鼠标所在显示器的逻辑 (#569) (#571)
1 parent 13bf0a8 commit db251fd

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

src/composables/useDevice.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { CursorPoint } from '@/utils/monitor'
2+
13
import { invoke } from '@tauri-apps/api/core'
24
import { isEqual, mapValues } from 'es-toolkit'
35
import { ref } from 'vue'
@@ -15,14 +17,9 @@ interface MouseButtonEvent {
1517
value: string
1618
}
1719

18-
interface MouseMoveValue {
19-
x: number
20-
y: number
21-
}
22-
2320
interface MouseMoveEvent {
2421
kind: 'MouseMove'
25-
value: MouseMoveValue
22+
value: CursorPoint
2623
}
2724

2825
interface KeyboardEvent {
@@ -34,7 +31,7 @@ type DeviceEvent = MouseButtonEvent | MouseMoveEvent | KeyboardEvent
3431

3532
export function useDevice() {
3633
const modelStore = useModelStore()
37-
const lastMousePoint = ref<MouseMoveValue>({ x: 0, y: 0 })
34+
const lastCursorPoint = ref<CursorPoint>({ x: 0, y: 0 })
3835
const releaseTimers = new Map<string, NodeJS.Timeout>()
3936
const { handlePress, handleRelease, handleMouseChange, handleMouseMove } = useModel()
4037

@@ -75,14 +72,14 @@ export function useDevice() {
7572
releaseTimers.set(key, timer)
7673
}
7774

78-
const processMouseMove = (value: MouseMoveValue) => {
79-
const roundedValue = mapValues(value, Math.round)
75+
const processMouseMove = (point: CursorPoint) => {
76+
const roundedValue = mapValues(point, Math.round)
8077

81-
if (isEqual(lastMousePoint.value, roundedValue)) return
78+
if (isEqual(lastCursorPoint.value, roundedValue)) return
8279

83-
lastMousePoint.value = roundedValue
80+
lastCursorPoint.value = roundedValue
8481

85-
return handleMouseMove()
82+
return handleMouseMove(point)
8683
}
8784

8885
useTauriListen<DeviceEvent>(LISTEN_KEY.DEVICE_CHANGED, ({ payload }) => {

src/composables/useModel.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { CursorPoint } from '@/utils/monitor'
2+
13
import { LogicalSize } from '@tauri-apps/api/dpi'
24
import { resolveResource, sep } from '@tauri-apps/api/path'
35
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
@@ -105,8 +107,8 @@ export function useModel() {
105107
live2d.setParameterValue(id, pressed)
106108
}
107109

108-
async function handleMouseMove() {
109-
const monitor = await getCursorMonitor()
110+
async function handleMouseMove(point: CursorPoint) {
111+
const monitor = await getCursorMonitor(point)
110112

111113
if (!monitor) return
112114

src/utils/monitor.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
2-
import { cursorPosition, monitorFromPoint } from '@tauri-apps/api/window'
2+
import { monitorFromPoint } from '@tauri-apps/api/window'
3+
import { mapValues } from 'es-toolkit'
34

4-
export async function getCursorMonitor() {
5-
const appWindow = getCurrentWebviewWindow()
5+
import { isMac } from './platform'
66

7-
const scaleFactor = await appWindow.scaleFactor()
7+
export interface CursorPoint {
8+
x: number
9+
y: number
10+
}
11+
12+
export async function getCursorMonitor(point: CursorPoint) {
13+
let cursorPoint = point
14+
15+
if (isMac) {
16+
const appWindow = getCurrentWebviewWindow()
817

9-
const cursorPoint = await cursorPosition()
18+
const scaleFactor = await appWindow.scaleFactor()
19+
20+
cursorPoint = mapValues(cursorPoint, value => value * scaleFactor)
21+
}
1022

11-
const { x, y } = cursorPoint.toLogical(scaleFactor)
23+
const { x, y } = point
1224

1325
const monitor = await monitorFromPoint(x, y)
1426

0 commit comments

Comments
 (0)