1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:27:35 +00:00

WindowServer: Support Alt+Character menu shortcuts :^)

This patch adds support for opening menus via keyboard shortcuts.
Use an ampersand in a menu name to automatically create a keyboard
shortcut (Alt + the character following the ampersand.)

Menus with an Alt shortcut have a small underline under the shortcut
character for discoverability.
This commit is contained in:
Andreas Kling 2021-04-05 23:03:55 +02:00
parent 89bc655765
commit d80fb6d9c0
6 changed files with 56 additions and 19 deletions

View file

@ -34,6 +34,7 @@
#include "WindowManager.h"
#include <AK/Badge.h>
#include <WindowServer/WindowClientEndpoint.h>
#include <ctype.h>
namespace WindowServer {
@ -445,12 +446,7 @@ void Window::event(Core::Event& event)
m_client->post_message(Messages::WindowClient::WindowLeft(m_window_id));
break;
case Event::KeyDown:
m_client->post_message(
Messages::WindowClient::KeyDown(m_window_id,
(u32) static_cast<const KeyEvent&>(event).code_point(),
(u32) static_cast<const KeyEvent&>(event).key(),
static_cast<const KeyEvent&>(event).modifiers(),
(u32) static_cast<const KeyEvent&>(event).scancode()));
handle_keydown_event(static_cast<const KeyEvent&>(event));
break;
case Event::KeyUp:
m_client->post_message(
@ -483,6 +479,25 @@ void Window::event(Core::Event& event)
}
}
void Window::handle_keydown_event(const KeyEvent& event)
{
if (event.modifiers() == Mod_Alt && event.code_point() && menubar()) {
Menu* menu_to_open = nullptr;
menubar()->for_each_menu([&](Menu& menu) {
if (tolower(menu.alt_shortcut_character()) == tolower(event.code_point())) {
menu_to_open = &menu;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (menu_to_open) {
frame().open_menubar_menu(*menu_to_open);
return;
}
}
m_client->post_message(Messages::WindowClient::KeyDown(m_window_id, (u32)event.code_point(), (u32)event.key(), event.modifiers(), (u32)event.scancode()));
}
void Window::set_global_cursor_tracking_enabled(bool enabled)
{
m_global_cursor_tracking_enabled = enabled;