change update_clipboard to threaded, since creating a context may take long

This commit is contained in:
rustdesk
2024-06-30 17:05:09 +08:00
parent 25d0ced8ba
commit 1f129e6ef3
5 changed files with 52 additions and 14 deletions

View File

@@ -144,8 +144,8 @@ const PUBLIC_SERVER: &str = "public";
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn get_old_clipboard_text() -> &'static Arc<Mutex<String>> {
&OLD_CLIPBOARD_TEXT
pub fn get_old_clipboard_text() -> Arc<Mutex<String>> {
OLD_CLIPBOARD_TEXT.clone()
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]

View File

@@ -1180,7 +1180,7 @@ impl<T: InvokeUiSession> Remote<T> {
Some(message::Union::Clipboard(cb)) => {
if !self.handler.lc.read().unwrap().disable_clipboard.v {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
update_clipboard(cb, Some(&crate::client::get_old_clipboard_text()));
update_clipboard(cb, Some(crate::client::get_old_clipboard_text()));
#[cfg(any(target_os = "android", target_os = "ios"))]
{
let content = if cb.compress {

View File

@@ -364,7 +364,7 @@ pub fn get_default_sound_input() -> Option<String> {
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc<Mutex<String>>>) {
fn update_clipboard_(clipboard: Clipboard, old: Option<Arc<Mutex<String>>>) {
let content = if clipboard.compress {
decompress(&clipboard.content)
} else {
@@ -378,7 +378,7 @@ pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc<Mutex<String>>>)
match ClipboardContext::new() {
Ok(mut ctx) => {
let side = if old.is_none() { "host" } else { "client" };
let old = if let Some(old) = old { old } else { &CONTENT };
let old = if let Some(old) = old { old } else { CONTENT.clone() };
*old.lock().unwrap() = content.clone();
let _lock = ARBOARD_MTX.lock().unwrap();
allow_err!(ctx.set_text(content));
@@ -391,6 +391,13 @@ pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc<Mutex<String>>>)
}
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn update_clipboard(clipboard: Clipboard, old: Option<Arc<Mutex<String>>>) {
std::thread::spawn(move || {
update_clipboard_(clipboard, old);
});
}
#[cfg(feature = "use_rubato")]
pub fn resample_channels(
data: &[f32],
@@ -1519,6 +1526,8 @@ impl ClipboardContext {
#[inline]
#[cfg(any(target_os = "windows", target_os = "macos"))]
pub fn new() -> ResultType<ClipboardContext> {
let x: Option<()> = None;
x.unwrap();
Ok(ClipboardContext(arboard::Clipboard::new()?))
}
@@ -1543,13 +1552,6 @@ impl ClipboardContext {
}
}
#[inline]
#[cfg(any(target_os = "windows", target_os = "macos"))]
pub fn get_text(&mut self) -> ResultType<String> {
Ok(self.0.get_text()?)
}
#[cfg(target_os = "linux")]
pub fn get_text(&mut self) -> ResultType<String> {
Ok(self.0.get_text()?)
}