diff options
author | Shannon Booth <shannon.ml.booth@gmail.com> | 2020-01-03 14:11:49 +1300 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2020-01-08 11:36:49 +0100 |
commit | 4683424e7ad7d4a05d5824f0b01f05ab18054b38 (patch) | |
tree | 15bb7054f7dcf6d442be7462cba6a61ae0e5d9e6 /Servers/WindowServer/WSMenuManager.cpp | |
parent | 27cb91e3e08705c8e5a13e8a8d61c99eb26ccf62 (diff) | |
download | serenity-4683424e7ad7d4a05d5824f0b01f05ab18054b38.zip |
WSMenu: Support menu navigation through key presses
Add event handling for key presses for navigating a menu. The currently
hovered menu item is tracked through an index which is either
incremented or decremented on up or down arrow key presses, changing the
hovered item.
Whenever there is a mouse move event, we ensure that the current index
matches the currently hovered item so that the mouse and keyboard do not
get out of sync.
If the right key is pressed, and we are on a submenu menu item, we
'enter' that submenu. While we are currently in a submenu, we forward
all keypress events to that submenu for handling. This allows us to
traverse the heirachy of a menu. While in a submenu, if the left key is
pressed, we leave that submenu and start handling the keypresses
ourselves again.
There is currently a small issue where the mouse hover and key hover can
get out of sync. The mouse can be traversing a submenu, but the parent
menu has no idea that the mouse has 'entered' a submenu, so will handle
the key presses itself, instead of forwarding them to the submenu. One
potential fix for this is for a menu to tell its menu parent that the
submenu is being traversed.
Diffstat (limited to 'Servers/WindowServer/WSMenuManager.cpp')
-rw-r--r-- | Servers/WindowServer/WSMenuManager.cpp | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/Servers/WindowServer/WSMenuManager.cpp b/Servers/WindowServer/WSMenuManager.cpp index fe474c7578..0ff230c700 100644 --- a/Servers/WindowServer/WSMenuManager.cpp +++ b/Servers/WindowServer/WSMenuManager.cpp @@ -263,6 +263,15 @@ void WSMenuManager::event(CEvent& event) applet->event(local_event); } } + + if (event.type() == WSEvent::KeyDown) { + for_each_active_menubar_menu([&](WSMenu& menu) { + if (is_open(menu)) + menu.dispatch_event(event); + return IterationDecision::Continue; + }); + } + return CObject::event(event); } @@ -270,7 +279,7 @@ void WSMenuManager::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& ev { bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && !m_open_menu_stack.is_empty() - && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == system_menu()); + && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr()); bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left; bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button); @@ -278,17 +287,7 @@ void WSMenuManager::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& ev m_bar_open = !m_bar_open; if (should_open_menu && m_bar_open) { - if (m_current_menu == &menu) - return; - close_everyone(); - if (!menu.is_empty()) { - menu.redraw_if_theme_changed(); - auto& menu_window = menu.ensure_menu_window(); - menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 }); - menu_window.set_visible(true); - } - set_current_menu(&menu); - refresh(); + open_menu(menu); return; } @@ -368,6 +367,20 @@ void WSMenuManager::close_menu_and_descendants(WSMenu& menu) close_menus(menus_to_close); } +void WSMenuManager::open_menu(WSMenu& menu) +{ + if (is_open(menu)) + return; + if (!menu.is_empty()) { + menu.redraw_if_theme_changed(); + auto& menu_window = menu.ensure_menu_window(); + menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 }); + menu_window.set_visible(true); + } + set_current_menu(&menu); + refresh(); +} + void WSMenuManager::set_current_menu(WSMenu* menu, bool is_submenu) { if (!is_submenu && m_current_menu) |