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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/platform/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...

///
/// For fine-grained control over which events call `event.preventDefault()`, call
/// `Window::set_prevent_default(false)` and add event listeners on the canvas that
/// selectively call `event.preventDefault()`.
///
/// Some events are impossible to prevent. E.g. Firefox allows to access the native browser
/// context menu with Shift+Rightclick.
///
/// # Example
/// This example calls `event.preventDefault()` for all relevant events except for ctrl-c,
/// ctrl-x, and ctrl-v.
Comment on lines +79 to +80
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.

/// ```
/// # 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.

/// # 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.

/// let key_events_to_partially_prevent_default = ["keyup", "keydown"];
/// let pointer_events_to_focus_and_prevent_default = ["pointerdown"];
///
/// for event_type in events_types_to_fully_prevent_default.into_iter() {
/// let prevent_default_listener =
/// Closure::<dyn FnMut(_)>::new(move |event: web_sys::Event| {
/// event.prevent_default();
/// });
///
/// let _ = canvas.add_event_listener_with_callback(
/// event_type,
/// prevent_default_listener.as_ref().unchecked_ref(),
/// );
/// prevent_default_listener.into_js_value();
/// }
///
/// for event_type in pointer_events_to_focus_and_prevent_default.into_iter() {
/// let stored_canvas = canvas.clone();
/// let prevent_default_listener =
/// Closure::<dyn FnMut(_)>::new(move |event: web_sys::Event| {
/// event.prevent_default();
/// let _ = stored_canvas.focus();
/// });
///
/// let _ = canvas.add_event_listener_with_callback(
/// event_type,
/// prevent_default_listener.as_ref().unchecked_ref(),
/// );
/// prevent_default_listener.forget();
/// }
///
/// for event_type in key_events_to_partially_prevent_default.into_iter() {
/// let prevent_default_listener =
/// Closure::<dyn FnMut(_)>::new(move |event: web_sys::KeyboardEvent| {
/// let only_ctrl_key =
/// event.ctrl_key() && !event.meta_key() && !event.shift_key() && !event.alt_key();
/// let allow_default =
/// only_ctrl_key && matches!(event.key().as_ref(), "c" | "x" | "v");
/// if !allow_default {
/// event.prevent_default();
/// }
/// });
///
/// let _ = canvas.add_event_listener_with_callback(
/// event_type,
/// prevent_default_listener.as_ref().unchecked_ref(),
/// );
/// prevent_default_listener.forget();
/// }
/// # }
/// ```
fn set_prevent_default(&self, prevent_default: bool);
}

Expand Down