Comments (11)
没错,我重现了你的问题,我暂时还么找到原因,不过下个版本中我会修复它,届时我会通知你
from clipboard-rs.
@HaroldLoui
请问你的操作系统环境,我使用你的代码运行是没问题的。我是 arm64 的 MacOS
from clipboard-rs.
@HaroldLoui
由于你的疑问,我改版了 watcher 的用法,使其更加易于与其他类型结合起来。你可以看这个 pr,有疑问敬请提出,我会在下个版本使用这个PR #6
from clipboard-rs.
@HaroldLoui 请问你的操作系统环境,我使用你的代码运行是没问题的。我是 arm64 的 MacOS
我那个代码是在win11下跑的,确实无法正常运行。后来改成使用mpsc::channel()
将shutdown
发送给结束线程后,倒是可以正常运行了。
from clipboard-rs.
另外这段代码在我的ArchLinux上也可以跑得通。
from clipboard-rs.
@HaroldLoui 请问你的操作系统环境,我使用你的代码运行是没问题的。我是 arm64 的 MacOS
我那个代码是在win11下跑的,确实无法正常运行。后来改成使用
mpsc::channel()
将shutdown
发送给结束线程后,倒是可以正常运行了。
@HaroldLoui 可以麻烦你用这个分支跑一下 watcher 的 demo 吗? dev-watcher-api-change 我从你是使用场景上,发现了我原本的 callback 写法局限性很大,从而做的一次改版,你看下这种 API 方式能不能满足你的需求
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 manager = Manager::new();
let mut watcher = ClipboardWatcherContext::new().unwrap();
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();
});
println!("start watch!");
watcher.start_watch();
}
from clipboard-rs.
可以,不过周末只能在我个人的ArchLinux上跑了,win11环境得等到周一我去公司的电脑上跑才行,那上面才有rust环境。
from clipboard-rs.
这个分支我跑了,我的Arch上没有问题,win11环境得等我周一回公司后才能测试了。另外我对这个新Api的ClipboardHandler
Trait有个个人的看法,应该再增加两个不需要重写的方法start
和stop
来开启和停止监听剪贴板,使用者只需要重写on_clipboard_change
方法,对剪贴板的内容进行定制化的操作(如保存到数据库或在Tauri中发送事件给前端,亦或进行简单的打印等等)。
from clipboard-rs.
已在windows上测试,结果还是不行:
[dependencies]
clipboard-rs = {git = "https://github.com/ChurchTao/clipboard-rs", branch = "dev-watcher-api-change"}
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 manager = Manager::new();
let mut watcher = ClipboardWatcherContext::new().unwrap();
let watcher_shutdown = watcher.add_handler(manager).get_shutdown_channel();
thread::spawn(move || {
thread::sleep(Duration::from_secs(10));
println!("stop watch!");
watcher_shutdown.stop();
});
let start = thread::spawn(move || {
println!("start watch!");
watcher.start_watch();
});
start.join().unwrap();
}
您给的代码可以跑通,但将watcher
丢入新线程后便无法停止监听,也无法监听到剪贴板的内容。
from clipboard-rs.
@HaroldLoui
好消息,我已经解决了你描述的问题,问题出在 Windows 的监听器对象在 rust 中,不是线程安全的,我做了相应的优化,改成了非阻塞的方案,目前按照你的代码应该可以运行了,可以请你在这个分支中测试一下你的代码吗?https://github.com/ChurchTao/clipboard-rs/tree/dev-watcher-api-change
from clipboard-rs.
@HaroldLoui 新版本已经发了,我暂时先关闭这个 issue了,如果还有问题,可以重新打开😄
from clipboard-rs.
Related Issues (14)
- Add support for wayland HOT 4
- Abnormal exit HOT 4
- Does the get_image Method Support Windows? HOT 12
- `get_files` Segmentation Fault when there is no files on clipboard HOT 1
- ClipboardContext 能否实现 Clone / Copy ? HOT 5
- Error: `The image format could not be determined` on Win11 HOT 2
- Bug: Cannot paste after writing image to clipboard (on Windows) HOT 2
- Bug: No transparent background for clipboard image read on Windows HOT 1
- Bug: Cannot read clipboard image on MacOS for screenshots taken by certain apps HOT 3
- `set_html()` doesn't update text. HOT 1
- 当写入html时,在Windows上长度似乎是有限制的,并且会被自动截断。截断的长度不固定 HOT 1
- 如何计算图像的哈希值? HOT 6
- 如何获取复制内容的 app 来源 HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clipboard-rs.