Files
mcp-memory/server/src/clipboard_watcher.rs
T

46 lines
1.6 KiB
Rust

use crate::state::MemoryState;
use crate::models::StickyNote;
use std::sync::Arc;
use tokio::time::{sleep, Duration};
use clipboard_win::{formats, get_clipboard, Clipboard};
pub fn spawn_watcher(state: Arc<MemoryState>) {
tokio::spawn(async move {
let mut last_text = String::new();
loop {
sleep(Duration::from_millis(1000)).await;
let is_enabled = {
let watch = state.clipboard_watch_mode.read().await;
*watch
};
if !is_enabled {
continue;
}
if let Ok(_clip) = tokio::task::spawn_blocking(|| Clipboard::new_attempts(3)).await.unwrap()
&& let Ok(text) = get_clipboard::<String, _>(formats::Unicode)
&& text != last_text && !text.trim().is_empty() {
last_text = text.clone();
let note = StickyNote {
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
content: text,
expires_at: None,
};
state.code.sticky.modify(|notes| {
notes.push(note.clone());
});
// We use rebuild_index to index the new sticky note
state.rebuild_index().await;
}
}
});
}