pub struct ClipboardHost { /* private fields */ }Expand description
Single-process clipboard host. Wraps arboard::Clipboard behind a
Mutex - the underlying handle is !Send on Linux/Wayland so this
type stays !Sync by virtue of Mutex<arboard::Clipboard> having
the same restriction.
Call it from the thread that owns the app, which is what holding it as
a NonSend resource enforces. macOS NSPasteboard mutates shared
state without locking it, so two threads reaching the clipboard at
once corrupts memory there rather than returning an error.
Implementations§
Source§impl ClipboardHost
impl ClipboardHost
Sourcepub fn try_new() -> Option<Self>
pub fn try_new() -> Option<Self>
Try to initialize the OS clipboard. Returns None if the
backend (Wayland with no compositor, headless CI, X11 without a
running window manager) refuses.
Sourcepub fn read(&self) -> MimePayload
pub fn read(&self) -> MimePayload
Read the current clipboard contents as a multi-format
MimePayload. Tries text first, then image; returns an empty
payload when neither is available.
Sourcepub fn write(&self, payload: &MimePayload) -> bool
pub fn write(&self, payload: &MimePayload) -> bool
Write a MimePayload onto the system clipboard. Picks the
first MIME kind arboard understands (text/plain -> set_text).
Returns true on success.
Sourcepub fn write_text(&self, text: &str) -> bool
pub fn write_text(&self, text: &str) -> bool
Convenience: write a plain-text payload. Same as write with a
MimePayload::from(&str) but avoids the allocation when callers
only have a &str (the text editor’s copy / cut path).
Sourcepub fn read_text(&self) -> String
pub fn read_text(&self) -> String
Convenience: read the current clipboard text. Returns an empty string when no text payload is available.
Sourcepub fn read_text_then(&self, done: impl FnOnce(String) + 'static)
pub fn read_text_then(&self, done: impl FnOnce(String) + 'static)
Read the clipboard text and hand it to done, which is called
exactly once. Empty when the clipboard holds no text.
On the desktop the read is synchronous and done runs before this
returns.