From ae8bffc192430d992df89b23d429ed7b380f47ef Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 08:22:13 +0700 Subject: [PATCH] platform: a boxed ClipboardBackend is a backend, forwarding every method to the one it holds --- crates/platform/src/clipboard/backend.rs | 38 ++++++++++++++++++++++++ crates/platform/src/clipboard_tests.rs | 11 +++++++ 2 files changed, 49 insertions(+) diff --git a/crates/platform/src/clipboard/backend.rs b/crates/platform/src/clipboard/backend.rs index d663f1cc8..84fb5a6dd 100644 --- a/crates/platform/src/clipboard/backend.rs +++ b/crates/platform/src/clipboard/backend.rs @@ -68,6 +68,44 @@ pub trait ClipboardBackend: Send { } } +/// A boxed backend is the backend it holds, so a host can choose one at run time +/// (`Clipboard::new(services.clipboard)`). +impl ClipboardBackend for Box { + fn write(&mut self, flavors: &[Flavor]) -> Result { + (**self).write(flavors) + } + fn read(&mut self, mimes: &[&str], limit: u64) -> Result, ClipboardError> { + (**self).read(mimes, limit) + } + fn read_image(&mut self, limit: u64) -> Result, ClipboardError> { + (**self).read_image(limit) + } + fn read_files(&mut self, limit: usize) -> Result>, ClipboardError> { + (**self).read_files(limit) + } + fn has(&mut self, mimes: &[&str]) -> bool { + (**self).has(mimes) + } + fn read_later(&mut self, mimes: &[&str], limit: u64) -> Result Option + Send>>, ClipboardError> { + (**self).read_later(mimes, limit) + } + fn readable_any_time(&self) -> bool { + (**self).readable_any_time() + } + fn offer(&mut self, flavors: Vec) { + (**self).offer(flavors); + } + fn formats(&mut self) -> Vec { + (**self).formats() + } + fn owns(&mut self) -> Option { + (**self).owns() + } + fn take_arrived(&mut self) -> bool { + (**self).take_arrived() + } +} + /// The desktop's clipboard through arboard and the platform's own offer code: X11 and Wayland, /// macOS, Windows. Not on wasm. /// diff --git a/crates/platform/src/clipboard_tests.rs b/crates/platform/src/clipboard_tests.rs index 28b12d251..b45541214 100644 --- a/crates/platform/src/clipboard_tests.rs +++ b/crates/platform/src/clipboard_tests.rs @@ -48,6 +48,17 @@ fn only_a_clipboard_outside_paste_events_is_readable_any_time() { assert!(!Clipboard::new(EventClipboard::new(|| 0, |_| Ok(0))).readable_any_time()); } +// A host picks its backend at run time and hands it over boxed; the box answers what the backend +// overrides, not the trait's defaults. +#[test] +fn a_boxed_backend_answers_as_the_backend_it_holds() { + let event: Box = Box::new(EventClipboard::new(|| 0, |_| Ok(0))); + assert!(!Clipboard::new(event).readable_any_time()); + let mut memory: Box = Box::new(MemoryClipboard::default()); + memory.write(&[Flavor::text("held")]).unwrap(); + assert_eq!(memory.formats(), vec![TEXT.to_owned()]); +} + /// A backend that takes longer to drop than the probe's old 250 ms wait, as an X11 handle does /// while it hands the copy to the clipboard manager. struct SlowToDrop(Arc);