From fca976404e6bec373a81332572458c4c44f7bb3a Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 23 Jun 2021 16:20:09 -0300 Subject: [PATCH] fix(wry): window event listeners being emitted to all windows (#2056) --- .changes/fix-window-events.md | 5 +++++ core/tauri-runtime-wry/src/lib.rs | 34 +++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .changes/fix-window-events.md diff --git a/.changes/fix-window-events.md b/.changes/fix-window-events.md new file mode 100644 index 000000000..f09eef5c8 --- /dev/null +++ b/.changes/fix-window-events.md @@ -0,0 +1,5 @@ +--- +"tauri-runtime-wry": patch +--- + +Fixes window event being emitted to all windows listeners. diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 87b0d892f..33f9ef37a 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -84,7 +84,8 @@ type CreateWebviewHandler = Box< dyn FnOnce(&EventLoopWindowTarget, &WebContextStore) -> Result + Send, >; type WindowEventHandler = Box; -type WindowEventListeners = Arc>>; +type WindowEventListenersMap = Arc>>; +type WindowEventListeners = Arc>>; type GlobalShortcutListeners = Arc>>>; macro_rules! dispatcher_getter { @@ -731,6 +732,10 @@ impl Dispatch for WryDispatcher { .window_event_listeners .lock() .unwrap() + .get(&self.window_id) + .unwrap() + .lock() + .unwrap() .insert(id, Box::new(f)); id } @@ -1523,7 +1528,15 @@ fn handle_event_loop( } Event::WindowEvent { event, window_id } => { if let Some(event) = WindowEventWrapper::from(&event).0 { - for handler in window_event_listeners.lock().unwrap().values() { + for handler in window_event_listeners + .lock() + .unwrap() + .get(&window_id) + .unwrap() + .lock() + .unwrap() + .values() + { handler(&event); } } @@ -1625,6 +1638,17 @@ fn handle_event_loop( WindowMessage::Show => window.set_visible(true), WindowMessage::Hide => window.set_visible(false), WindowMessage::Close => { + for handler in window_event_listeners + .lock() + .unwrap() + .get(&window.id()) + .unwrap() + .lock() + .unwrap() + .values() + { + handler(&WindowEvent::CloseRequested); + } on_window_close( callback, id, @@ -1859,6 +1883,12 @@ fn create_webview>( }; let window = window_builder.inner.build(event_loop).unwrap(); + context + .window_event_listeners + .lock() + .unwrap() + .insert(window.id(), WindowEventListenersMap::default()); + #[cfg(feature = "menu")] context .menu_event_listeners