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

@ -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();