On Web, add WindowBuilderExtWebSys::with_append() (#2953)

This commit is contained in:
daxpedda 2023-07-12 17:11:52 +02:00 committed by GitHub
parent 5b5ebc25d8
commit b63164645b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 6 deletions

View file

@ -66,6 +66,11 @@ pub trait WindowBuilderExtWebSys {
///
/// Enabled by default.
fn with_focusable(self, focusable: bool) -> Self;
/// 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;
}
impl WindowBuilderExtWebSys for WindowBuilder {
@ -86,6 +91,12 @@ impl WindowBuilderExtWebSys for WindowBuilder {
self
}
fn with_append(mut self, append: bool) -> Self {
self.platform_specific.append = append;
self
}
}
/// Additional methods on `EventLoop` that are specific to the web.

View file

@ -70,6 +70,14 @@ impl Canvas {
.unchecked_into(),
};
if platform_attr.append && !document.contains(Some(&canvas)) {
document
.body()
.expect("Failed to get body from document")
.append_child(&canvas)
.expect("Failed to append canvas to body");
}
// A tabindex is needed in order to capture local keyboard events.
// A "0" value means that the element should be focusable in
// sequential keyboard navigation, but its order is defined by the

View file

@ -483,6 +483,7 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub(crate) canvas: Option<backend::RawCanvasType>,
pub(crate) prevent_default: bool,
pub(crate) focusable: bool,
pub(crate) append: bool,
}
impl Default for PlatformSpecificWindowBuilderAttributes {
@ -491,6 +492,7 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
canvas: None,
prevent_default: true,
focusable: true,
append: false,
}
}
}