egui/egui-winit/src/clipboard.rs
Emil Ernerfeldt c6ac1827f6
Use tracing crate for logging (#1192)
* egui_web: use tracing crate
* egui_glow: use tracing crate
* Log at the debug level
* egui_demo_app: enable tracing to log to stdout
* Use tracing in egui-winit
* Add opt-in tracing support to egui
2022-02-01 12:27:39 +01:00

69 lines
1.9 KiB
Rust

/// Handles interfacing either with the OS clipboard.
/// If the "clipboard" feature is off it will instead simulate the clipboard locally.
pub struct Clipboard {
#[cfg(feature = "copypasta")]
copypasta: Option<copypasta::ClipboardContext>,
/// Fallback manual clipboard.
#[cfg(not(feature = "copypasta"))]
clipboard: String,
}
impl Default for Clipboard {
fn default() -> Self {
Self {
#[cfg(feature = "copypasta")]
copypasta: init_copypasta(),
#[cfg(not(feature = "copypasta"))]
clipboard: String::default(),
}
}
}
impl Clipboard {
pub fn get(&mut self) -> Option<String> {
#[cfg(feature = "copypasta")]
if let Some(clipboard) = &mut self.copypasta {
use copypasta::ClipboardProvider as _;
match clipboard.get_contents() {
Ok(contents) => Some(contents),
Err(err) => {
tracing::error!("Paste error: {}", err);
None
}
}
} else {
None
}
#[cfg(not(feature = "copypasta"))]
Some(self.clipboard.clone())
}
pub fn set(&mut self, text: String) {
#[cfg(feature = "copypasta")]
if let Some(clipboard) = &mut self.copypasta {
use copypasta::ClipboardProvider as _;
if let Err(err) = clipboard.set_contents(text) {
tracing::error!("Copy/Cut error: {}", err);
}
}
#[cfg(not(feature = "copypasta"))]
{
self.clipboard = text;
}
}
}
#[cfg(feature = "copypasta")]
fn init_copypasta() -> Option<copypasta::ClipboardContext> {
match copypasta::ClipboardContext::new() {
Ok(clipboard) => Some(clipboard),
Err(err) => {
tracing::error!("Failed to initialize clipboard: {}", err);
None
}
}
}