Skip to content

Commit

Permalink
Move WaitConfig to src/platform/linux/mod.rs, use WaitConfig insi…
Browse files Browse the repository at this point in the history
…de `struct Set`
  • Loading branch information
auguwu authored and complexspaces committed Apr 12, 2024
1 parent 6cf324c commit 2f4b502
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 69 deletions.
78 changes: 31 additions & 47 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,34 @@ impl GetExtLinux for crate::Get<'_> {
}
}

/// Configuration on how long to wait for a new X11 copy event is emitted.
#[derive(Default)]
pub(crate) enum WaitConfig {
/// Waits until the given [`Instant`] has reached.
Until(Instant),

/// Waits forever until a new event is reached.
Forever,

/// It shouldn't wait.
#[default]
None,
}

pub(crate) struct Set<'clipboard> {
clipboard: &'clipboard mut Clipboard,
wait: bool,
wait_until: Option<Instant>,
wait: WaitConfig,
selection: LinuxClipboardKind,
}

impl<'clipboard> Set<'clipboard> {
pub(crate) fn new(clipboard: &'clipboard mut Clipboard) -> Self {
Self { clipboard, wait: false, wait_until: None, selection: LinuxClipboardKind::Clipboard }
Self { clipboard, wait: WaitConfig::default(), selection: LinuxClipboardKind::Clipboard }
}

pub(crate) fn text(self, text: Cow<'_, str>) -> Result<(), Error> {
match self.clipboard {
Clipboard::X11(clipboard) => clipboard.set_text(
text,
self.selection,
match (self.wait, self.wait_until) {
(_, Some(deadline)) => x11::WaitConfig::Until(deadline),
(true, None) => x11::WaitConfig::Forever,
(false, None) => x11::WaitConfig::None,
},
),
Clipboard::X11(clipboard) => clipboard.set_text(text, self.selection, self.wait),

#[cfg(feature = "wayland-data-control")]
Clipboard::WlDataControl(clipboard) => clipboard.set_text(text, self.selection, self.wait),
Expand All @@ -171,16 +176,7 @@ impl<'clipboard> Set<'clipboard> {

pub(crate) fn html(self, html: Cow<'_, str>, alt: Option<Cow<'_, str>>) -> Result<(), Error> {
match self.clipboard {
Clipboard::X11(clipboard) => clipboard.set_html(
html,
alt,
self.selection,
match (self.wait, self.wait_until) {
(_, Some(deadline)) => x11::WaitConfig::Until(deadline),
(true, None) => x11::WaitConfig::Forever,
(false, None) => x11::WaitConfig::None,
},
),
Clipboard::X11(clipboard) => clipboard.set_html(html, alt, self.selection, self.wait),

#[cfg(feature = "wayland-data-control")]
Clipboard::WlDataControl(clipboard) => clipboard.set_html(html, alt, self.selection, self.wait),
Expand All @@ -190,15 +186,7 @@ impl<'clipboard> Set<'clipboard> {
#[cfg(feature = "image-data")]
pub(crate) fn image(self, image: ImageData<'_>) -> Result<(), Error> {
match self.clipboard {
Clipboard::X11(clipboard) => clipboard.set_image(
image,
self.selection,
match (self.wait, self.wait_until) {
(_, Some(deadline)) => x11::WaitConfig::Until(deadline),
(true, None) => x11::WaitConfig::Forever,
(false, None) => x11::WaitConfig::None,
},
),
Clipboard::X11(clipboard) => clipboard.set_image(image, self.selection, self.wait),

#[cfg(feature = "wayland-data-control")]
Clipboard::WlDataControl(clipboard) => clipboard.set_image(image, self.selection, self.wait),
Expand Down Expand Up @@ -235,6 +223,16 @@ pub trait SetExtLinux: private::Sealed {
/// [daemonize example]: https://github.com/1Password/arboard/blob/master/examples/daemonize.rs
fn wait(self) -> Self;

/// Whether or not to wait for the clipboard's content to be replaced after setting it. This waits until the
/// `deadline` has exceeded.
///
/// This is useful for short-lived programs so it won't block until new contents on the clipboard
/// were added.
///
/// Note: this is a superset of [`wait()`][SetExtLinux::wait] and will overwrite any state
/// that was previously set using it.
fn wait_until(self, deadline: Instant) -> Self;

/// Sets the clipboard the operation will store its data to.
///
/// If wayland support is enabled and available, attempting to use the Secondary clipboard will
Expand All @@ -256,21 +254,11 @@ pub trait SetExtLinux: private::Sealed {
/// # }
/// ```
fn clipboard(self, selection: LinuxClipboardKind) -> Self;

/// Whether or not to wait for the clipboard's content to be replaced after setting it. This waits until the
/// `deadline` has exceeded.
///
/// This is useful for short-lived programs so it won't block until new contents on the clipboard
/// were added.
///
/// Note: this is a superset of [`wait()`][SetExtLinux::wait] and will overwrite any state
/// that was previously set using it.
fn wait_until(self, deadline: Instant) -> Self;
}

impl SetExtLinux for crate::Set<'_> {
fn wait(mut self) -> Self {
self.platform.wait = true;
self.platform.wait = WaitConfig::Forever;
self
}

Expand All @@ -280,11 +268,7 @@ impl SetExtLinux for crate::Set<'_> {
}

fn wait_until(mut self, deadline: Instant) -> Self {
self.platform.wait_until = Some(deadline);
if !self.platform.wait {
self.platform.wait = true;
}

self.platform.wait = WaitConfig::Until(deadline);
self
}
}
Expand Down
26 changes: 19 additions & 7 deletions src/platform/linux/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use wl_clipboard_rs::{

#[cfg(feature = "image-data")]
use super::encode_as_png;
use super::{into_unknown, LinuxClipboardKind};
use super::{into_unknown, LinuxClipboardKind, WaitConfig};
use crate::common::Error;
#[cfg(feature = "image-data")]
use crate::common::ImageData;
Expand Down Expand Up @@ -79,10 +79,14 @@ impl Clipboard {
&self,
text: Cow<'_, str>,
selection: LinuxClipboardKind,
wait: bool,
wait: WaitConfig,
) -> Result<(), Error> {
let mut opts = Options::new();
opts.foreground(wait);
opts.foreground(match wait {
WaitConfig::Forever => true,
_ => false,
});

opts.clipboard(selection.try_into()?);
let source = Source::Bytes(text.into_owned().into_bytes().into_boxed_slice());
opts.copy(source, MimeType::Text).map_err(|e| match e {
Expand All @@ -97,11 +101,15 @@ impl Clipboard {
html: Cow<'_, str>,
alt: Option<Cow<'_, str>>,
selection: LinuxClipboardKind,
wait: bool,
wait: WaitConfig,
) -> Result<(), Error> {
let html_mime = MimeType::Specific(String::from("text/html"));
let mut opts = Options::new();
opts.foreground(wait);
opts.foreground(match wait {
WaitConfig::Forever => true,
_ => false,
});

opts.clipboard(selection.try_into()?);
let html_source = Source::Bytes(html.into_owned().into_bytes().into_boxed_slice());
match alt {
Expand Down Expand Up @@ -163,11 +171,15 @@ impl Clipboard {
&mut self,
image: ImageData,
selection: LinuxClipboardKind,
wait: bool,
wait: WaitConfig,
) -> Result<(), Error> {
let image = encode_as_png(&image)?;
let mut opts = Options::new();
opts.foreground(wait);
opts.foreground(match wait {
WaitConfig::Forever => true,
_ => false,
});

opts.clipboard(selection.try_into()?);
let source = Source::Bytes(image.into());
opts.copy(source, MimeType::Specific(MIME_PNG.into())).map_err(into_unknown)?;
Expand Down
16 changes: 1 addition & 15 deletions src/platform/linux/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use x11rb::{

#[cfg(feature = "image-data")]
use super::encode_as_png;
use super::{into_unknown, LinuxClipboardKind};
use super::{into_unknown, LinuxClipboardKind, WaitConfig};
#[cfg(feature = "image-data")]
use crate::ImageData;
use crate::{common::ScopeGuard, Error};
Expand Down Expand Up @@ -205,20 +205,6 @@ enum ReadSelNotifyResult {
EventNotRecognized,
}

/// Configuration on how long to wait for a new X11 copy event is emitted.
#[derive(Default)]
pub enum WaitConfig {
/// Waits until the given [`Instant`] has reached.
Until(Instant),

/// Waits forever until a new event is reached.
Forever,

/// It shouldn't wait.
#[default]
None,
}

impl Inner {
fn new() -> Result<Self> {
let server = XContext::new()?;
Expand Down

0 comments on commit 2f4b502

Please sign in to comment.