47 lines
1.5 KiB
Rust
47 lines
1.5 KiB
Rust
use crate::state::MemoryState;
|
|
use crate::models::StickyNote;
|
|
use std::sync::Arc;
|
|
use tokio::time::{sleep, Duration};
|
|
use arboard::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(mut clipboard) = Clipboard::new() {
|
|
if let Ok(text) = clipboard.get_text() {
|
|
if 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|