Skip to content

Commit

Permalink
examples: Exit when the user presses Escape
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Jan 4, 2024
1 parent a2b40f2 commit 98b4cd4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions glutin_examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::num::NonZeroU32;
use std::ops::Deref;

use raw_window_handle::HasRawWindowHandle;
use winit::event::{Event, WindowEvent};
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowBuilder;

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

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

let window = window.take().unwrap_or_else(|| {
let window_builder = WindowBuilder::new().with_transparent(true);
let window_builder = WindowBuilder::new()
.with_transparent(true)
.with_title("Glutin triangle gradient example (press Escape to exit)");
glutin_winit::finalize_window(window_target, window_builder, &gl_config)
.unwrap()
});
Expand Down Expand Up @@ -165,7 +175,11 @@ pub fn main(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), Box<dyn
}
}
},
WindowEvent::CloseRequested => window_target.exit(),
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event: KeyEvent { logical_key: Key::Named(NamedKey::Escape), .. },
..
} => window_target.exit(),
_ => (),
},
Event::AboutToWait => {
Expand Down

0 comments on commit 98b4cd4

Please sign in to comment.