Accept optionally borrowed data instead of String for setting text

This commit is contained in:
ComplexSpaces 2022-07-22 19:31:38 -05:00
parent b3e11c6fe4
commit 7fd3f07c05
8 changed files with 31 additions and 22 deletions

View file

@ -14,11 +14,11 @@ const DAEMONIZE_ARG: &str = "__internal_daemonize";
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
#[cfg(target_os = "linux")]
if env::args().nth(1).as_deref() == Some(DAEMONIZE_ARG) {
Clipboard::new()?.set().wait().text("Hello, world!".into())?;
Clipboard::new()?.set().wait().text("Hello, world!")?;
return Ok(());
}
let _logger = SimpleLogger::new().init().unwrap();
SimpleLogger::new().init().unwrap();
if cfg!(target_os = "linux") {
process::Command::new(env::current_exe()?)
@ -29,7 +29,7 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.current_dir("/")
.spawn()?;
} else {
Clipboard::new()?.set_text("Hello, world!".into())?;
Clipboard::new()?.set_text("Hello, world!")?;
}
Ok(())

View file

@ -2,11 +2,11 @@ use arboard::Clipboard;
use simple_logger::SimpleLogger;
fn main() {
let _logger = SimpleLogger::new().init().unwrap();
SimpleLogger::new().init().unwrap();
let mut clipboard = Clipboard::new().unwrap();
println!("Clipboard text was: {:?}", clipboard.get_text());
let the_string = "Hello, world!";
clipboard.set_text(the_string.into()).unwrap();
clipboard.set_text(the_string).unwrap();
println!("But now the clipboard text should be: \"{}\"", the_string);
}

View file

@ -12,6 +12,8 @@ and conditions of the chosen license apply to this file.
#![crate_type = "lib"]
mod common;
use std::borrow::Cow;
pub use common::Error;
#[cfg(feature = "image-data")]
pub use common::ImageData;
@ -54,7 +56,7 @@ impl Clipboard {
}
/// Places the text onto the clipboard. Any valid utf-8 string is accepted.
pub fn set_text(&mut self, text: String) -> Result<(), Error> {
pub fn set_text<'a, T: Into<Cow<'a, str>>>(&mut self, text: T) -> Result<(), Error> {
self.set().text(text)
}
@ -135,7 +137,8 @@ pub struct Set<'clipboard> {
impl Set<'_> {
/// Completes the "set" operation by placing text onto the clipboard. Any valid UTF-8 string
/// is accepted.
pub fn text(self, text: String) -> Result<(), Error> {
pub fn text<'a, T: Into<Cow<'a, str>>>(self, text: T) -> Result<(), Error> {
let text = text.into();
self.platform.text(text)
}
@ -177,7 +180,7 @@ fn all_tests() {
{
let mut ctx = Clipboard::new().unwrap();
let text = "some string";
ctx.set_text(text.to_owned()).unwrap();
ctx.set_text(text).unwrap();
assert_eq!(ctx.get_text().unwrap(), text);
// We also need to check that the content persists after the drop; this is
@ -195,14 +198,14 @@ fn all_tests() {
{
let mut ctx = Clipboard::new().unwrap();
let text = "Some utf8: 🤓 ∑φ(n)<ε 🐔";
ctx.set_text(text.to_owned()).unwrap();
ctx.set_text(text).unwrap();
assert_eq!(ctx.get_text().unwrap(), text);
}
{
let mut ctx = Clipboard::new().unwrap();
let text = "hello world";
ctx.set_text(text.into()).unwrap();
ctx.set_text(text).unwrap();
assert_eq!(ctx.get_text().unwrap(), text);
ctx.clear_default().unwrap();
@ -232,7 +235,7 @@ fn all_tests() {
ctx.set_image(img_data.clone()).unwrap();
assert!(matches!(ctx.get_text(), Err(Error::ContentNotAvailable)));
ctx.set_text("clipboard test".into()).unwrap();
ctx.set_text("clipboard test").unwrap();
assert!(matches!(ctx.get_image(), Err(Error::ContentNotAvailable)));
// Test if we get the same image that we put onto the clibboard

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
#[cfg(feature = "image-data")]
use std::{cell::RefCell, rc::Rc};
@ -172,7 +173,7 @@ impl<'clipboard> Set<'clipboard> {
Self { clipboard, wait: false, selection: LinuxClipboardKind::Clipboard }
}
pub(crate) fn text(self, text: String) -> Result<(), Error> {
pub(crate) fn text(self, text: Cow<'_, str>) -> Result<(), Error> {
match self.clipboard {
Clipboard::X11(clipboard) => clipboard.set_text(text, self.selection, self.wait),
#[cfg(feature = "wayland-data-control")]
@ -267,7 +268,7 @@ impl<'clipboard> Clear<'clipboard> {
pub(crate) fn clear(self) -> Result<(), Error> {
let mut set = Set::new(self.clipboard);
set.selection = self.selection;
set.text(String::new())
set.text(Cow::Borrowed(""))
}
}

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::convert::TryInto;
use std::io::Read;
@ -76,7 +77,7 @@ impl Clipboard {
pub(crate) fn set_text(
&self,
text: String,
text: Cow<'_, str>,
selection: LinuxClipboardKind,
wait: bool,
) -> Result<(), Error> {
@ -84,7 +85,7 @@ impl Clipboard {
let mut opts = Options::new();
opts.foreground(wait);
opts.clipboard(selection.try_into()?);
let source = Source::Bytes(text.as_bytes().into());
let source = Source::Bytes(text.into_owned().into_bytes().into_boxed_slice());
opts.copy(source, MimeType::Text).map_err(|e| match e {
CopyError::PrimarySelectionUnsupported => Error::ClipboardNotSupported,
other => into_unknown(other),

View file

@ -13,6 +13,7 @@ and conditions of the chosen license apply to this file.
// https://freedesktop.org/wiki/ClipboardManager/
use std::{
borrow::Cow,
cell::RefCell,
collections::{hash_map::Entry, HashMap},
sync::{
@ -852,12 +853,14 @@ impl Clipboard {
pub(crate) fn set_text(
&self,
message: String,
message: Cow<'_, str>,
selection: LinuxClipboardKind,
wait: bool,
) -> Result<()> {
let data =
ClipboardData { bytes: message.into_bytes(), format: self.inner.atoms.UTF8_STRING };
let data = ClipboardData {
bytes: message.into_owned().into_bytes(),
format: self.inner.atoms.UTF8_STRING,
};
self.inner.write(data, selection, wait)
}

View file

@ -26,6 +26,7 @@ use objc::{
use objc_foundation::{INSArray, INSObject, INSString, NSArray, NSDictionary, NSObject, NSString};
use objc_id::{Id, Owned};
use once_cell::sync::Lazy;
use std::borrow::Cow;
// Required to bring NSPasteboard into the path of the class-resolver
#[link(name = "AppKit", kind = "framework")]
@ -254,7 +255,7 @@ impl<'clipboard> Set<'clipboard> {
Self { clipboard }
}
pub(crate) fn text(self, data: String) -> Result<(), Error> {
pub(crate) fn text(self, data: Cow<'_, str>) -> Result<(), Error> {
self.clipboard.clear();
let string_array = NSArray::from_vec(vec![NSString::from_str(&data)]);

View file

@ -8,9 +8,9 @@ the Apache 2.0 or the MIT license at the licensee's choice. The terms
and conditions of the chosen license apply to this file.
*/
use std::marker::PhantomData;
use std::{borrow::Cow, marker::PhantomData};
#[cfg(feature = "image-data")]
use std::{borrow::Cow, convert::TryInto, mem::size_of};
use std::{convert::TryInto, mem::size_of};
use clipboard_win::Clipboard as SystemClipboard;
@ -461,7 +461,7 @@ impl<'clipboard> Set<'clipboard> {
Self { clipboard: PhantomData, exclude_from_cloud: false, exclude_from_history: false }
}
pub(crate) fn text(self, data: String) -> Result<(), Error> {
pub(crate) fn text(self, data: Cow<'_, str>) -> Result<(), Error> {
clipboard_win::raw::set_string(&data).map_err(|_| Error::Unknown {
description: "Could not place the specified text to the clipboard".into(),
})?;