Fix missing clipboard content

This commit is contained in:
cc-morning 2021-10-24 23:47:02 +08:00
parent 516904551c
commit 972ec8d79d
2 changed files with 48 additions and 68 deletions

View File

@ -1,12 +1,11 @@
use std::sync::mpsc::Sender;
use clipboard_master::{CallbackResult, ClipboardHandler, Master};
use super::*; use super::*;
pub use crate::common::{ pub use crate::common::{
check_clipboard, ClipboardContext, CLIPBOARD_INTERVAL as INTERVAL, CLIPBOARD_NAME as NAME, check_clipboard, ClipboardContext, CLIPBOARD_INTERVAL as INTERVAL, CLIPBOARD_NAME as NAME,
CONTENT, CONTENT,
}; };
use clipboard_master::{CallbackResult, ClipboardHandler, Master};
use hbb_common::ResultType;
use std::{sync, sync::mpsc::Sender, thread};
struct State { struct State {
ctx: Option<ClipboardContext>, ctx: Option<ClipboardContext>,
@ -31,33 +30,39 @@ impl super::service::Reset for State {
} }
} }
pub fn new() -> GenericService {
let sp = GenericService::new(NAME, true);
sp.run::<_>(listen::run);
sp
}
mod listen {
use super::*;
struct ClipHandle { struct ClipHandle {
tx: Sender<bool>, tx: Sender<()>,
} }
impl ClipboardHandler for ClipHandle { impl ClipboardHandler for ClipHandle {
fn on_clipboard_change(&mut self) -> CallbackResult { fn on_clipboard_change(&mut self) -> CallbackResult {
let _ = self.tx.send(true); let _ = self.tx.send(());
CallbackResult::Next CallbackResult::Next
} }
} }
pub fn new() -> GenericService { fn notify(tx: Sender<()>) -> ResultType<()> {
let sp = GenericService::new(NAME, true);
sp.listen::<State, _, _>(notify, run);
sp
}
fn notify(tx: Sender<bool>) -> ResultType<()> {
Master::new(ClipHandle { tx }).run()?; Master::new(ClipHandle { tx }).run()?;
Ok(()) Ok(())
} }
fn run(sp: GenericService, state: &mut State) -> ResultType<()> { pub fn run(sp: GenericService) -> ResultType<()> {
if let Some(ctx) = state.ctx.as_mut() { let mut state = State::default();
if let Some(msg) = check_clipboard(ctx, None) { let (tx, rx) = sync::mpsc::channel::<()>();
sp.send(msg); thread::spawn(|| {
} let _ = notify(tx);
});
while sp.ok() {
sp.snapshot(|sps| { sp.snapshot(|sps| {
let txt = crate::CONTENT.lock().unwrap().clone(); let txt = crate::CONTENT.lock().unwrap().clone();
if !txt.is_empty() { if !txt.is_empty() {
@ -66,6 +71,15 @@ fn run(sp: GenericService, state: &mut State) -> ResultType<()> {
} }
Ok(()) Ok(())
})?; })?;
if let Ok(()) = rx.recv() {
if let Some(ctx) = state.ctx.as_mut() {
if let Some(msg) = check_clipboard(ctx, None) {
sp.send(msg);
}
}
}
} }
Ok(()) Ok(())
} }
}

View File

@ -1,6 +1,5 @@
use super::*; use super::*;
use std::{ use std::{
sync::{self, mpsc::Sender},
thread::{self, JoinHandle}, thread::{self, JoinHandle},
time, time,
}; };
@ -154,39 +153,6 @@ impl<T: Subscriber + From<ConnInner>> ServiceTmpl<T> {
} }
} }
pub fn listen<S, N, F>(&self, notify: N, callback: F)
where
N: 'static + FnMut(Sender<bool>) -> ResultType<()> + Send,
F: 'static + FnMut(Self, &mut S) -> ResultType<()> + Send,
S: 'static + Default + Reset,
{
let mut notify = notify;
let mut callback = callback;
let sp = self.clone();
let (tx, rx) = sync::mpsc::channel();
thread::spawn(move || {
let _ = notify(tx);
});
let thread = thread::spawn(move || {
let mut state = S::default();
while let Ok(changed) = rx.recv() {
if changed && sp.active() {
if sp.has_subscribes() {
if let Err(err) = callback(sp.clone(), &mut state) {
log::error!("Error of {} service: {}", sp.name(), err);
#[cfg(windows)]
crate::platform::windows::try_change_desktop();
}
} else {
state.reset();
}
}
}
});
self.0.write().unwrap().handle = Some(thread);
}
pub fn repeat<S, F>(&self, interval_ms: u64, callback: F) pub fn repeat<S, F>(&self, interval_ms: u64, callback: F)
where where
F: 'static + FnMut(Self, &mut S) -> ResultType<()> + Send, F: 'static + FnMut(Self, &mut S) -> ResultType<()> + Send,