Add primary clipboard support for X11 and wayland
The X11 code already had the functionality. Just didn't expose it. Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>
This commit is contained in:
parent
40447db331
commit
cb8471828a
4 changed files with 70 additions and 6 deletions
18
src/lib.rs
18
src/lib.rs
|
|
@ -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 {
|
pub trait ClipboardProvider {
|
||||||
fn read(&self) -> Result<String, Box<dyn Error>>;
|
fn read(&self) -> Result<String, Box<dyn Error>>;
|
||||||
|
|
||||||
fn write(&mut self, contents: String) -> Result<(), 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,17 @@ impl ClipboardProvider for wayland::Clipboard {
|
||||||
self.read()
|
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>> {
|
fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
|
||||||
self.write(contents)
|
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 {
|
impl ClipboardProvider for x11::Clipboard {
|
||||||
|
|
@ -34,7 +42,15 @@ impl ClipboardProvider for x11::Clipboard {
|
||||||
self.read().map_err(Box::from)
|
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>> {
|
fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
|
||||||
self.write(contents).map_err(Box::from)
|
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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,9 +33,19 @@ impl Clipboard {
|
||||||
Ok(self.context.lock().unwrap().load()?)
|
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>> {
|
pub fn write(&mut self, data: String) -> Result<(), Box<dyn Error>> {
|
||||||
self.context.lock().unwrap().store(data);
|
self.context.lock().unwrap().store(data);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn write_primary(&mut self, data: String) -> Result<(), Box<dyn Error>> {
|
||||||
|
self.context.lock().unwrap().store_primary(data);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,10 +45,9 @@ impl Clipboard {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the current [`Clipboard`] value.
|
fn read_selection(&self, selection: Atom) -> Result<String, Error> {
|
||||||
pub fn read(&self) -> Result<String, Error> {
|
|
||||||
Ok(String::from_utf8(self.load(
|
Ok(String::from_utf8(self.load(
|
||||||
self.reader.atoms.clipboard,
|
selection,
|
||||||
self.reader.atoms.utf8_string,
|
self.reader.atoms.utf8_string,
|
||||||
self.reader.atoms.property,
|
self.reader.atoms.property,
|
||||||
std::time::Duration::from_secs(3),
|
std::time::Duration::from_secs(3),
|
||||||
|
|
@ -56,9 +55,18 @@ impl Clipboard {
|
||||||
.map_err(Error::InvalidUtf8)?)
|
.map_err(Error::InvalidUtf8)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write a new value to the [`Clipboard`].
|
/// Read the current CLIPBOARD [`Clipboard`] value.
|
||||||
pub fn write(&mut self, contents: String) -> Result<(), Error> {
|
pub fn read(&self) -> Result<String, Error> {
|
||||||
let selection = self.writer.atoms.clipboard;
|
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;
|
let target = self.writer.atoms.utf8_string;
|
||||||
|
|
||||||
self.selections
|
self.selections
|
||||||
|
|
@ -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.
|
/// load value.
|
||||||
fn load(
|
fn load(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue