Skip to content

Commit

Permalink
Refactor, fix edge-cases, improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mvlabat committed Mar 18, 2024
1 parent feb884c commit 3f46f82
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 154 deletions.
5 changes: 2 additions & 3 deletions examples/render_to_image_widget.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use bevy::{
prelude::*,
render::{
camera::{ClearColorConfig, RenderTarget},
camera::RenderTarget,
render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
view::RenderLayers,
},
};
use bevy_egui::{egui, EguiContexts, EguiPlugin, EguiUserTextures};
use egui::Widget;
use bevy_egui::{egui::Widget, EguiContexts, EguiPlugin, EguiUserTextures};

fn main() {
App::new()
Expand Down
4 changes: 2 additions & 2 deletions examples/side_panel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::{prelude::*, render::camera::Projection, window::PrimaryWindow};
use bevy_egui::{egui, EguiContexts, EguiPlugin};
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_egui::{EguiContexts, EguiPlugin};

#[derive(Default, Resource)]
struct OccupiedScreenSpace {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPlugin};
use bevy_egui::{EguiContexts, EguiPlugin};

fn main() {
App::new()
Expand Down
7 changes: 5 additions & 2 deletions examples/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_egui::{egui, EguiContexts, EguiPlugin, EguiSettings};
use bevy_egui::{EguiContexts, EguiPlugin, EguiSettings};

struct Images {
bevy_icon: Handle<Image>,
Expand Down Expand Up @@ -47,6 +47,7 @@ struct UiState {
inverted: bool,
egui_texture_handle: Option<egui::TextureHandle>,
is_window_open: bool,
text: String,
}

fn configure_visuals_system(mut contexts: EguiContexts) {
Expand Down Expand Up @@ -192,12 +193,14 @@ fn ui_example_system(

egui::Window::new("Window")
.vscroll(true)
.open(&mut ui_state.is_window_open)
// .open(&mut ui_state.is_window_open)
.show(ctx, |ui| {
ui.label("Windows can be moved by dragging them.");
ui.label("They are automatically sized based on contents.");
ui.label("You can turn on resizing and scrolling if you like.");
ui.label("You would normally chose either panels OR windows.");

ui.text_edit_multiline(&mut ui_state.text)
});

if invert {
Expand Down
45 changes: 22 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,19 @@
//!
//! - [`bevy-inspector-egui`](https://github.com/jakobhellermann/bevy-inspector-egui)

/// Egui render node.
#[cfg(feature = "render")]
pub mod egui_node;
/// Plugin systems for the render app.
#[cfg(feature = "render")]
pub mod render_systems;
/// Plugin systems.
pub mod systems;

/// Egui render node.
#[cfg(feature = "render")]
pub mod egui_node;

/// Clipboard management for web
#[cfg(all(feature = "manage_clipboard", target_arch = "wasm32"))]
pub mod web_clipboard;

pub use egui;
#[cfg(all(feature = "manage_clipboard", target_arch = "wasm32"))]
use web_clipboard::{WebEventCopy, WebEventCut, WebEventPaste};

use crate::systems::*;
#[cfg(feature = "render")]
Expand Down Expand Up @@ -117,11 +113,6 @@ use std::borrow::Cow;
not(any(target_arch = "wasm32", target_os = "android"))
))]
use std::cell::{RefCell, RefMut};
#[cfg(all(
feature = "manage_clipboard",
not(any(target_arch = "wasm32", target_os = "android"))
))]
use thread_local::ThreadLocal;

/// Adds all Egui resources and render graph nodes.
pub struct EguiPlugin;
Expand Down Expand Up @@ -184,16 +175,9 @@ pub struct EguiInput(pub egui::RawInput);
#[derive(Default, Resource)]
pub struct EguiClipboard {
#[cfg(not(target_arch = "wasm32"))]
clipboard: ThreadLocal<Option<RefCell<Clipboard>>>,
/// for copy events.
#[cfg(target_arch = "wasm32")]
pub web_copy: web_clipboard::WebChannel<WebEventCopy>,
/// for copy events.
clipboard: thread_local::ThreadLocal<Option<RefCell<Clipboard>>>,
#[cfg(target_arch = "wasm32")]
pub web_cut: web_clipboard::WebChannel<WebEventCut>,
/// for paste events, only supporting strings.
#[cfg(target_arch = "wasm32")]
pub web_paste: web_clipboard::WebChannel<WebEventPaste>,
clipboard: web_clipboard::WebClipboard,
}

