2019-09-24 19:33:32 -04:00
|
|
|
//! The web target does not automatically insert the canvas element object into the web page, to
|
2022-06-11 18:57:19 +02:00
|
|
|
//! allow end users to determine how the page should be laid out. Use the [`WindowExtWebSys`] trait
|
|
|
|
|
//! to retrieve the canvas from the Window. Alternatively, use the [`WindowBuilderExtWebSys`] trait
|
2021-05-24 19:06:21 +02:00
|
|
|
//! to provide your own canvas.
|
2023-07-10 12:50:28 +02:00
|
|
|
//!
|
|
|
|
|
//! It is recommended **not** to apply certain CSS properties to the canvas:
|
|
|
|
|
//! - [`transform`]
|
|
|
|
|
//! - [`border`]
|
|
|
|
|
//! - [`padding`]
|
|
|
|
|
//!
|
|
|
|
|
//! The following APIs can't take them into account and will therefore provide inaccurate results:
|
|
|
|
|
//! - [`WindowEvent::Resized`] and [`Window::(set_)inner_size()`]
|
|
|
|
|
//! - [`WindowEvent::Occluded`]
|
|
|
|
|
//! - [`WindowEvent::CursorMoved`], [`WindowEvent::CursorEntered`], [`WindowEvent::CursorLeft`],
|
|
|
|
|
//! and [`WindowEvent::Touch`].
|
2023-07-10 23:55:43 +02:00
|
|
|
//! - [`Window::set_outer_position()`]
|
2023-07-10 12:50:28 +02:00
|
|
|
//!
|
|
|
|
|
//! [`WindowEvent::Resized`]: crate::event::WindowEvent::Resized
|
|
|
|
|
//! [`Window::(set_)inner_size()`]: crate::window::Window::inner_size()
|
|
|
|
|
//! [`WindowEvent::Occluded`]: crate::event::WindowEvent::Occluded
|
|
|
|
|
//! [`WindowEvent::CursorMoved`]: crate::event::WindowEvent::CursorMoved
|
|
|
|
|
//! [`WindowEvent::CursorEntered`]: crate::event::WindowEvent::CursorEntered
|
|
|
|
|
//! [`WindowEvent::CursorLeft`]: crate::event::WindowEvent::CursorLeft
|
|
|
|
|
//! [`WindowEvent::Touch`]: crate::event::WindowEvent::Touch
|
2023-07-10 23:55:43 +02:00
|
|
|
//! [`Window::set_outer_position()`]: crate::window::Window::set_outer_position()
|
2023-07-10 12:50:28 +02:00
|
|
|
//! [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
|
|
|
|
|
//! [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
|
|
|
|
|
//! [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
|
2020-01-15 21:20:14 -05:00
|
|
|
|
2022-07-14 01:17:18 +10:00
|
|
|
use crate::event::Event;
|
|
|
|
|
use crate::event_loop::ControlFlow;
|
|
|
|
|
use crate::event_loop::EventLoop;
|
|
|
|
|
use crate::event_loop::EventLoopWindowTarget;
|
2023-08-14 21:19:57 +02:00
|
|
|
use crate::window::{Window, WindowBuilder};
|
2019-09-24 19:33:32 -04:00
|
|
|
|
2019-06-25 03:15:34 +02:00
|
|
|
use web_sys::HtmlCanvasElement;
|
|
|
|
|
|
|
|
|
|
pub trait WindowExtWebSys {
|
2023-06-05 02:44:54 +02:00
|
|
|
/// Only returns the canvas if called from inside the window.
|
|
|
|
|
fn canvas(&self) -> Option<HtmlCanvasElement>;
|
2019-06-25 03:15:34 +02:00
|
|
|
}
|
2020-01-15 21:20:14 -05:00
|
|
|
|
2023-08-14 21:19:57 +02:00
|
|
|
impl WindowExtWebSys for Window {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn canvas(&self) -> Option<HtmlCanvasElement> {
|
|
|
|
|
self.window.canvas()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 21:20:14 -05:00
|
|
|
pub trait WindowBuilderExtWebSys {
|
2023-08-24 18:29:31 +02:00
|
|
|
/// Pass an [`HtmlCanvasElement`] to be used for this [`Window`]. If [`None`],
|
|
|
|
|
/// [`WindowBuilder::build()`] will create one.
|
2023-07-12 17:11:17 +02:00
|
|
|
///
|
|
|
|
|
/// In any case, the canvas won't be automatically inserted into the web page.
|
|
|
|
|
///
|
|
|
|
|
/// [`None`] by default.
|
2020-01-15 21:20:14 -05:00
|
|
|
fn with_canvas(self, canvas: Option<HtmlCanvasElement>) -> Self;
|
2022-07-14 13:22:31 -02:30
|
|
|
|
|
|
|
|
/// Whether `event.preventDefault` should be automatically called to prevent event propagation
|
|
|
|
|
/// when appropriate.
|
|
|
|
|
///
|
|
|
|
|
/// For example, mouse wheel events are only handled by the canvas by default. This avoids
|
|
|
|
|
/// the default behavior of scrolling the page.
|
2023-06-05 01:47:01 +02:00
|
|
|
///
|
|
|
|
|
/// Some events are impossible to prevent. E.g. Firefox allows to access the native browser
|
|
|
|
|
/// context menu with Shift+Rightclick.
|
2023-07-12 17:11:17 +02:00
|
|
|
///
|
|
|
|
|
/// Enabled by default.
|
2022-07-14 13:22:31 -02:30
|
|
|
fn with_prevent_default(self, prevent_default: bool) -> Self;
|
|
|
|
|
|
|
|
|
|
/// Whether the canvas should be focusable using the tab key. This is necessary to capture
|
|
|
|
|
/// canvas keyboard events.
|
2023-07-12 17:11:17 +02:00
|
|
|
///
|
|
|
|
|
/// Enabled by default.
|
2022-07-14 13:22:31 -02:30
|
|
|
fn with_focusable(self, focusable: bool) -> Self;
|
2023-07-12 17:11:52 +02:00
|
|
|
|
|
|
|
|
/// On window creation, append the canvas element to the web page if it isn't already.
|
|
|
|
|
///
|
|
|
|
|
/// Disabled by default.
|
|
|
|
|
fn with_append(self, append: bool) -> Self;
|
2020-01-15 21:20:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WindowBuilderExtWebSys for WindowBuilder {
|
|
|
|
|
fn with_canvas(mut self, canvas: Option<HtmlCanvasElement>) -> Self {
|
|
|
|
|
self.platform_specific.canvas = canvas;
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
2022-07-14 13:22:31 -02:30
|
|
|
|
|
|
|
|
fn with_prevent_default(mut self, prevent_default: bool) -> Self {
|
|
|
|
|
self.platform_specific.prevent_default = prevent_default;
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn with_focusable(mut self, focusable: bool) -> Self {
|
|
|
|
|
self.platform_specific.focusable = focusable;
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
2023-07-12 17:11:52 +02:00
|
|
|
|
|
|
|
|
fn with_append(mut self, append: bool) -> Self {
|
|
|
|
|
self.platform_specific.append = append;
|
|
|
|
|
|
|
|
|
|
self
|
|
|
|
|
}
|
2020-01-15 21:20:14 -05:00
|
|
|
}
|
2022-07-14 01:17:18 +10:00
|
|
|
|
|
|
|
|
/// Additional methods on `EventLoop` that are specific to the web.
|
|
|
|
|
pub trait EventLoopExtWebSys {
|
|
|
|
|
/// A type provided by the user that can be passed through `Event::UserEvent`.
|
|
|
|
|
type UserEvent;
|
|
|
|
|
|
|
|
|
|
/// Initializes the winit event loop.
|
|
|
|
|
///
|
|
|
|
|
/// Unlike `run`, this returns immediately, and doesn't throw an exception in order to
|
|
|
|
|
/// satisfy its `!` return type.
|
2023-06-23 15:01:42 -02:30
|
|
|
///
|
|
|
|
|
/// Once the event loop has been destroyed, it's possible to reinitialize another event loop
|
|
|
|
|
/// by calling this function again. This can be useful if you want to recreate the event loop
|
|
|
|
|
/// while the WebAssembly module is still loaded. For example, this can be used to recreate the
|
|
|
|
|
/// event loop when switching between tabs on a single page application.
|
2022-07-14 01:17:18 +10:00
|
|
|
fn spawn<F>(self, event_handler: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static
|
2023-07-31 00:39:01 +04:00
|
|
|
+ FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>, &mut ControlFlow);
|
2022-07-14 01:17:18 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> EventLoopExtWebSys for EventLoop<T> {
|
|
|
|
|
type UserEvent = T;
|
|
|
|
|
|
|
|
|
|
fn spawn<F>(self, event_handler: F)
|
|
|
|
|
where
|
|
|
|
|
F: 'static
|
2023-07-31 00:39:01 +04:00
|
|
|
+ FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>, &mut ControlFlow),
|
2022-07-14 01:17:18 +10:00
|
|
|
{
|
|
|
|
|
self.event_loop.spawn(event_handler)
|
|
|
|
|
}
|
|
|
|
|
}
|