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

Add docs about how to get more fine-grained control of event.preventDefault() calls on web #3451

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ddfisher
Copy link

@ddfisher ddfisher commented Feb 2, 2024

The example is a bit verbose, unfortunately, but I'm hoping it's relatively clear and it's also quite close to being directly useful.

See #2831 for more context.

Copy link
Member

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this is great!

Let me know if you need any help with anything.

@@ -65,8 +65,76 @@ pub trait WindowExtWebSys {
/// For example, by default using the mouse wheel would cause the page to scroll, enabling this
/// would prevent that.
///
/// Specifically, this will call `event.preventDefault()` for the following event types:
/// `touchstart`, `wheel`, `contextmenu`, `pointerdown`, `pointermove`, `keyup`, and `keydown`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should mention that Event.preventDefault() is only called for pointermove in the case of a chorded button interaction.

I know this is getting even more into the weeds, but it might be important ...

src/platform/web.rs Outdated Show resolved Hide resolved
/// This example calls `event.preventDefault()` for all relevant events except for ctrl-c,
/// ctrl-x, and ctrl-v.
/// ```
/// # fn add_prevent_default_listeners(canvas: HtmlCanvasElement) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to actually make a full example with EventLoop::new() and such.
Will be adding cross-compilation testing for rustdoc into the CI soon.

Doesn't need much, just create a Window, use WindowBuilder::with_append() and get the canvas.

src/platform/web.rs Outdated Show resolved Hide resolved
/// # use wasm_bindgen::closure::Closure;
/// # use wasm_bindgen::JsCast;
/// let events_types_to_fully_prevent_default =
/// ["touchstart", "wheel", "contextmenu", "pointermove"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we are it, we might want to handle the chorded button interaction for pointermove.

Comment on lines +79 to +80
/// This example calls `event.preventDefault()` for all relevant events except for ctrl-c,
/// ctrl-x, and ctrl-v.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should pick a different example because I consider the whole preventing clipboard interactions a Winit bug.

E.g. ctrl-p for printing? Maybe also hook up beforeprint just to make the example complete.

ddfisher and others added 2 commits April 5, 2024 15:49
Co-authored-by: daxpedda <daxpedda@gmail.com>
Co-authored-by: daxpedda <daxpedda@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants