Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(clipboard): use the wayland clipboard for images (#222) (#222)

shou349dfa0e57a6
diff --git a/Cargo.lock b/Cargo.lock
index fa4e727d2..2dd6dd917 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -248,6 +248,7 @@ dependencies = [
"parking_lot",
"percent-encoding",
"windows-sys 0.60.2",
+ "wl-clipboard-rs",
"x11rb",
]
@@ -1556,6 +1557,12 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484"
+[[package]]
+name = "fixedbitset"
+version = "0.5.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99"
+
[[package]]
name = "flate2"
version = "1.1.10"
@@ -3204,6 +3211,16 @@ dependencies = [
"pin-project-lite",
]
+[[package]]
+name = "os_pipe"
+version = "1.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
+dependencies = [
+ "libc",
+ "windows-sys 0.52.0",
+]
+
[[package]]
name = "parking"
version = "2.2.1"
@@ -3303,6 +3320,17 @@ version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
+[[package]]
+name = "petgraph"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455"
+dependencies = [
+ "fixedbitset",
+ "hashbrown 0.15.5",
+ "indexmap",
+]
+
[[package]]
name = "phf"
version = "0.13.1"
@@ -5085,6 +5113,17 @@ dependencies = [
"once_cell",
]
+[[package]]
+name = "tree_magic_mini"
+version = "3.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8765b90061cba6c22b5831f675da109ae5561588290f9fa2317adab2714d5a6"
+dependencies = [
+ "memchr",
+ "nom",
+ "petgraph",
+]
+
[[package]]
name = "twox-hash"
version = "2.1.4"
@@ -6122,6 +6161,24 @@ version = "0.57.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e"
+[[package]]
+name = "wl-clipboard-rs"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4d7888ccd4896447b2d14d3a9350a85df2aeb6f181e2e7a31349d104ac46cac1"
+dependencies = [
+ "libc",
+ "log",
+ "os_pipe",
+ "rustix 1.1.5",
+ "thiserror 2.0.21",
+ "tree_magic_mini",
+ "wayland-backend",
+ "wayland-client",
+ "wayland-protocols",
+ "wayland-protocols-wlr",
+]
+
[[package]]
name = "writeable"
version = "0.6.4"
diff --git a/apps/photocraft/Cargo.toml b/apps/photocraft/Cargo.toml
index 250821238..af0c893f4 100644
--- a/apps/photocraft/Cargo.toml
+++ b/apps/photocraft/Cargo.toml
@@ -29,7 +29,7 @@ log = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rfd = { workspace = true }
-arboard = "3"
+arboard = { version = "3", features = ["wayland-data-control"] }
open = "5"
[target.'cfg(target_os = "macos")'.dependencies]
diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs
index d745d791b..d74dcfa74 100644
--- a/apps/photocraft/src/services.rs
+++ b/apps/photocraft/src/services.rs
@@ -80,6 +80,7 @@ fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), String> {
pub fn native(automation: Option<photocraft_automation::AuthorizedWorkspace>) -> Services {
let savers: Rc<RefCell<HashMap<u64, Autosaver>>> = Rc::default();
let savers2 = savers.clone();
+ let clip: Rc<RefCell<Option<arboard::Clipboard>>> = Rc::default();
let automation_read = automation.clone().map(|workspace| {
Box::new(move |path: &str| {
let bytes = workspace.read(path).map_err(|error| error.to_string())?;
@@ -132,14 +133,29 @@ pub fn native(automation: Option<photocraft_automation::AuthorizedWorkspace>) ->
})),
inbox: None,
open_url: Some(Box::new(|url: &str| open::that(url).map_err(|e| e.to_string()))),
- clipboard_set_image: Some(Box::new(|w: u32, h: u32, px: &[u8]| {
- let mut cb = arboard::Clipboard::new().map_err(|e| e.to_string())?;
- cb.set_image(arboard::ImageData { width: w as usize, height: h as usize, bytes: std::borrow::Cow::Borrowed(px) }).map_err(|e| e.to_string())
- })),
- clipboard_get_image: Some(Box::new(|| {
- let img = arboard::Clipboard::new().ok()?.get_image().ok()?;
- Some((img.width as u32, img.height as u32, img.bytes.into_owned()))
- })),
+ clipboard_set_image: Some({
+ let clip = clip.clone();
+ Box::new(move |w: u32, h: u32, px: &[u8]| {
+ let mut slot = clip.try_borrow_mut().map_err(|_| "clipboard is busy".to_string())?;
+ let cb = match slot.as_mut() {
+ Some(c) => c,
+ None => slot.insert(arboard::Clipboard::new().map_err(|e| e.to_string())?),
+ };
+ cb.set_image(arboard::ImageData { width: w as usize, height: h as usize, bytes: std::borrow::Cow::Borrowed(px) }).map_err(|e| e.to_string())
+ })
+ }),
+ clipboard_get_image: Some({
+ let clip = clip.clone();
+ Box::new(move || {
+ let mut slot = clip.try_borrow_mut().ok()?;
+ let cb = match slot.as_mut() {
+ Some(c) => c,
+ None => slot.insert(arboard::Clipboard::new().ok()?),
+ };
+ let img = cb.get_image().ok()?;
+ Some((img.width as u32, img.height as u32, img.bytes.into_owned()))
+ })
+ }),
load_prefs: Some(Box::new(|| std::fs::read_to_string(prefs_file()?).ok())),
save_prefs: Some(Box::new(|text: &str| write_atomic(&prefs_file().ok_or("no config directory")?, text.as_bytes()))),
// Crash recovery: background incremental .pcraft saves into the recovery directory.