-
Notifications
You must be signed in to change notification settings - Fork 868
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`. | ||
/// | ||
/// For fine-grained control over which events call `event.preventDefault()`, call | ||
/// `Window::set_prevent_default(false)` and add an event listener on the canvas for each of | ||
/// the above event types that selectively calls `event.preventDefault()`. | ||
ddfisher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
/// ``` | ||
/// # fn add_prevent_default_listeners(canvas: HtmlCanvasElement) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to actually make a full example with Doesn't need much, just create a |
||
/// # use wasm_bindgen::closure::Closure; | ||
/// # use wasm_bindgen::JsCast; | ||
/// let events_types_to_fully_prevent_default = | ||
/// ["touchstart", "wheel", "contextmenu", "pointermove"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
/// 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.forget(); | ||
ddfisher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// } | ||
/// | ||
/// 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); | ||
} | ||
|
||
|
There was a problem hiding this comment.
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 forpointermove
in the case of a chorded button interaction.I know this is getting even more into the weeds, but it might be important ...