m: Cache document in web frontend (#66)
This commit is contained in:
parent
125ad070c2
commit
33a4c3741b
2 changed files with 27 additions and 11 deletions
30
src/web.rs
30
src/web.rs
|
|
@ -7,14 +7,16 @@ use web_sys::ImageData;
|
|||
|
||||
use crate::SoftBufferError;
|
||||
|
||||
pub struct WebImpl {
|
||||
canvas: HtmlCanvasElement,
|
||||
ctx: CanvasRenderingContext2d,
|
||||
/// Display implementation for the web platform.
|
||||
///
|
||||
/// This just caches the document to prevent having to query it every time.
|
||||
pub struct WebDisplayImpl {
|
||||
document: web_sys::Document,
|
||||
}
|
||||
|
||||
impl WebImpl {
|
||||
pub fn new(handle: WebWindowHandle) -> Result<Self, SoftBufferError> {
|
||||
let canvas: HtmlCanvasElement = web_sys::window()
|
||||
impl WebDisplayImpl {
|
||||
pub(super) fn new() -> Result<Self, SoftBufferError> {
|
||||
let document = web_sys::window()
|
||||
.ok_or_else(|| {
|
||||
SoftBufferError::PlatformError(
|
||||
Some("`window` is not present in this runtime".into()),
|
||||
|
|
@ -27,7 +29,21 @@ impl WebImpl {
|
|||
Some("`document` is not present in this runtime".into()),
|
||||
None,
|
||||
)
|
||||
})?
|
||||
})?;
|
||||
|
||||
Ok(Self { document })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WebImpl {
|
||||
canvas: HtmlCanvasElement,
|
||||
ctx: CanvasRenderingContext2d,
|
||||
}
|
||||
|
||||
impl WebImpl {
|
||||
pub fn new(display: &WebDisplayImpl, handle: WebWindowHandle) -> Result<Self, SoftBufferError> {
|
||||
let canvas: HtmlCanvasElement = display
|
||||
.document
|
||||
.query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id))
|
||||
// `querySelector` only throws an error if the selector is invalid.
|
||||
.unwrap()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue