diff --git a/src/window/events.rs b/src/window/events.rs index fa7b649d6..4effafffc 100644 --- a/src/window/events.rs +++ b/src/window/events.rs @@ -99,11 +99,15 @@ impl Window { camera_2d: &mut dyn Camera2d, event: &WindowEvent, ) { - match *event { - WindowEvent::Key(Key::Escape, Action::Release, _) | WindowEvent::Close => { - self.close(); + if let Some(binding_key) = self.close_key { + if let WindowEvent::Key(key, Action::Release, modifiers) = event { + if binding_key == *key && (Some(*modifiers) == self.close_modifiers || self.close_modifiers.is_none()) { + self.close() + } } - _ => {} + } + if *event == WindowEvent::Close { + self.close(); } // Feed events to egui and check if it wants to capture input diff --git a/src/window/window.rs b/src/window/window.rs index 75fae397d..50db14451 100644 --- a/src/window/window.rs +++ b/src/window/window.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use crate::color::{Color, BLACK}; use crate::context::Context; -use crate::event::WindowEvent; +use crate::event::{Key, Modifiers, WindowEvent}; use crate::renderer::{PointRenderer2d, PointRenderer3d, PolylineRenderer2d, PolylineRenderer3d}; use crate::resource::{ FramebufferManager, MaterialManager2d, MeshManager2d, RenderTarget, Texture, TextureManager, @@ -45,6 +45,8 @@ pub struct Window { pub(super) framebuffer_manager: FramebufferManager, pub(super) post_process_render_target: RenderTarget, pub(super) should_close: bool, + pub(super) close_key: Option, + pub(super) close_modifiers: Option, #[cfg(feature = "egui")] pub(super) egui_context: EguiContext, pub(super) canvas: Canvas, @@ -259,6 +261,28 @@ impl Window { self.ambient_intensity } + /// Rebinds the key to close the window. + /// Set to None to disable. + pub fn rebind_close_key(&mut self, new_close_key: Option) { + self.close_key = new_close_key; + } + + /// Rebinds the modifiers to close the window. + /// Set to None make it work with any modifiers. + pub fn rebind_close_modifiers(&mut self, new_close_modifiers: Option) { + self.close_modifiers = new_close_modifiers; + } + + /// Returns the current key to close the window. + pub fn close_key(&self) -> Option { + self.close_key + } + + /// Returns the current modifiers to close the window. + pub fn close_modifiers(&self) -> Option { + self.close_modifiers + } + /// Creates a new hidden window. /// /// The window is created but not displayed. Use [`show()`](Self::show) to make it visible. @@ -356,6 +380,8 @@ impl Window { let framebuffer_manager = FramebufferManager::new(); let mut usr_window = Window { should_close: false, + close_key: Some(Key::Escape), + close_modifiers: None, canvas, events: Rc::new(event_receive), unhandled_events: Rc::new(RefCell::new(Vec::new())),