rework sql
This commit is contained in:
68
rklipd/src/clipboard.rs
Normal file
68
rklipd/src/clipboard.rs
Normal file
@ -0,0 +1,68 @@
|
||||
use arboard::{Clipboard, ImageData};
|
||||
use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder};
|
||||
use std::error::Error;
|
||||
use std::time::SystemTime;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::{ClipboardData, ClipboardEntry, Image};
|
||||
|
||||
use clipboard_master::{CallbackResult, ClipboardHandler};
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
pub struct Handler {
|
||||
pub clipboard_tx: Sender<()>,
|
||||
}
|
||||
|
||||
impl ClipboardHandler for Handler {
|
||||
fn on_clipboard_change(&mut self) -> CallbackResult {
|
||||
if let Err(e) = self.clipboard_tx.send(()) {
|
||||
eprintln!("{}", e);
|
||||
}
|
||||
CallbackResult::Next
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ImageDataExt {
|
||||
fn to_png(&self) -> Result<Vec<u8>, Box<dyn Error>>;
|
||||
}
|
||||
|
||||
impl ImageDataExt for ImageData<'_> {
|
||||
fn to_png(&self) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
let mut buffer = Vec::new();
|
||||
let encoder = PngEncoder::new(&mut buffer);
|
||||
encoder.write_image(
|
||||
&self.bytes,
|
||||
self.width as u32,
|
||||
self.height as u32,
|
||||
ExtendedColorType::Rgba8,
|
||||
)?;
|
||||
Ok(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
impl ClipboardEntry {
|
||||
pub fn new(clipboard: &mut Clipboard) -> Result<ClipboardEntry, Box<dyn Error>> {
|
||||
let clipboard_data_opt: Option<ClipboardData> = match clipboard.get_text() {
|
||||
Ok(text) => Some(ClipboardData::Text(text)),
|
||||
Err(_) => match clipboard.get_image() {
|
||||
Ok(image) => {
|
||||
let id = Uuid::new_v4();
|
||||
Some(ClipboardData::Image(Image {
|
||||
bytes: Some(image.to_png()?),
|
||||
id,
|
||||
}))
|
||||
}
|
||||
Err(_) => None,
|
||||
},
|
||||
};
|
||||
|
||||
let Some(clipboard_data) = clipboard_data_opt else {
|
||||
return Err("Clipboard empty".into());
|
||||
};
|
||||
|
||||
Ok(ClipboardEntry {
|
||||
content: clipboard_data,
|
||||
timestamp: SystemTime::now(),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user