1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 13:50:04 -04:00

Add Context::copy_image (#5533)

* Closes https://github.com/emilk/egui/issues/5424

This adds support for copying images to the system clipboard on native
and on web using `Context::copy_image`.
This commit is contained in:
Emil Ernerfeldt
2024-12-29 18:03:32 +01:00
committed by GitHub
parent e2c7e9e733
commit bf6ed3adfc
15 changed files with 285 additions and 41 deletions

View File

@@ -82,7 +82,7 @@ impl Clipboard {
Some(self.clipboard.clone())
}
pub fn set(&mut self, text: String) {
pub fn set_text(&mut self, text: String) {
#[cfg(all(
any(
target_os = "linux",
@@ -108,6 +108,24 @@ impl Clipboard {
self.clipboard = text;
}
pub fn set_image(&mut self, image: &egui::ColorImage) {
#[cfg(all(feature = "arboard", not(target_os = "android")))]
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(feature = "arboard", not(target_os = "android")))]

View File

@@ -190,7 +190,7 @@ impl State {
/// Places the text onto the clipboard.
pub fn set_clipboard_text(&mut self, text: String) {
self.clipboard.set(text);
self.clipboard.set_text(text);
}
/// Returns [`false`] or the last value that [`Window::set_ime_allowed()`] was called with, used for debouncing.
@@ -840,7 +840,10 @@ impl State {
for command in commands {
match command {
egui::OutputCommand::CopyText(text) => {
self.clipboard.set(text);
self.clipboard.set_text(text);
}
egui::OutputCommand::CopyImage(image) => {
self.clipboard.set_image(&image);
}
egui::OutputCommand::OpenUrl(open_url) => {
open_url_in_browser(&open_url.url);
@@ -855,7 +858,7 @@ impl State {
}
if !copied_text.is_empty() {
self.clipboard.set(copied_text);
self.clipboard.set_text(copied_text);
}
let allow_ime = ime.is_some();