diff --git a/src/server/clipboard_service.rs b/src/server/clipboard_service.rs index 0a1745fcb..e44b0ed75 100644 --- a/src/server/clipboard_service.rs +++ b/src/server/clipboard_service.rs @@ -24,7 +24,7 @@ mod listen { use super::*; static RUNNING: AtomicBool = AtomicBool::new(true); - static WAIT: Duration = Duration::from_millis(1500); + static WAIT: Duration = Duration::from_millis(33); struct ClipHandle { tx: SyncSender<()>, @@ -69,21 +69,34 @@ mod listen { let _ = Master::new(ClipHandle { tx }).run(); }); + check_clipboard(&mut ctx, None); // initialize CONTENT for snapshot while sp.ok() { + let mut update = None; + sp.snapshot(|sps| { + if sps.has_subscribes() { + update = check_clipboard(&mut ctx, None); + } + // if there is update, msg will be later together, + // otherwise it will be only sent to new subscriber, + // but old subscribers ignored + if update.is_none() { + let txt = crate::CONTENT.lock().unwrap().clone(); + if !txt.is_empty() { + let msg_out = crate::create_clipboard_msg(txt); + sps.send_shared(Arc::new(msg_out)); + } + } + Ok(()) + })?; + if let Some(msg) = update { + sp.send(msg); + } + if let Ok(_) = rx.recv_timeout(WAIT) { if let Some(msg) = check_clipboard(&mut ctx, None) { sp.send(msg); } } - - sp.snapshot(|sps| { - let txt = crate::CONTENT.lock().unwrap().clone(); - if !txt.is_empty() { - let msg_out = crate::create_clipboard_msg(txt); - sps.send_shared(Arc::new(msg_out)); - } - Ok(()) - })?; } RUNNING.store(false, Ordering::SeqCst); @@ -96,13 +109,13 @@ mod listen { } fn trigger(ctx: &mut ClipboardContext) { + let mut old_text = "".to_owned(); let _ = match ctx.get_text() { Ok(text) => { - if !text.is_empty() { - ctx.set_text(text).ok(); - } + old_text = text; } Err(_) => {} }; + ctx.set_text(old_text).ok(); } } diff --git a/src/server/connection.rs b/src/server/connection.rs index 9c1fa92b7..cb063d49d 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -330,7 +330,7 @@ impl Connection { if let Err(e) = handler_input.join() { log::error!("Failed to join input thread, {:?}", e); } else { - log::info!("Blank thread exited"); + log::info!("input thread exited"); } let _ = crate::platform::block_input(false);