Add support for primary clipboard

This commit is contained in:
Mattias Eriksson 2024-02-07 17:25:40 +01:00 committed by Héctor Ramón Jiménez
parent 7615b2240c
commit 4155edab8d
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
7 changed files with 96 additions and 1 deletions

View file

@ -8,6 +8,12 @@ pub trait Clipboard {
/// Writes the given text contents to the [`Clipboard`].
fn write(&mut self, contents: String);
/// Reads the current content of Primary as text.
fn read_primary(&self) -> Option<String>;
/// Writes the given text contents to Primary.
fn write_primary(&mut self, contents: String);
}
/// A null implementation of the [`Clipboard`] trait.
@ -20,4 +26,10 @@ impl Clipboard for Null {
}
fn write(&mut self, _contents: String) {}
fn read_primary(&self) -> Option<String> {
None
}
fn write_primary(&mut self, _contents: String) {}
}

View file

@ -51,3 +51,17 @@ pub fn read<Message>(
pub fn write<Message>(contents: String) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Write(contents)))
}
/// Read the current contents of primary.
pub fn read_primary<Message>(
f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::ClipboardPrimary(Action::Read(Box::new(
f,
))))
}
/// Write the given contents to primary.
pub fn write_primary<Message>(contents: String) -> Command<Message> {
Command::single(command::Action::ClipboardPrimary(Action::Write(contents)))
}

View file

@ -26,6 +26,9 @@ pub enum Action<T> {
/// Run a clipboard action.
Clipboard(clipboard::Action<T>),
/// Run a clipboard action on primary.
ClipboardPrimary(clipboard::Action<T>),
/// Run a window action.
Window(window::Action<T>),
@ -66,6 +69,9 @@ impl<T> Action<T> {
Self::Future(future) => Action::Future(Box::pin(future.map(f))),
Self::Stream(stream) => Action::Stream(Box::pin(stream.map(f))),
Self::Clipboard(action) => Action::Clipboard(action.map(f)),
Self::ClipboardPrimary(action) => {
Action::ClipboardPrimary(action.map(f))
}
Self::Window(window) => Action::Window(window.map(f)),
Self::System(system) => Action::System(system.map(f)),
Self::Widget(operation) => {
@ -88,6 +94,9 @@ impl<T> fmt::Debug for Action<T> {
Self::Clipboard(action) => {
write!(f, "Action::Clipboard({action:?})")
}
Self::ClipboardPrimary(action) => {
write!(f, "Action::ClipboardPrimary({action:?})")
}
Self::Window(action) => {
write!(f, "Action::Window({action:?})")
}

View file

@ -200,7 +200,9 @@ pub use crate::core::{
pub mod clipboard {
//! Access the clipboard.
pub use crate::runtime::clipboard::{read, write};
pub use crate::runtime::clipboard::{
read, read_primary, write, write_primary,
};
}
pub mod executor {

View file

@ -715,6 +715,18 @@ pub fn run_command<A, C, E>(
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);
}
},
command::Action::Window(action) => match action {
window::Action::Close(_id) => {
*should_exit = true;

View file

@ -52,6 +52,32 @@ impl Clipboard {
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) {
match &mut self.state {
State::Connected(clipboard) => {
match clipboard.write_primary(contents) {
Some(Ok(())) => {}
Some(Err(error)) => {
log::warn!("error writing to primary: {error}");
}
None => {}
}
}
State::Unavailable => {}
}
}
}
impl crate::core::Clipboard for Clipboard {
@ -62,4 +88,12 @@ impl crate::core::Clipboard for Clipboard {
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);
}
}

View file

@ -887,6 +887,18 @@ fn run_command<A, C, E>(
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);
}
},
command::Action::Window(action) => match action {
window::Action::Spawn(id, settings) => {
let monitor = window_manager.last_monitor();