2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-04-01 00:22:06 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-02-12 09:22:52 +03:00
|
|
|
* Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
|
2020-01-18 11:38:21 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-02-15 02:10:34 +03:00
|
|
|
#include <AK/Badge.h>
|
2021-03-25 23:01:19 +03:00
|
|
|
#include <WindowServer/ClientConnection.h>
|
2020-02-06 22:03:37 +03:00
|
|
|
#include <WindowServer/MenuManager.h>
|
|
|
|
#include <WindowServer/Screen.h>
|
|
|
|
#include <WindowServer/WindowManager.h>
|
2021-04-09 14:48:26 +03:00
|
|
|
#include <ctype.h>
|
2019-06-21 09:19:43 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
namespace WindowServer {
|
2020-01-08 15:19:31 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
static MenuManager* s_the;
|
|
|
|
|
|
|
|
MenuManager& MenuManager::the()
|
2020-01-08 15:19:31 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(s_the);
|
2020-01-08 15:19:31 +03:00
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
MenuManager::MenuManager()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
2020-01-08 15:19:31 +03:00
|
|
|
s_the = this;
|
2019-06-21 09:19:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
MenuManager::~MenuManager()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
bool MenuManager::is_open(const Menu& menu) const
|
2019-08-28 22:11:53 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < m_open_menu_stack.size(); ++i) {
|
2019-08-28 22:11:53 +03:00
|
|
|
if (&menu == m_open_menu_stack[i].ptr())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::refresh()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
2021-03-26 16:42:24 +03:00
|
|
|
ClientConnection::for_each_client([&](ClientConnection& client) {
|
|
|
|
client.for_each_menu([&](Menu& menu) {
|
|
|
|
menu.redraw();
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
});
|
2019-06-21 09:19:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::event(Core::Event& event)
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
2021-03-26 01:08:34 +03:00
|
|
|
auto& wm = WindowManager::the();
|
|
|
|
|
2020-02-12 09:22:52 +03:00
|
|
|
if (static_cast<Event&>(event).is_mouse_event()) {
|
|
|
|
handle_mouse_event(static_cast<MouseEvent&>(event));
|
|
|
|
return;
|
2019-06-21 16:02:11 +03:00
|
|
|
}
|
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.
2020-01-03 04:11:49 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
if (static_cast<Event&>(event).is_key_event()) {
|
|
|
|
auto& key_event = static_cast<const KeyEvent&>(event);
|
2020-01-12 06:03:41 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
if (key_event.type() == Event::KeyUp && key_event.key() == Key_Escape) {
|
2020-01-12 06:03:41 +03:00
|
|
|
close_everyone();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:57:25 +03:00
|
|
|
if (m_current_menu && event.type() == Event::KeyDown
|
|
|
|
&& ((key_event.key() >= Key_A && key_event.key() <= Key_Z)
|
|
|
|
|| (key_event.key() >= Key_0 && key_event.key() <= Key_9))) {
|
2021-04-09 14:48:26 +03:00
|
|
|
|
|
|
|
// 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;
|
2020-05-09 06:57:25 +03:00
|
|
|
for (int i = 0; i < m_current_menu->item_count(); ++i) {
|
2021-04-09 14:48:26 +03:00
|
|
|
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 (item.is_submenu())
|
|
|
|
m_current_menu->descend_into_submenu_at_hovered_item();
|
|
|
|
else
|
|
|
|
m_current_menu->open_hovered_item(false);
|
2020-05-09 06:57:25 +03:00
|
|
|
}
|
2021-04-09 14:48:26 +03:00
|
|
|
|
2020-05-09 06:57:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
if (event.type() == Event::KeyDown) {
|
2020-05-09 06:57:22 +03:00
|
|
|
|
|
|
|
if (key_event.key() == Key_Left) {
|
2020-12-23 21:35:20 +03:00
|
|
|
auto it = m_open_menu_stack.find_if([&](const auto& other) { return m_current_menu == other.ptr(); });
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!it.is_end());
|
2020-05-09 06:57:22 +03:00
|
|
|
|
|
|
|
// Going "back" a menu should be the previous menu in the stack
|
|
|
|
if (it.index() > 0)
|
|
|
|
set_current_menu(m_open_menu_stack.at(it.index() - 1));
|
2021-02-13 16:24:42 +03:00
|
|
|
else {
|
|
|
|
if (m_current_menu->hovered_item())
|
|
|
|
m_current_menu->set_hovered_item(-1);
|
|
|
|
else {
|
|
|
|
auto* target_menu = previous_menu(m_current_menu);
|
2021-03-26 01:08:34 +03:00
|
|
|
if (target_menu) {
|
|
|
|
target_menu->ensure_menu_window().move_to(target_menu->rect_in_window_menubar().bottom_left().translated(wm.window_with_active_menu()->frame().rect().location()).translated(wm.window_with_active_menu()->frame().menubar_rect().location()));
|
2021-03-26 10:54:33 +03:00
|
|
|
open_menu(*target_menu);
|
2021-03-26 10:59:32 +03:00
|
|
|
wm.window_with_active_menu()->invalidate_menubar();
|
2021-03-26 01:08:34 +03:00
|
|
|
}
|
2021-02-13 16:24:42 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-09 06:57:22 +03:00
|
|
|
close_everyone_not_in_lineage(*m_current_menu);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_event.key() == Key_Right) {
|
|
|
|
auto hovered_item = m_current_menu->hovered_item();
|
|
|
|
if (hovered_item && hovered_item->is_submenu())
|
|
|
|
m_current_menu->descend_into_submenu_at_hovered_item();
|
2021-03-26 01:08:34 +03:00
|
|
|
else if (m_open_menu_stack.size() <= 1 && wm.window_with_active_menu()) {
|
2021-02-13 16:24:42 +03:00
|
|
|
auto* target_menu = next_menu(m_current_menu);
|
|
|
|
if (target_menu) {
|
2021-03-26 01:08:34 +03:00
|
|
|
target_menu->ensure_menu_window().move_to(target_menu->rect_in_window_menubar().bottom_left().translated(wm.window_with_active_menu()->frame().rect().location()).translated(wm.window_with_active_menu()->frame().menubar_rect().location()));
|
2021-03-26 10:54:33 +03:00
|
|
|
open_menu(*target_menu);
|
2021-03-26 10:59:32 +03:00
|
|
|
wm.window_with_active_menu()->invalidate_menubar();
|
2021-02-13 16:24:42 +03:00
|
|
|
close_everyone_not_in_lineage(*target_menu);
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 06:57:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_event.key() == Key_Return) {
|
|
|
|
auto hovered_item = m_current_menu->hovered_item();
|
2020-12-07 21:26:25 +03:00
|
|
|
if (!hovered_item || !hovered_item->is_enabled())
|
2020-05-09 06:57:22 +03:00
|
|
|
return;
|
|
|
|
if (hovered_item->is_submenu())
|
|
|
|
m_current_menu->descend_into_submenu_at_hovered_item();
|
|
|
|
else
|
2021-03-27 13:16:19 +03:00
|
|
|
m_current_menu->open_hovered_item(key_event.modifiers() & KeyModifier::Mod_Ctrl);
|
2020-05-09 06:57:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_current_menu->dispatch_event(event);
|
2020-01-12 06:03:41 +03:00
|
|
|
}
|
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.
2020-01-03 04:11:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
return Core::Object::event(event);
|
2019-06-21 09:19:43 +03:00
|
|
|
}
|
2019-06-21 16:02:11 +03:00
|
|
|
|
2020-02-12 09:22:52 +03:00
|
|
|
void MenuManager::handle_mouse_event(MouseEvent& mouse_event)
|
|
|
|
{
|
2021-04-01 10:10:07 +03:00
|
|
|
if (!has_open_menu())
|
|
|
|
return;
|
|
|
|
auto* topmost_menu = m_open_menu_stack.last().ptr();
|
|
|
|
VERIFY(topmost_menu);
|
|
|
|
auto* window = topmost_menu->menu_window();
|
|
|
|
if (!window) {
|
|
|
|
dbgln("MenuManager::handle_mouse_event: No menu window");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VERIFY(window->is_visible());
|
2020-02-12 09:22:52 +03:00
|
|
|
|
2021-04-01 10:10:07 +03:00
|
|
|
bool event_is_inside_current_menu = window->rect().contains(mouse_event.position());
|
|
|
|
if (event_is_inside_current_menu) {
|
|
|
|
WindowManager::the().set_hovered_window(window);
|
|
|
|
auto translated_event = mouse_event.translated(-window->position());
|
|
|
|
WindowManager::the().deliver_mouse_event(*window, translated_event, true);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-12 09:22:52 +03:00
|
|
|
|
2021-04-01 10:10:07 +03:00
|
|
|
if (topmost_menu->hovered_item())
|
|
|
|
topmost_menu->clear_hovered_item();
|
|
|
|
if (mouse_event.type() == Event::MouseDown || mouse_event.type() == Event::MouseUp) {
|
|
|
|
auto* window_menu_of = topmost_menu->window_menu_of();
|
|
|
|
if (window_menu_of) {
|
|
|
|
bool event_is_inside_taskbar_button = window_menu_of->taskbar_rect().contains(mouse_event.position());
|
|
|
|
if (event_is_inside_taskbar_button && !topmost_menu->is_window_menu_open()) {
|
|
|
|
topmost_menu->set_window_menu_open(true);
|
|
|
|
return;
|
2020-02-12 09:22:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:10:07 +03:00
|
|
|
if (mouse_event.type() == Event::MouseDown) {
|
2020-02-20 10:06:11 +03:00
|
|
|
for (auto& menu : m_open_menu_stack) {
|
2020-02-12 09:22:52 +03:00
|
|
|
if (!menu)
|
|
|
|
continue;
|
|
|
|
if (!menu->menu_window()->rect().contains(mouse_event.position()))
|
|
|
|
continue;
|
2021-04-01 10:10:07 +03:00
|
|
|
return;
|
2020-02-12 09:22:52 +03:00
|
|
|
}
|
2021-04-01 10:10:07 +03:00
|
|
|
MenuManager::the().close_everyone();
|
|
|
|
topmost_menu->set_window_menu_open(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mouse_event.type() == Event::MouseMove) {
|
|
|
|
for (auto& menu : m_open_menu_stack) {
|
|
|
|
if (!menu)
|
|
|
|
continue;
|
|
|
|
if (!menu->menu_window()->rect().contains(mouse_event.position()))
|
|
|
|
continue;
|
|
|
|
WindowManager::the().set_hovered_window(menu->menu_window());
|
|
|
|
auto translated_event = mouse_event.translated(-menu->menu_window()->position());
|
|
|
|
WindowManager::the().deliver_mouse_event(*menu->menu_window(), translated_event, true);
|
|
|
|
break;
|
2020-02-12 09:22:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::close_all_menus_from_client(Badge<ClientConnection>, ClientConnection& client)
|
2020-01-02 22:03:32 +03:00
|
|
|
{
|
2020-02-20 10:06:11 +03:00
|
|
|
if (!has_open_menu())
|
2020-01-02 22:03:32 +03:00
|
|
|
return;
|
|
|
|
if (m_open_menu_stack.first()->client() != &client)
|
|
|
|
return;
|
|
|
|
close_everyone();
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::close_everyone()
|
2019-11-11 14:21:57 +03:00
|
|
|
{
|
|
|
|
for (auto& menu : m_open_menu_stack) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(menu);
|
2021-04-05 15:32:34 +03:00
|
|
|
menu->set_visible(false);
|
2020-01-03 04:09:12 +03:00
|
|
|
menu->clear_hovered_item();
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
|
|
|
m_open_menu_stack.clear();
|
2020-07-15 19:13:13 +03:00
|
|
|
clear_current_menu();
|
2019-11-11 14:21:57 +03:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::close_everyone_not_in_lineage(Menu& menu)
|
2019-11-11 14:21:57 +03:00
|
|
|
{
|
2020-02-06 22:03:37 +03:00
|
|
|
Vector<Menu*> menus_to_close;
|
2019-11-11 14:21:57 +03:00
|
|
|
for (auto& open_menu : m_open_menu_stack) {
|
|
|
|
if (!open_menu)
|
|
|
|
continue;
|
|
|
|
if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
|
|
|
|
continue;
|
|
|
|
menus_to_close.append(open_menu);
|
|
|
|
}
|
|
|
|
close_menus(menus_to_close);
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::close_menus(const Vector<Menu*>& menus)
|
2019-11-11 14:21:57 +03:00
|
|
|
{
|
|
|
|
for (auto& menu : menus) {
|
|
|
|
if (menu == m_current_menu)
|
2020-07-15 19:13:13 +03:00
|
|
|
clear_current_menu();
|
2021-04-05 15:32:34 +03:00
|
|
|
menu->set_visible(false);
|
2020-01-03 04:09:12 +03:00
|
|
|
menu->clear_hovered_item();
|
2019-11-11 14:21:57 +03:00
|
|
|
m_open_menu_stack.remove_first_matching([&](auto& entry) {
|
|
|
|
return entry == menu;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
|
2019-11-11 14:21:57 +03:00
|
|
|
{
|
|
|
|
menus.append(&menu);
|
|
|
|
for (int i = 0; i < menu.item_count(); ++i) {
|
|
|
|
auto& item = menu.item(i);
|
|
|
|
if (!item.is_submenu())
|
|
|
|
continue;
|
2020-05-18 13:09:05 +03:00
|
|
|
collect_menu_subtree(*item.submenu(), menus);
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::close_menu_and_descendants(Menu& menu)
|
2019-11-11 14:21:57 +03:00
|
|
|
{
|
2020-02-06 22:03:37 +03:00
|
|
|
Vector<Menu*> menus_to_close;
|
2019-11-11 14:21:57 +03:00
|
|
|
collect_menu_subtree(menu, menus_to_close);
|
|
|
|
close_menus(menus_to_close);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:01:19 +03:00
|
|
|
void MenuManager::set_hovered_menu(Menu* menu)
|
|
|
|
{
|
|
|
|
if (m_hovered_menu == menu)
|
|
|
|
return;
|
|
|
|
if (menu) {
|
|
|
|
m_hovered_menu = menu->make_weak_ptr<Menu>();
|
|
|
|
} else {
|
|
|
|
// FIXME: This is quite aggressive. If we knew which window the previously hovered menu was in,
|
|
|
|
// we could just invalidate that one instead of iterating all windows in the client.
|
|
|
|
if (auto* client = m_hovered_menu->client()) {
|
|
|
|
client->for_each_window([&](Window& window) {
|
|
|
|
window.invalidate_menubar();
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
m_hovered_menu = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 10:54:33 +03:00
|
|
|
void MenuManager::open_menu(Menu& menu, bool as_current_menu)
|
2020-01-18 02:13:15 +03:00
|
|
|
{
|
2020-05-09 06:57:22 +03:00
|
|
|
if (is_open(menu)) {
|
2020-07-15 19:13:13 +03:00
|
|
|
if (as_current_menu || current_menu() != &menu) {
|
|
|
|
// This menu is already open. If requested, or if the current
|
|
|
|
// window doesn't match this one, then set it to this
|
2020-05-09 06:57:22 +03:00
|
|
|
set_current_menu(&menu);
|
2020-07-15 19:13:13 +03:00
|
|
|
}
|
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.
2020-01-03 04:11:49 +03:00
|
|
|
return;
|
2020-05-09 06:57:22 +03:00
|
|
|
}
|
|
|
|
|
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.
2020-01-03 04:11:49 +03:00
|
|
|
if (!menu.is_empty()) {
|
|
|
|
menu.redraw_if_theme_changed();
|
2021-03-26 01:08:34 +03:00
|
|
|
if (!menu.menu_window())
|
2021-02-22 05:49:08 +03:00
|
|
|
menu.ensure_menu_window();
|
2021-04-05 15:32:34 +03:00
|
|
|
menu.set_visible(true);
|
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.
2020-01-03 04:11:49 +03:00
|
|
|
}
|
2020-05-09 06:57:22 +03:00
|
|
|
|
2020-12-23 21:35:20 +03:00
|
|
|
if (m_open_menu_stack.find_if([&menu](auto& other) { return &menu == other.ptr(); }).is_end())
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
m_open_menu_stack.append(menu);
|
2020-05-09 06:57:22 +03:00
|
|
|
|
2020-07-15 19:13:13 +03:00
|
|
|
if (as_current_menu || !current_menu()) {
|
|
|
|
// Only make this menu the current menu if requested, or if no
|
|
|
|
// other menu is current
|
2020-05-09 06:57:22 +03:00
|
|
|
set_current_menu(&menu);
|
2020-07-15 19:13:13 +03:00
|
|
|
}
|
2020-05-09 06:57:22 +03:00
|
|
|
|
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.
2020-01-03 04:11:49 +03:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2020-07-15 19:13:13 +03:00
|
|
|
void MenuManager::clear_current_menu()
|
|
|
|
{
|
|
|
|
Menu* previous_current_menu = m_current_menu;
|
|
|
|
m_current_menu = nullptr;
|
|
|
|
if (previous_current_menu) {
|
|
|
|
// When closing the last menu, restore the previous active input window
|
|
|
|
auto& wm = WindowManager::the();
|
|
|
|
wm.restore_active_input_window(m_previous_input_window);
|
2021-03-25 23:01:19 +03:00
|
|
|
if (auto* window = wm.window_with_active_menu()) {
|
|
|
|
window->invalidate_menubar();
|
|
|
|
}
|
|
|
|
wm.set_window_with_active_menu(nullptr);
|
2020-07-15 19:13:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:57:22 +03:00
|
|
|
void MenuManager::set_current_menu(Menu* menu)
|
2019-11-11 14:21:57 +03:00
|
|
|
{
|
2020-05-09 06:57:22 +03:00
|
|
|
if (!menu) {
|
2020-07-15 19:13:13 +03:00
|
|
|
clear_current_menu();
|
2020-02-19 11:58:11 +03:00
|
|
|
return;
|
2020-01-12 06:01:15 +03:00
|
|
|
}
|
2020-01-09 09:58:55 +03:00
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_open(*menu));
|
2020-05-09 06:57:22 +03:00
|
|
|
if (menu == m_current_menu) {
|
2020-01-09 09:58:55 +03:00
|
|
|
return;
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
2020-01-09 09:58:55 +03:00
|
|
|
|
2020-07-15 19:13:13 +03:00
|
|
|
Menu* previous_current_menu = m_current_menu;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
m_current_menu = menu;
|
2020-07-15 19:13:13 +03:00
|
|
|
|
|
|
|
auto& wm = WindowManager::the();
|
|
|
|
if (!previous_current_menu) {
|
|
|
|
// When opening the first menu, store the current active input window
|
2020-07-16 06:07:51 +03:00
|
|
|
if (auto* active_input = wm.active_input_window())
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
m_previous_input_window = *active_input;
|
2020-07-16 06:07:51 +03:00
|
|
|
else
|
|
|
|
m_previous_input_window = nullptr;
|
2020-07-15 19:13:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wm.set_active_input_window(m_current_menu->menu_window());
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
2019-11-11 14:54:23 +03:00
|
|
|
|
2021-02-13 16:24:42 +03:00
|
|
|
Menu* MenuManager::previous_menu(Menu* current)
|
|
|
|
{
|
2021-03-26 01:08:34 +03:00
|
|
|
auto& wm = WindowManager::the();
|
|
|
|
if (!wm.window_with_active_menu())
|
|
|
|
return nullptr;
|
2021-02-13 16:24:42 +03:00
|
|
|
Menu* found = nullptr;
|
|
|
|
Menu* previous = nullptr;
|
2021-03-26 01:08:34 +03:00
|
|
|
wm.window_with_active_menu()->menubar()->for_each_menu([&](Menu& menu) {
|
2021-02-13 16:24:42 +03:00
|
|
|
if (current == &menu) {
|
|
|
|
found = previous;
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
previous = &menu;
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
Menu* MenuManager::next_menu(Menu* current)
|
|
|
|
{
|
|
|
|
Menu* found = nullptr;
|
|
|
|
bool is_next = false;
|
2021-03-26 01:08:34 +03:00
|
|
|
auto& wm = WindowManager::the();
|
|
|
|
if (!wm.window_with_active_menu())
|
|
|
|
return nullptr;
|
|
|
|
wm.window_with_active_menu()->menubar()->for_each_menu([&](Menu& menu) {
|
2021-02-13 16:24:42 +03:00
|
|
|
if (is_next) {
|
|
|
|
found = &menu;
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
if (current == &menu)
|
|
|
|
is_next = true;
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:05:14 +03:00
|
|
|
void MenuManager::did_change_theme()
|
|
|
|
{
|
|
|
|
++m_theme_index;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
}
|