mirror of
https://github.com/emilk/egui.git
synced 2026-06-28 07:23:13 -04:00
<!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/main/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * [X] I have followed the instructions in the PR template Quick fix -- when the arboard and smithay features are both enabled, Clipboard::get returns early if it can't find a smithay clipboard. This PR just allows fallback to arboard instead of early-returning. --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
195 lines
5.7 KiB
Rust
195 lines
5.7 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(
|
|
not(any(target_os = "android", target_os = "ios")),
|
|
feature = "arboard",
|
|
))]
|
|
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(
|
|
not(any(target_os = "android", target_os = "ios")),
|
|
feature = "arboard",
|
|
))]
|
|
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 {
|
|
match clipboard.load() {
|
|
Ok(text) => return Some(text),
|
|
Err(err) => {
|
|
log::error!("smithay paste error: {err}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(all(
|
|
not(any(target_os = "android", target_os = "ios")),
|
|
feature = "arboard",
|
|
))]
|
|
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_text(&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(
|
|
not(any(target_os = "android", target_os = "ios")),
|
|
feature = "arboard",
|
|
))]
|
|
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;
|
|
}
|
|
|
|
pub fn set_image(&mut self, image: &egui::ColorImage) {
|
|
#[cfg(all(
|
|
not(any(target_os = "android", target_os = "ios")),
|
|
feature = "arboard",
|
|
))]
|
|
if let Some(clipboard) = &mut self.arboard {
|
|
if let Err(err) = clipboard.set_image(arboard::ImageData {
|
|
width: image.width(),
|
|
height: image.height(),
|
|
bytes: std::borrow::Cow::Borrowed(bytemuck::cast_slice(&image.pixels)),
|
|
}) {
|
|
log::error!("arboard copy/cut error: {err}");
|
|
}
|
|
log::debug!("Copied image to clipboard");
|
|
return;
|
|
}
|
|
|
|
log::error!(
|
|
"Copying images is not supported. Enable the 'clipboard' feature of `egui-winit` to enable it."
|
|
);
|
|
_ = image;
|
|
}
|
|
}
|
|
|
|
#[cfg(all(
|
|
not(any(target_os = "android", target_os = "ios")),
|
|
feature = "arboard",
|
|
))]
|
|
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> {
|
|
#![expect(clippy::undocumented_unsafe_blocks)]
|
|
|
|
profiling::function_scope!();
|
|
|
|
if let Some(RawDisplayHandle::Wayland(display)) = raw_display_handle {
|
|
log::trace!("Initializing smithay clipboard…");
|
|
#[expect(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
|
|
}
|
|
}
|