diff --git a/src/lib.rs b/src/lib.rs index bbab0b96..272b617d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -696,6 +696,10 @@ impl EventHandler for Stage { } }); + if get_context().quit_requested { + miniquad::window::quit(); + } + if result == false { if let Some(recovery_future) = get_context().recovery_future.take() { unsafe { diff --git a/src/ui.rs b/src/ui.rs index da7bfaf2..e2bd9146 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -70,7 +70,7 @@ use crate::{ Font, }, texture::Image, - ui::{canvas::DrawCanvas, render::Painter}, + ui::{canvas::DrawCanvas, render::Painter}, get_context, }; use std::{cell::RefCell, collections::HashMap, rc::Rc}; @@ -614,17 +614,19 @@ impl InputHandler for Ui { } } - fn char_event(&mut self, character: char, shift: bool, ctrl: bool) { + fn char_event(&mut self, character: char, shift: bool, ctrl: bool, cmd: bool) { self.input.modifier_ctrl = ctrl; self.input.input_buffer.push(input::InputCharacter { key: input::Key::Char(character), modifier_shift: shift, modifier_ctrl: ctrl, + modifier_cmd: cmd, }); } - fn key_down(&mut self, key: KeyCode, shift: bool, ctrl: bool) { + fn key_down(&mut self, key: KeyCode, shift: bool, ctrl: bool, cmd: bool) { self.input.modifier_ctrl = ctrl; + self.input.modifier_cmd = cmd; if key == KeyCode::Escape { self.input.escape = true; @@ -633,6 +635,9 @@ impl InputHandler for Ui { self.input.enter = true; } + if cmd && key == KeyCode::Q { + self.input.quit = true; + } if ctrl && (key == KeyCode::C || key == KeyCode::X) { self.clipboard.set(&self.clipboard_selection); } @@ -642,6 +647,7 @@ impl InputHandler for Ui { key: input::Key::KeyCode(key), modifier_shift: shift, modifier_ctrl: ctrl, + modifier_cmd: cmd, }); } } @@ -1191,7 +1197,7 @@ impl Ui { } pub(crate) mod ui_context { - use crate::prelude::*; + use crate::{prelude::*, get_context}; use crate::window::miniquad::*; use crate::ui as megaui; @@ -1237,17 +1243,18 @@ pub(crate) mod ui_context { let shift = is_key_down(KeyCode::LeftShift) || is_key_down(KeyCode::RightShift); let ctrl = is_key_down(KeyCode::LeftControl) || is_key_down(KeyCode::RightControl); + let cmd = is_key_down(KeyCode::LeftSuper) || is_key_down(KeyCode::RightSuper); while let Some(c) = get_char_pressed_ui() { - if ctrl == false { - ui.char_event(c, false, false); + if (ctrl == false) || (cmd == false) { + ui.char_event(c, false, false, false); } } macro_rules! process { ($code:tt) => { if is_key_pressed(KeyCode::$code) || is_key_down(KeyCode::$code) { - ui.key_down(megaui::KeyCode::$code, shift, ctrl); + ui.key_down(megaui::KeyCode::$code, shift, ctrl, cmd); } }; } @@ -1267,6 +1274,7 @@ pub(crate) mod ui_context { process!(X); process!(V); process!(A); + process!(Q); process!(Escape); process!(Enter); @@ -1275,7 +1283,10 @@ pub(crate) mod ui_context { || is_key_pressed(KeyCode::LeftControl) || is_key_pressed(KeyCode::RightControl) { - ui.key_down(megaui::KeyCode::Control, shift, ctrl); + ui.key_down(megaui::KeyCode::Control, shift, ctrl, cmd); + } + if ui.input.quit { + get_context().quit_requested = true; } let (wheel_x, wheel_y) = mouse_wheel(); ui.mouse_wheel(wheel_x, -wheel_y); diff --git a/src/ui/input.rs b/src/ui/input.rs index 6919165e..00781fdf 100644 --- a/src/ui/input.rs +++ b/src/ui/input.rs @@ -13,6 +13,7 @@ pub struct InputCharacter { pub key: Key, pub modifier_shift: bool, pub modifier_ctrl: bool, + pub modifier_cmd: bool, } #[derive(Default, Clone)] @@ -24,7 +25,9 @@ pub struct Input { pub(crate) mouse_wheel: Vec2, pub(crate) input_buffer: Vec, pub(crate) modifier_ctrl: bool, + pub(crate) modifier_cmd: bool, pub(crate) escape: bool, + pub(crate) quit: bool, pub(crate) enter: bool, pub(crate) cursor_grabbed: bool, pub(crate) window_active: bool, diff --git a/src/ui/input_handler.rs b/src/ui/input_handler.rs index 44e44fec..a1dc5d6c 100644 --- a/src/ui/input_handler.rs +++ b/src/ui/input_handler.rs @@ -18,6 +18,7 @@ pub enum KeyCode { C, // copy V, // paste X, // cut + Q, // quit } pub trait InputHandler { @@ -25,6 +26,6 @@ pub trait InputHandler { fn mouse_up(&mut self, _: (f32, f32)); fn mouse_wheel(&mut self, x: f32, y: f32); fn mouse_move(&mut self, position: (f32, f32)); - fn char_event(&mut self, character: char, shift: bool, ctrl: bool); - fn key_down(&mut self, key_down: KeyCode, shift: bool, ctrl: bool); + fn char_event(&mut self, character: char, shift: bool, ctrl: bool, cmd: bool); + fn key_down(&mut self, key_down: KeyCode, shift: bool, ctrl: bool, cmd: bool); } diff --git a/src/ui/widgets/editbox.rs b/src/ui/widgets/editbox.rs index 36b4bc61..aabf4d28 100644 --- a/src/ui/widgets/editbox.rs +++ b/src/ui/widgets/editbox.rs @@ -183,6 +183,7 @@ impl<'a> Editbox<'a> { key: Key::KeyCode(Right), modifier_shift, modifier_ctrl, + modifier_cmd, } => { if modifier_ctrl { state.move_cursor_next_word(text, modifier_shift); @@ -194,6 +195,7 @@ impl<'a> Editbox<'a> { key: Key::KeyCode(Left), modifier_shift, modifier_ctrl, + modifier_cmd, } => { if modifier_ctrl { state.move_cursor_prev_word(text, modifier_shift);