From dbdde3d7818e4f7ac0c96d8abe10a187575b750a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n?= Date: Thu, 8 Aug 2019 23:51:41 +0200 Subject: [PATCH] Stop appending canvas to document in web platform (#1089) * Stop appending canvas to document in web platform * Remove `tabindex` TODO in web backend * Return `OsError` instead of panicking on web canvas creation --- src/platform_impl/web/stdweb/canvas.rs | 13 ++++++------- src/platform_impl/web/web_sys/canvas.rs | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/platform_impl/web/stdweb/canvas.rs b/src/platform_impl/web/stdweb/canvas.rs index 14194858..1cd99e7e 100644 --- a/src/platform_impl/web/stdweb/canvas.rs +++ b/src/platform_impl/web/stdweb/canvas.rs @@ -49,15 +49,14 @@ impl Canvas { .try_into() .map_err(|_| os_error!(OsError("Failed to create canvas element".to_owned())))?; - document() - .body() - .ok_or_else(|| os_error!(OsError("Failed to find body node".to_owned())))? - .append_child(&canvas); - - // TODO: Set up unique ids + // 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 + // document's source order. + // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex canvas .set_attribute("tabindex", "0") - .expect("Failed to set a tabindex"); + .map_err(|_| os_error!(OsError("Failed to set a tabindex".to_owned())))?; Ok(Canvas { raw: canvas, diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index 4e435aaf..39a17e5b 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -34,24 +34,26 @@ impl Canvas { where F: 'static + Fn(), { - let window = web_sys::window().expect("Failed to obtain window"); - let document = window.document().expect("Failed to obtain document"); + let window = + web_sys::window().ok_or(os_error!(OsError("Failed to obtain window".to_owned())))?; + + let document = window + .document() + .ok_or(os_error!(OsError("Failed to obtain document".to_owned())))?; let canvas: HtmlCanvasElement = document .create_element("canvas") .map_err(|_| os_error!(OsError("Failed to create canvas element".to_owned())))? .unchecked_into(); - document - .body() - .ok_or_else(|| os_error!(OsError("Failed to find body node".to_owned())))? - .append_child(&canvas) - .map_err(|_| os_error!(OsError("Failed to append canvas".to_owned())))?; - - // TODO: Set up unique ids + // 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 + // document's source order. + // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex canvas .set_attribute("tabindex", "0") - .expect("Failed to set a tabindex"); + .map_err(|_| os_error!(OsError("Failed to set a tabindex".to_owned())))?; Ok(Canvas { raw: canvas,