-
Notifications
You must be signed in to change notification settings - Fork 13
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
关于ClipboardWatcherContext.start_watch()
方法的一点疑问。
#4
Comments
@HaroldLoui |
@HaroldLoui |
我那个代码是在win11下跑的,确实无法正常运行。后来改成使用 |
另外这段代码在我的ArchLinux上也可以跑得通。 |
@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();
} |
可以,不过周末只能在我个人的ArchLinux上跑了,win11环境得等到周一我去公司的电脑上跑才行,那上面才有rust环境。 |
这个分支我跑了,我的Arch上没有问题,win11环境得等我周一回公司后才能测试了。另外我对这个新Api的 |
已在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();
} 您给的代码可以跑通,但将 |
没错,我重现了你的问题,我暂时还么找到原因,不过下个版本中我会修复它,届时我会通知你 |
@HaroldLoui |
@HaroldLoui 新版本已经发了,我暂时先关闭这个 issue了,如果还有问题,可以重新打开😄 |
作者你好,由于
start_watch()
会阻塞当前线程,因此我将它放进新的线程中执行,结果无法监听到剪贴板的内容了,且同时无法shutdown
。以下是我的代码:还请作者帮忙看下是不是我的写法有问题,我又应该怎么去声明一个全局的
watcher
让它能够在任何地方都能够监听到剪贴板的内容和随时进行shutdown
操作。The text was updated successfully, but these errors were encountered: