-
Notifications
You must be signed in to change notification settings - Fork 257
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 all 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,5 @@ | ||
[alias] | ||
run-wasm = ["run", "--release", "--package", "run-wasm", "--"] | ||
|
||
[workspace] | ||
"run-wasm" |
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,6 @@ | ||
[alias] | ||
run-wasm = ["run", "--release", "--package", "run-wasm", "--"] | ||
|
||
# Using unstable APIs is required for writing to clipboard | ||
[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
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; }"); | ||
} |
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 |
---|---|---|
|
@@ -73,7 +73,7 @@ pub struct ModifierKeysState { | |
#[derive(SystemParam)] | ||
pub struct InputResources<'w, 's> { | ||
#[cfg(all(feature = "manage_clipboard", not(target_os = "android")))] | ||
pub egui_clipboard: Res<'w, crate::EguiClipboard>, | ||
pub egui_clipboard: ResMut<'w, crate::EguiClipboard>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #178 (comment) ; to be noted we can have mut only for wasm if we want. |
||
pub modifier_keys_state: Local<'s, ModifierKeysState>, | ||
#[system_param(ignore)] | ||
_marker: PhantomData<&'w ()>, | ||
|
@@ -110,7 +110,7 @@ pub fn process_input_system( | |
if let Some(window) = web_sys::window() { | ||
let nav = window.navigator(); | ||
if let Ok(user_agent) = nav.user_agent() { | ||
if user_agent.to_ascii_lowercase().contains("Mac") { | ||
if user_agent.to_ascii_lowercase().contains("mac") { | ||
*context_params.is_macos = true; | ||
} | ||
} | ||
|
@@ -130,7 +130,6 @@ pub fn process_input_system( | |
None | ||
}; | ||
} | ||
|
||
let mut keyboard_input_events = Vec::new(); | ||
for event in input_events.ev_keyboard_input.read() { | ||
// Copy the events as we might want to pass them to an Egui context later. | ||
|
@@ -149,7 +148,7 @@ pub fn process_input_system( | |
Key::Alt => { | ||
input_resources.modifier_keys_state.alt = state.is_pressed(); | ||
} | ||
Key::Super => { | ||
Key::Super | Key::Meta => { | ||
input_resources.modifier_keys_state.win = state.is_pressed(); | ||
} | ||
_ => {} | ||
|
@@ -306,20 +305,47 @@ pub fn process_input_system( | |
// We also check that it's an `ButtonState::Pressed` event, as we don't want to | ||
// copy, cut or paste on the key release. | ||
#[cfg(all(feature = "manage_clipboard", not(target_os = "android")))] | ||
if command && ev.state.is_pressed() { | ||
match key { | ||
egui::Key::C => { | ||
{ | ||
#[cfg(not(target_arch = "wasm32"))] | ||
if command && ev.state.is_pressed() { | ||
match key { | ||
egui::Key::C => { | ||
focused_input.events.push(egui::Event::Copy); | ||
} | ||
egui::Key::X => { | ||
focused_input.events.push(egui::Event::Cut); | ||
} | ||
egui::Key::V => { | ||
if let Some(contents) = | ||
input_resources.egui_clipboard.get_contents() | ||
{ | ||
focused_input.events.push(egui::Event::Text(contents)) | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
#[cfg(target_arch = "wasm32")] | ||
{ | ||
if input_resources | ||
.egui_clipboard | ||
.web_copy | ||
.try_read_clipboard_event() | ||
.is_some() | ||
{ | ||
focused_input.events.push(egui::Event::Copy); | ||
} | ||
egui::Key::X => { | ||
if input_resources | ||
.egui_clipboard | ||
.web_cut | ||
.try_read_clipboard_event() | ||
.is_some() | ||
{ | ||
focused_input.events.push(egui::Event::Cut); | ||
} | ||
egui::Key::V => { | ||
if let Some(contents) = input_resources.egui_clipboard.get_contents() { | ||
focused_input.events.push(egui::Event::Text(contents)) | ||
} | ||
if let Some(contents) = input_resources.egui_clipboard.get_contents() { | ||
focused_input.events.push(egui::Event::Text(contents)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
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