From b711a115492cdddf9830b614d638211dccd6fe65 Mon Sep 17 00:00:00 2001 From: Douglas Dwyer Date: Sat, 21 Jan 2023 10:58:05 -0500 Subject: [PATCH] Properly remove window mouse event listeners (#2632) * Properly remove window mouse event listeners * Update CHANGELOG.md * Fix formatting Co-authored-by: Mads Marquart --- CHANGELOG.md | 1 + Cargo.toml | 1 + src/platform_impl/web/web_sys/event_handle.rs | 8 ++++++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08338e90..7e7468dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ And please only add new entries to the top of this list, right below the `# Unre - Added `Window::set_transparent` to provide a hint about transparency of the window on Wayland and macOS. - On macOS, fix the mouse buttons other than left/right/middle being reported as middle. - On Wayland, support fractional scaling via the wp-fractional-scale protocol. +- On web, fix removal of mouse event listeners from the global object upon window distruction. # 0.27.5 diff --git a/Cargo.toml b/Cargo.toml index 2c223d97..88860eb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -133,6 +133,7 @@ features = [ 'DomRect', 'Element', 'Event', + "EventListenerOptions", 'EventTarget', 'FocusEvent', 'HtmlCanvasElement', diff --git a/src/platform_impl/web/web_sys/event_handle.rs b/src/platform_impl/web/web_sys/event_handle.rs index 08130d75..5a0a3531 100644 --- a/src/platform_impl/web/web_sys/event_handle.rs +++ b/src/platform_impl/web/web_sys/event_handle.rs @@ -1,10 +1,11 @@ use wasm_bindgen::{prelude::Closure, JsCast}; -use web_sys::{AddEventListenerOptions, EventTarget}; +use web_sys::{AddEventListenerOptions, EventListenerOptions, EventTarget}; pub(super) struct EventListenerHandle { target: EventTarget, event_type: &'static str, listener: Closure, + options: EventListenerOptions, } impl EventListenerHandle { @@ -20,6 +21,7 @@ impl EventListenerHandle { target, event_type, listener, + options: EventListenerOptions::new(), } } @@ -44,6 +46,7 @@ impl EventListenerHandle { target, event_type, listener, + options: options.clone().unchecked_into(), } } } @@ -51,9 +54,10 @@ impl EventListenerHandle { impl Drop for EventListenerHandle { fn drop(&mut self) { self.target - .remove_event_listener_with_callback( + .remove_event_listener_with_callback_and_event_listener_options( self.event_type, self.listener.as_ref().unchecked_ref(), + &self.options, ) .unwrap_or_else(|e| { web_sys::console::error_2(