From 650450bc3a01f8fe44ba89781597c3c8f60c2777 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 29 Dec 2020 12:42:15 +0100 Subject: [PATCH] Make cfg=web_sys_unstable_apis optional, fixing `cargo check` --- .github/workflows/rust.yml | 3 +++ build_demo_web.sh | 13 ++++++++----- build_example_web.sh | 13 ++++++++----- check.sh | 2 -- egui_web/src/lib.rs | 8 ++++++++ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ea294c16..2d366d89 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -3,6 +3,9 @@ on: [push, pull_request] name: CI env: + # This is required to enable the web_sys clipboard API which egui_web uses + # https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html + # https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html RUSTFLAGS: --cfg=web_sys_unstable_apis jobs: diff --git a/build_demo_web.sh b/build_demo_web.sh index 5a043251..f4e4663d 100755 --- a/build_demo_web.sh +++ b/build_demo_web.sh @@ -3,20 +3,23 @@ set -eu CRATE_NAME="egui_demo" -export RUSTFLAGS=--cfg=web_sys_unstable_apis # required for the clipboard API +# This is required to enable the web_sys clipboard API which egui_web uses +# https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html +# https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html +export RUSTFLAGS=--cfg=web_sys_unstable_apis # Clear output from old stuff: -rm -rf docs/$CRATE_NAME.wasm +rm -f docs/${CRATE_NAME}_bg.wasm echo "Building rust…" BUILD=release -cargo build --release -p $CRATE_NAME --lib --target wasm32-unknown-unknown +cargo build --release -p ${CRATE_NAME} --lib --target wasm32-unknown-unknown echo "Generating JS bindings for wasm…" -TARGET_NAME="$CRATE_NAME.wasm" +TARGET_NAME="${CRATE_NAME}.wasm" wasm-bindgen "target/wasm32-unknown-unknown/$BUILD/$TARGET_NAME" \ --out-dir docs --no-modules --no-typescript -echo "Finished: docs/$CRATE_NAME.wasm" +echo "Finished: docs/${CRATE_NAME}.wasm" open http://localhost:8888/index.html diff --git a/build_example_web.sh b/build_example_web.sh index 0959e8dc..439890fa 100755 --- a/build_example_web.sh +++ b/build_example_web.sh @@ -3,20 +3,23 @@ set -eu CRATE_NAME="example_web" -export RUSTFLAGS=--cfg=web_sys_unstable_apis # required for the clipboard API +# This is required to enable the web_sys clipboard API which egui_web uses +# https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html +# https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html +export RUSTFLAGS=--cfg=web_sys_unstable_apis # Clear output from old stuff: -rm -rf docs/$CRATE_NAME.wasm +rm -f docs/${CRATE_NAME}_bg.wasm echo "Building rust…" BUILD=release -cargo build --release -p $CRATE_NAME --lib --target wasm32-unknown-unknown +cargo build --release -p ${CRATE_NAME} --lib --target wasm32-unknown-unknown echo "Generating JS bindings for wasm…" -TARGET_NAME="$CRATE_NAME.wasm" +TARGET_NAME="${CRATE_NAME}.wasm" wasm-bindgen "target/wasm32-unknown-unknown/$BUILD/$TARGET_NAME" \ --out-dir docs --no-modules --no-typescript -echo "Finished: docs/$CRATE_NAME.wasm" +echo "Finished: docs/${CRATE_NAME}.wasm" open http://localhost:8888/example.html diff --git a/check.sh b/check.sh index 38c1f294..a4b38b12 100755 --- a/check.sh +++ b/check.sh @@ -1,8 +1,6 @@ #!/bin/bash set -eu -export RUSTFLAGS=--cfg=web_sys_unstable_apis # required for the clipboard API - cargo check --workspace --all-targets --all-features --release cargo fmt --all -- --check CARGO_INCREMENTAL=0 cargo clippy --workspace --all-targets --all-features -- -D warnings -W clippy::all #-W clippy::pedantic -W clippy::restriction -W clippy::nursery diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index 73dcc46b..67ce1a68 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -197,9 +197,13 @@ pub fn handle_output(output: &egui::Output) { crate::open_url(url); } + #[cfg(web_sys_unstable_apis)] if !copied_text.is_empty() { set_clipboard_text(copied_text); } + + #[cfg(not(web_sys_unstable_apis))] + let _ = copied_text; } pub fn set_cursor_icon(cursor: egui::CursorIcon) -> Option<()> { @@ -211,6 +215,7 @@ pub fn set_cursor_icon(cursor: egui::CursorIcon) -> Option<()> { .ok() } +#[cfg(web_sys_unstable_apis)] pub fn set_clipboard_text(s: &str) { if let Some(window) = web_sys::window() { let clipboard = window.navigator().clipboard(); @@ -436,6 +441,7 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { closure.forget(); } + #[cfg(web_sys_unstable_apis)] { // paste let runner_ref = runner_ref.clone(); @@ -452,6 +458,7 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { closure.forget(); } + #[cfg(web_sys_unstable_apis)] { // cut let runner_ref = runner_ref.clone(); @@ -464,6 +471,7 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { closure.forget(); } + #[cfg(web_sys_unstable_apis)] { // copy let runner_ref = runner_ref.clone();