Skip to content

Commit

Permalink
Track mouseup event outside of canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed May 21, 2024
1 parent 0767d91 commit 3bc7978
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,31 +352,36 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
},
)?;

runner_ref.add_event_listener(&canvas, "mouseup", |event: web_sys::MouseEvent, runner| {
let modifiers = modifiers_from_mouse_event(&event);
runner.input.raw.modifiers = modifiers;
if let Some(button) = button_from_mouse_event(&event) {
let pos = pos_from_mouse_event(runner.canvas(), &event, runner.egui_ctx());
let modifiers = runner.input.raw.modifiers;
runner.input.raw.events.push(egui::Event::PointerButton {
pos,
button,
pressed: false,
modifiers,
});
// Same with mouseup: we want to notice if a drag stops outside the canvas
runner_ref.add_event_listener(
&document,
"mouseup",
|event: web_sys::MouseEvent, runner| {
let modifiers = modifiers_from_mouse_event(&event);
runner.input.raw.modifiers = modifiers;
if let Some(button) = button_from_mouse_event(&event) {
let pos = pos_from_mouse_event(runner.canvas(), &event, runner.egui_ctx());
let modifiers = runner.input.raw.modifiers;
runner.input.raw.events.push(egui::Event::PointerButton {
pos,
button,
pressed: false,
modifiers,
});

// In Safari we are only allowed to write to the clipboard during the
// event callback, which is why we run the app logic here and now:
runner.logic();
// In Safari we are only allowed to write to the clipboard during the
// event callback, which is why we run the app logic here and now:
runner.logic();

// Make sure we paint the output of the above logic call asap:
runner.needs_repaint.repaint_asap();
// Make sure we paint the output of the above logic call asap:
runner.needs_repaint.repaint_asap();

text_agent::update_text_agent(runner);
}
event.stop_propagation();
event.prevent_default();
})?;
text_agent::update_text_agent(runner);
}
event.stop_propagation();
event.prevent_default();
},
)?;

runner_ref.add_event_listener(
&canvas,
Expand Down

0 comments on commit 3bc7978

Please sign in to comment.