Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: Exit when the user presses Escape #1657

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 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,11 @@ 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 = cfg!(wgl_backend).then(|| {
WindowBuilder::new()
.with_transparent(true)
.with_title("Glutin triangle gradient example (press Escape to exit)")
});

// The template will match only the configurations supporting rendering
// to windows.
Expand Down Expand Up @@ -107,7 +111,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 +171,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
Loading