Skip to content

Commit 26b883b

Browse files
MarijnS95kchibisov
authored andcommitted
examples: exit when the user presses Escape
1 parent 81ea39a commit 26b883b

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

glutin_examples/src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::num::NonZeroU32;
44
use std::ops::Deref;
55

66
use raw_window_handle::HasRawWindowHandle;
7-
use winit::event::{Event, WindowEvent};
7+
use winit::event::{Event, KeyEvent, WindowEvent};
8+
use winit::keyboard::{Key, NamedKey};
89
use winit::window::WindowBuilder;
910

1011
use glutin::config::ConfigTemplateBuilder;
@@ -28,8 +29,11 @@ pub fn main(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), Box<dyn
2829
//
2930
// XXX if you don't care about running on Android or so you can safely remove
3031
// this condition and always pass the window builder.
31-
let window_builder =
32-
if cfg!(wgl_backend) { Some(WindowBuilder::new().with_transparent(true)) } else { None };
32+
let window_builder = cfg!(wgl_backend).then(|| {
33+
WindowBuilder::new()
34+
.with_transparent(true)
35+
.with_title("Glutin triangle gradient example (press Escape to exit)")
36+
});
3337

3438
// The template will match only the configurations supporting rendering
3539
// to windows.
@@ -107,7 +111,9 @@ pub fn main(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), Box<dyn
107111
println!("Android window available");
108112

109113
let window = window.take().unwrap_or_else(|| {
110-
let window_builder = WindowBuilder::new().with_transparent(true);
114+
let window_builder = WindowBuilder::new()
115+
.with_transparent(true)
116+
.with_title("Glutin triangle gradient example (press Escape to exit)");
111117
glutin_winit::finalize_window(window_target, window_builder, &gl_config)
112118
.unwrap()
113119
});
@@ -165,7 +171,11 @@ pub fn main(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), Box<dyn
165171
}
166172
}
167173
},
168-
WindowEvent::CloseRequested => window_target.exit(),
174+
WindowEvent::CloseRequested
175+
| WindowEvent::KeyboardInput {
176+
event: KeyEvent { logical_key: Key::Named(NamedKey::Escape), .. },
177+
..
178+
} => window_target.exit(),
169179
_ => (),
170180
},
171181
Event::AboutToWait => {

0 commit comments

Comments
 (0)