Skip to content

Commit

Permalink
Add Context::copy_image
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Dec 29, 2024
1 parent e2c7e9e commit ba23528
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df099ccb16cd014ff054ac1bf392c67feeef57164b05c42f037cd40f5d4357f4"
dependencies = [
"clipboard-win",
"core-graphics",
"image",
"log",
"objc2",
"objc2-app-kit",
"objc2-foundation",
"parking_lot",
"windows-sys 0.48.0",
"x11rb",
]

Expand Down Expand Up @@ -1292,6 +1295,7 @@ dependencies = [
"accesskit_winit",
"ahash",
"arboard",
"bytemuck",
"document-features",
"egui",
"log",
Expand Down Expand Up @@ -2205,6 +2209,7 @@ dependencies = [
"image-webp",
"num-traits",
"png",
"tiff",
"zune-core",
"zune-jpeg",
]
Expand Down Expand Up @@ -2311,6 +2316,12 @@ dependencies = [
"libc",
]

[[package]]
name = "jpeg-decoder"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0"

[[package]]
name = "js-sys"
version = "0.3.72"
Expand Down Expand Up @@ -3882,6 +3893,17 @@ dependencies = [
"syn",
]

[[package]]
name = "tiff"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba1310fcea54c6a9a4fd1aad794ecc02c31682f6bfbecdf460bf19533eed1e3e"
dependencies = [
"flate2",
"jpeg-decoder",
"weezl",
]

[[package]]
name = "time"
version = "0.3.36"
Expand Down
10 changes: 7 additions & 3 deletions crates/egui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ android-game-activity = ["winit/android-game-activity"]
android-native-activity = ["winit/android-native-activity"]

## [`bytemuck`](https://docs.rs/bytemuck) enables you to cast [`egui::epaint::Vertex`], [`egui::Vec2`] etc to `&[u8]`.
bytemuck = ["egui/bytemuck"]
bytemuck = ["egui/bytemuck", "dep:bytemuck"]

## Enable cut/copy/paste to OS clipboard.
## If disabled a clipboard will be simulated so you can still copy/paste within the egui app.
clipboard = ["arboard", "smithay-clipboard"]
clipboard = ["arboard", "bytemuck", "smithay-clipboard"]

## Enable opening links in a browser when an egui hyperlink is clicked.
links = ["webbrowser"]
Expand Down Expand Up @@ -69,6 +69,8 @@ winit = { workspace = true, default-features = false }
# feature accesskit
accesskit_winit = { version = "0.23", optional = true }

bytemuck = { workspace = true, optional = true }

## Enable this when generating docs.
document-features = { workspace = true, optional = true }

Expand All @@ -84,4 +86,6 @@ smithay-clipboard = { version = "0.7.2", optional = true }
wayland-cursor = { version = "0.31.1", default-features = false, optional = true }

[target.'cfg(not(target_os = "android"))'.dependencies]
arboard = { version = "3.3", optional = true, default-features = false }
arboard = { version = "3.3", optional = true, default-features = false, features = [
"image-data",
] }
19 changes: 18 additions & 1 deletion crates/egui-winit/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -108,6 +108,23 @@ 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.");
}
}

#[cfg(all(feature = "arboard", not(target_os = "android")))]
Expand Down
9 changes: 6 additions & 3 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
11 changes: 10 additions & 1 deletion crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,13 +1439,22 @@ impl Context {

/// Copy the given text to the system clipboard.
///
/// Note that in wasm applications, the clipboard is only accessible in secure contexts (e.g.,
/// Note that in web applications, the clipboard is only accessible in secure contexts (e.g.,
/// HTTPS or localhost). If this method is used outside of a secure context, it will log an
/// error and do nothing. See <https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts>.
pub fn copy_text(&self, text: String) {
self.send_cmd(crate::OutputCommand::CopyText(text));
}

/// Copy the given image to the system clipboard.
///
/// Note that in web applications, the clipboard is only accessible in secure contexts (e.g.,
/// HTTPS or localhost). If this method is used outside of a secure context, it will log an
/// error and do nothing. See <https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts>.
pub fn copy_image(&self, image: crate::ColorImage) {
self.send_cmd(crate::OutputCommand::CopyImage(image));
}

/// Format the given shortcut in a human-readable way (e.g. `Ctrl+Shift+X`).
///
/// Can be used to get the text for [`crate::Button::shortcut_text`].
Expand Down
5 changes: 4 additions & 1 deletion crates/egui/src/data/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ pub struct IMEOutput {
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum OutputCommand {
/// Put this text in the system clipboard.
/// Put this text to the system clipboard.
///
/// This is often a response to [`crate::Event::Copy`] or [`crate::Event::Cut`].
CopyText(String),

/// Put this image to the system clipboard.
CopyImage(crate::ColorImage),

/// Open this url in a browser.
OpenUrl(OpenUrl),
}
Expand Down

0 comments on commit ba23528

Please sign in to comment.