-
Notifications
You must be signed in to change notification settings - Fork 568
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
Simple wasm example #801
Merged
Merged
Simple wasm example #801
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ members = [ | |
"druid-derive", | ||
"docs/book_examples", | ||
"druid/examples/wasm", | ||
"druid/examples/hello_wasm_web", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "hello-wasm-web" | ||
version = "0.1.0" | ||
license = "Apache-2.0" | ||
description = "Minimal wasm example for web" | ||
repository = "https://github.com/xi-editor/druid" | ||
edition = "2018" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
druid = { path="../.."} | ||
wasm-bindgen = "0.2.60" | ||
console_error_panic_hook = { version = "0.1.6" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# druid WASM hello world | ||
|
||
This is a minimal example of building a single druid application for the web. To build all the druid examples as wasm, check out the main wasm example. | ||
|
||
## Building | ||
|
||
You will need `cargo` and `wasm-pack` for building the code and a simple | ||
server like [`http`](https://crates.io/crates/https) for serving the web page. | ||
|
||
First build with. | ||
|
||
``` | ||
> wasm-pack build --target web --dev | ||
``` | ||
|
||
This generates a JavaScript module that exports the `wasm_main` function that's been annotated with the `#[wasm_bindgen]` macro. Leave off the `--dev` flag if you're doing a release build. | ||
|
||
Now run | ||
|
||
``` | ||
> http | ||
``` | ||
|
||
which should start serving this folder. | ||
|
||
Finally, point your browser to the appropriate localhost url (usually http://localhost:8000) and you | ||
should see your app. | ||
|
||
When you make changes to the project, re-run `wasm-pack build --target web --dev` and you can see the changes in your browser when you refresh -- no need to restart `http`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Druid WASM example</title> | ||
<style> | ||
html, | ||
body, | ||
canvas { | ||
margin: 0px; | ||
padding: 0px; | ||
width: 100%; | ||
height: 100%; | ||
overflow: hidden; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<noscript>This page contains webassembly and javascript content, please enable javascript in your | ||
browser.</noscript> | ||
<canvas id="canvas"></canvas> | ||
<script type="module" src="./index.js" type="application/javascript"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import init, { wasm_main } from "./pkg/hello_wasm_web.js"; | ||
|
||
async function run() { | ||
await init(); | ||
wasm_main(); | ||
} | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2020 The xi-editor Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use druid::widget::{Align, Flex, Label, TextBox}; | ||
use druid::{AppLauncher, Data, Env, Lens, Widget, WidgetExt, WindowDesc}; | ||
|
||
use wasm_bindgen::prelude::*; | ||
|
||
const VERTICAL_WIDGET_SPACING: f64 = 20.0; | ||
const TEXT_BOX_WIDTH: f64 = 200.0; | ||
|
||
#[derive(Clone, Data, Lens)] | ||
struct HelloState { | ||
name: String, | ||
} | ||
|
||
// This wrapper function is the primary modification we're making to the vanilla | ||
// hello.rs example. | ||
#[wasm_bindgen] | ||
pub fn wasm_main() { | ||
// This hook is necessary to get panic messages on wasm32 | ||
std::panic::set_hook(Box::new(console_error_panic_hook::hook)); | ||
main() | ||
} | ||
|
||
pub fn main() { | ||
// describe the main window | ||
// | ||
// Window title is set in index.html and window size is ignored on the web, | ||
// so can we leave those off. | ||
let main_window = WindowDesc::new(build_root_widget); | ||
|
||
// create the initial app state | ||
let initial_state = HelloState { | ||
name: "World".into(), | ||
}; | ||
|
||
// start the application | ||
AppLauncher::with_window(main_window) | ||
.launch(initial_state) | ||
.expect("Failed to launch application"); | ||
} | ||
|
||
fn build_root_widget() -> impl Widget<HelloState> { | ||
// a label that will determine its text based on the current app data. | ||
let label = Label::new(|data: &HelloState, _env: &Env| format!("Hello {}!", data.name)); | ||
// a textbox that modifies `name`. | ||
let textbox = TextBox::new() | ||
.with_placeholder("Who are we greeting?") | ||
.fix_width(TEXT_BOX_WIDTH) | ||
.lens(HelloState::name); | ||
|
||
// arrange the two widgets vertically, with some padding | ||
let layout = Flex::column() | ||
.with_child(label) | ||
.with_spacer(VERTICAL_WIDGET_SPACING) | ||
.with_child(textbox); | ||
|
||
// center the two widgets in the available space | ||
Align::centered(layout) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Would be nice if this one could get a comment, explaining that this is necessary to get panic messages on wasm32.
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 was unaware of what this actually did, so I hope my copy of your explanation is sufficient.