Skip to content

Commit

Permalink
Merge pull request #33 from ChurchTao/fix/win/set-with-clear
Browse files Browse the repository at this point in the history
fix: `set` on windows without clear
  • Loading branch information
ChurchTao authored Jul 22, 2024
2 parents bb24541 + dcb46a0 commit 6945393
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
34 changes: 24 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
# Changelog

## v0.1.9 (2024-07-22) [released]

- Fix: Bug: `set` on windows without clear [issues#32](https://github.com/ChurchTao/clipboard-rs/issues/32)

## v0.1.8 (2024-07-18) [released]

-Fix: Bug: When read rimeout on Linux there is throw error but not loop [issues#30](https://github.com/ChurchTao/clipboard-rs/issues/30)
- Fix: Bug: When read rimeout on Linux there is throw error but not
loop [issues#30](https://github.com/ChurchTao/clipboard-rs/issues/30)

## v0.1.7 (2024-04-30) [released]

-Fix: Bug: Cannot write all content when writing to html on Windows [issues#23](https://github.com/ChurchTao/clipboard-rs/issues/23)
- Fix: Bug: Cannot write all content when writing to html on
Windows [issues#23](https://github.com/ChurchTao/clipboard-rs/issues/23)

## v0.1.6 (2024-04-12) [released]

- Fix: Bug: Cannot paste after writing image to clipboard (on Windows) #17 [issues#17](https://github.com/ChurchTao/clipboard-rs/issues/17)
- Fix: Bug: No transparent background for clipboard image read on Windows #18 [issues#18](https://github.com/ChurchTao/clipboard-rs/issues/18)
- Fix: Bug: Cannot read clipboard image on MacOS for screenshots taken by certain apps #19 [issues#19](https://github.com/ChurchTao/clipboard-rs/issues/19)
- Fix: Bug: Cannot paste after writing image to clipboard (on Windows)
#17 [issues#17](https://github.com/ChurchTao/clipboard-rs/issues/17)
- Fix: Bug: No transparent background for clipboard image read on Windows
#18 [issues#18](https://github.com/ChurchTao/clipboard-rs/issues/18)
- Fix: Bug: Cannot read clipboard image on MacOS for screenshots taken by certain apps
#19 [issues#19](https://github.com/ChurchTao/clipboard-rs/issues/19)

## v0.1.5 (2024-04-11) [released]

- Fix: Fix the bug `fn get_image()` where image type is `CF_DIBV5` in `win11`. [issues#14](https://github.com/ChurchTao/clipboard-rs/issues/14)
- Fix: Fix the bug `fn get_image()` where image type is `CF_DIBV5`
in `win11`. [issues#14](https://github.com/ChurchTao/clipboard-rs/issues/14)

## v0.1.4 (2024-03-18) [released]

- Fix: Fix the bug `fn read_files()` where no files in clipboard. [issues#11](https://github.com/ChurchTao/clipboard-rs/issues/11)
- Fix: Fix the bug `fn read_files()` where no files in
clipboard. [issues#11](https://github.com/ChurchTao/clipboard-rs/issues/11)

## v0.1.3 (2024-03-14) [released]

- Fix: Fix the bug on `Windows` can't read DIBV5 format image from clipboard [issues#8](https://github.com/ChurchTao/clipboard-rs/issues/8)
- Fix: Fix the bug on `Windows` can't move `WatcherContext` to another thread [issues#4](https://github.com/ChurchTao/clipboard-rs/issues/4)
- Change: Demo `watch_change.rs` The callback function for monitoring changes in the clipboard is changed to implement a trait. [pr#6](https://github.com/ChurchTao/clipboard-rs/pull/6)
- Fix: Fix the bug on `Windows` can't read DIBV5 format image from
clipboard [issues#8](https://github.com/ChurchTao/clipboard-rs/issues/8)
- Fix: Fix the bug on `Windows` can't move `WatcherContext` to another
thread [issues#4](https://github.com/ChurchTao/clipboard-rs/issues/4)
- Change: Demo `watch_change.rs` The callback function for monitoring changes in the clipboard is changed to implement a
trait. [pr#6](https://github.com/ChurchTao/clipboard-rs/pull/6)

## v0.1.2 (2024-03-08) [released]

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clipboard-rs"
version = "0.1.8"
version = "0.1.9"
authors = ["ChurchTao <[email protected]>"]
description = "Cross-platform clipboard API (text | image | rich text | html | files | monitoring changes) | 跨平台剪贴板 API(文本|图片|富文本|html|文件|监听变化) Windows,MacOS,Linux"
repository = "https://github.com/ChurchTao/clipboard-rs"
Expand All @@ -12,10 +12,10 @@ edition = "2021"
rust-version = "1.63.0"

[dependencies]
image = "0.25"
image = "0.25.2"

[target.'cfg(target_os = "windows")'.dependencies]
clipboard-win = { version = "5.3.1", features = ["monitor"] }
clipboard-win = { version = "5.4.0", features = ["monitor"] }

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.25.0"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub trait Clipboard: Send {

fn set_files(&self, files: Vec<String>) -> Result<()>;

/// set image will clear clipboard
fn set(&self, contents: Vec<ClipboardContent>) -> Result<()>;
}

Expand Down
44 changes: 24 additions & 20 deletions src/platform/win.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use crate::common::{ContentData, Result, RustImage, RustImageData};
use crate::{Clipboard, ClipboardContent, ClipboardHandler, ClipboardWatcher, ContentFormat};
use std::collections::HashMap;
use std::io::Cursor;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;

use clipboard_win::formats::{CF_DIB, CF_DIBV5};
use clipboard_win::raw::set_without_clear;
use clipboard_win::raw::{set_file_list_with, set_string_with, set_without_clear};
use clipboard_win::types::c_uint;
use clipboard_win::{
formats, get, get_clipboard, raw, set_clipboard, Clipboard as ClipboardWin, Monitor, Setter,
formats, get, get_clipboard, options, raw, set_clipboard, Clipboard as ClipboardWin, Monitor,
SysResult,
};
use image::codecs::bmp::BmpDecoder;
use image::{DynamicImage, EncodableLayout};
use std::collections::HashMap;
use std::io::Cursor;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;
use image::DynamicImage;

use crate::common::{ContentData, Result, RustImage, RustImageData};
use crate::{Clipboard, ClipboardContent, ClipboardHandler, ClipboardWatcher, ContentFormat};

pub struct WatcherShutdown {
stop_signal: Sender<()>,
Expand Down Expand Up @@ -355,19 +357,21 @@ impl Clipboard for ClipboardContext {
fn set_files(&self, files: Vec<String>) -> Result<()> {
let _clip = ClipboardWin::new_attempts(10)
.map_err(|code| format!("Open clipboard error, code = {}", code));
let res = formats::FileList.write_clipboard(&files);
let res = set_file_list_with(&files, options::DoClear);
res.map_err(|e| format!("set files error, code = {}", e).into())
}

fn set(&self, contents: Vec<ClipboardContent>) -> Result<()> {
let _clip = ClipboardWin::new_attempts(10)
.map_err(|code| format!("Open clipboard error, code = {}", code));
let res = clipboard_win::empty();
if let Err(e) = res {
return Err(format!("Empty clipboard error, code = {}", e).into());
}
for content in contents {
match content {
ClipboardContent::Text(txt) => {
let format_uint = formats::CF_UNICODETEXT;
let u16_str = utf8_to_utf16(txt.as_str());
let res = set_without_clear(format_uint, u16_str.as_bytes());
let res = set_string_with(txt.as_str(), options::NoClear);
if res.is_err() {
continue;
}
Expand All @@ -394,7 +398,7 @@ impl Clipboard for ClipboardContext {
}
}
ClipboardContent::Files(file_list) => {
let res = formats::FileList.write_clipboard(&file_list);
let res = set_file_list_with(&file_list, options::NoClear);
if res.is_err() {
continue;
}
Expand Down Expand Up @@ -463,11 +467,11 @@ impl Drop for WatcherShutdown {
}

/// 将输入的 UTF-8 字符串转换为宽字符(UTF-16)字符串
fn utf8_to_utf16(input: &str) -> Vec<u16> {
let mut vec: Vec<u16> = input.encode_utf16().collect();
vec.push(0);
vec
}
// fn utf8_to_utf16(input: &str) -> Vec<u16> {
// let mut vec: Vec<u16> = input.encode_utf16().collect();
// vec.push(0);
// vec
// }

// https://learn.microsoft.com/en-us/windows/win32/dataxchg/html-clipboard-format
// The description header includes the clipboard version number and offsets, indicating where the context and the fragment start and end. The description is a list of ASCII text keywords followed by a string and separated by a colon (:).
Expand Down

0 comments on commit 6945393

Please sign in to comment.