Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

touchend listener on document prevents listening for clicks on dom elements (web) #4752

Closed
lucasmerlin opened this issue Jul 1, 2024 · 2 comments · Fixed by #4775
Closed
Labels
bug Something is broken web Related to running Egui on the web

Comments

@lucasmerlin
Copy link
Collaborator

lucasmerlin commented Jul 1, 2024

Describe the bug
Egui is registering a touchend event listener on the document and is calling preventdefault on the event. This prevents any touch interactions with dom elements shown on top of the canvas.

To Reproduce

Paste the following in the console on egui.rs (to create a simple html dialog):

let dialog = document.createElement("dialog");
let button = document.createElement("button");
button.innerHTML = "Hello";
dialog.appendChild(button);
button.onclick = () => alert("button click!!");
document.body.appendChild(dialog);
dialog.show()

Clicking the button now does nothing. (Make sure you enabled the device toolbar and set it to e.g. iPhone SE)

If we forcefully remove the egui touchend listener from document via

document.removeEventListener("touchend", getEventListeners(document).touchend[0].listener)

the button will become clickable.

Expected behavior
Egui shouldn't interfere with other dom elements. Maybe the touchend listener doesn't need preventDefault or it could be made configurable.

Additional context

The listener is added here:

install_touchend(runner_ref, &document)?;

@lucasmerlin lucasmerlin added the bug Something is broken label Jul 1, 2024
@emilk
Copy link
Owner

emilk commented Jul 2, 2024

The reason touchend is installed on document is so that egui is told of drags that start inside the canvas, but stops outside of it.

This should only be a problem if the button overlaps the canvas, due to

runner_ref.add_event_listener(target, "touchend", |event: web_sys::TouchEvent, runner| {
if let Some(pos) = primary_touch_pos(runner, &event) {
if is_interested_in_pointer_event(runner.egui_ctx(), pos) {
// First release mouse to click:

fn is_interested_in_pointer_event(egui_ctx: &egui::Context, pos: egui::Pos2) -> bool {
egui_ctx.input(|i| i.screen_rect().contains(pos) || i.pointer.any_down() || i.any_touches())
}

The i.screen_rect().contains(pos) is what is causing the problem. If it could be replaced with a DOM-aware call ("is the touch above the canvas element?") we should be able to solve this. document.elementFromPoint looks promising for this.

@emilk emilk added the web Related to running Egui on the web label Jul 2, 2024
@emilk emilk added this to the Next Patch Release milestone Jul 3, 2024
@lucasmerlin
Copy link
Collaborator Author

@emilk Makes sense! I'll try to implement this

lucasmerlin added a commit to lucasmerlin/egui that referenced this issue Jul 3, 2024
lucasmerlin added a commit to lucasmerlin/egui that referenced this issue Jul 3, 2024
lucasmerlin added a commit to lucasmerlin/egui that referenced this issue Jul 3, 2024
@emilk emilk closed this as completed in 9a4c462 Jul 5, 2024
hacknus pushed a commit to hacknus/egui that referenced this issue Oct 30, 2024
…_point (emilk#4775)

* Closes emilk#4752 

To test, start the web demo and follow the repro steps from emilk#4752
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken web Related to running Egui on the web
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants