Merge 6c6fcf277b
into 25cb339a12
This commit is contained in:
commit
e786ee49b0
15
Cargo.toml
15
Cargo.toml
@ -131,6 +131,7 @@ default = [
|
|||||||
"animation",
|
"animation",
|
||||||
"bevy_asset",
|
"bevy_asset",
|
||||||
"bevy_audio",
|
"bevy_audio",
|
||||||
|
"bevy_clipboard",
|
||||||
"bevy_color",
|
"bevy_color",
|
||||||
"bevy_core_pipeline",
|
"bevy_core_pipeline",
|
||||||
"bevy_core_widgets",
|
"bevy_core_widgets",
|
||||||
@ -307,6 +308,9 @@ bevy_log = ["bevy_internal/bevy_log"]
|
|||||||
# Enable input focus subsystem
|
# Enable input focus subsystem
|
||||||
bevy_input_focus = ["bevy_internal/bevy_input_focus"]
|
bevy_input_focus = ["bevy_internal/bevy_input_focus"]
|
||||||
|
|
||||||
|
# Clipboard access
|
||||||
|
bevy_clipboard = ["bevy_internal/bevy_clipboard"]
|
||||||
|
|
||||||
# Headless widget collection for Bevy UI.
|
# Headless widget collection for Bevy UI.
|
||||||
bevy_core_widgets = ["bevy_internal/bevy_core_widgets"]
|
bevy_core_widgets = ["bevy_internal/bevy_core_widgets"]
|
||||||
|
|
||||||
@ -3356,6 +3360,17 @@ description = "Illustrates creating and updating a button"
|
|||||||
category = "UI (User Interface)"
|
category = "UI (User Interface)"
|
||||||
wasm = true
|
wasm = true
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "clipboard"
|
||||||
|
path = "examples/ui/clipboard.rs"
|
||||||
|
doc-scrape-examples = true
|
||||||
|
|
||||||
|
[package.metadata.example.clipboard]
|
||||||
|
name = "Clipboard"
|
||||||
|
description = "Demonstrates accessing the clipboard to retrieve and display text"
|
||||||
|
category = "UI (User Interface)"
|
||||||
|
wasm = true
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "display_and_visibility"
|
name = "display_and_visibility"
|
||||||
path = "examples/ui/display_and_visibility.rs"
|
path = "examples/ui/display_and_visibility.rs"
|
||||||
|
30
crates/bevy_clipboard/Cargo.toml
Normal file
30
crates/bevy_clipboard/Cargo.toml
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
[package]
|
||||||
|
name = "bevy_clipboard"
|
||||||
|
version = "0.17.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"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
# bevy
|
||||||
|
bevy_app = { path = "../bevy_app", version = "0.17.0-dev", default-features = false }
|
||||||
|
bevy_ecs = { path = "../bevy_ecs", version = "0.17.0-dev", default-features = false }
|
||||||
|
bevy_platform = { path = "../bevy_platform", version = "0.17.0-dev", default-features = false }
|
||||||
|
|
||||||
|
[target.'cfg(any(windows, unix))'.dependencies]
|
||||||
|
arboard = { version = "3.5.0", default-features = false }
|
||||||
|
|
||||||
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
|
wasm-bindgen = { version = "0.2" }
|
||||||
|
web-sys = { version = "0.3", features = ["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
|
176
crates/bevy_clipboard/LICENSE-APACHE
Normal file
176
crates/bevy_clipboard/LICENSE-APACHE
Normal file
@ -0,0 +1,176 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
19
crates/bevy_clipboard/LICENSE-MIT
Normal file
19
crates/bevy_clipboard/LICENSE-MIT
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
7
crates/bevy_clipboard/README.md
Normal file
7
crates/bevy_clipboard/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# Bevy Clipboard
|
||||||
|
|
||||||
|
[](https://github.com/bevyengine/bevy#license)
|
||||||
|
[](https://crates.io/crates/bevy_clipboard)
|
||||||
|
[](https://crates.io/crates/bevy_clipboard)
|
||||||
|
[](https://docs.rs/bevy_clipboard/latest/bevy_clipboard/)
|
||||||
|
[](https://discord.gg/bevy)
|
251
crates/bevy_clipboard/src/lib.rs
Normal file
251
crates/bevy_clipboard/src/lib.rs
Normal file
@ -0,0 +1,251 @@
|
|||||||
|
//! This crate provides a platform-agnostic interface for accessing the clipboard
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
use bevy_ecs::resource::Resource;
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
use {alloc::sync::Arc, bevy_platform::sync::Mutex, wasm_bindgen_futures::JsFuture};
|
||||||
|
|
||||||
|
/// The clipboard prelude
|
||||||
|
pub mod prelude {
|
||||||
|
pub use crate::{Clipboard, ClipboardRead};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Clipboard plugin
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct ClipboardPlugin;
|
||||||
|
|
||||||
|
impl bevy_app::Plugin for ClipboardPlugin {
|
||||||
|
fn build(&self, app: &mut bevy_app::App) {
|
||||||
|
app.init_resource::<Clipboard>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents an attempt to read from the clipboard.
|
||||||
|
///
|
||||||
|
/// On desktop targets the result is available immediately.
|
||||||
|
/// On wasm32 the result is fetched asynchronously.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ClipboardRead {
|
||||||
|
/// The clipboard contents are ready to be accessed.
|
||||||
|
Ready(Result<String, ClipboardError>),
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
/// The clipboard contents are being fetched asynchronously.
|
||||||
|
Pending(Arc<Mutex<Option<Result<String, ClipboardError>>>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ClipboardRead {
|
||||||
|
/// The result of an attempt to read from the clipboard, once ready.
|
||||||
|
/// If the result is still pending, returns `None`.
|
||||||
|
pub fn poll_result(&mut self) -> Option<Result<String, ClipboardError>> {
|
||||||
|
match self {
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
Self::Pending(shared) => {
|
||||||
|
if let Some(contents) = shared.lock().ok().and_then(|mut inner| inner.take()) {
|
||||||
|
*self = Self::Ready(Err(ClipboardError::ContentTaken));
|
||||||
|
Some(contents)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self::Ready(inner) => {
|
||||||
|
Some(core::mem::replace(inner, Err(ClipboardError::ContentTaken)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resource providing access to the clipboard
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct Clipboard(Option<arboard::Clipboard>);
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
impl Default for Clipboard {
|
||||||
|
fn default() -> Self {
|
||||||
|
{
|
||||||
|
Self(arboard::Clipboard::new().ok())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resource providing access to the clipboard
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
#[derive(Resource, Default)]
|
||||||
|
pub struct Clipboard;
|
||||||
|
|
||||||
|
impl Clipboard {
|
||||||
|
/// Fetches UTF-8 text from the clipboard and returns it via a `ClipboardRead`.
|
||||||
|
///
|
||||||
|
/// On Windows and Unix `ClipboardRead`s are completed instantly, on wasm32 the result is fetched asynchronously.
|
||||||
|
pub fn fetch_text(&mut self) -> ClipboardRead {
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
ClipboardRead::Ready(if let Some(clipboard) = self.0.as_mut() {
|
||||||
|
clipboard.get_text().map_err(ClipboardError::from)
|
||||||
|
} else {
|
||||||
|
Err(ClipboardError::ClipboardNotSupported)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
{
|
||||||
|
ClipboardRead::Ready(
|
||||||
|
arboard::Clipboard::new()
|
||||||
|
.and_then(|mut clipboard| clipboard.get_text())
|
||||||
|
.map_err(ClipboardError::from),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
{
|
||||||
|
if let Some(clipboard) = web_sys::window().map(|w| w.navigator().clipboard()) {
|
||||||
|
let shared = Arc::new(Mutex::new(None));
|
||||||
|
let shared_clone = shared.clone();
|
||||||
|
wasm_bindgen_futures::spawn_local(async move {
|
||||||
|
let text = JsFuture::from(clipboard.read_text()).await;
|
||||||
|
let text = match text {
|
||||||
|
Ok(text) => text.as_string().ok_or(ClipboardError::ConversionFailure),
|
||||||
|
Err(_) => Err(ClipboardError::ContentNotAvailable),
|
||||||
|
};
|
||||||
|
shared.lock().unwrap().replace(text);
|
||||||
|
});
|
||||||
|
ClipboardRead::Pending(shared_clone)
|
||||||
|
} else {
|
||||||
|
ClipboardRead::Ready(Err(ClipboardError::ClipboardNotSupported))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(unix, windows, target_arch = "wasm32")))]
|
||||||
|
{
|
||||||
|
ClipboardRead::Ready(Err(ClipboardError::ClipboardNotSupported))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Asynchronously retrieves UTF-8 text from the system clipboard.
|
||||||
|
pub async fn fetch_text_async(&mut self) -> Result<String, ClipboardError> {
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
if let Some(clipboard) = self.0.as_mut() {
|
||||||
|
clipboard.get_text().map_err(ClipboardError::from)
|
||||||
|
} else {
|
||||||
|
Err(ClipboardError::ClipboardNotSupported)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
{
|
||||||
|
arboard::Clipboard::new()
|
||||||
|
.and_then(|mut clipboard| clipboard.get_text())
|
||||||
|
.map_err(ClipboardError::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
{
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
use wasm_bindgen_futures::JsFuture;
|
||||||
|
|
||||||
|
let clipboard = web_sys::window()
|
||||||
|
.and_then(|w| w.navigator().clipboard())
|
||||||
|
.ok_or(ClipboardError::ClipboardNotSupported)?;
|
||||||
|
|
||||||
|
let result = JsFuture::from(clipboard.read_text()).await;
|
||||||
|
match result {
|
||||||
|
Ok(val) => val.as_string().ok_or(ClipboardError::ConversionFailure),
|
||||||
|
Err(_) => Err(ClipboardError::ContentNotAvailable),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(unix, windows, target_arch = "wasm32")))]
|
||||||
|
{
|
||||||
|
Err(ClipboardError::ClipboardNotSupported)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Places the text onto the clipboard. Any valid UTF-8 string is accepted.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns error if `text` failed to be stored on the clipboard.
|
||||||
|
pub fn set_text<'a, T: Into<alloc::borrow::Cow<'a, str>>>(
|
||||||
|
&mut self,
|
||||||
|
text: T,
|
||||||
|
) -> Result<(), ClipboardError> {
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
if let Some(clipboard) = self.0.as_mut() {
|
||||||
|
clipboard.set_text(text).map_err(ClipboardError::from)
|
||||||
|
} else {
|
||||||
|
Err(ClipboardError::ClipboardNotSupported)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
{
|
||||||
|
arboard::Clipboard::new()
|
||||||
|
.and_then(|mut clipboard| clipboard.set_text(text))
|
||||||
|
.map_err(ClipboardError::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
{
|
||||||
|
if let Some(clipboard) = web_sys::window().map(|w| w.navigator().clipboard()) {
|
||||||
|
let text = text.into().to_string();
|
||||||
|
wasm_bindgen_futures::spawn_local(async move {
|
||||||
|
let _ = JsFuture::from(clipboard.write_text(&text)).await;
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(ClipboardError::ClipboardNotSupported)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(unix, windows, target_arch = "wasm32")))]
|
||||||
|
{
|
||||||
|
Err(ClipboardError::ClipboardNotSupported)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An error that might happen during a clipboard operation.
|
||||||
|
#[non_exhaustive]
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum ClipboardError {
|
||||||
|
/// Clipboard contents were unavailable or not in the expected format.
|
||||||
|
ContentNotAvailable,
|
||||||
|
|
||||||
|
/// No suitable clipboard backend was available
|
||||||
|
ClipboardNotSupported,
|
||||||
|
|
||||||
|
/// Clipboard access is temporarily locked by another process or thread.
|
||||||
|
ClipboardOccupied,
|
||||||
|
|
||||||
|
/// The data could not be converted to or from the required format.
|
||||||
|
ConversionFailure,
|
||||||
|
|
||||||
|
/// The clipboard content was already taken from the `ClipboardRead`.
|
||||||
|
ContentTaken,
|
||||||
|
|
||||||
|
/// An unknown error
|
||||||
|
Unknown {
|
||||||
|
/// String describing the error
|
||||||
|
description: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(windows, unix))]
|
||||||
|
impl From<arboard::Error> for ClipboardError {
|
||||||
|
fn from(value: arboard::Error) -> Self {
|
||||||
|
match value {
|
||||||
|
arboard::Error::ContentNotAvailable => ClipboardError::ContentNotAvailable,
|
||||||
|
arboard::Error::ClipboardNotSupported => ClipboardError::ClipboardNotSupported,
|
||||||
|
arboard::Error::ClipboardOccupied => ClipboardError::ClipboardOccupied,
|
||||||
|
arboard::Error::ConversionFailure => ClipboardError::ConversionFailure,
|
||||||
|
arboard::Error::Unknown { description } => ClipboardError::Unknown { description },
|
||||||
|
_ => ClipboardError::Unknown {
|
||||||
|
description: "Unknown arboard error variant".to_owned(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -367,6 +367,9 @@ web = [
|
|||||||
"bevy_tasks/web",
|
"bevy_tasks/web",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Clipboard support
|
||||||
|
bevy_clipboard = ["dep:bevy_clipboard"]
|
||||||
|
|
||||||
hotpatching = ["bevy_app/hotpatching", "bevy_ecs/hotpatching"]
|
hotpatching = ["bevy_app/hotpatching", "bevy_ecs/hotpatching"]
|
||||||
|
|
||||||
gltf_convert_coordinates_default = [
|
gltf_convert_coordinates_default = [
|
||||||
@ -456,6 +459,7 @@ bevy_window = { path = "../bevy_window", optional = true, version = "0.17.0-dev"
|
|||||||
"bevy_reflect",
|
"bevy_reflect",
|
||||||
] }
|
] }
|
||||||
bevy_winit = { path = "../bevy_winit", optional = true, version = "0.17.0-dev", default-features = false }
|
bevy_winit = { path = "../bevy_winit", optional = true, version = "0.17.0-dev", default-features = false }
|
||||||
|
bevy_clipboard = { path = "../bevy_clipboard", optional = true, version = "0.17.0-dev" }
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
@ -68,6 +68,8 @@ plugin_group! {
|
|||||||
bevy_dev_tools:::DevToolsPlugin,
|
bevy_dev_tools:::DevToolsPlugin,
|
||||||
#[cfg(feature = "bevy_ci_testing")]
|
#[cfg(feature = "bevy_ci_testing")]
|
||||||
bevy_dev_tools::ci_testing:::CiTestingPlugin,
|
bevy_dev_tools::ci_testing:::CiTestingPlugin,
|
||||||
|
#[cfg(feature = "bevy_clipboard")]
|
||||||
|
bevy_clipboard:::ClipboardPlugin,
|
||||||
#[cfg(feature = "hotpatching")]
|
#[cfg(feature = "hotpatching")]
|
||||||
bevy_app::hotpatch:::HotPatchPlugin,
|
bevy_app::hotpatch:::HotPatchPlugin,
|
||||||
#[plugin_group]
|
#[plugin_group]
|
||||||
|
@ -27,6 +27,8 @@ pub use bevy_asset as asset;
|
|||||||
pub use bevy_audio as audio;
|
pub use bevy_audio as audio;
|
||||||
#[cfg(feature = "bevy_camera")]
|
#[cfg(feature = "bevy_camera")]
|
||||||
pub use bevy_camera as camera;
|
pub use bevy_camera as camera;
|
||||||
|
#[cfg(feature = "bevy_clipboard")]
|
||||||
|
pub use bevy_clipboard as clipboard;
|
||||||
#[cfg(feature = "bevy_color")]
|
#[cfg(feature = "bevy_color")]
|
||||||
pub use bevy_color as color;
|
pub use bevy_color as color;
|
||||||
#[cfg(feature = "bevy_core_pipeline")]
|
#[cfg(feature = "bevy_core_pipeline")]
|
||||||
|
@ -86,3 +86,7 @@ pub use crate::gltf::prelude::*;
|
|||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[cfg(feature = "bevy_picking")]
|
#[cfg(feature = "bevy_picking")]
|
||||||
pub use crate::picking::prelude::*;
|
pub use crate::picking::prelude::*;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "bevy_clipboard")]
|
||||||
|
pub use crate::clipboard::prelude::*;
|
||||||
|
@ -19,6 +19,7 @@ The default feature set enables most of the expected features of a game engine,
|
|||||||
|bevy_anti_aliasing|Provides various anti aliasing solutions|
|
|bevy_anti_aliasing|Provides various anti aliasing solutions|
|
||||||
|bevy_asset|Provides asset functionality|
|
|bevy_asset|Provides asset functionality|
|
||||||
|bevy_audio|Provides audio functionality|
|
|bevy_audio|Provides audio functionality|
|
||||||
|
|bevy_clipboard|Clipboard access|
|
||||||
|bevy_color|Provides shared color types and operations|
|
|bevy_color|Provides shared color types and operations|
|
||||||
|bevy_core_pipeline|Provides cameras and other basic render pipeline features|
|
|bevy_core_pipeline|Provides cameras and other basic render pipeline features|
|
||||||
|bevy_core_widgets|Headless widget collection for Bevy UI.|
|
|bevy_core_widgets|Headless widget collection for Bevy UI.|
|
||||||
|
@ -550,6 +550,7 @@ Example | Description
|
|||||||
[Box Shadow](../examples/ui/box_shadow.rs) | Demonstrates how to create a node with a shadow
|
[Box Shadow](../examples/ui/box_shadow.rs) | Demonstrates how to create a node with a shadow
|
||||||
[Button](../examples/ui/button.rs) | Illustrates creating and updating a button
|
[Button](../examples/ui/button.rs) | Illustrates creating and updating a button
|
||||||
[CSS Grid](../examples/ui/grid.rs) | An example for CSS Grid layout
|
[CSS Grid](../examples/ui/grid.rs) | An example for CSS Grid layout
|
||||||
|
[Clipboard](../examples/ui/clipboard.rs) | Demonstrates accessing the clipboard to retrieve and display text
|
||||||
[Core Widgets](../examples/ui/core_widgets.rs) | Demonstrates use of core (headless) widgets in Bevy UI
|
[Core Widgets](../examples/ui/core_widgets.rs) | Demonstrates use of core (headless) widgets in Bevy UI
|
||||||
[Core Widgets (w/Observers)](../examples/ui/core_widgets_observers.rs) | Demonstrates use of core (headless) widgets in Bevy UI, with Observers
|
[Core Widgets (w/Observers)](../examples/ui/core_widgets_observers.rs) | Demonstrates use of core (headless) widgets in Bevy UI, with Observers
|
||||||
[Directional Navigation](../examples/ui/directional_navigation.rs) | Demonstration of Directional Navigation between UI elements
|
[Directional Navigation](../examples/ui/directional_navigation.rs) | Demonstration of Directional Navigation between UI elements
|
||||||
|
160
examples/ui/clipboard.rs
Normal file
160
examples/ui/clipboard.rs
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
//! This example demonstrates accessing the clipboard to retrieve and display text.
|
||||||
|
|
||||||
|
use bevy::{
|
||||||
|
clipboard::{Clipboard, ClipboardRead},
|
||||||
|
color::palettes::css::{GREY, NAVY, RED},
|
||||||
|
diagnostic::FrameTimeDiagnosticsPlugin,
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
App::new()
|
||||||
|
.add_plugins((DefaultPlugins, FrameTimeDiagnosticsPlugin::default()))
|
||||||
|
.add_systems(Startup, setup)
|
||||||
|
.add_systems(Update, paste_text_system)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
|
||||||
|
const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25);
|
||||||
|
|
||||||
|
/// Button discriminator
|
||||||
|
#[derive(Component)]
|
||||||
|
pub enum ButtonAction {
|
||||||
|
/// The button pastes some text from the clipboard
|
||||||
|
PasteText,
|
||||||
|
/// The button sends some text to the clipboard
|
||||||
|
SetText,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Marker component for text box paste target
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct PasteTarget;
|
||||||
|
|
||||||
|
fn setup(mut commands: Commands) {
|
||||||
|
// UI camera
|
||||||
|
commands.spawn(Camera2d);
|
||||||
|
|
||||||
|
commands.spawn((
|
||||||
|
Node {
|
||||||
|
width: Val::Percent(100.0),
|
||||||
|
height: Val::Percent(100.0),
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
children![(
|
||||||
|
Node {
|
||||||
|
flex_direction: FlexDirection::Column,
|
||||||
|
padding: UiRect::all(Val::Px(30.)),
|
||||||
|
row_gap: Val::Px(20.),
|
||||||
|
justify_content: JustifyContent::SpaceBetween,
|
||||||
|
align_items: AlignItems::Stretch,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
BackgroundColor(NAVY.into()),
|
||||||
|
children![
|
||||||
|
(
|
||||||
|
Node {
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Text::new("Bevy clipboard example"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Node {
|
||||||
|
width: Val::Px(500.),
|
||||||
|
height: Val::Px(250.),
|
||||||
|
padding: UiRect::all(Val::Px(3.)),
|
||||||
|
border: UiRect::all(Val::Px(2.)),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
BorderColor::all(Color::WHITE),
|
||||||
|
BackgroundColor(Color::BLACK),
|
||||||
|
children![(
|
||||||
|
Text::new("Nothing pasted yet."),
|
||||||
|
TextColor(GREY.into()),
|
||||||
|
PasteTarget
|
||||||
|
)],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Node {
|
||||||
|
border: UiRect::all(Val::Px(2.)),
|
||||||
|
padding: UiRect::all(Val::Px(10.)),
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Button,
|
||||||
|
ButtonAction::PasteText,
|
||||||
|
BorderColor::all(Color::WHITE),
|
||||||
|
BackgroundColor(Color::BLACK),
|
||||||
|
children![Text::new("Click to paste text")],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Node {
|
||||||
|
border: UiRect::all(Val::Px(2.)),
|
||||||
|
padding: UiRect::all(Val::Px(10.)),
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Button,
|
||||||
|
ButtonAction::SetText,
|
||||||
|
BorderColor::all(Color::WHITE),
|
||||||
|
BackgroundColor(Color::BLACK),
|
||||||
|
children![Text::new("Click to copy 'Hello bevy!'\nto the clipboard")],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn paste_text_system(
|
||||||
|
mut paste: Local<Option<ClipboardRead>>,
|
||||||
|
mut clipboard: ResMut<Clipboard>,
|
||||||
|
mut interaction_query: Query<
|
||||||
|
(
|
||||||
|
&Interaction,
|
||||||
|
&mut BackgroundColor,
|
||||||
|
&mut BorderColor,
|
||||||
|
&ButtonAction,
|
||||||
|
),
|
||||||
|
(Changed<Interaction>, With<Button>),
|
||||||
|
>,
|
||||||
|
mut text_query: Query<(&mut Text, &mut TextColor), With<PasteTarget>>,
|
||||||
|
) {
|
||||||
|
if let Some(contents) = paste.as_mut() {
|
||||||
|
if let Some(contents) = contents.poll_result() {
|
||||||
|
let (message, color) = match contents {
|
||||||
|
Ok(text) => (text, Color::WHITE),
|
||||||
|
Err(error) => (format!("{error:?}"), RED.into()),
|
||||||
|
};
|
||||||
|
for (mut text, mut text_color) in text_query.iter_mut() {
|
||||||
|
text.0 = message.clone();
|
||||||
|
text_color.0 = color;
|
||||||
|
}
|
||||||
|
*paste = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (interaction, mut color, mut border_color, button_action) in &mut interaction_query {
|
||||||
|
match *interaction {
|
||||||
|
Interaction::Pressed => {
|
||||||
|
match button_action {
|
||||||
|
ButtonAction::PasteText => {
|
||||||
|
*paste = Some(clipboard.fetch_text());
|
||||||
|
}
|
||||||
|
ButtonAction::SetText => {
|
||||||
|
clipboard.set_text("Hello bevy!").ok();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Interaction::Hovered => {
|
||||||
|
*color = HOVERED_BUTTON.into();
|
||||||
|
border_color.set_all(Color::WHITE);
|
||||||
|
}
|
||||||
|
Interaction::None => {
|
||||||
|
*color = NORMAL_BUTTON.into();
|
||||||
|
border_color.set_all(GREY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user