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

Impl cmd q macos #674

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 19 additions & 8 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -642,6 +647,7 @@ impl InputHandler for Ui {
key: input::Key::KeyCode(key),
modifier_shift: shift,
modifier_ctrl: ctrl,
modifier_cmd: cmd,
});
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
};
}
Expand All @@ -1267,6 +1274,7 @@ pub(crate) mod ui_context {
process!(X);
process!(V);
process!(A);
process!(Q);
process!(Escape);
process!(Enter);

Expand All @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macroquad::ui is a module about drawing game UI, I am not quite sure if handling platform quit request belongs here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am inclined to agree. Would this be better moved to something like begin_frame() in src/lib.rs, or could you make a suggestion where a platform quit request would be better handled?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

begin_frame looks like a right place to me.

Justa side question, but is manual handling Cmd-Q a common practice on MacOs? Isnt there some sort of a system-level hotkeys for this kind of things?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only thought about that after I last commented. I can do some research; I bet I can find something. I will get back to this in a day or two with relevant changes.

}
let (wheel_x, wheel_y) = mouse_wheel();
ui.mouse_wheel(wheel_x, -wheel_y);
Expand Down
3 changes: 3 additions & 0 deletions src/ui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -24,7 +25,9 @@ pub struct Input {
pub(crate) mouse_wheel: Vec2,
pub(crate) input_buffer: Vec<InputCharacter>,
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,
Expand Down
5 changes: 3 additions & 2 deletions src/ui/input_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ pub enum KeyCode {
C, // copy
V, // paste
X, // cut
Q, // quit
}

pub trait InputHandler {
fn mouse_down(&mut self, position: (f32, f32));
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);
}
2 changes: 2 additions & 0 deletions src/ui/widgets/editbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down