Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add primary clipboard support for X11 and wayland #25

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,26 @@ impl Clipboard {
}
}

impl Clipboard {
pub fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
self.raw.read_primary()
}

pub fn write_primary(&mut self, contents: String) -> Option<Result<(), Box<dyn Error>>> {
self.raw.write_primary(contents)
}
}

pub trait ClipboardProvider {
fn read(&self) -> Result<String, Box<dyn Error>>;

fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>>;

fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
None
}

fn write_primary(&mut self, _contents: String) -> Option<Result<(), Box<dyn Error>>> {
None
}
Comment on lines +90 to +96
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make these available only on Linux?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both me and @MoSal created a fix for this, I choose to expose this for unix only, but that is really not very nice to have in the api, since that leaks up all the way through iced to applications trying to use this. So, even if it is more technically correct to only expose this for unix systems, it is much more clean and user friendly with this implementation that have a default implementation that returns None on systems without primary.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

}
16 changes: 16 additions & 0 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,33 @@ impl ClipboardProvider for wayland::Clipboard {
self.read()
}

fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
Some(self.read_primary())
}

fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
self.write(contents)
}

fn write_primary(&mut self, contents: String) -> Option<Result<(), Box<dyn Error>>> {
Some(self.write_primary(contents))
}
}

impl ClipboardProvider for x11::Clipboard {
fn read(&self) -> Result<String, Box<dyn Error>> {
self.read().map_err(Box::from)
}

fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
Some(self.read_primary().map_err(Box::from))
}

fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
self.write(contents).map_err(Box::from)
}

fn write_primary(&mut self, contents: String) -> Option<Result<(), Box<dyn Error>>> {
Some(self.write_primary(contents).map_err(Box::from))
}
}
10 changes: 10 additions & 0 deletions wayland/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,19 @@ impl Clipboard {
Ok(self.context.lock().unwrap().load()?)
}

pub fn read_primary(&self) -> Result<String, Box<dyn Error>> {
Ok(self.context.lock().unwrap().load_primary()?)
}

pub fn write(&mut self, data: String) -> Result<(), Box<dyn Error>> {
self.context.lock().unwrap().store(data);

Ok(())
}

pub fn write_primary(&mut self, data: String) -> Result<(), Box<dyn Error>> {
self.context.lock().unwrap().store_primary(data);

Ok(())
}
}
32 changes: 26 additions & 6 deletions x11/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,28 @@ impl Clipboard {
})
}

/// Read the current [`Clipboard`] value.
pub fn read(&self) -> Result<String, Error> {
fn read_selection(&self, selection: Atom) -> Result<String, Error> {
Ok(String::from_utf8(self.load(
self.reader.atoms.clipboard,
selection,
self.reader.atoms.utf8_string,
self.reader.atoms.property,
std::time::Duration::from_secs(3),
)?)
.map_err(Error::InvalidUtf8)?)
}

/// Write a new value to the [`Clipboard`].
pub fn write(&mut self, contents: String) -> Result<(), Error> {
let selection = self.writer.atoms.clipboard;
/// Read the current CLIPBOARD [`Clipboard`] value.
pub fn read(&self) -> Result<String, Error> {
self.read_selection(self.reader.atoms.clipboard)
}


/// Read the current PRIMARY [`Clipboard`] value.
pub fn read_primary(&self) -> Result<String, Error> {
self.read_selection(self.reader.atoms.primary)
}

fn write_selection(&mut self, selection: Atom, contents: String) -> Result<(), Error> {
let target = self.writer.atoms.utf8_string;

self.selections
Expand Down Expand Up @@ -87,6 +95,18 @@ impl Clipboard {
}
}

/// Write a new value to the CLIPBOARD [`Clipboard`].
pub fn write(&mut self, contents: String) -> Result<(), Error> {
let selection = self.writer.atoms.clipboard;
self.write_selection(selection, contents)
}

/// Write a new value to the PRIMARY [`Clipboard`].
pub fn write_primary(&mut self, contents: String) -> Result<(), Error> {
let selection = self.writer.atoms.primary;
self.write_selection(selection, contents)
}

/// load value.
fn load(
&self,
Expand Down
Loading