-
Notifications
You must be signed in to change notification settings - Fork 941
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
Add WASM CustomEvent Example #2852
Add WASM CustomEvent Example #2852
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rukai I guess I didn't really think my last comment through (#2816 (comment)) , but you can't really show the JS in the generated HTML file because it's inserted during runtime, or is there something cargo-run-wasm
can do about that I'm not aware of?
Additionally, it would also be really nice to not have to build a bunch of HTML through JS or web-sys
. Again, none of this can actually be shown to the user because it's all added during runtime.
examples/wasm_custom_event.rs
Outdated
// Because EventLoopProxy is not Send, we need to wrap it in a RefCell and use thread_local! | ||
thread_local! { | ||
pub static EVENT_LOOP_PROXY: RefCell<Option<EventLoopProxy<CustomEvent>>> = RefCell::new(None); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since #2834 it is actually!
In any case it's still appropriate to use thread_local!
if we aren't doing anything with web workers.
We should also probably use once_cell::unsync::OnceCell
instead of RefCell
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not certain want to make the comment now. Not super familiar with web workers. (js is single threaded unless you use web-workers is my understanding (?))
// Function to be called from JS | ||
fn wasm_call() -> u32 { | ||
42 | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum CustomEvent { | ||
WasmCall, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make a slightly more useful example. Instead of just printing 42 we could e.g. use Window::set_inner_size()
, which is a valid use-case and would also show how to pass parameters. This would require two input fields for width and height and a button to send.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awaiting if the cargo-wasm-runner will go through first. rukai/cargo-run-wasm#36
Event::WindowEvent { | ||
event: WindowEvent::CloseRequested, | ||
window_id, | ||
} if window_id == window.id() => control_flow.set_exit(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can skip that here, it's not a thing in Wasm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skip this match arm?
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => control_flow.set_exit(),
Oh uh... true. 😅 Next best thing I can think of would be just manually creating an unused html file with the appropriate javascript in it :/ |
Would you accept a feature request extending the API of |
Yep, I think we do need this feature, I've raised rukai/cargo-run-wasm#35 |
I could consider looking into adding that feature. Looks relatively straightforward via web_sys |
Co-authored-by: daxpedda <[email protected]>
Co-authored-by: daxpedda <[email protected]>
…venhuyn/winit into stevenhuyn/wasm-custom-event
@stevenhuyn you should keep the conversation you are having in #2816 (comment) here! |
Ah thought the issue is the ground truth, although I guess implementation details should be in this PR (?) I have hooked it up to test (pointing cargo run wasm crate to my fork) and have it all working. Need to look into why CI failing, cause it builds perfectly fine on my machine. |
The current CustomEvent example in the ./examples folder doesn't show how to do it via WASM.
Atm I'm just printing to console, however we could reuse the
examples/web.rs
code to append to a HTML list. But I wanted to keep the example a bit more streamline.Also I added some extra CSS to centre align it. The button will be placed on the right of the canvas otherwise. lmk if it's uggo and I'll revert it.
Closes #2816
Answers #2061 (comment)
CHANGELOG.md
if knowledge of this change could be valuable to users