Skip to content

Commit 29a2117

Browse files
committed
feat: add macOS support for screenshot capture using system screencapture cmd
1 parent 753365e commit 29a2117

File tree

1 file changed

+68
-12
lines changed

1 file changed

+68
-12
lines changed

src-tauri/src/window.rs

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use std::fs;
12
use crate::config::get;
23
use crate::config::set;
34
use crate::StringWrapper;
45
use crate::APP;
6+
use dirs::cache_dir;
57
use log::{info, warn};
68
use tauri::Manager;
79
use tauri::Monitor;
@@ -318,6 +320,7 @@ pub fn recognize_window() {
318320
window.emit("new_image", "").unwrap();
319321
}
320322

323+
#[cfg(not(target_os = "macos"))]
321324
fn screenshot_window() -> Window {
322325
let (window, _exists) = build_window("screenshot", "Screenshot");
323326

@@ -338,20 +341,73 @@ fn screenshot_window() -> Window {
338341
}
339342

340343
pub fn ocr_recognize() {
341-
let window = screenshot_window();
342-
let window_ = window.clone();
343-
window.listen("success", move |event| {
344-
recognize_window();
345-
window_.unlisten(event.id())
346-
});
344+
#[cfg(target_os = "macos")]
345+
{
346+
use dirs::cache_dir;
347+
348+
let app_handle = APP.get().unwrap();
349+
let mut app_cache_dir_path = cache_dir().expect("Get Cache Dir Failed");
350+
app_cache_dir_path.push(&app_handle.config().tauri.bundle.identifier);
351+
if !app_cache_dir_path.exists() {
352+
// 创建目录
353+
fs::create_dir_all(&app_cache_dir_path).expect("Create Cache Dir Failed");
354+
}
355+
app_cache_dir_path.push("pot_screenshot_cut.png");
356+
357+
let path = app_cache_dir_path.to_string_lossy().replace("\\\\?\\", "");
358+
println!("Screenshot path: {}", path);
359+
if let Ok(_output) = std::process::Command::new("/usr/sbin/screencapture")
360+
.arg("-i")
361+
.arg("-r")
362+
.arg(path)
363+
.output()
364+
{
365+
recognize_window();
366+
}
367+
}
368+
#[cfg(not(target_os = "macos"))]
369+
{
370+
let window = screenshot_window();
371+
let window_ = window.clone();
372+
window.listen("success", move |event| {
373+
recognize_window();
374+
window_.unlisten(event.id())
375+
});
376+
}
347377
}
348378
pub fn ocr_translate() {
349-
let window = screenshot_window();
350-
let window_ = window.clone();
351-
window.listen("success", move |event| {
352-
image_translate();
353-
window_.unlisten(event.id())
354-
});
379+
#[cfg(target_os = "macos")]
380+
{
381+
let app_handle = APP.get().unwrap();
382+
let mut app_cache_dir_path = cache_dir().expect("Get Cache Dir Failed");
383+
app_cache_dir_path.push(&app_handle.config().tauri.bundle.identifier);
384+
if !app_cache_dir_path.exists() {
385+
// 创建目录
386+
fs::create_dir_all(&app_cache_dir_path).expect("Create Cache Dir Failed");
387+
}
388+
app_cache_dir_path.push("pot_screenshot_cut.png");
389+
390+
let path = app_cache_dir_path.to_string_lossy().replace("\\\\?\\", "");
391+
println!("Screenshot path: {}", path);
392+
if let Ok(_output) = std::process::Command::new("/usr/sbin/screencapture")
393+
.arg("-i")
394+
.arg("-r")
395+
.arg(path)
396+
.output()
397+
{
398+
image_translate();
399+
();
400+
}
401+
}
402+
#[cfg(not(target_os = "macos"))]
403+
{
404+
let window = screenshot_window();
405+
let window_ = window.clone();
406+
window.listen("success", move |event| {
407+
image_translate();
408+
window_.unlisten(event.id())
409+
});
410+
}
355411
}
356412

357413
#[tauri::command(async)]

0 commit comments

Comments
 (0)