Update examples to winit 0.29

This commit is contained in:
Ian Douglas Scott 2024-01-16 21:09:18 -08:00
parent d6224e1b03
commit 555678e6ed
4 changed files with 56 additions and 50 deletions

View file

@ -27,7 +27,7 @@ clipboard_wayland = { version = "0.2", path = "./wayland" }
[dev-dependencies] [dev-dependencies]
rand = "0.8" rand = "0.8"
winit = "0.28" winit = { version = "0.29", features = ["rwh_05"] }
[workspace] [workspace]
members = [ members = [

View file

@ -1,8 +1,9 @@
use rand::distributions::{Alphanumeric, Distribution}; use rand::distributions::{Alphanumeric, Distribution};
use window_clipboard::Clipboard; use window_clipboard::Clipboard;
use winit::{ use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
keyboard::Key,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -15,7 +16,7 @@ fn main() {
.map(char::from) .map(char::from)
.collect(); .collect();
let event_loop = EventLoop::new(); let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new() let window = WindowBuilder::new()
.with_title("Press G to start the test!") .with_title("Press G to start the test!")
@ -27,20 +28,21 @@ fn main() {
clipboard.write(data.clone()).unwrap(); clipboard.write(data.clone()).unwrap();
event_loop.run(move |event, _, control_flow| match event { event_loop
.run(move |event, elwt| match event {
Event::WindowEvent { Event::WindowEvent {
event: event:
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: event:
KeyboardInput { KeyEvent {
virtual_keycode: Some(VirtualKeyCode::G), logical_key: Key::Character(c),
state: ElementState::Released, state: ElementState::Released,
.. ..
}, },
.. ..
}, },
.. ..
} => { } if c == "G" => {
let new_data = clipboard.read().expect("Read data"); let new_data = clipboard.read().expect("Read data");
assert_eq!(data, new_data, "Data is equal"); assert_eq!(data, new_data, "Data is equal");
println!("Data copied successfully!"); println!("Data copied successfully!");
@ -48,7 +50,8 @@ fn main() {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
window_id, window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit, } if window_id == window.id() => elwt.exit(),
_ => *control_flow = ControlFlow::Wait, _ => {}
}); })
.unwrap();
} }

View file

@ -1,12 +1,12 @@
use window_clipboard::Clipboard; use window_clipboard::Clipboard;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
fn main() { fn main() {
let event_loop = EventLoop::new(); let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new() let window = WindowBuilder::new()
.with_title("A fantastic window!") .with_title("A fantastic window!")
@ -15,14 +15,16 @@ fn main() {
let clipboard = Clipboard::connect(&window).expect("Connect to clipboard"); let clipboard = Clipboard::connect(&window).expect("Connect to clipboard");
event_loop.run(move |event, _, control_flow| match event { event_loop
Event::MainEventsCleared => { .run(move |event, elwt| match event {
Event::AboutToWait => {
println!("{:?}", clipboard.read()); println!("{:?}", clipboard.read());
} }
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
window_id, window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit, } if window_id == window.id() => elwt.exit(),
_ => *control_flow = ControlFlow::Wait, _ => {}
}); })
.unwrap();
} }

View file

@ -1,12 +1,12 @@
use window_clipboard::Clipboard; use window_clipboard::Clipboard;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
fn main() { fn main() {
let event_loop = EventLoop::new(); let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new() let window = WindowBuilder::new()
.with_title("A fantastic window!") .with_title("A fantastic window!")
@ -20,12 +20,13 @@ fn main() {
.write(String::from("Hello, world!")) .write(String::from("Hello, world!"))
.expect("Write to clipboard"); .expect("Write to clipboard");
event_loop.run(move |event, _, control_flow| match event { event_loop
Event::MainEventsCleared => {} .run(move |event, elwt| match event {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
window_id, window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit, } if window_id == window.id() => elwt.exit(),
_ => *control_flow = ControlFlow::Wait, _ => {}
}); })
.unwrap();
} }