Skip to content

Commit

Permalink
解决由于发给main-1窗口的open-image事件被main-0窗口接受到,导致main-0窗口已经隐藏的工具条被重新显示的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
FengZhongShaoNian committed Jun 11, 2024
1 parent d7f880d commit 784db0c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src-tauri/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#[derive(Clone, serde::Deserialize, Debug)]
pub struct PageLoadedEvent {
pub struct PageLoadedEventPayload {
/// label of sender window
pub send_from: String,
}

#[derive(Clone, serde::Serialize, Debug)]
pub struct OpenImageEventPayload {
/// label of receiver window
pub send_to: String,

/// path of image to open
pub image_path: String,
}
10 changes: 7 additions & 3 deletions src-tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use log::{info};
use tauri::{AppHandle, LogicalSize, Manager, PhysicalSize, Window};
use crate::events;
use crate::events::OpenImageEventPayload;
use crate::image_io::get_image_size;

// A window counter whose value increments by 1 each time a window is created
Expand Down Expand Up @@ -64,11 +65,14 @@ fn create_main_window_with_initial_window_size(handle: &AppHandle, image_path: S
handle.listen_global("page-loaded", move |event| {
let payload = event.payload();
if let Some(data) = payload {
let page_loaded_event: events::PageLoadedEvent = serde_json::from_str(data).unwrap();
if page_loaded_event.send_from == window_label {
let page_loaded_event_payload: events::PageLoadedEventPayload = serde_json::from_str(data).unwrap();
if page_loaded_event_payload.send_from == window_label {
info!("[{window_label}] receive page-loaded event");

main_window.emit("open-image", image_path.clone()).unwrap()
main_window.emit("open-image", OpenImageEventPayload {
send_to: window_label.clone(),
image_path: image_path.clone()
}).unwrap();
}
}
});
Expand Down
16 changes: 14 additions & 2 deletions src/pages/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,23 @@ async function saveImageFile(base64EncodedImage: string){
return null;
}

interface OpenImageEventPayload{
// label of receiver window
send_to: string,

// path of image to open
image_path: string,
}

document.addEventListener("DOMContentLoaded", async function () {
await logger.info('DOMContentLoaded').catch(console.error);

await listen<string>('open-image', async (event: Event<string>) => {
const imagePath = event.payload;
await listen<OpenImageEventPayload>('open-image', async (event: Event<OpenImageEventPayload>) => {
const openImageEventPayload = event.payload;
if(openImageEventPayload.send_to != appWindow.label){
return;
}
const imagePath = openImageEventPayload.image_path;
await logger.info('received imagePath: ' + imagePath);

await openImage(imagePath);
Expand Down

0 comments on commit 784db0c

Please sign in to comment.