Skip to content

Commit

Permalink
Added WaitConfig, fix wait_until note in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu authored and complexspaces committed Apr 12, 2024
1 parent eabb191 commit 6cf324c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
47 changes: 32 additions & 15 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,15 @@ impl<'clipboard> Set<'clipboard> {

pub(crate) fn text(self, text: Cow<'_, str>) -> Result<(), Error> {
match self.clipboard {
Clipboard::X11(clipboard) => {
clipboard.set_text(text, self.selection, self.wait, self.wait_until)
}
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,
},
),

#[cfg(feature = "wayland-data-control")]
Clipboard::WlDataControl(clipboard) => clipboard.set_text(text, self.selection, self.wait),
Expand All @@ -165,9 +171,16 @@ 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, self.wait, self.wait_until)
}
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,
},
),

#[cfg(feature = "wayland-data-control")]
Clipboard::WlDataControl(clipboard) => clipboard.set_html(html, alt, self.selection, self.wait),
Expand All @@ -177,9 +190,15 @@ 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, self.wait, self.wait_until)
}
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,
},
),

#[cfg(feature = "wayland-data-control")]
Clipboard::WlDataControl(clipboard) => clipboard.set_image(image, self.selection, self.wait),
Expand Down Expand Up @@ -241,13 +260,11 @@ pub trait SetExtLinux: private::Sealed {
/// 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 that it doesn't block until new contents on the clipboard
/// were added and will exit as so.
///
/// For X11, this will wait until it either had new contents available in the clipboard or if the
/// `deadline` was exceeded. This isn't available for Wayland and will not do anything.
/// This is useful for short-lived programs so it won't block until new contents on the clipboard
/// were added.
///
/// Note: this will call [`wait()`][SetExtLinux::wait] if it wasn't previously set.
/// 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;
}

Expand Down
50 changes: 30 additions & 20 deletions src/platform/linux/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,20 @@ 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 All @@ -227,8 +241,7 @@ impl Inner {
&self,
data: Vec<ClipboardData>,
selection: LinuxClipboardKind,
wait: bool,
until: Option<Instant>,
wait: WaitConfig,
) -> Result<()> {
if self.serve_stopped.load(Ordering::Relaxed) {
return Err(Error::Unknown {
Expand Down Expand Up @@ -261,16 +274,16 @@ impl Inner {
// It is important that the mutex is locked to prevent this notification getting lost.
selection.data_changed.notify_all();

if wait {
drop(data_guard);

// Wait for the clipboard's content to be changed.
match until {
Some(deadline) => {
selection.data_changed.wait_until(&mut guard, deadline);
}
match wait {
WaitConfig::None => {}
WaitConfig::Forever => {
drop(data_guard);
selection.data_changed.wait(&mut guard);
}

None => selection.data_changed.wait(&mut guard),
WaitConfig::Until(deadline) => {
drop(data_guard);
selection.data_changed.wait_until(&mut guard, deadline);
}
}

Expand Down Expand Up @@ -882,23 +895,21 @@ impl Clipboard {
&self,
message: Cow<'_, str>,
selection: LinuxClipboardKind,
wait: bool,
until: Option<Instant>,
wait: WaitConfig,
) -> Result<()> {
let data = vec![ClipboardData {
bytes: message.into_owned().into_bytes(),
format: self.inner.atoms.UTF8_STRING,
}];
self.inner.write(data, selection, wait, until)
self.inner.write(data, selection, wait)
}

pub(crate) fn set_html(
&self,
html: Cow<'_, str>,
alt: Option<Cow<'_, str>>,
selection: LinuxClipboardKind,
wait: bool,
until: Option<Instant>,
wait: WaitConfig,
) -> Result<()> {
let mut data = vec![];
if let Some(alt_text) = alt {
Expand All @@ -911,7 +922,7 @@ impl Clipboard {
bytes: html.into_owned().into_bytes(),
format: self.inner.atoms.HTML,
});
self.inner.write(data, selection, wait, until)
self.inner.write(data, selection, wait)
}

#[cfg(feature = "image-data")]
Expand All @@ -937,12 +948,11 @@ impl Clipboard {
&self,
image: ImageData,
selection: LinuxClipboardKind,
wait: bool,
until: Option<Instant>,
wait: WaitConfig,
) -> Result<()> {
let encoded = encode_as_png(&image)?;
let data = vec![ClipboardData { bytes: encoded, format: self.inner.atoms.PNG_MIME }];
self.inner.write(data, selection, wait, until)
self.inner.write(data, selection, wait)
}
}

Expand Down

0 comments on commit 6cf324c

Please sign in to comment.