Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions src/window/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion src/window/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Key>,
pub(super) close_modifiers: Option<Modifiers>,
#[cfg(feature = "egui")]
pub(super) egui_context: EguiContext,
pub(super) canvas: Canvas,
Expand Down Expand Up @@ -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<Key>) {
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<Modifiers>) {
self.close_modifiers = new_close_modifiers;
}

/// Returns the current key to close the window.
pub fn close_key(&self) -> Option<Key> {
self.close_key
}

/// Returns the current modifiers to close the window.
pub fn close_modifiers(&self) -> Option<Modifiers> {
self.close_modifiers
}

/// Creates a new hidden window.
///
/// The window is created but not displayed. Use [`show()`](Self::show) to make it visible.
Expand Down Expand Up @@ -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())),
Expand Down