patch: fix macos clipboard

1. wrong namings of NsPasteboard
2. wrap Pasteboard in Lazy

Signed-off-by: ClSlaid <cailue@bupt.edu.cn>
This commit is contained in:
ClSlaid 2023-10-29 03:30:15 +08:00
parent 7880cba0f9
commit 36d4baaa8e
No known key found for this signature in database
GPG Key ID: E0A5F564C51C056E
4 changed files with 16 additions and 20 deletions

View File

@ -24,10 +24,10 @@ libc = {version = "0.2"}
dashmap = "5.5"
percent-encoding = "2.3"
utf16string = "0.2"
once_cell = "1.18"
[target.'cfg(target_os = "linux")'.dependencies]
x11-clipboard = {git="https://github.com/clslaid/x11-clipboard", branch = "feat/store-batch"}
once_cell = "1.18"
x11rb = {version = "0.12", features = ["all-extensions"]}
[target.'cfg(target_os = "macos")'.dependencies]

View File

@ -40,7 +40,6 @@ use utf16string::WStr;
use crate::{send_data, ClipboardFile, CliprdrError};
#[cfg(target_os = "linux")]
use super::LDAP_EPOCH_DELTA;
/// fuse server ready retry max times

View File

@ -89,7 +89,7 @@ fn get_sys_clipboard(ignore_path: &PathBuf) -> Result<Box<dyn SysClipboard>, Cli
#[cfg(target_os = "macos")]
fn get_sys_clipboard(ignore_path: &PathBuf) -> Result<Box<dyn SysClipboard>, CliprdrError> {
use ns_clipboard::*;
let ns_pb = NSPasteboard::new(ignore_path)?;
let ns_pb = NsPasteboard::new(ignore_path)?;
Ok(Box::new(ns_pb) as Box<_>)
}

View File

@ -5,15 +5,24 @@ use std::{
};
use cacao::pasteboard::{Pasteboard, PasteboardName};
use hbb_common::log;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use crate::{platform::unix::send_format_list, CliprdrError};
use super::SysClipboard;
static NS_PASTEBOARD: Lazy<Pasteboard> = Lazy::new(|| Pasteboard::named(PasteboardName::General));
#[inline]
fn wait_file_list() -> Option<Vec<PathBuf>> {
NS_PASTEBOARD
.get_file_urls()
.ok()
.map(|v| v.into_iter().map(|nsurl| nsurl.to_path_buf()).collect())
}
pub struct NsPasteboard {
stopped: AtomicBool,
pasteboard: Pasteboard,
ignore_path: PathBuf,
former_file_list: Mutex<Vec<PathBuf>>,
@ -21,22 +30,13 @@ pub struct NsPasteboard {
impl NsPasteboard {
pub fn new(ignore_path: &PathBuf) -> Result<Self, CliprdrError> {
let pasteboard = Pasteboard::named(PasteboardName::General);
Ok(Self {
stopped: AtomicBool::new(false),
ignore_path: ignore_path.to_owned(),
pasteboard,
former_file_list: Mutex::new(vec![]),
})
}
fn wait_file_list(&self) -> Option<Vec<PathBuf>> {
self.pasteboard
.get_file_urls()
.ok()
.map(|v| v.into_iter().map(|nsurl| nsurl.to_path_buf()).collect())
}
#[inline]
fn is_stopped(&self) -> bool {
self.stopped.load(Ordering::Relaxed)
@ -46,11 +46,8 @@ impl NsPasteboard {
impl SysClipboard for NsPasteboard {
fn set_file_list(&self, paths: &[PathBuf]) -> Result<(), CliprdrError> {
*self.former_file_list.lock() = paths.to_vec();
let uri_list: Vec<String> = paths.iter().map(encode_path_to_uri).collect();
let uri_list = uri_list.join("\n");
let uri_list = uri_list.as_bytes().to_vec();
self.pasteboard
.set_file_urls(uri_list)
NS_PASTEBOARD
.set_file_urls(paths)
.map_err(|_| CliprdrError::ClipboardInternalError)
}
@ -62,7 +59,7 @@ impl SysClipboard for NsPasteboard {
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
}
let file_list = match self.wait_file_list() {
let file_list = match wait_file_list() {
Some(v) => v,
None => {
std::thread::sleep(std::time::Duration::from_millis(100));