2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, 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>
|
2020-01-05 05:05:41 +03:00
|
|
|
#include <AK/FileSystemPath.h>
|
|
|
|
#include <AK/QuickSort.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/DirIterator.h>
|
2020-02-06 14:04:00 +03:00
|
|
|
#include <LibGfx/Font.h>
|
|
|
|
#include <LibGfx/Painter.h>
|
2020-02-09 20:37:42 +03:00
|
|
|
#include <WindowServer/AppletManager.h>
|
2020-02-06 22:03:37 +03:00
|
|
|
#include <WindowServer/MenuManager.h>
|
|
|
|
#include <WindowServer/Screen.h>
|
|
|
|
#include <WindowServer/WindowManager.h>
|
2019-06-21 09:19:43 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-01-05 05:05:41 +03:00
|
|
|
//#define DEBUG_MENUS
|
|
|
|
|
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;
|
2020-05-09 06:57:25 +03:00
|
|
|
static constexpr int s_search_timeout = 3000;
|
2020-02-06 22:03:37 +03:00
|
|
|
|
|
|
|
MenuManager& MenuManager::the()
|
2020-01-08 15:19:31 +03:00
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
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-12-15 20:07:48 +03:00
|
|
|
m_needs_window_resize = true;
|
2020-01-05 05:05:41 +03:00
|
|
|
|
|
|
|
// NOTE: This ensures that the system menu has the correct dimensions.
|
|
|
|
set_current_menubar(nullptr);
|
2019-06-21 09:19:43 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
m_window = Window::construct(*this, WindowType::Menubar);
|
2020-01-08 15:19:31 +03:00
|
|
|
m_window->set_rect(menubar_rect());
|
2020-05-09 06:57:25 +03:00
|
|
|
|
|
|
|
m_search_timer = Core::Timer::create_single_shot(0, [this] {
|
|
|
|
m_current_search.clear();
|
|
|
|
});
|
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::draw()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
2020-02-06 22:03:37 +03:00
|
|
|
auto& wm = WindowManager::the();
|
2019-12-29 02:47:49 +03:00
|
|
|
auto palette = wm.palette();
|
2019-12-05 21:59:11 +03:00
|
|
|
auto menubar_rect = this->menubar_rect();
|
2019-06-21 09:19:43 +03:00
|
|
|
|
2019-10-30 13:06:32 +03:00
|
|
|
if (m_needs_window_resize) {
|
|
|
|
m_window->set_rect(menubar_rect);
|
2020-02-09 20:37:42 +03:00
|
|
|
AppletManager::the().calculate_applet_rects(window());
|
2019-10-30 13:06:32 +03:00
|
|
|
m_needs_window_resize = false;
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
Gfx::Painter painter(*window().backing_store());
|
2019-06-21 09:19:43 +03:00
|
|
|
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.fill_rect(menubar_rect, palette.window());
|
|
|
|
painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow1());
|
2020-03-10 10:56:07 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
for_each_active_menubar_menu([&](Menu& menu) {
|
2019-12-24 22:57:54 +03:00
|
|
|
Color text_color = palette.window_text();
|
2019-08-28 22:11:53 +03:00
|
|
|
if (is_open(menu)) {
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.fill_rect(menu.rect_in_menubar(), palette.menu_selection());
|
|
|
|
painter.draw_rect(menu.rect_in_menubar(), palette.menu_selection().darkened());
|
2020-02-20 11:20:32 +03:00
|
|
|
text_color = palette.menu_selection_text();
|
2019-06-21 09:19:43 +03:00
|
|
|
}
|
|
|
|
painter.draw_text(
|
|
|
|
menu.text_rect_in_menubar(),
|
|
|
|
menu.name(),
|
2020-03-10 10:56:07 +03:00
|
|
|
menu.title_font(),
|
2020-02-06 13:56:38 +03:00
|
|
|
Gfx::TextAlignment::CenterLeft,
|
2019-06-21 09:19:43 +03:00
|
|
|
text_color);
|
2019-12-16 17:33:42 +03:00
|
|
|
return IterationDecision::Continue;
|
2019-06-21 09:19:43 +03:00
|
|
|
});
|
|
|
|
|
2020-02-09 20:37:42 +03:00
|
|
|
AppletManager::the().draw();
|
2019-06-21 09:19:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::refresh()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
|
|
|
if (!m_window)
|
|
|
|
return;
|
|
|
|
draw();
|
|
|
|
window().invalidate();
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::event(Core::Event& event)
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
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 (key_event.key() == Key_Backspace) {
|
|
|
|
m_current_search.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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))) {
|
|
|
|
m_current_search.append(key_event.character());
|
|
|
|
m_search_timer->restart(s_search_timeout);
|
|
|
|
for (int i = 0; i < m_current_menu->item_count(); ++i) {
|
|
|
|
auto text = m_current_menu->item(i).text();
|
|
|
|
if (text.to_lowercase().starts_with(m_current_search.to_string().to_lowercase())) {
|
|
|
|
m_current_menu->set_hovered_item(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
auto it = m_open_menu_stack.find([&](auto& other) { return m_current_menu == other.ptr(); });
|
|
|
|
ASSERT(!it.is_end());
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
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();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_event.key() == Key_Return) {
|
|
|
|
auto hovered_item = m_current_menu->hovered_item();
|
|
|
|
|
|
|
|
if (!hovered_item->is_enabled())
|
|
|
|
return;
|
|
|
|
if (hovered_item->is_submenu())
|
|
|
|
m_current_menu->descend_into_submenu_at_hovered_item();
|
|
|
|
else
|
|
|
|
m_current_menu->open_hovered_item();
|
|
|
|
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)
|
|
|
|
{
|
2020-04-21 17:20:50 +03:00
|
|
|
auto* active_window = WindowManager::the().active_window();
|
2020-02-12 09:22:52 +03:00
|
|
|
bool handled_menubar_event = false;
|
|
|
|
for_each_active_menubar_menu([&](Menu& menu) {
|
|
|
|
if (menu.rect_in_menubar().contains(mouse_event.position())) {
|
2020-04-21 17:46:32 +03:00
|
|
|
handled_menubar_event = &menu == m_system_menu || !active_window || !active_window->is_modal();
|
|
|
|
if (handled_menubar_event)
|
|
|
|
handle_menu_mouse_event(menu, mouse_event);
|
2020-02-12 09:22:52 +03:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
if (handled_menubar_event)
|
|
|
|
return;
|
|
|
|
|
2020-02-20 10:06:11 +03:00
|
|
|
if (has_open_menu()) {
|
|
|
|
auto* topmost_menu = m_open_menu_stack.last().ptr();
|
2020-02-12 09:22:52 +03:00
|
|
|
ASSERT(topmost_menu);
|
|
|
|
auto* window = topmost_menu->menu_window();
|
2020-04-09 20:44:59 +03:00
|
|
|
if (!window) {
|
|
|
|
dbg() << "MenuManager::handle_mouse_event: No menu window";
|
|
|
|
return;
|
|
|
|
}
|
2020-02-19 11:58:11 +03:00
|
|
|
ASSERT(window->is_visible());
|
2020-02-12 09:22:52 +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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mouse_event.type() == Event::MouseDown) {
|
|
|
|
close_bar();
|
|
|
|
topmost_menu->set_window_menu_open(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mouse_event.type() == Event::MouseMove) {
|
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;
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AppletManager::the().dispatch_event(static_cast<Event&>(mouse_event));
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::handle_menu_mouse_event(Menu& menu, const MouseEvent& event)
|
2019-06-21 16:02:11 +03:00
|
|
|
{
|
2020-02-06 22:03:37 +03:00
|
|
|
bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove
|
2020-02-20 10:06:11 +03:00
|
|
|
&& has_open_menu()
|
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
|
|
|
&& (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr());
|
2020-02-06 22:03:37 +03:00
|
|
|
bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Left;
|
2019-11-11 14:21:57 +03:00
|
|
|
bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
|
2019-06-21 16:02:11 +03:00
|
|
|
|
2019-11-11 14:54:23 +03:00
|
|
|
if (is_mousedown_with_left_button)
|
|
|
|
m_bar_open = !m_bar_open;
|
|
|
|
|
|
|
|
if (should_open_menu && m_bar_open) {
|
2020-05-09 06:57:22 +03:00
|
|
|
close_everyone();
|
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
|
|
|
open_menu(menu);
|
2019-06-21 16:02:11 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-11-11 14:54:23 +03:00
|
|
|
|
|
|
|
if (!m_bar_open)
|
2019-11-11 14:21:57 +03:00
|
|
|
close_everyone();
|
2019-06-21 16:02:11 +03:00
|
|
|
}
|
2019-10-30 13:06:32 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::set_needs_window_resize()
|
2019-10-30 13:06:32 +03:00
|
|
|
{
|
|
|
|
m_needs_window_resize = true;
|
2019-11-11 14:21:57 +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) {
|
2020-05-09 06:57:22 +03:00
|
|
|
ASSERT(menu);
|
|
|
|
if (menu->menu_window())
|
2019-11-11 14:21:57 +03:00
|
|
|
menu->menu_window()->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-05-09 06:57:25 +03:00
|
|
|
m_current_search.clear();
|
2019-11-11 14:54:23 +03:00
|
|
|
m_current_menu = nullptr;
|
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)
|
|
|
|
m_current_menu = nullptr;
|
|
|
|
if (menu->menu_window())
|
|
|
|
menu->menu_window()->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-02-06 22:03:37 +03:00
|
|
|
collect_menu_subtree(*const_cast<MenuItem&>(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);
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::toggle_menu(Menu& menu)
|
2020-01-18 02:13:15 +03:00
|
|
|
{
|
|
|
|
if (is_open(menu)) {
|
|
|
|
close_menu_and_descendants(menu);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
open_menu(menu);
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:57:22 +03:00
|
|
|
void MenuManager::open_menu(Menu& menu, bool as_current_menu)
|
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
|
|
|
if (is_open(menu)) {
|
|
|
|
if (as_current_menu)
|
|
|
|
set_current_menu(&menu);
|
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();
|
2020-05-09 06:57:22 +03:00
|
|
|
if (!menu.menu_window()) {
|
|
|
|
auto& menu_window = menu.ensure_menu_window();
|
|
|
|
menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
|
|
|
|
}
|
|
|
|
menu.menu_window()->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
|
|
|
|
|
|
|
if (m_open_menu_stack.find([&menu](auto& other) { return &menu == other.ptr(); }).is_end())
|
|
|
|
m_open_menu_stack.append(menu.make_weak_ptr());
|
|
|
|
|
|
|
|
if (as_current_menu)
|
|
|
|
set_current_menu(&menu);
|
|
|
|
|
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-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) {
|
|
|
|
m_current_menu = nullptr;
|
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
|
|
|
|
2020-05-09 06:57:22 +03:00
|
|
|
ASSERT(is_open(*menu));
|
|
|
|
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-05-09 06:57:25 +03:00
|
|
|
m_current_search.clear();
|
2020-01-09 09:58:55 +03:00
|
|
|
m_current_menu = menu->make_weak_ptr();
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
2019-11-11 14:54:23 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::close_bar()
|
2019-11-11 14:54:23 +03:00
|
|
|
{
|
|
|
|
close_everyone();
|
|
|
|
m_bar_open = false;
|
|
|
|
}
|
2019-12-05 21:36:01 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
Gfx::Rect MenuManager::menubar_rect() const
|
2019-12-05 21:59:11 +03:00
|
|
|
{
|
2020-02-06 22:03:37 +03:00
|
|
|
return { 0, 0, Screen::the().rect().width(), 18 };
|
2019-12-05 21:59:11 +03:00
|
|
|
}
|
2020-01-05 05:05:41 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::set_current_menubar(MenuBar* menubar)
|
2020-01-05 05:05:41 +03:00
|
|
|
{
|
|
|
|
if (menubar)
|
|
|
|
m_current_menubar = menubar->make_weak_ptr();
|
|
|
|
else
|
|
|
|
m_current_menubar = nullptr;
|
|
|
|
#ifdef DEBUG_MENUS
|
|
|
|
dbg() << "[WM] Current menubar is now " << menubar;
|
|
|
|
#endif
|
2020-02-06 22:03:37 +03:00
|
|
|
Gfx::Point next_menu_location { MenuManager::menubar_menu_margin() / 2, 0 };
|
|
|
|
for_each_active_menubar_menu([&](Menu& menu) {
|
2020-03-10 10:56:07 +03:00
|
|
|
int text_width = menu.title_font().width(menu.name());
|
2020-02-06 22:03:37 +03:00
|
|
|
menu.set_rect_in_menubar({ next_menu_location.x() - MenuManager::menubar_menu_margin() / 2, 0, text_width + MenuManager::menubar_menu_margin(), menubar_rect().height() - 1 });
|
2020-01-05 05:05:41 +03:00
|
|
|
menu.set_text_rect_in_menubar({ next_menu_location, { text_width, menubar_rect().height() } });
|
|
|
|
next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void MenuManager::close_menubar(MenuBar& menubar)
|
2020-01-05 05:05:41 +03:00
|
|
|
{
|
|
|
|
if (current_menubar() == &menubar)
|
|
|
|
set_current_menubar(nullptr);
|
|
|
|
}
|
2020-02-06 22:03:37 +03:00
|
|
|
|
2020-02-17 22:05:14 +03:00
|
|
|
void MenuManager::set_system_menu(Menu& menu)
|
|
|
|
{
|
|
|
|
m_system_menu = menu.make_weak_ptr();
|
|
|
|
set_current_menubar(m_current_menubar);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuManager::did_change_theme()
|
|
|
|
{
|
|
|
|
++m_theme_index;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
}
|