-
Notifications
You must be signed in to change notification settings - Fork 252
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
web clipboard handling #178
Closed
Closed
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
493a21f
wip clipboard example
Vrixyz 205cdde
wip read clipboard, fail :(
Vrixyz 9e85a90
wip attempt to use web_sys paste event
Vrixyz 69c447d
progress, paste with bugs implemented :)
Vrixyz 0ec3ed9
integrate clipboard code into bevy_egui
Vrixyz 9032a4e
apply review; remove bad comment
Vrixyz ea933e7
unified mut api for clipboard
Vrixyz b45bea2
use crossbeam-channel to avoid explicit mutex
Vrixyz 070f108
Update .cargo/config.toml
Vrixyz 0e7dd24
add copy and cut events
Vrixyz e3fa6e3
Update src/web_clipboard.rs
Vrixyz 43af68a
comments and naming improvements
Vrixyz 9d743fd
store new resource to detect if we're on a mac
Vrixyz 8768a9c
res IsMac now a local, init via Once; removed comments, use user-agent
Vrixyz 5c4f550
Merge branch 'main' of github.com:mvlabat/bevy_egui into 113-clipboard
Vrixyz fe877d7
Merge branch 'main' of github.com:mvlabat/bevy_egui into 113-clipboard
Vrixyz f97ae8d
Merge branch 'main' into 113-clipboard
Vrixyz d6b726b
remove info logs ; fix user agent detection
Vrixyz feb884c
fmt
Vrixyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[alias] | ||
run-wasm = ["run", "--release", "--package", "run-wasm", "--"] | ||
|
||
# Credits to https://github.com/emilk/egui | ||
|
||
# clipboard api is still unstable, so web-sys requires the below flag to be passed for copy (ctrl + c) to work | ||
# https://rustwasm.github.io/docs/wasm-bindgen/web-sys/unstable-apis.html | ||
# check status at https://developer.mozilla.org/en-US/docs/Web/API/Clipboard#browser_compatibility | ||
#[build] | ||
#target = "wasm32-unknown-unknown" | ||
|
||
Vrixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[target.wasm32-unknown-unknown] | ||
rustflags = ["--cfg=web_sys_unstable_apis"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use bevy::{input::common_conditions::input_just_pressed, prelude::*}; | ||
use bevy_egui::{egui, EguiContexts, EguiPlugin}; | ||
use egui::{text_edit::CursorRange, TextEdit, Widget}; | ||
use wasm_bindgen_futures::spawn_local; | ||
use web_sys::ClipboardPermissionDescriptor; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
Vrixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.add_plugin(EguiPlugin) | ||
.init_resource::<CustomText>() | ||
// Systems that create Egui widgets should be run during the `CoreSet::Update` set, | ||
// or after the `EguiSet::BeginFrame` system (which belongs to the `CoreSet::PreUpdate` set). | ||
.add_system(ui_edit) | ||
.add_system(clipboard_copy.run_if(input_just_pressed(KeyCode::C))) | ||
.add_system(clipboard_paste.run_if(input_just_pressed(KeyCode::V))) | ||
.run(); | ||
} | ||
|
||
#[derive(Resource, Default)] | ||
struct CustomText(pub String, pub Option<CursorRange>); | ||
|
||
fn ui_edit(mut contexts: EguiContexts, mut text: ResMut<CustomText>) { | ||
egui::Window::new("Hello").show(contexts.ctx_mut(), |ui| { | ||
let edit = TextEdit::multiline(&mut text.0).show(ui); | ||
text.1 = edit.cursor_range; | ||
}); | ||
} | ||
|
||
fn clipboard_copy(mut text: ResMut<CustomText>) { | ||
//text.0 = "copy".into(); | ||
|
||
let text = if let Some(selected) = text.1 { | ||
text.0[selected.primary.ccursor.index..selected.secondary.ccursor.index].to_string() | ||
} else { | ||
"".into() | ||
}; | ||
let _task = spawn_local(async move { | ||
let window = web_sys::window().expect("window"); // { obj: val }; | ||
|
||
let nav = window.navigator(); | ||
|
||
let clipboard = nav.clipboard(); | ||
match clipboard { | ||
Some(a) => { | ||
let p = a.write_text(&text); | ||
let result = wasm_bindgen_futures::JsFuture::from(p) | ||
.await | ||
.expect("clipboard populated"); | ||
info!("clippyboy worked"); | ||
} | ||
None => { | ||
warn!("failed to write clippyboy"); | ||
} | ||
}; | ||
}); | ||
} | ||
|
||
fn clipboard_paste(mut text: ResMut<CustomText>) { | ||
let _task = spawn_local(async move { | ||
let window = web_sys::window().expect("window"); // { obj: val }; | ||
|
||
let nav = window.navigator(); | ||
let clipboard = nav.clipboard(); | ||
|
||
let Ok(permissions) = nav.permissions() else { | ||
return; | ||
}; | ||
let clipboard_permission_desc = js_sys::Object::new(); | ||
js_sys::Reflect::set( | ||
&clipboard_permission_desc, | ||
&"name".into(), | ||
// 'clipboard-read' fails on firefox because it's not implemented. | ||
// more info: https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API#browser_compatibility | ||
&"clipboard-read".into(), | ||
); | ||
dbg!(permissions.query(&clipboard_permission_desc.into())); | ||
match clipboard { | ||
Some(a) => { | ||
let p = a.read(); | ||
let result = wasm_bindgen_futures::JsFuture::from(p) | ||
.await | ||
.expect("clipboard populated"); | ||
dbg!("result: ", &result); | ||
info!("clippyboy worked"); | ||
} | ||
None => { | ||
warn!("failed to read clippyboy"); | ||
} | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "run-wasm" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
cargo-run-wasm = "0.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
cargo_run_wasm::run_wasm_with_css("body { margin: 0px; }"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think cargo run-wasm is quite handy to easily test a wasm version ; I can make another PR if interested