Skip to content
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

Better error on Web when missing web_sys_unstable_apis #270

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(include_str!("../static/error_web_sys_unstable_apis.txt"));

/// Egui render node.
#[cfg(feature = "render")]
pub mod egui_node;
Expand All @@ -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;
Expand Down Expand Up @@ -176,11 +187,15 @@ pub struct EguiInput(pub egui::RawInput);
pub struct EguiClipboard {
#[cfg(not(target_arch = "wasm32"))]
clipboard: thread_local::ThreadLocal<Option<RefCell<Clipboard>>>,
#[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) {
Expand All @@ -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);
}
Expand All @@ -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<String> {
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<web_clipboard::WebClipboardEvent> {
self.clipboard.try_receive_clipboard_event()
}
Expand All @@ -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);
}
Expand All @@ -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<String> {
self.clipboard.get_contents()
Expand Down Expand Up @@ -598,7 +613,11 @@ impl Plugin for EguiPlugin {
world.init_resource::<EguiManagedTextures>();
#[cfg(all(feature = "manage_clipboard", not(target_os = "android")))]
world.init_resource::<EguiClipboard>();
#[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::<web_clipboard::SubscribedEvents>();
#[cfg(feature = "render")]
world.init_resource::<EguiUserTextures>();
Expand All @@ -617,7 +636,11 @@ impl Plugin for EguiPlugin {
#[cfg(feature = "render")]
app.add_plugins(ExtractComponentPlugin::<EguiRenderOutput>::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,
Expand Down
18 changes: 15 additions & 3 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions static/error_web_sys_unstable_apis.txt
Original file line number Diff line number Diff line change
@@ -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

Loading