rust_minifb icon indicating copy to clipboard operation
rust_minifb copied to clipboard

Wasm Improvements

Open NwE0kmCE opened this issue 1 year ago • 2 comments

This PR makes some changes to how Windows are instantiated on wasm targets

  • The title parameter in Window::new() has been replaced with container, which takes an element the canvas should be appended to
    • Allows more control over where and how to embed a minifb canvas in web projects, and still allows the user to change the window title with set_title
    • Rustdoc new window example has been updated for wasm32 target
  • set_cursor_visibility now properly sets the the html canvas cursor style
  • is_active now checks if the canvas element is focused

NwE0kmCE avatar Aug 29 '24 19:08 NwE0kmCE

To avoid a breaking API change, title could be used to select a dom element by id...

NwE0kmCE avatar Aug 29 '24 23:08 NwE0kmCE

Hey,

Thanks for the PR! I wonder if it's possible to include an example that works with these changes also? It would be nice to keep the API same between desktop and wasm, but if it doesn't make sense from a web perspective I'm fine with the change.

emoon avatar Aug 30 '24 06:08 emoon

Hi! Sorry for the delay! Working on the example now, and pretty quickly I'm running into a case where having a unified API would be better: all non-web examples fail to compile when the target is set to wasm... I do think that on web the page title shouldn't be changed, since most of the time (i assume) someone would be embedding minifb into a larger project/site where the page title is already defined somewhere else. I think passing the id of a dom element works, as long as there's some sign (e.g. rustdoc comment) that the behavior is different between desktop & web

NwE0kmCE avatar Sep 24 '24 17:09 NwE0kmCE

Also, I noticed that the web feature is only used in 2 places (rate.rs and key_handler.rs) and could probably be replaced with a target = "wasm32" attribute, would it make sense to replace those and remove the web feature?

NwE0kmCE avatar Sep 24 '24 17:09 NwE0kmCE

Also, I noticed that the web feature is only used in 2 places (rate.rs and key_handler.rs) and could probably be replaced with a target = "wasm32" attribute, would it make sense to replace those and remove the web feature?

Sounds good to me

emoon avatar Sep 24 '24 17:09 emoon

This should be ready to go!

NwE0kmCE avatar Sep 24 '24 18:09 NwE0kmCE

Great! Thanks.

I wonder how hard would it be to add a build step for this in https://github.com/emoon/rust_minifb/blob/master/.github/workflows/ci.yml ? Just to make sure it doesn't break when doing other changes.

emoon avatar Sep 25 '24 05:09 emoon

I think this change should work... Building is no problem, but testing on wasm requires a whole bunch of extra tooling since the generated wasm isn't an executable on its own. Since the only tests being run anyways are doc tests this should be fine:

    - name: Build
-     run: cargo build --verbose
+     run: |
+          cargo build --verbose
+          cargo build --verbose --target wasm32-unknown-unknown 
    - name: Run tests
-     run: cargo test --verbose
+     run: |
+          cargo test --verbose
+          cargo test --doc --verbose --target wasm32-unknown-unknown 

more info on wasm testing here

NwE0kmCE avatar Sep 25 '24 18:09 NwE0kmCE

Thanks for the PR. I will look into getting the build step for the CI running in another change.

emoon avatar Sep 26 '24 06:09 emoon