|
| 1 | +use arboard::Clipboard; |
| 2 | +use input_event::{ClipboardEvent, Event}; |
| 3 | +use std::sync::{Arc, Mutex}; |
| 4 | +use std::time::{Duration, Instant}; |
| 5 | +use tokio::sync::mpsc::{self, Receiver, Sender}; |
| 6 | +use tokio::task::spawn_blocking; |
| 7 | +use tokio::time::interval; |
| 8 | + |
| 9 | +use crate::{CaptureError, CaptureEvent}; |
| 10 | + |
| 11 | +/// Clipboard monitor that watches for clipboard changes |
| 12 | +pub struct ClipboardMonitor { |
| 13 | + event_rx: Receiver<CaptureEvent>, |
| 14 | + _event_tx: Sender<CaptureEvent>, |
| 15 | + last_content: Arc<Mutex<Option<String>>>, |
| 16 | + last_change: Arc<Mutex<Option<Instant>>>, |
| 17 | + enabled: Arc<Mutex<bool>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl ClipboardMonitor { |
| 21 | + pub fn new() -> Result<Self, CaptureError> { |
| 22 | + let (event_tx, event_rx) = mpsc::channel(16); |
| 23 | + let last_content: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None)); |
| 24 | + let last_change: Arc<Mutex<Option<Instant>>> = Arc::new(Mutex::new(None)); |
| 25 | + let enabled = Arc::new(Mutex::new(true)); |
| 26 | + |
| 27 | + let last_content_clone = last_content.clone(); |
| 28 | + let last_change_clone = last_change.clone(); |
| 29 | + let enabled_clone = enabled.clone(); |
| 30 | + let event_tx_clone = event_tx.clone(); |
| 31 | + |
| 32 | + // Spawn monitoring task |
| 33 | + tokio::spawn(async move { |
| 34 | + let mut check_interval = interval(Duration::from_millis(500)); |
| 35 | + |
| 36 | + loop { |
| 37 | + check_interval.tick().await; |
| 38 | + |
| 39 | + // Check if enabled |
| 40 | + let is_enabled = { |
| 41 | + let enabled = enabled_clone.lock().unwrap(); |
| 42 | + *enabled |
| 43 | + }; |
| 44 | + |
| 45 | + if !is_enabled { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + // Read clipboard in blocking task |
| 50 | + let last_content_clone2 = last_content_clone.clone(); |
| 51 | + let last_change_clone2 = last_change_clone.clone(); |
| 52 | + let event_tx_clone2 = event_tx_clone.clone(); |
| 53 | + |
| 54 | + let _ = spawn_blocking(move || { |
| 55 | + // Create clipboard instance |
| 56 | + let mut clipboard = match Clipboard::new() { |
| 57 | + Ok(c) => c, |
| 58 | + Err(e) => { |
| 59 | + log::debug!("Failed to create clipboard: {}", e); |
| 60 | + return; |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + // Get current clipboard text |
| 65 | + let current_text = match clipboard.get_text() { |
| 66 | + Ok(text) => { |
| 67 | + log::trace!("Clipboard text read: {} bytes", text.len()); |
| 68 | + text |
| 69 | + } |
| 70 | + Err(e) => { |
| 71 | + // Clipboard might be empty or contain non-text data |
| 72 | + log::trace!("Failed to get clipboard text: {}", e); |
| 73 | + return; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + // Check if content changed |
| 78 | + let mut last_content = last_content_clone2.lock().unwrap(); |
| 79 | + let mut last_change = last_change_clone2.lock().unwrap(); |
| 80 | + |
| 81 | + let content_changed = match last_content.as_ref() { |
| 82 | + None => true, |
| 83 | + Some(last) => last != ¤t_text, |
| 84 | + }; |
| 85 | + |
| 86 | + if content_changed { |
| 87 | + // Debounce: ignore changes within 200ms of last change |
| 88 | + // This prevents infinite loops when both sides update clipboard |
| 89 | + let should_emit = match *last_change { |
| 90 | + None => true, |
| 91 | + Some(instant) => instant.elapsed() > Duration::from_millis(200), |
| 92 | + }; |
| 93 | + |
| 94 | + if should_emit { |
| 95 | + log::info!("Clipboard changed, length: {} bytes", current_text.len()); |
| 96 | + *last_content = Some(current_text.clone()); |
| 97 | + *last_change = Some(Instant::now()); |
| 98 | + |
| 99 | + // Send event |
| 100 | + let event = CaptureEvent::Input(Event::Clipboard( |
| 101 | + ClipboardEvent::Text(current_text), |
| 102 | + )); |
| 103 | + let _ = event_tx_clone2.blocking_send(event); |
| 104 | + } else { |
| 105 | + log::trace!("Clipboard changed but debounced (too recent)"); |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | + .await; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + Ok(Self { |
| 114 | + event_rx, |
| 115 | + _event_tx: event_tx, |
| 116 | + last_content, |
| 117 | + last_change, |
| 118 | + enabled, |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + /// Receive the next clipboard event |
| 123 | + pub async fn recv(&mut self) -> Option<CaptureEvent> { |
| 124 | + self.event_rx.recv().await |
| 125 | + } |
| 126 | + |
| 127 | + /// Enable clipboard monitoring |
| 128 | + pub fn enable(&self) { |
| 129 | + let mut enabled = self.enabled.lock().unwrap(); |
| 130 | + *enabled = true; |
| 131 | + log::info!("Clipboard monitoring enabled"); |
| 132 | + } |
| 133 | + |
| 134 | + /// Disable clipboard monitoring |
| 135 | + pub fn disable(&self) { |
| 136 | + let mut enabled = self.enabled.lock().unwrap(); |
| 137 | + *enabled = false; |
| 138 | + log::info!("Clipboard monitoring disabled"); |
| 139 | + } |
| 140 | + |
| 141 | + /// Update the last known clipboard content (called when we set the clipboard) |
| 142 | + /// This prevents detecting our own clipboard changes as external changes |
| 143 | + pub fn update_last_content(&self, content: String) { |
| 144 | + let mut last_content = self.last_content.lock().unwrap(); |
| 145 | + let mut last_change = self.last_change.lock().unwrap(); |
| 146 | + *last_content = Some(content); |
| 147 | + *last_change = Some(Instant::now()); |
| 148 | + } |
| 149 | +} |
0 commit comments