Skip to content

Commit 0a0d596

Browse files
committed
fix: 修复获取监视器序号错误问题
1 parent 497244b commit 0a0d596

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

src-tauri/src/screenshot.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -481,22 +481,17 @@ pub async fn get_monitor_id_from_point(x: i32, y: i32) -> Result<u32, String> {
481481
return Ok(target_id);
482482
}
483483

484-
// 兼容前端传入"物理像素坐标"的情况:
485-
// xcap monitor 的 x/y/width/height 是逻辑坐标,乘以 scale_factor 后按物理坐标匹配一次。
484+
// 二次 fallback:直接用逻辑坐标与 monitor 的逻辑边界匹配
485+
// (Monitor::from_point 在某些平台/版本可能失败,此处手动遍历)
486486
for monitor in monitors.iter() {
487487
let mx = monitor.x().unwrap_or_default() as f64;
488488
let my = monitor.y().unwrap_or_default() as f64;
489489
let mw = monitor.width().unwrap_or_default() as f64;
490490
let mh = monitor.height().unwrap_or_default() as f64;
491-
let scale = monitor.scale_factor().unwrap_or(1.0) as f64;
492-
493-
let px = x as f64;
494-
let py = y as f64;
495-
let sx = mx * scale;
496-
let sy = my * scale;
497-
let sw = mw * scale;
498-
let sh = mh * scale;
499-
let hit = px >= sx && px < sx + sw && py >= sy && py < sy + sh;
491+
492+
let lx = x as f64;
493+
let ly = y as f64;
494+
let hit = lx >= mx && lx < mx + mw && ly >= my && ly < my + mh;
500495
if hit {
501496
let id = monitor.id().map_err(|e| format!("获取显示器ID失败: {}", e))?;
502497
return Ok(id);

src/utils/screenshot-trigger.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { invoke } from "@tauri-apps/api/core";
2+
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
23
import { cursorPosition } from "@tauri-apps/api/window";
34
import {
45
logPerf,
@@ -26,8 +27,14 @@ const createScreenshotRequestId = (): string =>
2627

2728
const resolveMonitorIdFromCursor = async (): Promise<number> => {
2829
const cursor = await cursorPosition();
29-
const x = Math.round(cursor.x);
30-
const y = Math.round(cursor.y);
30+
31+
// cursorPosition() 返回物理像素坐标,需转换为逻辑坐标
32+
// 以匹配 Rust 端 Monitor::from_point 使用的逻辑坐标系
33+
const appWindow = getCurrentWebviewWindow();
34+
const scaleFactor = await appWindow.scaleFactor();
35+
const logical = cursor.toLogical(scaleFactor);
36+
const x = Math.round(logical.x);
37+
const y = Math.round(logical.y);
3138

3239
try {
3340
return await invoke<number>("get_monitor_id_from_point", { x, y });

0 commit comments

Comments
 (0)