#[cfg(all(feature = "manage_clipboard", not(target_os = "android")))]
Expand All @@ -203,6 +187,13 @@ impl EguiClipboard {
self.set_contents_impl(contents);
}

/// Sets the internal buffer of clipboard contents.
/// This buffer is used to remember the contents of the last "Paste" event.
#[cfg(target_arch = "wasm32")]
pub fn set_contents_internal(&mut self, contents: &str) {
self.clipboard.set_contents_internal(contents);
}

/// Gets clipboard contents. Returns [`None`] if clipboard provider is unavailable or returns an error.
#[must_use]
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -217,6 +208,12 @@ impl EguiClipboard {
self.get_contents_impl()
}

/// Receives a clipboard event sent by the `copy`/`cut`/`paste` listeners.
#[cfg(target_arch = "wasm32")]
pub fn try_receive_clipboard_event(&self) -> Option<web_clipboard::WebClipboardEvent> {
self.clipboard.try_receive_clipboard_event()
}

#[cfg(not(target_arch = "wasm32"))]
fn set_contents_impl(&mut self, contents: &str) {
if let Some(mut clipboard) = self.get() {
Expand All @@ -228,7 +225,7 @@ impl EguiClipboard {

#[cfg(target_arch = "wasm32")]
fn set_contents_impl(&mut self, contents: &str) {
web_clipboard::clipboard_copy(contents.to_owned());
self.clipboard.set_contents(contents);
}

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -245,7 +242,7 @@ impl EguiClipboard {
#[cfg(target_arch = "wasm32")]
#[allow(clippy::unnecessary_wraps)]
fn get_contents_impl(&mut self) -> Option<String> {
self.web_paste.try_read_clipboard_event().map(|e| e.0)
self.clipboard.get_contents()
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -601,6 +598,8 @@ 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"))]
world.init_non_send_resource::<web_clipboard::SubscribedEvents>();
#[cfg(feature = "render")]
world.init_resource::<EguiUserTextures>();
world.init_resource::<EguiMousePosition>();
Expand Down
1 change: 0 additions & 1 deletion src/render_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use bevy::{
DynamicUniformBuffer, PipelineCache, ShaderType, SpecializedRenderPipelines,
},
renderer::{RenderDevice, RenderQueue},
texture::Image,
view::ExtractedWindows,
Extract,
},
Expand Down
69 changes: 32 additions & 37 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,53 +304,48 @@ 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")))]
{
#[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()
{
#[cfg(all(
feature = "manage_clipboard",
not(target_os = "android"),
not(target_arch = "wasm32")
))]
if command && ev.state.is_pressed() {
match key {
egui::Key::C => {
focused_input.events.push(egui::Event::Copy);
}
if input_resources
.egui_clipboard
.web_cut
.try_read_clipboard_event()
.is_some()
{
egui::Key::X => {
focused_input.events.push(egui::Event::Cut);
}
if let Some(contents) = input_resources.egui_clipboard.get_contents() {
focused_input.events.push(egui::Event::Text(contents));
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")]
while let Some(event) = input_resources.egui_clipboard.try_receive_clipboard_event() {
match event {
crate::web_clipboard::WebClipboardEvent::Copy => {
focused_input.events.push(egui::Event::Copy);
}
crate::web_clipboard::WebClipboardEvent::Cut => {
focused_input.events.push(egui::Event::Cut);
}
crate::web_clipboard::WebClipboardEvent::Paste(contents) => {
input_resources
.egui_clipboard
.set_contents_internal(&contents);
focused_input.events.push(egui::Event::Text(contents))
}
}
}

for touch in input_events.ev_touch.read() {
let scale_factor = egui_settings.scale_factor;
let touch_position: (f32, f32) = (touch.position / scale_factor).into();
Expand Down
Loading

0 comments on commit 3f46f82

Please sign in to comment.