Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass HtmlCanvasElement element directly in WebRunner::start #4780

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/eframe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@
//!
//! /// Call this once from JavaScript to start your app.
//! #[wasm_bindgen]
//! pub async fn start(&self, canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> {
//! pub async fn start(&self, canvas: web_sys::HtmlCanvasElement) -> Result<(), wasm_bindgen::JsValue> {
//! self.runner
//! .start(
//! canvas_id,
//! canvas,
//! eframe::WebOptions::default(),
//! Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc))),)
//! )
Expand Down
4 changes: 2 additions & 2 deletions crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ impl AppRunner {
/// # Errors
/// Failure to initialize WebGL renderer, or failure to create app.
pub async fn new(
canvas_id: &str,
canvas: web_sys::HtmlCanvasElement,
web_options: crate::WebOptions,
app_creator: epi::AppCreator,
text_agent: TextAgent,
) -> Result<Self, String> {
let painter = super::ActiveWebPainter::new(canvas_id, &web_options).await?;
let painter = super::ActiveWebPainter::new(canvas, &web_options).await?;

let system_theme = if web_options.follow_system_theme {
super::system_theme()
Expand Down
4 changes: 2 additions & 2 deletions crates/eframe/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn theme_from_dark_mode(dark_mode: bool) -> Theme {
}
}

fn get_canvas_element_by_id(canvas_id: &str) -> Option<web_sys::HtmlCanvasElement> {
/* fn get_canvas_element_by_id(canvas_id: &str) -> Option<web_sys::HtmlCanvasElement> {
let document = web_sys::window()?.document()?;
let canvas = document.get_element_by_id(canvas_id)?;
canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok()
Expand All @@ -131,7 +131,7 @@ fn get_canvas_element_by_id(canvas_id: &str) -> Option<web_sys::HtmlCanvasElemen
fn get_canvas_element_by_id_or_die(canvas_id: &str) -> web_sys::HtmlCanvasElement {
get_canvas_element_by_id(canvas_id)
.unwrap_or_else(|| panic!("Failed to find canvas with id {canvas_id:?}"))
}
} */

/// Returns the canvas in client coordinates.
fn canvas_content_rect(canvas: &web_sys::HtmlCanvasElement) -> egui::Rect {
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/src/web/web_painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use wasm_bindgen::JsValue;
/// therefore this trait is merely there for specifying and documenting the interface.
pub(crate) trait WebPainter {
// Create a new web painter targeting a given canvas.
// fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String>
// fn new(canvas: HtmlCanvasElement, options: &WebOptions) -> Result<Self, String>
// where
// Self: Sized;

Expand Down
4 changes: 1 addition & 3 deletions crates/eframe/src/web/web_painter_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ impl WebPainterGlow {
self.painter.gl()
}

pub async fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String> {
let canvas = super::get_canvas_element_by_id_or_die(canvas_id);

pub async fn new(canvas: HtmlCanvasElement, options: &WebOptions) -> Result<Self, String> {
let (gl, shader_prefix) =
init_glow_context_from_canvas(&canvas, options.webgl_context_option)?;
#[allow(clippy::arc_with_non_send_sync)]
Expand Down
6 changes: 4 additions & 2 deletions crates/eframe/src/web/web_painter_wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ impl WebPainterWgpu {
}

#[allow(unused)] // only used if `wgpu` is the only active feature.
pub async fn new(canvas_id: &str, options: &WebOptions) -> Result<Self, String> {
pub async fn new(
canvas: web_sys::HtmlCanvasElement,
options: &WebOptions,
) -> Result<Self, String> {
log::debug!("Creating wgpu painter");

let mut backends = options.wgpu_options.supported_backends;
Expand Down Expand Up @@ -162,7 +165,6 @@ impl WebPainterWgpu {
}
}

let canvas = super::get_canvas_element_by_id_or_die(canvas_id);
let surface = instance
.create_surface(wgpu::SurfaceTarget::Canvas(canvas.clone()))
.map_err(|err| format!("failed to create wgpu surface: {err}"))?;
Expand Down
4 changes: 2 additions & 2 deletions crates/eframe/src/web/web_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl WebRunner {
/// Failing to initialize graphics, or failure to create app.
pub async fn start(
&self,
canvas_id: &str,
canvas: web_sys::HtmlCanvasElement,
web_options: crate::WebOptions,
app_creator: epi::AppCreator,
) -> Result<(), JsValue> {
Expand All @@ -67,7 +67,7 @@ impl WebRunner {

let text_agent = TextAgent::attach(self)?;

let runner = AppRunner::new(canvas_id, web_options, app_creator, text_agent).await?;
let runner = AppRunner::new(canvas, web_options, app_creator, text_agent).await?;

{
// Make sure the canvas can be given focus.
Expand Down
7 changes: 5 additions & 2 deletions crates/egui_demo_app/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ impl WebHandle {

/// Call this once from JavaScript to start your app.
#[wasm_bindgen]
pub async fn start(&self, canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> {
pub async fn start(
&self,
canvas: web_sys::HtmlCanvasElement,
) -> Result<(), wasm_bindgen::JsValue> {
self.runner
.start(
canvas_id,
canvas,
eframe::WebOptions::default(),
Box::new(|cc| Ok(Box::new(WrapApp::new(cc)))),
)
Expand Down
2 changes: 1 addition & 1 deletion web_demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@

check_for_panic();

handle.start("the_canvas_id").then(on_app_started).catch(on_error);
handle.start(document.getElementById("the_canvas_id")).then(on_app_started).catch(on_error);
}

function on_app_started(handle) {
Expand Down
5 changes: 4 additions & 1 deletion web_demo/multiple_apps.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@
const handle_one = new wasm_bindgen.WebHandle();
const handle_two = new wasm_bindgen.WebHandle();

Promise.all([handle_one.start("canvas_id_one"), handle_two.start("canvas_id_two")]).then((handles) => {
Promise.all([
handle_one.start(document.getElementById("canvas_id_one")),
handle_two.start(document.getElementById("canvas_id_two")),
]).then((handles) => {
on_apps_started(handle_one, handle_two)
}).catch(on_error);
}
Expand Down
Loading