Skip to content

Commit

Permalink
fix: empty clipboard issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wed0n committed Dec 24, 2023
1 parent 09b4181 commit 8a65a74
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "paster",
"private": true,
"version": "0.1.3",
"version": "0.1.4",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "paster"
version = "0.1.3"
version = "0.1.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
58 changes: 28 additions & 30 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,38 @@ fn get_clipboard() -> Result<Vec<u16>, &'static str> {

//参考 https://learn.microsoft.com/zh-cn/windows/win32/dataxchg/using-the-clipboard#pasting-information-from-the-clipboard
unsafe {
if let Err(_) = OpenClipboard(HWND(0)) {
return Err("打开剪切板错误");
}
match GetClipboardData(CF_UNICODETEXT) {
Ok(hglb) => {
let locker = HGLOBAL(hglb.0 as *mut c_void);
let raw_data = GlobalLock(locker);
let data = raw_data as *const u16;
let mut i = 0usize;

loop {
let item = *data.add(i);
i += 1;
if item == 0 {
break;
}
if item == 13 {
//舍弃'\r'
continue;
}
result.push(item);
}

if let Err(_) = GlobalUnlock(locker) {
return Err("解除剪切板锁定失败");
}
OpenClipboard(HWND(0)).or(Err("打开剪切板错误"))?;
let hglb = GetClipboardData(CF_UNICODETEXT).map_err(|_| {
if let Err(_) = CloseClipboard() {
return "关闭剪切板失败";
}
"获取剪切板数据错误"
})?;
let locker = HGLOBAL(hglb.0 as *mut c_void);
let raw_data = GlobalLock(locker);
let data = raw_data as *const u16;
let mut i = 0usize;

Err(_) => return Err("获取剪切板数据错误"),
loop {
let item = *data.add(i);
i += 1;
if item == 0 {
break;
}
if item == 13 {
//舍弃'\r'
continue;
}
result.push(item);
}

if let Err(_) = CloseClipboard() {
return Err("关闭剪切板失败");
}
GlobalUnlock(locker).map_err(|_| {
if let Err(_) = CloseClipboard() {
return "关闭剪切板失败";
}
"解除剪切板锁定失败"
})?;
CloseClipboard().or(Err("关闭剪切板失败"))?;
}
return Ok(result);
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "paster",
"version": "0.1.3"
"version": "0.1.4"
},
"tauri": {
"allowlist": {
Expand Down
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export default function App() {
float: parseInt(lastFloat.current),
})
setErrMsg('')
} catch (e: any) {
setErrMsg(e)
} catch (e) {
setErrMsg(e as string)
}
setButtonDisabled(false)
setCounter(-1)
Expand Down

0 comments on commit 8a65a74

Please sign in to comment.