Skip to content

Commit 075eba9

Browse files
committed
fix: Screenshot Size Error on Wayland
#278 #195
1 parent 2bad6d5 commit 075eba9

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src-tauri/src/utils.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ pub fn get_translate_text(state: tauri::State<StringWrapper>) -> String {
2727
}
2828

2929
#[tauri::command]
30-
pub fn screenshot(x: i32, y: i32) {
30+
pub fn screenshot(x: i32, y: i32) -> u32 {
3131
use crate::APP;
3232
use dirs::cache_dir;
3333
use screenshots::Screen;
3434
use std::fs;
3535

3636
let screens = Screen::all().unwrap();
37+
let mut width = 0u32;
3738
for screen in screens {
3839
let info = screen.display_info;
3940
if info.x == x && info.y == y {
@@ -49,9 +50,11 @@ pub fn screenshot(x: i32, y: i32) {
4950
let image = screen.capture().unwrap();
5051
let buffer = image.to_png().unwrap();
5152
fs::write(app_cache_dir_path, buffer).unwrap();
53+
width = image.width();
5254
break;
5355
}
5456
}
57+
width
5558
}
5659

5760
#[tauri::command]

src/windows/Screenshot/index.jsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ export default function Screenshot() {
1313
const [mouseDownY, setMouseDownY] = useState(0);
1414
const [mouseMoveX, setMouseMoveX] = useState(0);
1515
const [mouseMoveY, setMouseMoveY] = useState(0);
16+
const [imgWidth, setImgWidth] = useState(0);
1617

1718
useEffect(() => {
1819
currentMonitor().then((monitor) => {
19-
const size = monitor.size;
2020
const position = monitor.position;
21-
invoke('screenshot', { x: position.x, y: position.y }).then((_) => {
21+
invoke('screenshot', { x: position.x, y: position.y }).then((width) => {
22+
//真实图片宽度
23+
setImgWidth(width);
2224
appCacheDir().then((appCacheDirPath) => {
2325
join(appCacheDirPath, 'pot_screenshot.png').then((filePath) => {
2426
setImgurl(convertFileSrc(filePath));
@@ -100,8 +102,7 @@ export default function Screenshot() {
100102
appWindow.hide();
101103
setIsDown(false);
102104
setIsMoved(false);
103-
const monitor = await currentMonitor();
104-
const dpi = monitor.size.width / screen.width;
105+
const dpi = imgWidth / screen.width;
105106
// const dpi = monitor.scaleFactor; //这是系统的dpi,不一定是网页内容的dpi
106107
const left = Math.floor(Math.min(mouseDownX, e.clientX) * dpi);
107108
const top = Math.floor(Math.min(mouseDownY, e.clientY) * dpi);
@@ -113,7 +114,6 @@ export default function Screenshot() {
113114
} else {
114115
await emit('translate');
115116
}
116-
117117
await appWindow.close();
118118
}}
119119
/>

0 commit comments

Comments
 (0)