diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 3b36150e47a8..8e23bec2fa3c 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -2397,6 +2397,12 @@ impl Context { crate::gui_zoom::zoom_with_keyboard(self); } + for shortcut in self.options(|o| o.quit_shortcuts.clone()) { + if self.input_mut(|i| i.consume_shortcut(&shortcut)) { + self.send_viewport_cmd(ViewportCommand::Close); + } + } + #[cfg(debug_assertions)] self.debug_painting(); diff --git a/crates/egui/src/memory/mod.rs b/crates/egui/src/memory/mod.rs index 51ab2cde4fc4..08b08a462b9c 100644 --- a/crates/egui/src/memory/mod.rs +++ b/crates/egui/src/memory/mod.rs @@ -234,6 +234,16 @@ pub struct Options { #[cfg_attr(feature = "serde", serde(skip))] pub zoom_with_keyboard: bool, + /// Keyboard shortcuts to close the application. + /// + /// Pressing any of these will send [`crate::ViewportCommand::Close`] + /// to the root viewport. + /// + /// Defaults to `Cmd-Q` (which is Ctrl-Q on Linux/Windows, Cmd-Q on Mac). + /// Set to empty to disable. + #[cfg_attr(feature = "serde", serde(skip))] + pub quit_shortcuts: Vec, + /// Controls the tessellator. pub tessellation_options: epaint::TessellationOptions, @@ -304,6 +314,10 @@ impl Default for Options { system_theme: None, zoom_factor: 1.0, zoom_with_keyboard: true, + quit_shortcuts: vec![crate::KeyboardShortcut::new( + crate::Modifiers::COMMAND, + crate::Key::Q, + )], tessellation_options: Default::default(), repaint_on_widget_change: false, @@ -363,6 +377,7 @@ impl Options { system_theme: _, zoom_factor, zoom_with_keyboard, + quit_shortcuts: _, // not shown in ui tessellation_options, repaint_on_widget_change, max_passes,