mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-22 18:41:27 +01:00
platform: a boxed ClipboardBackend is a backend, forwarding every method to the one it holds
This commit is contained in:
1 parent
8713c93c6c
commit
ae8bffc192
2 files changed
+49
No files matched your search
@@ -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<B: ClipboardBackend + ?Sized> ClipboardBackend for Box<B> {
|
||||
fn write(&mut self, flavors: &[Flavor]) -> Result<usize, ClipboardError> {
|
||||
(**self).write(flavors)
|
||||
}
|
||||
fn read(&mut self, mimes: &[&str], limit: u64) -> Result<Option<Flavor>, ClipboardError> {
|
||||
(**self).read(mimes, limit)
|
||||
}
|
||||
fn read_image(&mut self, limit: u64) -> Result<Option<ClipboardImage>, ClipboardError> {
|
||||
(**self).read_image(limit)
|
||||
}
|
||||
fn read_files(&mut self, limit: usize) -> Result<Option<Vec<PathBuf>>, 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<Box<dyn FnOnce() -> Option<Flavor> + Send>>, ClipboardError> {
|
||||
(**self).read_later(mimes, limit)
|
||||
}
|
||||
fn readable_any_time(&self) -> bool {
|
||||
(**self).readable_any_time()
|
||||
}
|
||||
fn offer(&mut self, flavors: Vec<Flavor>) {
|
||||
(**self).offer(flavors);
|
||||
}
|
||||
fn formats(&mut self) -> Vec<String> {
|
||||
(**self).formats()
|
||||
}
|
||||
fn owns(&mut self) -> Option<bool> {
|
||||
(**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.
|
||||
///
|
||||
|
||||
@@ -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<dyn ClipboardBackend> = Box::new(EventClipboard::new(|| 0, |_| Ok(0)));
|
||||
assert!(!Clipboard::new(event).readable_any_time());
|
||||
let mut memory: Box<dyn ClipboardBackend> = 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<std::sync::atomic::AtomicBool>);
|
||||
|
||||
Reference in new issue
Block a user