Struct clipboard_rs::ClipboardContext

source ·
pub struct ClipboardContext { /* private fields */ }

Implementations§

source§

impl ClipboardContext

source

pub fn new() -> Result<Self>

Examples found in repository?
examples/watch_change.rs (line 12)
11
12
13
14
	pub fn new() -> Self {
		let ctx = ClipboardContext::new().unwrap();
		Manager { ctx }
	}
More examples
Hide additional examples
examples/buffer.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
fn main() {
	let ctx = ClipboardContext::new().unwrap();
	let types = ctx.available_formats().unwrap();
	println!("{:?}", types);

	let buffer = ctx.get_buffer("public.html").unwrap();

	let string = String::from_utf8(buffer).unwrap();

	println!("{}", string);
}
examples/files.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
	let ctx = ClipboardContext::new().unwrap();

	// change the file paths to your own
	// let files = vec![
	//     "file:///home/parallels/clipboard-rs/Cargo.toml".to_string(),
	//     "file:///home/parallels/clipboard-rs/CHANGELOG.md".to_string(),
	// ];

	// ctx.set_files(files).unwrap();

	let types = ctx.available_formats().unwrap();
	println!("{:?}", types);

	let has = ctx.has(ContentFormat::Files);
	println!("has_files={}", has);

	let files = ctx.get_files().unwrap_or_default();
	println!("{:?}", files);
}
examples/helloworld.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
	let ctx = ClipboardContext::new().unwrap();
	let types = ctx.available_formats().unwrap();
	println!("{:?}", types);

	let has_rtf = ctx.has(ContentFormat::Rtf);
	println!("has_rtf={}", has_rtf);

	let rtf = ctx.get_rich_text().unwrap_or("".to_string());

	println!("rtf={}", rtf);

	let has_html = ctx.has(ContentFormat::Html);
	println!("has_html={}", has_html);

	let html = ctx.get_html().unwrap_or("".to_string());

	println!("html={}", html);

	let content = ctx.get_text().unwrap_or("".to_string());

	println!("txt={}", content);
}
examples/multi.rs (line 6)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
	let ctx = ClipboardContext::new().unwrap();

	let contents: Vec<ClipboardContent> = vec![
		ClipboardContent::Text("hell@$#%^&U都98好的😊o Rust!!!".to_string()),
		ClipboardContent::Rtf("\x1b[1m\x1b[4m\x1b[31mHello, Rust!\x1b[0m".to_string()),
		ClipboardContent::Html("<html><body><h1>Hello, Rust!</h1></body></html>".to_string()),
	];

	ctx.set(contents).unwrap();

	let types = ctx.available_formats().unwrap();
	println!("{:?}", types);

	let read = ctx
		.get(&[ContentFormat::Text, ContentFormat::Rtf, ContentFormat::Html])
		.unwrap();

	for c in read {
		println!("{}", c.as_str().unwrap());
	}
}
examples/image.rs (line 19)
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
fn main() {
	let ctx = ClipboardContext::new().unwrap();
	let types = ctx.available_formats().unwrap();
	println!("{:?}", types);

	let img = ctx.get_image();

	match img {
		Ok(img) => {
			let _ = img
				.save_to_path(format!("{}test.png", TMP_PATH).as_str())
				.map_err(|e| println!("save test.png err={}", e));

			let resize_img = img
				.thumbnail(300, 300)
				.map_err(|e| println!("thumbnail err={}", e))
				.unwrap();

			let _ = resize_img
				.save_to_path(format!("{}test_thumbnail.png", TMP_PATH).as_str())
				.map_err(|e| println!("save test_thumbnail.png err={}", e));
		}
		Err(err) => {
			println!("err={}", err);
		}
	}
}

Trait Implementations§

source§

impl Clipboard for ClipboardContext

source§

fn available_formats(&self) -> Result<Vec<String>>

zh: 获得剪切板当前内容的所有格式 en: Get all formats of the current content in the clipboard
source§

fn has(&self, format: ContentFormat) -> bool

source§

fn clear(&self) -> Result<()>

zh: 清空剪切板 en: clear clipboard
source§

fn get_buffer(&self, format: &str) -> Result<Vec<u8>>

zh: 获得指定格式的数据,以字节数组形式返回 en: Get the data in the specified format in the clipboard as a byte array
source§

fn get_text(&self) -> Result<String>

zh: 仅获得无格式纯文本,以字符串形式返回 en: Get plain text content in the clipboard as string
source§

fn get_rich_text(&self) -> Result<String>

zh: 获得剪贴板中的富文本内容,以字符串形式返回 en: Get the rich text content in the clipboard as string
source§

fn get_html(&self) -> Result<String>

zh: 获得剪贴板中的html内容,以字符串形式返回 en: Get the html format content in the clipboard as string
source§

fn get_image(&self) -> Result<RustImageData>

source§

fn get_files(&self) -> Result<Vec<String>>

source§

fn get(&self, formats: &[ContentFormat]) -> Result<Vec<ClipboardContent>>

source§

fn set_buffer(&self, format: &str, buffer: Vec<u8>) -> Result<()>

source§

fn set_text(&self, text: String) -> Result<()>

source§

fn set_rich_text(&self, text: String) -> Result<()>

source§

fn set_html(&self, html: String) -> Result<()>

source§

fn set_image(&self, image: RustImageData) -> Result<()>

source§

fn set_files(&self, files: Vec<String>) -> Result<()>

source§

fn set(&self, contents: Vec<ClipboardContent>) -> Result<()>

set image will clear clipboard

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> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

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>,

§

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.