WindowServer: Cache internal Alt shortcuts on the Menu object

This way we don't have to recompute the set of shortcut keys every
time we're handling an in-menu keydown event.
This commit is contained in:
Andreas Kling 2021-04-09 16:33:37 +02:00
parent c2b760e335
commit 2bac9eb79d
Notes: sideshowbarker 2024-07-18 20:37:27 +09:00
3 changed files with 29 additions and 19 deletions

View file

@ -30,7 +30,6 @@
#include <WindowServer/MenuManager.h>
#include <WindowServer/Screen.h>
#include <WindowServer/WindowManager.h>
#include <ctype.h>
namespace WindowServer {
@ -91,23 +90,13 @@ void MenuManager::event(Core::Event& event)
&& ((key_event.key() >= Key_A && key_event.key() <= Key_Z)
|| (key_event.key() >= Key_0 && key_event.key() <= Key_9))) {
// FIXME: Maybe cache this on the menu instead of recomputing it on every alphanumeric menu keystroke?
HashMap<u32, size_t> alt_shortcut_to_item_index;
for (int i = 0; i < m_current_menu->item_count(); ++i) {
auto& item = m_current_menu->item(i);
if (!item.is_enabled())
continue;
auto alt_shortcut = find_ampersand_shortcut_character(item.text());
if (!alt_shortcut)
continue;
alt_shortcut_to_item_index.set(tolower(alt_shortcut), i);
}
auto it = alt_shortcut_to_item_index.find(tolower(key_event.code_point()));
if (it != alt_shortcut_to_item_index.end()) {
auto& item = m_current_menu->item(it->value);
m_current_menu->set_hovered_item(it->value);
if (auto* shortcut_item_indexes = m_current_menu->items_with_alt_shortcut(key_event.code_point())) {
VERIFY(!shortcut_item_indexes->is_empty());
// FIXME: If there are multiple items with the same Alt shortcut, we should cycle through them
// with each keypress instead of activating immediately.
auto index = shortcut_item_indexes->at(0);
auto& item = m_current_menu->item(index);
m_current_menu->set_hovered_item(index);
if (item.is_submenu())
m_current_menu->descend_into_submenu_at_hovered_item();
else