Skip to content

Commit

Permalink
Don't subscribe to "resize" event, and explain why
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 23, 2025
1 parent 90f7d78 commit 12a769e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,26 +361,28 @@ fn install_copy_cut_paste(runner_ref: &WebRunner, target: &EventTarget) -> Resul
Ok(())
}

fn install_window_events(web_runner: &WebRunner, window: &EventTarget) -> Result<(), JsValue> {
fn install_window_events(runner_ref: &WebRunner, window: &EventTarget) -> Result<(), JsValue> {
// Save-on-close
web_runner.add_event_listener(window, "onbeforeunload", |_: web_sys::Event, runner| {
runner_ref.add_event_listener(window, "onbeforeunload", |_: web_sys::Event, runner| {
runner.save();
})?;

// We want to handle the case of dragging the browser from one monitor to another,
// which can cause the DPR to change without any resize event (e.g. Safari).
install_dpr_change_event(web_runner)?;
install_dpr_change_event(runner_ref)?;

for event_name in &["load", "pagehide", "pageshow", "resize"] {
web_runner.add_event_listener(window, event_name, move |_: web_sys::Event, runner| {
// No need to subscribe to "resize": we already subscribe to the canvas
// size using a ResizeObserver, and we also subscribe to DPR changes of the monitor.
for event_name in &["load", "pagehide", "pageshow"] {
runner_ref.add_event_listener(window, event_name, move |_: web_sys::Event, runner| {
if DEBUG_RESIZE {
log::debug!("{event_name:?}");
}
runner.needs_repaint.repaint_asap();
})?;
}

web_runner.add_event_listener(window, "hashchange", |_: web_sys::Event, runner| {
runner_ref.add_event_listener(window, "hashchange", |_: web_sys::Event, runner| {
// `epi::Frame::info(&self)` clones `epi::IntegrationInfo`, but we need to modify the original here
runner.frame.info.web_info.location.hash = location_hash();
runner.needs_repaint.repaint_asap(); // tell the user about the new hash
Expand Down

0 comments on commit 12a769e

Please sign in to comment.