-
Notifications
You must be signed in to change notification settings - Fork 920
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?
Conversation
…efault() calls on web
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.
Thank you, this is great!
Let me know if you need any help with anything.
src/platform/web.rs
Outdated
/// event_type, | ||
/// prevent_default_listener.as_ref().unchecked_ref(), | ||
/// ); | ||
/// prevent_default_listener.forget(); |
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.
/// prevent_default_listener.forget(); | |
/// prevent_default_listener.into_js_value(); |
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.
Oh, interesting -- thanks!
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 is still missing.
src/platform/web.rs
Outdated
/// This example calls `event.preventDefault()` for all relevant events except for ctrl-c, | ||
/// ctrl-x, and ctrl-v. |
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.
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.
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.
Switched to ctrl-p
.
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.
Hooking up beforeprint
would look something like this:
canvas
.add_event_listener_with_callback(
"beforeprint",
&Closure::<dyn Fn()>::new(|| {
// This event is now triggered!
})
.into_js_value()
.unchecked_into(),
)
.unwrap();
Co-authored-by: daxpedda <daxpedda@gmail.com>
Co-authored-by: daxpedda <daxpedda@gmail.com>
Let me know if this looks good now! Sorry for the slow turnaround here! |
@@ -80,8 +80,138 @@ pub trait WindowExtWeb { | |||
/// 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: |
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.
/// Specifically, this will call `event.preventDefault()` for the following event types: | |
/// # Implementation Notes | |
/// | |
/// Specifically, this will call `event.preventDefault()` for the following event types: |
/// `touchstart`, `wheel`, `contextmenu`, `pointerdown`, `pointermove` (for chorded button | ||
/// interactions only), `keyup`, and `keydown`. |
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.
/// `touchstart`, `wheel`, `contextmenu`, `pointerdown`, `pointermove` (for chorded button | |
/// interactions only), `keyup`, and `keydown`. | |
/// `touchstart`, `wheel`, `contextmenu`, `pointerdown`, `pointermove` (for | |
/// [chorded button events only]), `keyup`, and `keydown`. |
And link to https://www.w3.org/TR/2019/REC-pointerevents2-20190404/#chorded-button-interactions.
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.
Lets link `event.preventDefault()`
to https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault as well.
/// # Example | ||
/// This example calls `event.preventDefault()` for all relevant events except for ctrl-p. |
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.
/// # Example | |
/// This example calls `event.preventDefault()` for all relevant events except for ctrl-p. | |
/// # Example | |
/// | |
/// This example calls `event.preventDefault()` for all relevant events except for Ctrl-P. | |
/// |
/// # } | ||
/// # | ||
/// # impl ApplicationHandler for App { | ||
/// # fn resumed(&mut self, event_loop: &ActiveEventLoop) { |
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.
/// # window.set_prevent_default(false); | ||
/// # add_prevent_default_listeners(window.canvas().unwrap()); |
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 part is essential to the example, so it should be visible.
I don't think there is anything wrong with showing the whole struct App
and impl ApplicationHandler for App
.
/// # WindowEvent::RedrawRequested => { | ||
/// # self.window.as_ref().unwrap().request_redraw(); | ||
/// # } |
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 is probably not a recommended setup, especially for Web.
I think we can get away with only handling WindowEvent::CloseRequested
.
/// event.prevent_default(); | ||
/// }); | ||
/// | ||
/// let _ = canvas.add_event_listener_with_callback( |
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.
We shouldn't ignore the error, just unwrap()
here.
src/platform/web.rs
Outdated
/// event_type, | ||
/// prevent_default_listener.as_ref().unchecked_ref(), | ||
/// ); | ||
/// prevent_default_listener.forget(); |
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 is still missing.
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.