Skip to content

Commit

Permalink
fix: use screen action canceled --close #10
Browse files Browse the repository at this point in the history
  • Loading branch information
JunaYa committed Dec 10, 2024
1 parent ca2f1a6 commit e26fc81
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 46 deletions.
60 changes: 42 additions & 18 deletions src-tauri/src/global_shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ pub fn tauri_plugin_global_shortcut() -> TauriPlugin<tauri::Wry> {
&app,
"images".to_string(),
));
window::hide_main_window(&app);
let window = window::show_preview_window(&app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", filename).unwrap();
});
match filename {
Ok(name) => {
if name == "NoExist" {
return;
}
window::hide_main_window(&app);
let window = window::show_preview_window(&app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", name).unwrap();
});
}
Err(_) => return
}
}
ShortcutState::Released => {
info!("Capture Screen Released!");
Expand All @@ -67,12 +75,20 @@ pub fn tauri_plugin_global_shortcut() -> TauriPlugin<tauri::Wry> {
&app,
"images".to_string(),
));
window::hide_main_window(app);
let window = window::show_preview_window(app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", filename).unwrap();
});
match filename {
Ok(name) => {
if name == "NoExist" {
return;
}
window::hide_main_window(&app);
let window = window::show_preview_window(&app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", name).unwrap();
});
}
Err(_) => return
}
}
ShortcutState::Released => {
info!("Capture Select Released!");
Expand All @@ -85,12 +101,20 @@ pub fn tauri_plugin_global_shortcut() -> TauriPlugin<tauri::Wry> {
&app,
"images".to_string(),
));
window::hide_main_window(app);
let window = window::show_preview_window(app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", filename).unwrap();
});
match filename {
Ok(name) => {
if name == "NoExist" {
return;
}
window::hide_main_window(&app);
let window = window::show_preview_window(&app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", name).unwrap();
});
}
Err(_) => return
}
}
ShortcutState::Released => {
info!("Capture Window Released!");
Expand Down
62 changes: 43 additions & 19 deletions src-tauri/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,39 +169,63 @@ fn handle_tray_menu_events(app: &AppHandle, event: MenuEvent) {
&app,
"images".to_string(),
));
window::hide_main_window(app);
let window = window::show_preview_window(app);
// notify preview window payload
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", filename).unwrap();
});
match filename {
Ok(name) => {
if name == "NoExist" {
return;
}
window::hide_main_window(app);
let window = window::show_preview_window(app);
// notify preview window payload
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", name).unwrap();
});
}
Err(_) => return
}
}
MenuID::CAPTURE_SELECT => {
info!("Capture Select");
let filename = tauri::async_runtime::block_on(platform::capture_select(
&app,
"images".to_string(),
));
window::hide_main_window(app);
let window = window::show_preview_window(app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", filename).unwrap();
});
match filename {
Ok(name) => {
if name == "NoExist" {
return;
}
window::hide_main_window(app);
let window = window::show_preview_window(app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", name).unwrap();
});
}
Err(_) => return
}
}
MenuID::CAPTURE_WINDOW => {
info!("Capture Window");
let filename = tauri::async_runtime::block_on(platform::capture_window(
&app,
"images".to_string(),
));
window::hide_main_window(app);
let window = window::show_preview_window(app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", filename).unwrap();
});
match filename {
Ok(name) => {
if name == "NoExist" {
return;
}
window::hide_main_window(app);
let window = window::show_preview_window(app);
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(500));
window.emit("image-prepared", name).unwrap();
});
}
Err(_) => return
}
}
MenuID::SHOW_MAIN_WINDOW => {
info!("Show Home");
Expand Down
14 changes: 7 additions & 7 deletions src-tauri/src/platform/mac/screenshot.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{
process::Command,
thread,
time::{Duration, Instant},
path::Path, process::Command, thread, time::{Duration, Instant}
};

use chrono::Local;
Expand Down Expand Up @@ -47,6 +45,12 @@ pub async fn capture_select(app_handle: &tauri::AppHandle, path: String) -> Resu
.output()
.map_err(|e| e.to_string())?;

// check file exist
if !Path::new(&output_path).exists() {
info!("no exist: {:?}", output_path);
return Err("NoExist".to_string());
}

info!("capture_select 运行耗时: {:?}", start.elapsed());
Ok(filename)
}
Expand Down Expand Up @@ -83,10 +87,6 @@ pub async fn capture_window(app_handle: &tauri::AppHandle, path: String) -> Resu
.output()
.map_err(|e| e.to_string())?;

if !output.status.success() {
info!("screencapture -wx 失败: {:?}", output.status);
}

info!("capture_window 运行耗时: {:?}", start.elapsed());

Ok(filename)
Expand Down
3 changes: 1 addition & 2 deletions src/pages/preview/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script setup lang="ts">
import { PinturaEditor } from '@pqina/vue-pintura'
import { invoke } from '@tauri-apps/api/core'
import { getCurrentWindow } from '@tauri-apps/api/window'
import { readFile } from '@tauri-apps/plugin-fs'
Expand Down Expand Up @@ -52,7 +51,7 @@ function onSave() {
onMounted(async () => {
const val = await store.get<{ value: string }>('screenshot_path')
appWindow.listen<string>('image-prepared', (event: any) => {
imagePath.value = `${val?.value}/images/${event.payload.Ok}`
imagePath.value = `${val?.value}/images/${event.payload}`
})
})
</script>
Expand Down

0 comments on commit e26fc81

Please sign in to comment.