mirror of
https://github.com/icewind1991/clipboard-sync
synced 2026-06-03 18:34:07 +02:00
dont make a clipboard context for every incoming message
This commit is contained in:
parent
e84fe3467b
commit
2fa67d45f0
4 changed files with 8 additions and 4 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -881,6 +881,7 @@ dependencies = [
|
||||||
"mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
"mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
||||||
|
|
@ -18,5 +18,5 @@ serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
clipboard = "0.5"
|
clipboard = "0.5"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
ws = {version = "0.8", features = ["native-tls"]}
|
ws = {version = "0.8", features = ["ssl", "native-tls"]}
|
||||||
err-derive = "0.1"
|
err-derive = "0.1"
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::common::ClipboardCommand;
|
use crate::common::ClipboardCommand;
|
||||||
use clipboard::{ClipboardContext, ClipboardProvider};
|
use clipboard::{ClipboardContext, ClipboardProvider};
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
@ -14,7 +15,7 @@ fn handle_command(
|
||||||
ctx: &mut ClipboardContext,
|
ctx: &mut ClipboardContext,
|
||||||
current_clipboard: Arc<Mutex<String>>,
|
current_clipboard: Arc<Mutex<String>>,
|
||||||
) {
|
) {
|
||||||
if let ClipboardCommand::Set { value, session: _ } = command {
|
if let ClipboardCommand::Set { value, .. } = command {
|
||||||
let mut clip = current_clipboard.lock().unwrap();
|
let mut clip = current_clipboard.lock().unwrap();
|
||||||
if *clip != value {
|
if *clip != value {
|
||||||
let _ = ctx.set_contents(value.clone());
|
let _ = ctx.set_contents(value.clone());
|
||||||
|
|
@ -41,10 +42,11 @@ fn main() {
|
||||||
let current_clipboard = Arc::new(Mutex::new(String::new()));
|
let current_clipboard = Arc::new(Mutex::new(String::new()));
|
||||||
clipboard_thread(session.clone(), out, current_clipboard.clone());
|
clipboard_thread(session.clone(), out, current_clipboard.clone());
|
||||||
|
|
||||||
|
let ctx: RefCell<ClipboardContext> = RefCell::new(ClipboardProvider::new().unwrap());
|
||||||
|
|
||||||
move |msg: Message| {
|
move |msg: Message| {
|
||||||
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
|
|
||||||
if let Ok(command) = ClipboardCommand::try_from(msg) {
|
if let Ok(command) = ClipboardCommand::try_from(msg) {
|
||||||
handle_command(command, &mut ctx, current_clipboard.clone());
|
handle_command(command, &mut ctx.borrow_mut(), current_clipboard.clone());
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use std::convert::TryFrom;
|
||||||
use ws::{Error as WsError, ErrorKind, Message};
|
use ws::{Error as WsError, ErrorKind, Message};
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
#[serde(rename_all = "lowercase")]
|
#[serde(rename_all = "lowercase")]
|
||||||
pub enum ClipboardCommand {
|
pub enum ClipboardCommand {
|
||||||
Listen { session: String },
|
Listen { session: String },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue