Files
bevy/crates/bevy_clipboard/Cargo.toml
T
ickshonpe 41f170c0a3 Basic clipboard support (#19106)
# Objective

Add a platform-agnostic interface for interacting with the clipboard.

## Solution

New crate `bevy_clipboard` with a `ClipboardPlugin` that adds a
`Clipboard` resource. The clipboard is accessed using the methods
`fetch_text`, `fetch_image`, `set_text` and `set_image` on the
`Clipboard` resource. `fetch_text` returns a `ClipboardRead` with a
`poll_result` method that's used to get the actual value once it's
ready.

The `windows` and `unix` implementations both use the `arboard` crate.
On windows the `Clipboard` resource is a unit struct and a new arboard
clipboard instance is created and dropped for each clipboard access. On
unix targets the `Clipboard` resource holds a clipboard instance it
reuses each time. On both targets the `fetch_*` and `set_*` methods work
instantly.

On `wasm32` `Clipboard` is a unit struct. The `fetch_text` and
`set_text` functions spawn async tasks. The task spawned by `fetch_text`
updates the shared arc mutex option once the future is evaluated to get
the clipboard text. There is no image support on `wasm32`.

Everything seems to work but it feels like the design is a bit clumsy
and not very idiomatic. I don't tend to do much asynchronous
programming, maybe a reviewer can suggest an improved construction.

I also added an alternative `fetch_text_async` function for async access
that returns a `Result<String, ClipboardError>` future.

### Notes
* Doesn't support android targets yet. 
* The wasm32 implementation doesn't support images. It's much more
complicated and probably best to left to a follow up.

## Testing

The PR includes a basic example `clipboard` that can be used for
testing.
The image display will only work if the image is already in the
clipboard before the example starts.

---------

Co-authored-by: Gilles Henaux <ghx_github_priv@fastmail.com>
Co-authored-by: Andrew Zhurov <zhurov.andrew@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2026-04-23 21:18:07 +00:00

46 lines
1.6 KiB
TOML

[package]
name = "bevy_clipboard"
version = "0.19.0-dev"
edition = "2024"
description = "Provides clipboard support for Bevy Engine"
homepage = "https://bevyengine.org"
repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"
keywords = ["bevy", "clipboard"]
[features]
default = []
system_clipboard = ["dep:arboard"]
image = [
"system_clipboard",
"dep:bevy_asset",
"dep:bevy_image",
"dep:wgpu-types",
"arboard/image-data",
]
[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.19.0-dev", default-features = false }
bevy_ecs = { path = "../bevy_ecs", version = "0.19.0-dev", default-features = false }
bevy_log = { path = "../bevy_log", version = "0.19.0-dev", default-features = false }
bevy_platform = { path = "../bevy_platform", version = "0.19.0-dev", default-features = false }
[target.'cfg(any(windows, unix))'.dependencies]
bevy_asset = { path = "../bevy_asset", version = "0.19.0-dev", default-features = false, optional = true }
bevy_image = { path = "../bevy_image", version = "0.19.0-dev", default-features = false, optional = true }
wgpu-types = { version = "29.0.1", default-features = false, optional = true }
arboard = { version = "3.6.1", default-features = false, optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = ["Window", "Navigator", "Clipboard"] }
wasm-bindgen-futures = "0.4"
[lints]
workspace = true
[package.metadata.docs.rs]
rustdoc-args = ["-Zunstable-options", "--generate-link-to-definition"]
all-features = true