Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New watcher API 新的监视器 API,更加人性化 😄 #6

Merged
merged 10 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ name: Test

on:
push:
branches: [master]
paths-ignore:
- "README_ZH.md"
- "README.md"
branches:
- master
paths:
- '.github/workflows/test.yml'
- 'src/**/*.rs'
- 'tests/**/*.rs'
- 'Cargo.toml'
pull_request:
branches: [master]
paths-ignore:
- "README_ZH.md"
- "README.md"
types: [opened, synchronize, reopened, ready_for_review]
branches:
- '**'
paths:
- '.github/workflows/test.yml'
- 'src/**/*.rs'
- 'tests/**/*.rs'
- 'Cargo.toml'

jobs:
rustfmt:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 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)

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

- Change `rust-version = "1.75.0"` to `rust-version = "1.63.0"` [pr#3](https://github.com/ChurchTao/clipboard-rs/pull/3)
Expand Down
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clipboard-rs"
version = "0.1.2"
version = "0.1.3"
authors = ["ChurchTao <swkzymlyy@gmail.com>"]
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,11 +12,10 @@ edition = "2021"
rust-version = "1.63.0"

[dependencies]
image = "0.24"
image = "0.25"

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

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.25.0"
Expand Down
115 changes: 78 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,44 @@ Add the following content to your `Cargo.toml`:

```toml
[dependencies]
clipboard-rs = "0.1.2"
clipboard-rs = "0.1.3"
```

## [CHANGELOG](CHANGELOG.md)

## Examples

### All Usage Examples

[Examples](examples)

### Simple Read and Write

```rust
use clipboard_rs::{Clipboard, ClipboardContext};
use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat};

fn main() {
let ctx = ClipboardContext::new().unwrap();
let types = ctx.available_formats().unwrap();
println!("{:?}", types);
let ctx = ClipboardContext::new().unwrap();
let types = ctx.available_formats().unwrap();
println!("{:?}", types);

let rtf = ctx.get_rich_text().unwrap();
let has_rtf = ctx.has(ContentFormat::Rtf);
println!("has_rtf={}", has_rtf);

println!("rtf={}", rtf);
let rtf = ctx.get_rich_text().unwrap_or("".to_string());

let html = ctx.get_html().unwrap();
println!("rtf={}", rtf);

println!("html={}", html);
let has_html = ctx.has(ContentFormat::Html);
println!("has_html={}", has_html);

let content = ctx.get_text().unwrap();
let html = ctx.get_html().unwrap_or("".to_string());

println!("txt={}", content);
println!("html={}", html);

let content = ctx.get_text().unwrap_or("".to_string());

println!("txt={}", content);
}

```
Expand All @@ -62,18 +72,30 @@ fn main() {
```rust
use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};

fn main() {
let ctx = ClipboardContext::new().unwrap();
let types = ctx.available_formats().unwrap();
println!("{:?}", types);

let img = ctx.get_image().unwrap();

img.save_to_path("/tmp/test.png").unwrap();

let resize_img = img.thumbnail(300, 300).unwrap();
const TMP_PATH: &str = "/tmp/";

resize_img.save_to_path("/tmp/test_thumbnail.png").unwrap();
fn main() {
let ctx = ClipboardContext::new().unwrap();
let types = ctx.available_formats().unwrap();
println!("{:?}", types);

let img = ctx.get_image();

match img {
Ok(img) => {
img.save_to_path(format!("{}test.png", TMP_PATH).as_str())
.unwrap();

let resize_img = img.thumbnail(300, 300).unwrap();

resize_img
.save_to_path(format!("{}test_thumbnail.png", TMP_PATH).as_str())
.unwrap();
}
Err(err) => {
println!("err={}", err);
}
}
}

```
Expand All @@ -100,30 +122,49 @@ fn main() {
### Listening to Clipboard Changes

```rust
use clipboard_rs::{Clipboard, ClipboardContext, ClipboardWatcher, ClipboardWatcherContext};
use clipboard_rs::{
Clipboard, ClipboardContext, ClipboardHandler, ClipboardWatcher, ClipboardWatcherContext,
};
use std::{thread, time::Duration};

struct Manager {
ctx: ClipboardContext,
}

impl Manager {
pub fn new() -> Self {
let ctx = ClipboardContext::new().unwrap();
Manager { ctx }
}
}

impl ClipboardHandler for Manager {
fn on_clipboard_change(&mut self) {
println!(
"on_clipboard_change, txt = {}",
self.ctx.get_text().unwrap()
);
}
}

fn main() {
let ctx = ClipboardContext::new().unwrap();
let mut watcher = ClipboardWatcherContext::new().unwrap();
let manager = Manager::new();

watcher.add_handler(Box::new(move || {
let content = ctx.get_text().unwrap();
println!("read:{}", content);
}));
let mut watcher = ClipboardWatcherContext::new().unwrap();

let watcher_shutdown = watcher.get_shutdown_channel();
let watcher_shutdown = watcher.add_handler(manager).get_shutdown_channel();

thread::spawn(move || {
thread::sleep(Duration::from_secs(5));
println!("stop watch!");
watcher_shutdown.stop();
});
thread::spawn(move || {
thread::sleep(Duration::from_secs(5));
println!("stop watch!");
watcher_shutdown.stop();
});

println!("start watch!");
watcher.start_watch();
println!("start watch!");
watcher.start_watch();
}


```

## Contributing
Expand Down
113 changes: 77 additions & 36 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,37 @@ clipboard-rs = "0.1.2"

## 示例

### 所有使用示例

[Examples](examples)

### 简单读写

```rust
use clipboard_rs::{Clipboard, ClipboardContext};
use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat};

fn main() {
let ctx = ClipboardContext::new().unwrap();
let types = ctx.available_formats().unwrap();
println!("{:?}", types);
let ctx = ClipboardContext::new().unwrap();
let types = ctx.available_formats().unwrap();
println!("{:?}", types);

let rtf = ctx.get_rich_text().unwrap();
let has_rtf = ctx.has(ContentFormat::Rtf);
println!("has_rtf={}", has_rtf);

println!("rtf={}", rtf);
let rtf = ctx.get_rich_text().unwrap_or("".to_string());

let html = ctx.get_html().unwrap();
println!("rtf={}", rtf);

println!("html={}", html);
let has_html = ctx.has(ContentFormat::Html);
println!("has_html={}", has_html);

let content = ctx.get_text().unwrap();
let html = ctx.get_html().unwrap_or("".to_string());

println!("txt={}", content);
println!("html={}", html);

let content = ctx.get_text().unwrap_or("".to_string());

println!("txt={}", content);
}

```
Expand All @@ -60,18 +70,30 @@ fn main() {
```rust
use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};

fn main() {
let ctx = ClipboardContext::new().unwrap();
let types = ctx.available_formats().unwrap();
println!("{:?}", types);

let img = ctx.get_image().unwrap();

img.save_to_path("/tmp/test.png").unwrap();

let resize_img = img.thumbnail(300, 300).unwrap();
const TMP_PATH: &str = "/tmp/";

resize_img.save_to_path("/tmp/test_thumbnail.png").unwrap();
fn main() {
let ctx = ClipboardContext::new().unwrap();
let types = ctx.available_formats().unwrap();
println!("{:?}", types);

let img = ctx.get_image();

match img {
Ok(img) => {
img.save_to_path(format!("{}test.png", TMP_PATH).as_str())
.unwrap();

let resize_img = img.thumbnail(300, 300).unwrap();

resize_img
.save_to_path(format!("{}test_thumbnail.png", TMP_PATH).as_str())
.unwrap();
}
Err(err) => {
println!("err={}", err);
}
}
}


Expand Down Expand Up @@ -99,30 +121,49 @@ fn main() {
### 监听剪贴板变化

```rust
use clipboard_rs::{Clipboard, ClipboardContext, ClipboardWatcher, ClipboardWatcherContext};
use clipboard_rs::{
Clipboard, ClipboardContext, ClipboardHandler, ClipboardWatcher, ClipboardWatcherContext,
};
use std::{thread, time::Duration};

struct Manager {
ctx: ClipboardContext,
}

impl Manager {
pub fn new() -> Self {
let ctx = ClipboardContext::new().unwrap();
Manager { ctx }
}
}

impl ClipboardHandler for Manager {
fn on_clipboard_change(&mut self) {
println!(
"on_clipboard_change, txt = {}",
self.ctx.get_text().unwrap()
);
}
}

fn main() {
let ctx = ClipboardContext::new().unwrap();
let mut watcher = ClipboardWatcherContext::new().unwrap();
let manager = Manager::new();

watcher.add_handler(Box::new(move || {
let content = ctx.get_text().unwrap();
println!("read:{}", content);
}));
let mut watcher = ClipboardWatcherContext::new().unwrap();

let watcher_shutdown = watcher.get_shutdown_channel();
let watcher_shutdown = watcher.add_handler(manager).get_shutdown_channel();

thread::spawn(move || {
thread::sleep(Duration::from_secs(5));
println!("stop watch!");
watcher_shutdown.stop();
});
thread::spawn(move || {
thread::sleep(Duration::from_secs(5));
println!("stop watch!");
watcher_shutdown.stop();
});

println!("start watch!");
watcher.start_watch();
println!("start watch!");
watcher.start_watch();
}


```

## 贡献
Expand Down
2 changes: 1 addition & 1 deletion examples/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn main() {
let has = ctx.has(ContentFormat::Files);
println!("has_files={}", has);

let files = ctx.get_files().unwrap();
let files = ctx.get_files().unwrap_or(vec![]);
println!("{:?}", files);
}
Loading