[BEEEP] [PM-565] Implement clipboard logic in rust (#4516)
Implement the Desktop clipboard logic natively using rust. This uses the arboard crate for clipboard functionality. This change consists of 3 portions: * Rust component. * Updating renderer to call main using electron ipc. * Update main to listen to renderer ipc and forward calls to the native clipboard module.pull/6373/head
parent
6aed74d241
commit
30feb60645
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,56 @@
|
||||
use anyhow::Result;
|
||||
use arboard::{Clipboard, Set};
|
||||
|
||||
pub fn read() -> Result<String> {
|
||||
let mut clipboard = Clipboard::new()?;
|
||||
|
||||
Ok(clipboard.get_text()?)
|
||||
}
|
||||
|
||||
pub fn write(text: &str, password: bool) -> Result<()> {
|
||||
let mut clipboard = Clipboard::new()?;
|
||||
|
||||
let set = clipboard_set(clipboard.set(), password);
|
||||
|
||||
set.text(text)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Exclude from windows clipboard history
|
||||
#[cfg(target_os = "windows")]
|
||||
fn clipboard_set(set: Set, password: bool) -> Set {
|
||||
use arboard::SetExtWindows;
|
||||
|
||||
if password {
|
||||
set.exclude_from_cloud().exclude_from_history()
|
||||
} else {
|
||||
set
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for clipboard to be available on linux
|
||||
#[cfg(target_os = "linux")]
|
||||
fn clipboard_set(set: Set, _password: bool) -> Set {
|
||||
use arboard::SetExtLinux;
|
||||
|
||||
set.wait()
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn clipboard_set(set: Set, _password: bool) -> Set {
|
||||
set
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "manual_test", not(target_os = "linux")))]
|
||||
fn test_write_read() {
|
||||
let message = "Hello world!";
|
||||
|
||||
write(message, false).unwrap();
|
||||
assert_eq!(message, read().unwrap());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
import { ipcMain } from "electron";
|
||||
|
||||
import { clipboards } from "@bitwarden/desktop-native";
|
||||
|
||||
import { ClipboardWriteMessage } from "../types/clipboard";
|
||||
|
||||
export class ClipboardMain {
|
||||
init() {
|
||||
ipcMain.handle("clipboard.read", async (_event: any, _message: any) => {
|
||||
return await clipboards.read();
|
||||
});
|
||||
|
||||
ipcMain.handle("clipboard.write", async (_event: any, message: ClipboardWriteMessage) => {
|
||||
return await clipboards.write(message.text, message.password ?? false);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
export type ClipboardWriteMessage = {
|
||||
text: string;
|
||||
password?: boolean;
|
||||
};
|
||||
Loading…
Reference in new issue