make clipboard dropped after no sub

This commit is contained in:
rustdesk 2024-07-01 00:24:23 +08:00
parent 84b5cd70ed
commit cd73368cb9
2 changed files with 14 additions and 12 deletions

View File

@ -4,12 +4,18 @@ pub use crate::common::{
CONTENT,
};
#[derive(Default)]
struct State {
ctx: Option<ClipboardContext>,
}
impl Default for State {
fn default() -> Self {
impl super::service::Reset for State {
fn reset(&mut self) {
*CONTENT.lock().unwrap() = Default::default();
self.ctx = None;
}
fn init(&mut self) {
let ctx = match ClipboardContext::new(true) {
Ok(ctx) => Some(ctx),
Err(err) => {
@ -17,13 +23,7 @@ impl Default for State {
None
}
};
Self { ctx }
}
}
impl super::service::Reset for State {
fn reset(&mut self) {
*CONTENT.lock().unwrap() = Default::default();
self.ctx = ctx;
}
}

View File

@ -35,6 +35,7 @@ pub struct ServiceInner<T: Subscriber + From<ConnInner>> {
pub trait Reset {
fn reset(&mut self);
fn init(&mut self) {}
}
pub struct ServiceTmpl<T: Subscriber + From<ConnInner>>(Arc<RwLock<ServiceInner<T>>>);
@ -266,15 +267,16 @@ impl<T: Subscriber + From<ConnInner>> ServiceTmpl<T> {
while sp.active() {
let now = time::Instant::now();
if sp.has_subscribes() {
if !may_reset {
may_reset = true;
state.init();
}
if let Err(err) = callback(sp.clone(), &mut state) {
log::error!("Error of {} service: {}", sp.name(), err);
thread::sleep(time::Duration::from_millis(MAX_ERROR_TIMEOUT));
#[cfg(windows)]
crate::platform::windows::try_change_desktop();
}
if !may_reset {
may_reset = true;
}
} else if may_reset {
state.reset();
may_reset = false;