From 721686628171f24d656b64f89bbbe3c0841099b4 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 27 Mar 2024 21:53:42 +0100 Subject: [PATCH 1/2] use compile error to detect missing web_sys_unstable_apis and output a helpful error --- src/lib.rs | 43 +++++++++++++++++++++++++++++++++---------- src/systems.rs | 18 +++++++++++++++--- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7cd739ac0..41477b21f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,13 @@ //! //! - [`bevy-inspector-egui`](https://github.com/jakobhellermann/bevy-inspector-egui) +#[cfg(all( + feature = "manage_clipboard", + target_arch = "wasm32", + not(web_sys_unstable_apis) +))] +compile_error!("bevy_egui uses unstable APIs to support clipboard on web, please add `--cfg=web_sys_unstable_apis` in your rustflags or disable the `bevy_egui::manage_clipboard` feature."); + /// Egui render node. #[cfg(feature = "render")] pub mod egui_node; @@ -59,7 +66,11 @@ pub mod render_systems; /// Plugin systems. pub mod systems; /// Clipboard management for web -#[cfg(all(feature = "manage_clipboard", target_arch = "wasm32"))] +#[cfg(all( + feature = "manage_clipboard", + target_arch = "wasm32", + web_sys_unstable_apis +))] pub mod web_clipboard; pub use egui; @@ -176,11 +187,15 @@ pub struct EguiInput(pub egui::RawInput); pub struct EguiClipboard { #[cfg(not(target_arch = "wasm32"))] clipboard: thread_local::ThreadLocal>>, - #[cfg(target_arch = "wasm32")] + #[cfg(all(target_arch = "wasm32", web_sys_unstable_apis))] clipboard: web_clipboard::WebClipboard, } -#[cfg(all(feature = "manage_clipboard", not(target_os = "android")))] +#[cfg(all( + feature = "manage_clipboard", + not(target_os = "android"), + not(all(target_arch = "wasm32", not(web_sys_unstable_apis))) +))] impl EguiClipboard { /// Sets clipboard contents. pub fn set_contents(&mut self, contents: &str) { @@ -189,7 +204,7 @@ impl EguiClipboard { /// Sets the internal buffer of clipboard contents. /// This buffer is used to remember the contents of the last "Paste" event. - #[cfg(target_arch = "wasm32")] + #[cfg(all(target_arch = "wasm32", web_sys_unstable_apis))] pub fn set_contents_internal(&mut self, contents: &str) { self.clipboard.set_contents_internal(contents); } @@ -203,13 +218,13 @@ impl EguiClipboard { /// Gets clipboard contents. Returns [`None`] if clipboard provider is unavailable or returns an error. #[must_use] - #[cfg(target_arch = "wasm32")] + #[cfg(all(target_arch = "wasm32", web_sys_unstable_apis))] pub fn get_contents(&mut self) -> Option { self.get_contents_impl() } /// Receives a clipboard event sent by the `copy`/`cut`/`paste` listeners. - #[cfg(target_arch = "wasm32")] + #[cfg(all(target_arch = "wasm32", web_sys_unstable_apis))] pub fn try_receive_clipboard_event(&self) -> Option { self.clipboard.try_receive_clipboard_event() } @@ -223,7 +238,7 @@ impl EguiClipboard { } } - #[cfg(target_arch = "wasm32")] + #[cfg(all(target_arch = "wasm32", web_sys_unstable_apis))] fn set_contents_impl(&mut self, contents: &str) { self.clipboard.set_contents(contents); } @@ -239,7 +254,7 @@ impl EguiClipboard { None } - #[cfg(target_arch = "wasm32")] + #[cfg(all(target_arch = "wasm32", web_sys_unstable_apis))] #[allow(clippy::unnecessary_wraps)] fn get_contents_impl(&mut self) -> Option { self.clipboard.get_contents() @@ -598,7 +613,11 @@ impl Plugin for EguiPlugin { world.init_resource::(); #[cfg(all(feature = "manage_clipboard", not(target_os = "android")))] world.init_resource::(); - #[cfg(all(feature = "manage_clipboard", target_arch = "wasm32"))] + #[cfg(all( + feature = "manage_clipboard", + target_arch = "wasm32", + web_sys_unstable_apis + ))] world.init_non_send_resource::(); #[cfg(feature = "render")] world.init_resource::(); @@ -617,7 +636,11 @@ impl Plugin for EguiPlugin { #[cfg(feature = "render")] app.add_plugins(ExtractComponentPlugin::::default()); - #[cfg(all(feature = "manage_clipboard", target_arch = "wasm32"))] + #[cfg(all( + feature = "manage_clipboard", + target_arch = "wasm32", + web_sys_unstable_apis + ))] app.add_systems(PreStartup, web_clipboard::startup_setup_web_events); app.add_systems( PreStartup, diff --git a/src/systems.rs b/src/systems.rs index eb03b99d2..b4c81f970 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -72,7 +72,11 @@ pub struct ModifierKeysState { #[allow(missing_docs)] #[derive(SystemParam)] pub struct InputResources<'w, 's> { - #[cfg(all(feature = "manage_clipboard", not(target_os = "android")))] + #[cfg(all( + feature = "manage_clipboard", + not(target_os = "android"), + not(all(target_arch = "wasm32", not(web_sys_unstable_apis))) + ))] pub egui_clipboard: ResMut<'w, crate::EguiClipboard>, pub modifier_keys_state: Local<'s, ModifierKeysState>, #[system_param(ignore)] @@ -328,7 +332,11 @@ pub fn process_input_system( } } - #[cfg(all(feature = "manage_clipboard", target_arch = "wasm32"))] + #[cfg(all( + feature = "manage_clipboard", + target_arch = "wasm32", + web_sys_unstable_apis + ))] while let Some(event) = input_resources.egui_clipboard.try_receive_clipboard_event() { match event { crate::web_clipboard::WebClipboardEvent::Copy => { @@ -505,7 +513,11 @@ pub fn process_output_system( context.egui_output.platform_output = platform_output.clone(); - #[cfg(all(feature = "manage_clipboard", not(target_os = "android")))] + #[cfg(all( + feature = "manage_clipboard", + not(target_os = "android"), + not(all(target_arch = "wasm32", not(web_sys_unstable_apis))) + ))] if !platform_output.copied_text.is_empty() { egui_clipboard.set_contents(&platform_output.copied_text); } From eb57930ca23741dda1eb22d631c31e5a9022bd16 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 27 Mar 2024 23:11:24 +0100 Subject: [PATCH 2/2] link to rustwasm doc + cleaner message (through include_str) --- src/lib.rs | 2 +- static/error_web_sys_unstable_apis.txt | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 static/error_web_sys_unstable_apis.txt diff --git a/src/lib.rs b/src/lib.rs index 41477b21f..03a2f0587 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,7 +55,7 @@ target_arch = "wasm32", not(web_sys_unstable_apis) ))] -compile_error!("bevy_egui uses unstable APIs to support clipboard on web, please add `--cfg=web_sys_unstable_apis` in your rustflags or disable the `bevy_egui::manage_clipboard` feature."); +compile_error!(include_str!("../static/error_web_sys_unstable_apis.txt")); /// Egui render node. #[cfg(feature = "render")] diff --git a/static/error_web_sys_unstable_apis.txt b/static/error_web_sys_unstable_apis.txt new file mode 100644 index 000000000..7b07db435 --- /dev/null +++ b/static/error_web_sys_unstable_apis.txt @@ -0,0 +1,6 @@ +bevy_egui uses unstable APIs to support clipboard on web. + +Please add `--cfg=web_sys_unstable_apis` to your rustflags or disable the `bevy_egui::manage_clipboard` feature. + +More Info: https://rustwasm.github.io/wasm-bindgen/web-sys/unstable-apis.html + \ No newline at end of file