Introduce Kind in core::clipboard

This commit is contained in:
Héctor Ramón Jiménez 2024-02-13 03:14:08 +01:00
parent 4155edab8d
commit 508b3fe1f1
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 97 additions and 130 deletions

View file

@ -704,27 +704,15 @@ pub fn run_command<A, C, E>(
runtime.run(stream);
}
command::Action::Clipboard(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read());
clipboard::Action::Read(tag, kind) => {
let message = tag(clipboard.read(kind));
proxy
.send_event(message)
.expect("Send message to event loop");
}
clipboard::Action::Write(contents) => {
clipboard.write(contents);
}
},
command::Action::ClipboardPrimary(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read_primary());
proxy
.send_event(message)
.expect("Send message to event loop");
}
clipboard::Action::Write(contents) => {
clipboard.write_primary(contents);
clipboard::Action::Write(contents, kind) => {
clipboard.write(kind, contents);
}
},
command::Action::Window(action) => match action {

View file

@ -1,5 +1,7 @@
//! Access the clipboard.
use crate::core::clipboard::Kind;
/// A buffer for short-term storage and transfer within and between
/// applications.
#[allow(missing_debug_implementations)]
@ -33,46 +35,32 @@ impl Clipboard {
}
/// Reads the current content of the [`Clipboard`] as text.
pub fn read(&self) -> Option<String> {
pub fn read(&self, kind: Kind) -> Option<String> {
match &self.state {
State::Connected(clipboard) => clipboard.read().ok(),
State::Connected(clipboard) => match kind {
Kind::Standard => clipboard.read().ok(),
Kind::Primary => clipboard.read_primary().and_then(Result::ok),
},
State::Unavailable => None,
}
}
/// Writes the given text contents to the [`Clipboard`].
pub fn write(&mut self, contents: String) {
match &mut self.state {
State::Connected(clipboard) => match clipboard.write(contents) {
Ok(()) => {}
Err(error) => {
log::warn!("error writing to clipboard: {error}");
}
},
State::Unavailable => {}
}
}
/// Reads the current content of primary as text.
pub fn read_primary(&self) -> Option<String> {
match &self.state {
State::Connected(clipboard) => {
clipboard.read_primary().and_then(|r| r.ok())
}
State::Unavailable => None,
}
}
/// Writes the given text contents to primary.
pub fn write_primary(&mut self, contents: String) {
pub fn write(&mut self, kind: Kind, contents: String) {
match &mut self.state {
State::Connected(clipboard) => {
match clipboard.write_primary(contents) {
Some(Ok(())) => {}
Some(Err(error)) => {
log::warn!("error writing to primary: {error}");
let result = match kind {
Kind::Standard => clipboard.write(contents),
Kind::Primary => {
clipboard.write_primary(contents).unwrap_or(Ok(()))
}
};
match result {
Ok(()) => {}
Err(error) => {
log::warn!("error writing to clipboard: {error}");
}
None => {}
}
}
State::Unavailable => {}
@ -81,19 +69,11 @@ impl Clipboard {
}
impl crate::core::Clipboard for Clipboard {
fn read(&self) -> Option<String> {
self.read()
fn read(&self, kind: Kind) -> Option<String> {
self.read(kind)
}
fn write(&mut self, contents: String) {
self.write(contents);
}
fn read_primary(&self) -> Option<String> {
self.read_primary()
}
fn write_primary(&mut self, contents: String) {
self.write_primary(contents);
fn write(&mut self, kind: Kind, contents: String) {
self.write(kind, contents);
}
}

View file

@ -876,27 +876,15 @@ fn run_command<A, C, E>(
runtime.run(Box::pin(stream));
}
command::Action::Clipboard(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read());
clipboard::Action::Read(tag, kind) => {
let message = tag(clipboard.read(kind));
proxy
.send_event(message)
.expect("Send message to event loop");
}
clipboard::Action::Write(contents) => {
clipboard.write(contents);
}
},
command::Action::ClipboardPrimary(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read_primary());
proxy
.send_event(message)
.expect("Send message to event loop");
}
clipboard::Action::Write(contents) => {
clipboard.write_primary(contents);
clipboard::Action::Write(contents, kind) => {
clipboard.write(kind, contents);
}
},
command::Action::Window(action) => match action {