Skip to content

Commit

Permalink
Redo API for clipboard clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
complexspaces committed Sep 19, 2022
1 parent 2256b4e commit 12664f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ impl Clipboard {
self.set().image(image)
}

/// Clears the default clipboard for the platform of any contents that
/// may be present, regardless of the format.
pub fn clear_default(&mut self) -> Result<(), Error> {
self.clear().clear_any()
/// Clears any contents that may be present from the platform's default clipboard,
/// regardless of the format of the data.
pub fn clear(&mut self) -> Result<(), Error> {
self.clear_with().default()
}

/// Begins a "clear" option to remove data from the clipboard.
pub fn clear(&mut self) -> Clear<'_> {
pub fn clear_with(&mut self) -> Clear<'_> {
Clear { platform: platform::Clear::new(&mut self.platform) }
}

Expand Down Expand Up @@ -164,7 +164,7 @@ pub struct Clear<'clipboard> {
impl Clear<'_> {
/// Completes the "clear" operation by deleting any existing clipboard data,
/// regardless of the format.
pub fn clear_any(self) -> Result<(), Error> {
pub fn default(self) -> Result<(), Error> {
self.platform.clear()
}
}
Expand Down Expand Up @@ -208,7 +208,7 @@ fn all_tests() {
ctx.set_text(text).unwrap();
assert_eq!(ctx.get_text().unwrap(), text);

ctx.clear_default().unwrap();
ctx.clear().unwrap();

match ctx.get_text() {
Ok(text) => assert!(text.is_empty()),
Expand All @@ -217,7 +217,7 @@ fn all_tests() {
};

// confirm it is OK to clear when already empty.
ctx.clear_default().unwrap();
ctx.clear().unwrap();
}
#[cfg(feature = "image-data")]
{
Expand Down
30 changes: 22 additions & 8 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,33 +257,47 @@ impl SetExtLinux for crate::Set<'_> {

pub(crate) struct Clear<'clipboard> {
clipboard: &'clipboard mut Clipboard,
selection: LinuxClipboardKind,
}

impl<'clipboard> Clear<'clipboard> {
pub(crate) fn new(clipboard: &'clipboard mut Clipboard) -> Self {
Self { clipboard, selection: LinuxClipboardKind::Clipboard }
Self { clipboard }
}

pub(crate) fn clear(self) -> Result<(), Error> {
self.clear_inner(LinuxClipboardKind::Clipboard)
}

fn clear_inner(self, selection: LinuxClipboardKind) -> Result<(), Error> {
let mut set = Set::new(self.clipboard);
set.selection = self.selection;
set.selection = selection;

set.text(Cow::Borrowed(""))
}
}

/// Linux specific extensions to the [Clear] builder.
pub trait ClearExtLinux: private::Sealed {
/// Sets the clipboard that will be cleared.
/// Performs the "clear" operation on the selected clipboard.
///
/// ### Example
///
/// ```no_run
/// # use arboard::Clipboard;
/// let mut clipboard = Clipboard::new()?;
///
/// clipboard
/// .clear_with()
/// .clipboard(LinuxClipboardKind::Secondary)?;
/// ```
///
/// If wayland support is enabled and available, attempting to use the Secondary clipboard will
/// return an error.
fn clipboard(self, selection: LinuxClipboardKind) -> Self;
fn clipboard(self, selection: LinuxClipboardKind) -> Result<(), Error>;
}

impl ClearExtLinux for crate::Clear<'_> {
fn clipboard(mut self, selection: LinuxClipboardKind) -> Self {
self.platform.selection = selection;
self
fn clipboard(self, selection: LinuxClipboardKind) -> Result<(), Error> {
self.platform.clear_inner(selection)
}
}

0 comments on commit 12664f2

Please sign in to comment.