Skip to content

Commit 33a4c37

Browse files
authored
m: Cache document in web frontend (#66)
1 parent 125ad07 commit 33a4c37

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ make_dispatch! {
9393
#[cfg(target_os = "macos")]
9494
CG((), cg::CGImpl),
9595
#[cfg(target_arch = "wasm32")]
96-
Web((), web::WebImpl),
96+
Web(web::WebDisplayImpl, web::WebImpl),
9797
#[cfg(target_os = "redox")]
9898
Orbital((), orbital::OrbitalImpl),
9999
}
@@ -134,7 +134,7 @@ impl Context {
134134
#[cfg(target_os = "macos")]
135135
RawDisplayHandle::AppKit(_) => ContextDispatch::CG(()),
136136
#[cfg(target_arch = "wasm32")]
137-
RawDisplayHandle::Web(_) => ContextDispatch::Web(()),
137+
RawDisplayHandle::Web(_) => ContextDispatch::Web(web::WebDisplayImpl::new()?),
138138
#[cfg(target_os = "redox")]
139139
RawDisplayHandle::Orbital(_) => ContextDispatch::Orbital(()),
140140
unimplemented_display_handle => {
@@ -212,8 +212,8 @@ impl Surface {
212212
SurfaceDispatch::CG(unsafe { cg::CGImpl::new(appkit_handle)? })
213213
}
214214
#[cfg(target_arch = "wasm32")]
215-
(ContextDispatch::Web(()), RawWindowHandle::Web(web_handle)) => {
216-
SurfaceDispatch::Web(web::WebImpl::new(web_handle)?)
215+
(ContextDispatch::Web(context), RawWindowHandle::Web(web_handle)) => {
216+
SurfaceDispatch::Web(web::WebImpl::new(context, web_handle)?)
217217
}
218218
#[cfg(target_os = "redox")]
219219
(ContextDispatch::Orbital(()), RawWindowHandle::Orbital(orbital_handle)) => {

src/web.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ use web_sys::ImageData;
77

88
use crate::SoftBufferError;
99

10-
pub struct WebImpl {
11-
canvas: HtmlCanvasElement,
12-
ctx: CanvasRenderingContext2d,
10+
/// Display implementation for the web platform.
11+
///
12+
/// This just caches the document to prevent having to query it every time.
13+
pub struct WebDisplayImpl {
14+
document: web_sys::Document,
1315
}
1416

15-
impl WebImpl {
16-
pub fn new(handle: WebWindowHandle) -> Result<Self, SoftBufferError> {
17-
let canvas: HtmlCanvasElement = web_sys::window()
17+
impl WebDisplayImpl {
18+
pub(super) fn new() -> Result<Self, SoftBufferError> {
19+
let document = web_sys::window()
1820
.ok_or_else(|| {
1921
SoftBufferError::PlatformError(
2022
Some("`window` is not present in this runtime".into()),
@@ -27,7 +29,21 @@ impl WebImpl {
2729
Some("`document` is not present in this runtime".into()),
2830
None,
2931
)
30-
})?
32+
})?;
33+
34+
Ok(Self { document })
35+
}
36+
}
37+
38+
pub struct WebImpl {
39+
canvas: HtmlCanvasElement,
40+
ctx: CanvasRenderingContext2d,
41+
}
42+
43+
impl WebImpl {
44+
pub fn new(display: &WebDisplayImpl, handle: WebWindowHandle) -> Result<Self, SoftBufferError> {
45+
let canvas: HtmlCanvasElement = display
46+
.document
3147
.query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id))
3248
// `querySelector` only throws an error if the selector is invalid.
3349
.unwrap()

0 commit comments

Comments
 (0)