Skip to main content

ClipboardHost

Struct ClipboardHost 

Source
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

Source

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.

Source

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.

Source

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.

Source

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).

Source

pub fn read_text(&self) -> String

Convenience: read the current clipboard text. Returns an empty string when no text payload is available.

Source

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.

Source

pub fn clear(&self) -> bool

Clear the clipboard. Returns true on success.

Source

pub fn set_rgba8_image(&self, width: u32, height: u32, rgba: Vec<u8>) -> bool

Write the supplied RGBA8 image (width x height x 4 bytes) to the system clipboard. Backs the copy_image script builtin.

Source

pub fn get_rgba8_image(&self) -> Option<(u32, u32, Vec<u8>)>

Read the current clipboard image as RGBA8. Returns (width, height, rgba_bytes) when an image is present.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.