window_clipboard/examples/big_file.rs
Ian Douglas Scott 869737dadc Use HasDisplayHandle instead of deprecated HasRawDisplayHandle
Currently `connect` is unsafe because it doesn't take ownership of the
display or have a lifetime bound.
2024-01-16 21:37:13 -08:00

57 lines
1.7 KiB
Rust

use rand::distributions::{Alphanumeric, Distribution};
use window_clipboard::Clipboard;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
keyboard::Key,
window::WindowBuilder,
};
fn main() {
let mut rng = rand::thread_rng();
let data: String = Alphanumeric
.sample_iter(&mut rng)
.take(10_000_000)
.map(char::from)
.collect();
let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
.with_title("Press G to start the test!")
.build(&event_loop)
.unwrap();
let mut clipboard =
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");
clipboard.write(data.clone()).unwrap();
event_loop
.run(move |event, elwt| match event {
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Character(c),
state: ElementState::Released,
..
},
..
},
..
} if c == "G" => {
let new_data = clipboard.read().expect("Read data");
assert_eq!(data, new_data, "Data is equal");
println!("Data copied successfully!");
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => elwt.exit(),
_ => {}
})
.unwrap();
}