细节调整及优化(26.8.3)

This commit is contained in:
zhongluofeng
2026-08-21 17:25:47 +08:00
parent 0a19b4b38a
commit 4dd60f42a1
72 changed files with 4779 additions and 1395 deletions
+14 -7
View File
@@ -1,7 +1,7 @@
//! 剪贴板监听线程:基于 GetClipboardSequenceNumber 轮询
//!
//! 选用轮询而非 AddClipboardFormatListener 消息窗口:实现更简单、无需消息循环,
//! 800ms 间隔对剪贴板场景延迟可接受,且 GetClipboardSequenceNumber 不需要 OpenClipboard,开销极小。
//! 250ms 间隔兼顾响应速度与开销,且 GetClipboardSequenceNumber 不需要 OpenClipboard,开销极小。
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
@@ -15,18 +15,19 @@ use super::storage::{NewItem, Storage};
use windows_sys::Win32::System::DataExchange::GetClipboardSequenceNumber;
/// 启动监听线程,返回 JoinHandle。
/// `suppress` 记录本应用 copy_back 写入后的剪贴板序列号,用于跳过自身写入产生的记录。
pub fn start_monitor(
storage: Arc<Storage>,
app: AppHandle,
settings: Arc<Mutex<super::manager::ClipboardSettings>>,
suppress: Arc<AtomicBool>,
suppress: Arc<Mutex<Option<u32>>>,
stop: Arc<AtomicBool>,
) -> thread::JoinHandle<()> {
thread::spawn(move || loop {
if stop.load(Ordering::SeqCst) {
break;
}
thread::sleep(Duration::from_millis(800));
thread::sleep(Duration::from_millis(250));
if stop.load(Ordering::SeqCst) {
break;
}
@@ -40,10 +41,13 @@ pub fn start_monitor(
if seq == last {
continue;
}
// 序列号变化,处理一次
if suppress.swap(false, Ordering::SeqCst) {
// 由本应用 copy_back 触发,跳过记录
continue;
// 序列号变化:仅当变化来自本应用 copy_back(序列号精确匹配)时跳过
// 避免旧布尔标志在用户后续复制时被误吞。
if let Ok(mut s) = suppress.lock() {
if *s == Some(seq) {
*s = None;
continue;
}
}
let (rec_text, rec_image, rec_files, max_items, max_image_kb, dedup) = {
let s = settings.lock().unwrap_or_else(|e| e.into_inner());
@@ -99,6 +103,7 @@ fn build_text_item(t: &str) -> NewItem {
kind: "text".into(),
content: Some(t.to_string()),
blob: None,
thumb: None,
preview: make_preview(t, 200),
size: t.len() as i64,
hash: hash_str(t),
@@ -110,6 +115,7 @@ fn build_image_item(dib: &[u8], w: u32, h: u32) -> NewItem {
kind: "image".into(),
content: None,
blob: Some(dib.to_vec()),
thumb: super::reader::dib_to_thumbnail(dib, 256),
preview: format!("图片 {}×{}", w, h),
size: dib.len() as i64,
hash: hash_bytes(dib),
@@ -132,6 +138,7 @@ fn build_files_item(files: &[String]) -> NewItem {
kind: "files".into(),
content: Some(content),
blob: None,
thumb: None,
preview,
size: files.iter().map(|f| f.len()).sum::<usize>() as i64,
hash,