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

Fix wasm32 deployments not displaying anything #2574

Merged
merged 4 commits into from
Sep 13, 2024

Conversation

derezzedex
Copy link
Member

This fixes a bug where web deployments of an iced application would silently fail, not displaying anything.

The problem can be tested by using trunk serve in any of the examples that support it.

It seems to have started after #2469, where changes were introduced to allow an application to be started without an window. Although an window is not necessarily shown at the start, it's still created silently, and this is necessary for the acquisition of the graphics device and it's initialization, after that the window is discarded and only created again when requested by the runtime (or quickly after that on "non-deamon" applications).

On the wasm32 path of the code, the window contains the HTMLCanvasElement, which in turn, has the getContext method and contains the WebGLRenderingContext which is necessary for properly displaying graphics on the canvas.
By discarding the window, we end up discarding the HTMLCanvasElement which is associated with the context and other graphics dependencies for proper rendering; creating a new window without using the correct associated canvas element which was used to initialize the context, then makes the canvas graphically display nothing.

There could multiple solutions to this, but the simplest seems to be to simply retain the canvas element, and re-use it to create the window through the with_canvas window attribute.

This PR changes the wasm32 path to store the web_sys::HTMLCanvasElement and use it on the with_canvas method when creating an window, I've also added logging for the window attributes to the relevant code, which could be helpful in future debugging of similar problems.

As for possible problems with this, the code doesn't make any guarantees for other windows, it'll certainly silently fail when asked to create new windows, since those are basically canvas elements on wasm32 that are not properly created or maintained. Although I would add that this was already the case before the PR.

Closes #2480.

@hecrj hecrj added this to the 0.13 milestone Sep 13, 2024
@hecrj hecrj added bug Something isn't working rendering shell web fix labels Sep 13, 2024
Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet!

Made some changes to discard the empty polling events during boot.

@hecrj hecrj enabled auto-merge (squash) September 13, 2024 22:08
@hecrj hecrj merged commit d46f6f9 into iced-rs:master Sep 13, 2024
12 checks passed
@derezzedex
Copy link
Member Author

Sweet!

Made some changes to discard the empty polling events during boot.

Nice catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running trunk serve on examples results in blank web pages
2 participants