2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2020-01-19 12:13:26 +13:00
|
|
|
* Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
|
2020-01-18 09:38:21 +01: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.
|
|
|
|
*/
|
|
|
|
|
2019-02-11 09:47:10 +01:00
|
|
|
#include "WSMenu.h"
|
2019-04-14 05:23:37 +02:00
|
|
|
#include "WSEvent.h"
|
|
|
|
#include "WSEventLoop.h"
|
2019-06-07 11:47:19 +02:00
|
|
|
#include "WSMenuItem.h"
|
2019-08-28 21:11:53 +02:00
|
|
|
#include "WSMenuManager.h"
|
2019-05-16 01:06:21 +02:00
|
|
|
#include "WSScreen.h"
|
2019-06-07 11:47:19 +02:00
|
|
|
#include "WSWindow.h"
|
|
|
|
#include "WSWindowManager.h"
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/CharacterBitmap.h>
|
|
|
|
#include <LibGfx/Font.h>
|
2020-02-06 12:07:05 +01:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-02-06 12:04:00 +01:00
|
|
|
#include <LibGfx/Painter.h>
|
|
|
|
#include <LibGfx/StylePainter.h>
|
|
|
|
#include <LibGfx/Triangle.h>
|
2019-06-07 11:47:19 +02:00
|
|
|
#include <WindowServer/WSClientConnection.h>
|
2019-12-02 09:33:37 +01:00
|
|
|
#include <WindowServer/WindowClientEndpoint.h>
|
2019-02-11 09:47:10 +01:00
|
|
|
|
2019-05-12 14:57:15 +02:00
|
|
|
WSMenu::WSMenu(WSClientConnection* client, int menu_id, const String& name)
|
2020-02-02 12:34:39 +01:00
|
|
|
: Core::Object(client)
|
2019-08-18 12:05:33 +02:00
|
|
|
, m_client(client)
|
2019-02-14 10:44:47 +01:00
|
|
|
, m_menu_id(menu_id)
|
2019-02-12 00:52:19 +01:00
|
|
|
, m_name(move(name))
|
2019-02-11 09:47:10 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WSMenu::~WSMenu()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
const Gfx::Font& WSMenu::font() const
|
2019-02-11 09:47:10 +01:00
|
|
|
{
|
2020-02-06 11:56:38 +01:00
|
|
|
return Gfx::Font::default_font();
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 21:09:56 +02:00
|
|
|
static const char* s_checked_bitmap_data = {
|
|
|
|
" "
|
2019-05-10 22:50:42 +02:00
|
|
|
" # "
|
2019-04-26 21:09:56 +02:00
|
|
|
" ## "
|
2019-05-10 22:50:42 +02:00
|
|
|
" ### "
|
|
|
|
" ## ### "
|
|
|
|
" ##### "
|
|
|
|
" ### "
|
|
|
|
" # "
|
2019-04-26 21:09:56 +02:00
|
|
|
" "
|
|
|
|
};
|
|
|
|
|
2019-08-28 21:11:53 +02:00
|
|
|
static const char* s_submenu_arrow_bitmap_data = {
|
|
|
|
" "
|
|
|
|
" # "
|
|
|
|
" ## "
|
|
|
|
" ### "
|
|
|
|
" #### "
|
|
|
|
" ### "
|
|
|
|
" ## "
|
|
|
|
" # "
|
|
|
|
" "
|
|
|
|
};
|
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
static Gfx::CharacterBitmap* s_checked_bitmap;
|
2019-04-26 21:09:56 +02:00
|
|
|
static const int s_checked_bitmap_width = 9;
|
|
|
|
static const int s_checked_bitmap_height = 9;
|
2019-08-28 21:11:53 +02:00
|
|
|
static const int s_submenu_arrow_bitmap_width = 9;
|
|
|
|
static const int s_submenu_arrow_bitmap_height = 9;
|
2019-08-26 18:54:44 +02:00
|
|
|
static const int s_item_icon_width = 16;
|
2019-08-26 20:43:30 +02:00
|
|
|
static const int s_stripe_width = 23;
|
2019-04-26 21:09:56 +02:00
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
int WSMenu::content_width() const
|
2019-02-11 09:47:10 +01:00
|
|
|
{
|
2019-04-29 23:41:48 +02:00
|
|
|
int widest_text = 0;
|
|
|
|
int widest_shortcut = 0;
|
2019-02-11 09:47:10 +01:00
|
|
|
for (auto& item : m_items) {
|
2019-07-24 09:04:57 +02:00
|
|
|
if (item.type() != WSMenuItem::Text)
|
2019-04-29 23:41:48 +02:00
|
|
|
continue;
|
2019-07-24 09:04:57 +02:00
|
|
|
int text_width = font().width(item.text());
|
|
|
|
if (!item.shortcut_text().is_empty()) {
|
|
|
|
int shortcut_width = font().width(item.shortcut_text());
|
2019-04-29 23:41:48 +02:00
|
|
|
widest_shortcut = max(shortcut_width, widest_shortcut);
|
2019-03-02 10:04:49 +01:00
|
|
|
}
|
2019-04-29 23:41:48 +02:00
|
|
|
widest_text = max(widest_text, text_width);
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 20:43:30 +02:00
|
|
|
int widest_item = widest_text + s_stripe_width;
|
2019-04-29 23:41:48 +02:00
|
|
|
if (widest_shortcut)
|
|
|
|
widest_item += padding_between_text_and_shortcut() + widest_shortcut;
|
|
|
|
|
|
|
|
return max(widest_item, rect_in_menubar().width()) + horizontal_padding() + frame_thickness() * 2;
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:37 +01:00
|
|
|
void WSMenu::redraw()
|
|
|
|
{
|
2019-04-12 02:53:27 +02:00
|
|
|
if (!menu_window())
|
|
|
|
return;
|
2019-02-11 10:08:37 +01:00
|
|
|
draw();
|
|
|
|
menu_window()->invalidate();
|
|
|
|
}
|
|
|
|
|
2019-02-11 09:47:10 +01:00
|
|
|
WSWindow& WSMenu::ensure_menu_window()
|
|
|
|
{
|
2020-01-19 21:28:38 +01:00
|
|
|
int width = this->content_width();
|
2019-02-11 09:47:10 +01:00
|
|
|
if (!m_menu_window) {
|
2019-04-16 17:02:26 +02:00
|
|
|
Point next_item_location(frame_thickness(), frame_thickness());
|
2019-02-11 09:47:10 +01:00
|
|
|
for (auto& item : m_items) {
|
|
|
|
int height = 0;
|
2019-07-24 09:04:57 +02:00
|
|
|
if (item.type() == WSMenuItem::Text)
|
2019-02-11 09:47:10 +01:00
|
|
|
height = item_height();
|
2019-07-24 09:04:57 +02:00
|
|
|
else if (item.type() == WSMenuItem::Separator)
|
2019-04-18 19:58:25 +02:00
|
|
|
height = 8;
|
2019-07-24 09:04:57 +02:00
|
|
|
item.set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
|
2019-02-11 09:47:10 +01:00
|
|
|
next_item_location.move_by(0, height);
|
|
|
|
}
|
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
int window_height_available = WSScreen::the().height() - WSMenuManager::the().menubar_rect().height() - frame_thickness() * 2;
|
|
|
|
int max_window_height = (window_height_available / item_height()) * item_height() + frame_thickness() * 2;
|
|
|
|
int content_height = m_items.is_empty() ? 0 : (m_items.last().rect().bottom() + 1) + frame_thickness();
|
|
|
|
int window_height = min(max_window_height, content_height);
|
|
|
|
if (window_height < content_height) {
|
|
|
|
m_scrollable = true;
|
|
|
|
m_max_scroll_offset = item_count() - window_height / item_height() + 2;
|
|
|
|
}
|
|
|
|
|
2019-09-22 00:17:53 +02:00
|
|
|
auto window = WSWindow::construct(*this, WSWindowType::Menu);
|
2020-01-19 21:28:38 +01:00
|
|
|
window->set_rect(0, 0, width, window_height);
|
2019-02-11 09:47:10 +01:00
|
|
|
m_menu_window = move(window);
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
return *m_menu_window;
|
|
|
|
}
|
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
int WSMenu::visible_item_count() const
|
|
|
|
{
|
|
|
|
if (!is_scrollable())
|
|
|
|
return m_items.size();
|
|
|
|
ASSERT(m_menu_window);
|
|
|
|
// Make space for up/down arrow indicators
|
|
|
|
return m_menu_window->height() / item_height() - 2;
|
|
|
|
}
|
|
|
|
|
2019-02-11 09:47:10 +01:00
|
|
|
void WSMenu::draw()
|
|
|
|
{
|
2019-12-29 00:47:49 +01:00
|
|
|
auto palette = WSWindowManager::the().palette();
|
2020-01-08 13:19:31 +01:00
|
|
|
m_theme_index_at_last_paint = WSMenuManager::the().theme_index();
|
2019-12-23 20:24:26 +01:00
|
|
|
|
2019-02-11 10:08:37 +01:00
|
|
|
ASSERT(menu_window());
|
2019-02-20 21:59:13 +01:00
|
|
|
ASSERT(menu_window()->backing_store());
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Painter painter(*menu_window()->backing_store());
|
2019-02-11 09:47:10 +01:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect rect { {}, menu_window()->size() };
|
2019-12-24 20:57:54 +01:00
|
|
|
painter.fill_rect(rect.shrunken(6, 6), palette.menu_base());
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::StylePainter::paint_window_frame(painter, rect, palette);
|
2020-01-19 21:28:38 +01:00
|
|
|
int width = this->content_width();
|
2019-02-11 09:47:10 +01:00
|
|
|
|
2019-04-26 21:09:56 +02:00
|
|
|
if (!s_checked_bitmap)
|
2020-02-06 11:56:38 +01:00
|
|
|
s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
|
2019-04-26 21:09:56 +02:00
|
|
|
|
2019-08-26 18:10:07 +02:00
|
|
|
bool has_checkable_items = false;
|
2019-08-26 18:54:44 +02:00
|
|
|
bool has_items_with_icon = false;
|
|
|
|
for (auto& item : m_items) {
|
2019-08-26 18:10:07 +02:00
|
|
|
has_checkable_items = has_checkable_items | item.is_checkable();
|
2019-08-26 18:54:44 +02:00
|
|
|
has_items_with_icon = has_items_with_icon | !!item.icon();
|
|
|
|
}
|
2019-08-26 18:10:07 +02:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect stripe_rect { frame_thickness(), frame_thickness(), s_stripe_width, menu_window()->height() - frame_thickness() * 2 };
|
2019-12-24 20:57:54 +01:00
|
|
|
painter.fill_rect(stripe_rect, palette.menu_stripe());
|
|
|
|
painter.draw_line(stripe_rect.top_right(), stripe_rect.bottom_right(), palette.menu_stripe().darkened());
|
2019-08-26 20:43:30 +02:00
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
int visible_item_count = this->visible_item_count();
|
|
|
|
|
|
|
|
if (is_scrollable()) {
|
|
|
|
bool can_go_up = m_scroll_offset > 0;
|
|
|
|
bool can_go_down = m_scroll_offset < m_max_scroll_offset;
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect up_indicator_rect { frame_thickness(), frame_thickness(), content_width(), item_height() };
|
|
|
|
painter.draw_text(up_indicator_rect, "\xc3\xb6", Gfx::TextAlignment::Center, can_go_up ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
|
|
|
|
Gfx::Rect down_indicator_rect { frame_thickness(), menu_window()->height() - item_height() - frame_thickness(), content_width(), item_height() };
|
|
|
|
painter.draw_text(down_indicator_rect, "\xc3\xb7", Gfx::TextAlignment::Center, can_go_down ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
|
2020-01-19 21:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < visible_item_count; ++i) {
|
|
|
|
auto& item = m_items.at(m_scroll_offset + i);
|
2019-07-24 09:04:57 +02:00
|
|
|
if (item.type() == WSMenuItem::Text) {
|
2019-12-26 00:58:46 +01:00
|
|
|
Color text_color = palette.menu_base_text();
|
2020-01-18 20:56:51 +13:00
|
|
|
if (&item == hovered_item() && item.is_enabled()) {
|
2019-12-24 20:57:54 +01:00
|
|
|
painter.fill_rect(item.rect(), palette.menu_selection());
|
|
|
|
painter.draw_rect(item.rect(), palette.menu_selection().darkened());
|
2019-12-26 00:58:46 +01:00
|
|
|
text_color = palette.menu_selection_text();
|
2019-08-27 10:20:10 +02:00
|
|
|
} else if (!item.is_enabled()) {
|
|
|
|
text_color = Color::MidGray;
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect text_rect = item.rect().translated(stripe_rect.width() + 6, 0);
|
2019-07-24 09:04:57 +02:00
|
|
|
if (item.is_checkable()) {
|
2020-01-08 20:44:41 +01:00
|
|
|
if (item.is_exclusive()) {
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect radio_rect { item.rect().x() + 5, 0, 12, 12 };
|
2020-01-08 20:44:41 +01:00
|
|
|
radio_rect.center_vertically_within(text_rect);
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, item.is_checked(), false);
|
2020-01-08 20:44:41 +01:00
|
|
|
} else {
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect checkmark_rect { item.rect().x() + 7, 0, s_checked_bitmap_width, s_checked_bitmap_height };
|
2020-01-08 20:44:41 +01:00
|
|
|
checkmark_rect.center_vertically_within(text_rect);
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect checkbox_rect = checkmark_rect.inflated(4, 4);
|
2020-01-08 20:44:41 +01:00
|
|
|
painter.fill_rect(checkbox_rect, palette.base());
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::StylePainter::paint_frame(painter, checkbox_rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
|
2020-01-08 20:44:41 +01:00
|
|
|
if (item.is_checked()) {
|
|
|
|
painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, palette.button_text());
|
|
|
|
}
|
2019-04-26 21:09:56 +02:00
|
|
|
}
|
2019-08-26 18:54:44 +02:00
|
|
|
} else if (item.icon()) {
|
2020-02-06 11:56:38 +01:00
|
|
|
Gfx::Rect icon_rect { item.rect().x() + 3, 0, s_item_icon_width, s_item_icon_width };
|
2019-08-26 18:54:44 +02:00
|
|
|
icon_rect.center_vertically_within(text_rect);
|
|
|
|
painter.blit(icon_rect.location(), *item.icon(), item.icon()->rect());
|
2019-04-26 21:09:56 +02:00
|
|
|
}
|
2020-02-06 11:56:38 +01:00
|
|
|
painter.draw_text(text_rect, item.text(), Gfx::TextAlignment::CenterLeft, text_color);
|
2019-07-24 09:04:57 +02:00
|
|
|
if (!item.shortcut_text().is_empty()) {
|
2020-02-06 11:56:38 +01:00
|
|
|
painter.draw_text(item.rect().translated(-right_padding(), 0), item.shortcut_text(), Gfx::TextAlignment::CenterRight, text_color);
|
2019-03-02 10:04:49 +01:00
|
|
|
}
|
2019-08-28 21:11:53 +02:00
|
|
|
if (item.is_submenu()) {
|
2020-02-06 11:56:38 +01:00
|
|
|
static auto& submenu_arrow_bitmap = Gfx::CharacterBitmap::create_from_ascii(s_submenu_arrow_bitmap_data, s_submenu_arrow_bitmap_width, s_submenu_arrow_bitmap_height).leak_ref();
|
|
|
|
Gfx::Rect submenu_arrow_rect {
|
2019-08-28 21:11:53 +02:00
|
|
|
item.rect().right() - s_submenu_arrow_bitmap_width - 2,
|
|
|
|
0,
|
|
|
|
s_submenu_arrow_bitmap_width,
|
|
|
|
s_submenu_arrow_bitmap_height
|
|
|
|
};
|
|
|
|
submenu_arrow_rect.center_vertically_within(item.rect());
|
2019-12-26 00:58:46 +01:00
|
|
|
painter.draw_bitmap(submenu_arrow_rect.location(), submenu_arrow_bitmap, text_color);
|
2019-08-28 21:11:53 +02:00
|
|
|
}
|
2019-07-24 09:04:57 +02:00
|
|
|
} else if (item.type() == WSMenuItem::Separator) {
|
2019-08-26 20:53:33 +02:00
|
|
|
Point p1(item.rect().translated(stripe_rect.width() + 4, 0).x(), item.rect().center().y() - 1);
|
|
|
|
Point p2(width - 7, item.rect().center().y() - 1);
|
2019-12-24 20:57:54 +01:00
|
|
|
painter.draw_line(p1, p2, palette.threed_shadow1());
|
|
|
|
painter.draw_line(p1.translated(0, 1), p2.translated(0, 1), palette.threed_highlight());
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 20:56:51 +13:00
|
|
|
WSMenuItem* WSMenu::hovered_item() const
|
|
|
|
{
|
|
|
|
if (m_hovered_item_index == -1)
|
|
|
|
return nullptr;
|
|
|
|
return const_cast<WSMenuItem*>(&item(m_hovered_item_index));
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:52:54 +13:00
|
|
|
void WSMenu::update_for_new_hovered_item()
|
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 14:11:49 +13:00
|
|
|
{
|
2020-01-18 20:56:51 +13:00
|
|
|
if (hovered_item() && hovered_item()->is_submenu()) {
|
|
|
|
WSMenuManager::the().close_everyone_not_in_lineage(*hovered_item()->submenu());
|
|
|
|
hovered_item()->submenu()->popup(hovered_item()->rect().top_right().translated(menu_window()->rect().location()), 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 14:11:49 +13:00
|
|
|
} else {
|
2020-01-08 13:19:31 +01:00
|
|
|
WSMenuManager::the().close_everyone_not_in_lineage(*this);
|
2020-01-12 15:52:54 +13:00
|
|
|
WSMenuManager::the().set_current_menu(this);
|
|
|
|
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 14:11:49 +13:00
|
|
|
}
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenu::open_hovered_item()
|
|
|
|
{
|
|
|
|
ASSERT(menu_window());
|
|
|
|
ASSERT(menu_window()->is_visible());
|
2020-01-18 20:56:51 +13:00
|
|
|
if (!hovered_item())
|
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 14:11:49 +13:00
|
|
|
return;
|
2020-01-18 20:56:51 +13:00
|
|
|
if (hovered_item()->is_enabled())
|
|
|
|
did_activate(*hovered_item());
|
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 14:11:49 +13:00
|
|
|
clear_hovered_item();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenu::decend_into_submenu_at_hovered_item()
|
|
|
|
{
|
2020-01-18 20:56:51 +13:00
|
|
|
ASSERT(hovered_item());
|
|
|
|
ASSERT(hovered_item()->is_submenu());
|
|
|
|
auto submenu = hovered_item()->submenu();
|
|
|
|
submenu->m_hovered_item_index = 0;
|
|
|
|
ASSERT(submenu->hovered_item()->type() != WSMenuItem::Separator);
|
2020-01-12 15:52:54 +13:00
|
|
|
submenu->update_for_new_hovered_item();
|
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 14:11:49 +13:00
|
|
|
m_in_submenu = true;
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:03:57 +13:00
|
|
|
void WSMenu::handle_mouse_move_event(const WSMouseEvent& mouse_event)
|
2019-02-11 09:47:10 +01:00
|
|
|
{
|
2020-01-19 21:28:38 +01:00
|
|
|
ASSERT(menu_window());
|
|
|
|
if (hovered_item() && hovered_item()->is_submenu()) {
|
2020-01-19 12:13:26 +13:00
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
auto item = *hovered_item();
|
|
|
|
auto submenu_top_left = item.rect().location() + Point { item.rect().width(), 0 };
|
|
|
|
auto submenu_bottom_left = submenu_top_left + Point { 0, item.submenu()->menu_window()->height() };
|
2020-01-19 12:13:26 +13:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
auto safe_hover_triangle = Gfx::Triangle { m_last_position_in_hover, submenu_top_left, submenu_bottom_left };
|
2020-01-19 21:28:38 +01:00
|
|
|
m_last_position_in_hover = mouse_event.position();
|
2020-01-19 12:13:26 +13:00
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
// Don't update the hovered item if mouse is moving towards a submenu
|
|
|
|
if (safe_hover_triangle.contains(mouse_event.position()))
|
2019-02-11 10:08:37 +01:00
|
|
|
return;
|
2020-01-19 21:28:38 +01: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 14:11:49 +13:00
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
int index = item_index_at(mouse_event.position());
|
|
|
|
if (m_hovered_item_index == index)
|
|
|
|
return;
|
|
|
|
m_hovered_item_index = index;
|
|
|
|
|
|
|
|
// FIXME: Tell parent menu (if it exists) that it is currently in a submenu
|
|
|
|
m_in_submenu = false;
|
|
|
|
update_for_new_hovered_item();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void WSMenu::event(Core::Event& event)
|
2020-01-19 21:28:38 +01:00
|
|
|
{
|
|
|
|
if (event.type() == WSEvent::MouseMove) {
|
2020-01-20 22:03:57 +13:00
|
|
|
handle_mouse_move_event(static_cast<const WSMouseEvent&>(event));
|
2019-02-11 10:55:02 +01:00
|
|
|
return;
|
2019-02-11 10:08:37 +01:00
|
|
|
}
|
2019-02-11 10:55:02 +01:00
|
|
|
|
2019-04-14 05:23:37 +02:00
|
|
|
if (event.type() == WSEvent::MouseUp) {
|
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 14:11:49 +13:00
|
|
|
open_hovered_item();
|
2019-02-11 10:55:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-04-22 01:15:47 +02:00
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
if (event.type() == WSEvent::MouseWheel && is_scrollable()) {
|
2020-01-20 22:03:57 +13:00
|
|
|
ASSERT(menu_window());
|
2020-01-19 21:28:38 +01:00
|
|
|
auto& mouse_event = static_cast<const WSMouseEvent&>(event);
|
|
|
|
m_scroll_offset += mouse_event.wheel_delta();
|
2020-01-20 22:03:57 +13:00
|
|
|
m_scroll_offset = clamp(m_scroll_offset, 0, m_max_scroll_offset);
|
|
|
|
|
|
|
|
int index = item_index_at(mouse_event.position());
|
|
|
|
if (m_hovered_item_index == index)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_hovered_item_index = index;
|
|
|
|
update_for_new_hovered_item();
|
2020-01-19 21:28:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 14:11:49 +13:00
|
|
|
if (event.type() == WSEvent::KeyDown) {
|
|
|
|
auto key = static_cast<WSKeyEvent&>(event).key();
|
|
|
|
|
|
|
|
if (!(key == Key_Up || key == Key_Down || key == Key_Left || key == Key_Right || key == Key_Return))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASSERT(menu_window());
|
|
|
|
ASSERT(menu_window()->is_visible());
|
|
|
|
|
|
|
|
// Default to the first item on key press if one has not been selected yet
|
2020-01-18 20:56:51 +13:00
|
|
|
if (!hovered_item()) {
|
|
|
|
m_hovered_item_index = 0;
|
2020-01-12 15:52:54 +13:00
|
|
|
update_for_new_hovered_item();
|
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 14:11:49 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the event for the submenu that we are currently in to handle
|
|
|
|
if (m_in_submenu && key != Key_Left) {
|
2020-01-18 20:56:51 +13:00
|
|
|
ASSERT(hovered_item()->is_submenu());
|
|
|
|
hovered_item()->submenu()->dispatch_event(event);
|
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 14:11:49 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == Key_Return) {
|
2020-01-18 20:56:51 +13:00
|
|
|
if (hovered_item()->is_submenu())
|
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 14:11:49 +13:00
|
|
|
decend_into_submenu_at_hovered_item();
|
|
|
|
else
|
|
|
|
open_hovered_item();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == Key_Up) {
|
|
|
|
ASSERT(m_items.at(0).type() != WSMenuItem::Separator);
|
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
if (is_scrollable() && m_hovered_item_index == 0)
|
|
|
|
return;
|
|
|
|
|
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 14:11:49 +13:00
|
|
|
do {
|
2020-01-30 23:06:01 +01:00
|
|
|
if (m_hovered_item_index == 0)
|
|
|
|
m_hovered_item_index = m_items.size() - 1;
|
|
|
|
else if (m_hovered_item_index < 0)
|
2020-01-20 21:56:43 +13:00
|
|
|
return;
|
2020-01-30 23:06:01 +01:00
|
|
|
else
|
|
|
|
--m_hovered_item_index;
|
2020-01-18 20:56:51 +13:00
|
|
|
} while (hovered_item()->type() == WSMenuItem::Separator);
|
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 14:11:49 +13:00
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
if (is_scrollable() && m_hovered_item_index < m_scroll_offset)
|
|
|
|
--m_scroll_offset;
|
|
|
|
|
2020-01-12 15:52:54 +13:00
|
|
|
update_for_new_hovered_item();
|
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 14:11:49 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == Key_Down) {
|
|
|
|
ASSERT(m_items.at(0).type() != WSMenuItem::Separator);
|
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
if (is_scrollable() && m_hovered_item_index == m_items.size() - 1)
|
|
|
|
return;
|
|
|
|
|
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 14:11:49 +13:00
|
|
|
do {
|
2020-01-30 23:06:01 +01:00
|
|
|
if (m_hovered_item_index == m_items.size() - 1)
|
|
|
|
m_hovered_item_index = 0;
|
|
|
|
else if (m_hovered_item_index > m_items.size() - 1)
|
2020-01-20 21:56:43 +13:00
|
|
|
return;
|
2020-01-30 23:06:01 +01:00
|
|
|
else
|
|
|
|
++m_hovered_item_index;
|
2020-01-18 20:56:51 +13:00
|
|
|
} while (hovered_item()->type() == WSMenuItem::Separator);
|
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 14:11:49 +13:00
|
|
|
|
2020-01-19 21:28:38 +01:00
|
|
|
if (is_scrollable() && m_hovered_item_index >= (m_scroll_offset + visible_item_count()))
|
|
|
|
++m_scroll_offset;
|
|
|
|
|
2020-01-12 15:52:54 +13:00
|
|
|
update_for_new_hovered_item();
|
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 14:11:49 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == Key_Left) {
|
|
|
|
if (!m_in_submenu)
|
|
|
|
return;
|
|
|
|
|
2020-01-18 20:56:51 +13:00
|
|
|
ASSERT(hovered_item()->is_submenu());
|
|
|
|
hovered_item()->submenu()->clear_hovered_item();
|
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 14:11:49 +13:00
|
|
|
m_in_submenu = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == Key_Right) {
|
2020-01-18 20:56:51 +13:00
|
|
|
if (hovered_item()->is_submenu())
|
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 14:11:49 +13:00
|
|
|
decend_into_submenu_at_hovered_item();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::Object::event(event);
|
2019-02-11 10:55:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 11:06:41 +01:00
|
|
|
void WSMenu::clear_hovered_item()
|
|
|
|
{
|
2020-01-18 20:56:51 +13:00
|
|
|
if (!hovered_item())
|
2019-02-11 11:06:41 +01:00
|
|
|
return;
|
2020-01-18 20:56:51 +13:00
|
|
|
m_hovered_item_index = -1;
|
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 14:11:49 +13:00
|
|
|
m_in_submenu = false;
|
2019-02-11 11:06:41 +01:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:55:02 +01:00
|
|
|
void WSMenu::did_activate(WSMenuItem& item)
|
|
|
|
{
|
2019-11-11 12:56:13 +01:00
|
|
|
if (item.type() == WSMenuItem::Type::Separator)
|
|
|
|
return;
|
|
|
|
|
2019-02-11 10:55:02 +01:00
|
|
|
if (on_item_activation)
|
|
|
|
on_item_activation(item);
|
2019-02-12 10:08:35 +01:00
|
|
|
|
2020-01-08 13:19:31 +01:00
|
|
|
WSMenuManager::the().close_bar();
|
2019-02-12 10:08:35 +01:00
|
|
|
|
2019-02-17 09:06:47 +01:00
|
|
|
if (m_client)
|
2019-12-02 09:33:37 +01:00
|
|
|
m_client->post_message(WindowClient::MenuItemActivated(m_menu_id, item.identifier()));
|
2019-02-11 10:08:37 +01:00
|
|
|
}
|
|
|
|
|
2019-04-12 02:53:27 +02:00
|
|
|
WSMenuItem* WSMenu::item_with_identifier(unsigned identifer)
|
|
|
|
{
|
|
|
|
for (auto& item : m_items) {
|
2019-07-24 09:04:57 +02:00
|
|
|
if (item.identifier() == identifer)
|
|
|
|
return &item;
|
2019-04-12 02:53:27 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
int WSMenu::item_index_at(const Gfx::Point& position)
|
2019-02-11 10:08:37 +01:00
|
|
|
{
|
2020-01-18 20:56:51 +13:00
|
|
|
int i = 0;
|
2019-02-11 10:08:37 +01:00
|
|
|
for (auto& item : m_items) {
|
2019-07-24 09:04:57 +02:00
|
|
|
if (item.rect().contains(position))
|
2020-01-18 20:56:51 +13:00
|
|
|
return i;
|
|
|
|
++i;
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
2020-01-18 20:56:51 +13:00
|
|
|
return -1;
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
2019-02-11 13:59:26 +01:00
|
|
|
|
|
|
|
void WSMenu::close()
|
|
|
|
{
|
2020-01-08 13:19:31 +01:00
|
|
|
WSMenuManager::the().close_menu_and_descendants(*this);
|
2019-04-12 17:10:30 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 20:24:26 +01:00
|
|
|
void WSMenu::redraw_if_theme_changed()
|
|
|
|
{
|
2020-01-08 13:19:31 +01:00
|
|
|
if (m_theme_index_at_last_paint != WSMenuManager::the().theme_index())
|
2019-12-23 20:24:26 +01:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
void WSMenu::popup(const Gfx::Point& position, bool is_submenu)
|
2019-04-12 17:10:30 +02:00
|
|
|
{
|
|
|
|
ASSERT(!is_empty());
|
2019-05-16 01:06:21 +02:00
|
|
|
|
2019-04-12 17:10:30 +02:00
|
|
|
auto& window = ensure_menu_window();
|
2019-12-23 20:24:26 +01:00
|
|
|
redraw_if_theme_changed();
|
|
|
|
|
2019-05-16 01:06:21 +02:00
|
|
|
const int margin = 30;
|
|
|
|
Point adjusted_pos = position;
|
2020-01-19 21:28:38 +01:00
|
|
|
|
|
|
|
if (adjusted_pos.x() + window.width() >= WSScreen::the().width() - margin) {
|
|
|
|
adjusted_pos = adjusted_pos.translated(-window.width(), 0);
|
2019-05-16 01:06:21 +02:00
|
|
|
}
|
2020-01-19 21:28:38 +01:00
|
|
|
if (adjusted_pos.y() + window.height() >= WSScreen::the().height() - margin) {
|
|
|
|
adjusted_pos = adjusted_pos.translated(0, -window.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adjusted_pos.y() < WSMenuManager::the().menubar_rect().height())
|
|
|
|
adjusted_pos.set_y(WSMenuManager::the().menubar_rect().height());
|
2019-05-16 01:06:21 +02:00
|
|
|
|
|
|
|
window.move_to(adjusted_pos);
|
2019-04-12 17:10:30 +02:00
|
|
|
window.set_visible(true);
|
2020-01-08 13:19:31 +01:00
|
|
|
WSMenuManager::the().set_current_menu(this, is_submenu);
|
2019-04-12 17:10:30 +02:00
|
|
|
}
|
2019-11-11 10:43:03 +01:00
|
|
|
|
|
|
|
bool WSMenu::is_menu_ancestor_of(const WSMenu& other) const
|
|
|
|
{
|
|
|
|
for (auto& item : m_items) {
|
|
|
|
if (!item.is_submenu())
|
|
|
|
continue;
|
|
|
|
auto& submenu = *const_cast<WSMenuItem&>(item).submenu();
|
|
|
|
if (&submenu == &other)
|
|
|
|
return true;
|
|
|
|
if (submenu.is_menu_ancestor_of(other))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|