Skip to content

Commit

Permalink
Properly remove window mouse event listeners (#2632)
Browse files Browse the repository at this point in the history
* Properly remove window mouse event listeners

* Update CHANGELOG.md

* Fix formatting

Co-authored-by: Mads Marquart <[email protected]>
  • Loading branch information
DouglasDwyer and madsmtm authored Jan 21, 2023
1 parent 809162f commit b711a11
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ features = [
'DomRect',
'Element',
'Event',
"EventListenerOptions",
'EventTarget',
'FocusEvent',
'HtmlCanvasElement',
Expand Down
8 changes: 6 additions & 2 deletions src/platform_impl/web/web_sys/event_handle.rs
Original file line number Diff line number Diff line change
@@ -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<T: ?Sized> {
target: EventTarget,
event_type: &'static str,
listener: Closure<T>,
options: EventListenerOptions,
}

impl<T: ?Sized> EventListenerHandle<T> {
Expand All @@ -20,6 +21,7 @@ impl<T: ?Sized> EventListenerHandle<T> {
target,
event_type,
listener,
options: EventListenerOptions::new(),
}
}

Expand All @@ -44,16 +46,18 @@ impl<T: ?Sized> EventListenerHandle<T> {
target,
event_type,
listener,
options: options.clone().unchecked_into(),
}
}
}

impl<T: ?Sized> Drop for EventListenerHandle<T> {
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(
Expand Down

0 comments on commit b711a11

Please sign in to comment.