use hyprforge_clipboard::{ClipboardWriter, Entry, Shortcut};
use std::sync::Mutex;
use std::time::Duration;
const SELECTION_WAIT: Duration = Duration::from_secs(2);
pub trait Chooser {
fn set_clipboard(&self, entry: &Entry) -> Result<(), String>;
fn finish_paste(&self, shortcut: Shortcut);
}
pub struct Wired {
writer: hyprforge_clipboard::WaylandWriter,
paster: hyprforge_clipboard::WaylandPaster,
guard: Mutex<Option<<hyprforge_clipboard::WaylandWriter as ClipboardWriter>::Guard>>,
}
impl Wired {
pub fn connect() -> anyhow::Result<Self> {
Ok(Wired {
writer: hyprforge_clipboard::WaylandWriter::new(),
paster: hyprforge_clipboard::WaylandPaster::connect()?,
guard: Mutex::new(None),
})
}
}
impl Chooser for Wired {
fn set_clipboard(&self, entry: &Entry) -> Result<(), String> {
match hyprforge_clipboard::ipc::set_clipboard(entry.id.as_str()) {
Ok(()) => return Ok(()),
Err(reason) => {
eprintln!(
"hyprforge-clipd didn't take the clipboard ({reason}); \
holding it here instead, so it will last only until this popup exits"
);
}
}
let guard = self
.writer
.set_selection(entry.content.clone())
.map_err(|e| e.to_string())?;
*self.guard.lock().unwrap_or_else(|e| e.into_inner()) = Some(guard);
Ok(())
}
fn finish_paste(&self, shortcut: Shortcut) {
use hyprforge_clipboard::{PasteOutcome, PasteSynthesizer, SelectionGuard, SelectionOutcome};
if self.paster.paste(shortcut) == PasteOutcome::Unavailable {
let keys = match shortcut {
Shortcut::CtrlV => "Ctrl+V",
Shortcut::CtrlShiftV => "Ctrl+Shift+V",
};
eprintln!(
"copied to the clipboard — this compositor has no virtual-keyboard \
protocol, so press {keys} yourself to paste it"
);
}
let Some(guard) = self.guard.lock().unwrap_or_else(|e| e.into_inner()).take() else {
return;
};
match guard.wait(SELECTION_WAIT) {
SelectionOutcome::Served | SelectionOutcome::Superseded => {}
SelectionOutcome::TimedOut => {
eprintln!(
"copied to the clipboard, but nothing pasted it within {SELECTION_WAIT:?} — \
if the paste didn't happen, copy it again before pasting by hand"
);
}
}
}
}
#[cfg(test)]
pub mod mock {
use super::*;
use hyprforge_clipboard::EntryId;
use std::cell::RefCell;
pub struct MockChooser {
pub calls: RefCell<Vec<EntryId>>,
pub result: Result<(), String>,
pub log: RefCell<Vec<&'static str>>,
}
impl MockChooser {
pub fn succeeding() -> Self {
MockChooser { calls: RefCell::new(Vec::new()), result: Ok(()), log: RefCell::new(Vec::new()) }
}
pub fn failing(message: &str) -> Self {
MockChooser {
calls: RefCell::new(Vec::new()),
result: Err(message.to_string()),
log: RefCell::new(Vec::new()),
}
}
}
impl Chooser for MockChooser {
fn set_clipboard(&self, entry: &Entry) -> Result<(), String> {
self.calls.borrow_mut().push(entry.id.clone());
self.log.borrow_mut().push("set_clipboard");
self.result.clone()
}
fn finish_paste(&self, _shortcut: Shortcut) {
self.log.borrow_mut().push("finish_paste");
}
}
}
#[cfg(test)]
mod tests {
use super::mock::MockChooser;
use super::*;
use hyprforge_clipboard::{Content, EntryId};
fn entry(text: &str) -> Entry {
let content = Content::Text(text.to_string());
Entry { id: EntryId::of(&content), content, copied_at: 0, pinned: false }
}
#[test]
fn choosing_an_entry_hands_exactly_that_entry_to_the_chooser() {
let chooser = MockChooser::succeeding();
let picked = entry("pick me");
chooser.set_clipboard(&picked).unwrap();
assert_eq!(chooser.calls.borrow().as_slice(), std::slice::from_ref(&picked.id));
}
#[test]
fn a_failing_chooser_reports_its_message() {
let chooser = MockChooser::failing("no seat");
let err = chooser.set_clipboard(&entry("x")).unwrap_err();
assert_eq!(err, "no seat");
}
#[test]
fn finishing_a_choice_runs_set_clipboard_before_finish_paste() {
let chooser = MockChooser::succeeding();
chooser.set_clipboard(&entry("x")).unwrap();
chooser.finish_paste(Shortcut::CtrlV);
assert_eq!(chooser.log.borrow().as_slice(), &["set_clipboard", "finish_paste"]);
}
}