1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 18:53:29 +00:00
Files
browser/apps/desktop/desktop_native/src/clipboard.rs
Oscar Hinton 30feb60645 [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.
2023-10-02 18:50:57 +02:00

57 lines
1.1 KiB
Rust

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());
}
}