mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 22:00:03 -04:00
Hey! I am not sure if this is something that's been considered before and decided against (I couldn't find any PR's or issues). This change removes the internal profiling macros in library crates and the `puffin` feature and replaces it with similar functions in the [profiling](https://github.com/aclysma/profiling) crate. This crate provides a layer of abstraction over various profiler instrumentation crates and allows library users to pick their favorite (supported) profiler. An additional benefit for puffin users is that dependencies of egui are included in the instrumentation output too (mainly wgpu which uses the profiling crate), so more details might be available when profiling. A breaking change is that instead of using the `puffin` feature on egui, users that want to profile the crate with puffin instead have to enable the `profile-with-puffin` feature on the profiling crate. Similarly they could instead choose to use `profile-with-tracy` etc. I tried to add a 'tracy' feature to egui_demo_app in order to showcase , however the /scripts/check.sh currently breaks on mutually exclusive features (which this introduces), so I decided against including it for the initial PR. I'm happy to iterate more on this if there is interest in taking this PR though. Screenshot showing the additional info for wgpu now available when using puffin 
158 lines
4.6 KiB
Rust
158 lines
4.6 KiB
Rust
use raw_window_handle::RawDisplayHandle;
|
|
|
|
/// Handles interfacing with the OS clipboard.
|
|
///
|
|
/// If the "clipboard" feature is off, or we cannot connect to the OS clipboard,
|
|
/// then a fallback clipboard that just works within the same app is used instead.
|
|
pub struct Clipboard {
|
|
#[cfg(all(feature = "arboard", not(target_os = "android")))]
|
|
arboard: Option<arboard::Clipboard>,
|
|
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd",
|
|
target_os = "openbsd"
|
|
),
|
|
feature = "smithay-clipboard"
|
|
))]
|
|
smithay: Option<smithay_clipboard::Clipboard>,
|
|
|
|
/// Fallback manual clipboard.
|
|
clipboard: String,
|
|
}
|
|
|
|
impl Clipboard {
|
|
/// Construct a new instance
|
|
pub fn new(_raw_display_handle: Option<RawDisplayHandle>) -> Self {
|
|
Self {
|
|
#[cfg(all(feature = "arboard", not(target_os = "android")))]
|
|
arboard: init_arboard(),
|
|
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd",
|
|
target_os = "openbsd"
|
|
),
|
|
feature = "smithay-clipboard"
|
|
))]
|
|
smithay: init_smithay_clipboard(_raw_display_handle),
|
|
|
|
clipboard: Default::default(),
|
|
}
|
|
}
|
|
|
|
pub fn get(&mut self) -> Option<String> {
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd",
|
|
target_os = "openbsd"
|
|
),
|
|
feature = "smithay-clipboard"
|
|
))]
|
|
if let Some(clipboard) = &mut self.smithay {
|
|
return match clipboard.load() {
|
|
Ok(text) => Some(text),
|
|
Err(err) => {
|
|
log::error!("smithay paste error: {err}");
|
|
None
|
|
}
|
|
};
|
|
}
|
|
|
|
#[cfg(all(feature = "arboard", not(target_os = "android")))]
|
|
if let Some(clipboard) = &mut self.arboard {
|
|
return match clipboard.get_text() {
|
|
Ok(text) => Some(text),
|
|
Err(err) => {
|
|
log::error!("arboard paste error: {err}");
|
|
None
|
|
}
|
|
};
|
|
}
|
|
|
|
Some(self.clipboard.clone())
|
|
}
|
|
|
|
pub fn set(&mut self, text: String) {
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd",
|
|
target_os = "openbsd"
|
|
),
|
|
feature = "smithay-clipboard"
|
|
))]
|
|
if let Some(clipboard) = &mut self.smithay {
|
|
clipboard.store(text);
|
|
return;
|
|
}
|
|
|
|
#[cfg(all(feature = "arboard", not(target_os = "android")))]
|
|
if let Some(clipboard) = &mut self.arboard {
|
|
if let Err(err) = clipboard.set_text(text) {
|
|
log::error!("arboard copy/cut error: {err}");
|
|
}
|
|
return;
|
|
}
|
|
|
|
self.clipboard = text;
|
|
}
|
|
}
|
|
|
|
#[cfg(all(feature = "arboard", not(target_os = "android")))]
|
|
fn init_arboard() -> Option<arboard::Clipboard> {
|
|
profiling::function_scope!();
|
|
|
|
log::trace!("Initializing arboard clipboard…");
|
|
match arboard::Clipboard::new() {
|
|
Ok(clipboard) => Some(clipboard),
|
|
Err(err) => {
|
|
log::warn!("Failed to initialize arboard clipboard: {err}");
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd",
|
|
target_os = "openbsd"
|
|
),
|
|
feature = "smithay-clipboard"
|
|
))]
|
|
fn init_smithay_clipboard(
|
|
raw_display_handle: Option<RawDisplayHandle>,
|
|
) -> Option<smithay_clipboard::Clipboard> {
|
|
#![allow(clippy::undocumented_unsafe_blocks)]
|
|
|
|
profiling::function_scope!();
|
|
|
|
if let Some(RawDisplayHandle::Wayland(display)) = raw_display_handle {
|
|
log::trace!("Initializing smithay clipboard…");
|
|
#[allow(unsafe_code)]
|
|
Some(unsafe { smithay_clipboard::Clipboard::new(display.display.as_ptr()) })
|
|
} else {
|
|
#[cfg(feature = "wayland")]
|
|
log::debug!("Cannot init smithay clipboard without a Wayland display handle");
|
|
#[cfg(not(feature = "wayland"))]
|
|
log::debug!(
|
|
"Cannot init smithay clipboard: the 'wayland' feature of 'egui-winit' is not enabled"
|
|
);
|
|
None
|
|
}
|
|
}
|