From 1c0be718e7a0e963a05669197a4ae95b98f32878 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Sun, 24 Mar 2024 07:31:51 +0800 Subject: [PATCH] feat: improve screenshot speed --- src-tauri/Cargo.lock | 12 ++++++------ src-tauri/src/window.rs | 28 +++++++++++++++++----------- src/window/Screenshot/index.jsx | 11 +++-------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 88e10a2d1d..6f54871781 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -30,9 +30,9 @@ dependencies = [ [[package]] name = "ahash" -version = "0.8.6" +version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91429305e9f0a25f6205c5b8e0d2db09e0708a7a6df0f42212bb56c32c8ac97a" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if", "const-random", @@ -7237,18 +7237,18 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.7.26" +version = "0.7.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e97e415490559a91254a2979b4829267a57d2fcd741a98eee8b722fb57289aa0" +checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.26" +version = "0.7.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd7e48ccf166952882ca8bd778a43502c64f33bf94c12ebe2a7f08e5a0f6689f" +checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", diff --git a/src-tauri/src/window.rs b/src-tauri/src/window.rs index 98a12d2f4d..71383de160 100644 --- a/src-tauri/src/window.rs +++ b/src-tauri/src/window.rs @@ -1,12 +1,10 @@ -use crate::config::get; -use crate::config::set; -use crate::StringWrapper; -use crate::APP; +use crate::{ + config::{get, set}, + screenshot, StringWrapper, APP, +}; use log::{info, warn}; -use tauri::Manager; -use tauri::Monitor; -use tauri::Window; -use tauri::WindowBuilder; +use mouse_position::mouse_position::{Mouse, Position}; +use tauri::{Manager, Monitor, Window, WindowBuilder}; #[cfg(any(target_os = "macos", target_os = "windows"))] use window_shadows::set_shadow; @@ -56,8 +54,6 @@ fn get_current_monitor(x: i32, y: i32) -> Monitor { // Creating a window on the mouse monitor fn build_window(label: &str, title: &str) -> (Window, bool) { - use mouse_position::mouse_position::{Mouse, Position}; - let mouse_position = match Mouse::get_mouse_position() { Mouse::Position { x, y } => Position { x, y }, Mouse::Error => { @@ -120,7 +116,6 @@ pub fn config_window() { } fn translate_window() -> Window { - use mouse_position::mouse_position::{Mouse, Position}; // Mouse physical position let mut mouse_position = match Mouse::get_mouse_position() { Mouse::Position { x, y } => Position { x, y }, @@ -337,6 +332,17 @@ pub fn ocr_recognize() { }); } pub fn ocr_translate() { + let mouse_position = match Mouse::get_mouse_position() { + Mouse::Position { x, y } => Position { x, y }, + Mouse::Error => { + warn!("Mouse position not found, using (0, 0) as default"); + Position { x: 0, y: 0 } + } + }; + let current_monitor = get_current_monitor(mouse_position.x, mouse_position.y); + let position = current_monitor.position(); + screenshot(position.x, position.y); + let window = screenshot_window(); let window_ = window.clone(); window.listen("success", move |event| { diff --git a/src/window/Screenshot/index.jsx b/src/window/Screenshot/index.jsx index 23302a4ad5..a354eaf3c4 100644 --- a/src/window/Screenshot/index.jsx +++ b/src/window/Screenshot/index.jsx @@ -19,14 +19,9 @@ export default function Screenshot() { const imgRef = useRef(); useEffect(() => { - currentMonitor().then((monitor) => { - const position = monitor.position; - invoke('screenshot', { x: position.x, y: position.y }).then(() => { - appCacheDir().then((appCacheDirPath) => { - join(appCacheDirPath, 'pot_screenshot.png').then((filePath) => { - setImgurl(convertFileSrc(filePath)); - }); - }); + appCacheDir().then((appCacheDirPath) => { + join(appCacheDirPath, 'pot_screenshot.png').then((filePath) => { + setImgurl(convertFileSrc(filePath)); }); }); }, []);