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"
|
2019-07-18 10:15:00 +02:00
|
|
|
#include <LibDraw/CharacterBitmap.h>
|
|
|
|
#include <LibDraw/Font.h>
|
2019-08-26 18:54:44 +02:00
|
|
|
#include <LibDraw/GraphicsBitmap.h>
|
2019-07-18 10:15:00 +02:00
|
|
|
#include <LibDraw/Painter.h>
|
|
|
|
#include <LibDraw/StylePainter.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)
|
2019-08-18 12:05:33 +02:00
|
|
|
: CObject(client)
|
|
|
|
, 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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const Font& WSMenu::font() const
|
|
|
|
{
|
|
|
|
return Font::default_font();
|
|
|
|
}
|
|
|
|
|
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 = {
|
|
|
|
" "
|
|
|
|
" # "
|
|
|
|
" ## "
|
|
|
|
" ### "
|
|
|
|
" #### "
|
|
|
|
" ### "
|
|
|
|
" ## "
|
|
|
|
" # "
|
|
|
|
" "
|
|
|
|
};
|
|
|
|
|
2019-04-26 21:09:56 +02:00
|
|
|
static CharacterBitmap* s_checked_bitmap;
|
|
|
|
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;
|
|
|
|
static const int s_checkbox_or_icon_padding = 6;
|
2019-08-26 20:43:30 +02:00
|
|
|
static const int s_stripe_width = 23;
|
2019-04-26 21:09:56 +02:00
|
|
|
|
2019-02-11 09:47:10 +01:00
|
|
|
int WSMenu::width() const
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int WSMenu::height() const
|
|
|
|
{
|
|
|
|
if (m_items.is_empty())
|
|
|
|
return 0;
|
2019-08-27 13:49:48 +02:00
|
|
|
return (m_items.last().rect().bottom() + 1) + frame_thickness();
|
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()
|
|
|
|
{
|
2019-04-18 19:58:25 +02:00
|
|
|
int width = this->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);
|
|
|
|
}
|
|
|
|
|
2019-09-22 00:17:53 +02:00
|
|
|
auto window = WSWindow::construct(*this, WSWindowType::Menu);
|
2019-04-18 19:58:25 +02:00
|
|
|
window->set_rect(0, 0, width, height());
|
2019-02-11 09:47:10 +01:00
|
|
|
m_menu_window = move(window);
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
return *m_menu_window;
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
Painter painter(*menu_window()->backing_store());
|
2019-02-11 09:47:10 +01:00
|
|
|
|
2019-06-07 11:47:19 +02:00
|
|
|
Rect rect { {}, menu_window()->size() };
|
2019-12-24 20:57:54 +01:00
|
|
|
painter.fill_rect(rect.shrunken(6, 6), palette.menu_base());
|
|
|
|
StylePainter::paint_window_frame(painter, rect, palette);
|
2019-04-18 19:58:25 +02:00
|
|
|
int width = this->width();
|
2019-02-11 09:47:10 +01:00
|
|
|
|
2019-04-26 21:09:56 +02:00
|
|
|
if (!s_checked_bitmap)
|
|
|
|
s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
|
|
|
|
|
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
|
|
|
|
2019-08-26 20:43:30 +02:00
|
|
|
Rect stripe_rect { frame_thickness(), frame_thickness(), s_stripe_width, 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
|
|
|
|
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-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
|
|
|
}
|
2019-08-26 20:43:30 +02:00
|
|
|
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()) {
|
|
|
|
Rect radio_rect { item.rect().x() + 5, 0, 12, 12 };
|
|
|
|
radio_rect.center_vertically_within(text_rect);
|
|
|
|
StylePainter::paint_radio_button(painter, radio_rect, palette, item.is_checked(), false);
|
|
|
|
} else {
|
|
|
|
Rect checkmark_rect { item.rect().x() + 7, 0, s_checked_bitmap_width, s_checked_bitmap_height };
|
|
|
|
checkmark_rect.center_vertically_within(text_rect);
|
|
|
|
Rect checkbox_rect = checkmark_rect.inflated(4, 4);
|
|
|
|
painter.fill_rect(checkbox_rect, palette.base());
|
|
|
|
StylePainter::paint_frame(painter, checkbox_rect, palette, FrameShape::Container, FrameShadow::Sunken, 2);
|
|
|
|
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()) {
|
2019-08-26 20:43:30 +02:00
|
|
|
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
|
|
|
}
|
2019-07-24 09:04:57 +02:00
|
|
|
painter.draw_text(text_rect, item.text(), TextAlignment::CenterLeft, text_color);
|
|
|
|
if (!item.shortcut_text().is_empty()) {
|
|
|
|
painter.draw_text(item.rect().translated(-right_padding(), 0), item.shortcut_text(), TextAlignment::CenterRight, text_color);
|
2019-03-02 10:04:49 +01:00
|
|
|
}
|
2019-08-28 21:11:53 +02:00
|
|
|
if (item.is_submenu()) {
|
|
|
|
static auto& submenu_arrow_bitmap = CharacterBitmap::create_from_ascii(s_submenu_arrow_bitmap_data, s_submenu_arrow_bitmap_width, s_submenu_arrow_bitmap_height).leak_ref();
|
|
|
|
Rect submenu_arrow_rect {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-14 05:23:37 +02:00
|
|
|
void WSMenu::event(CEvent& event)
|
2019-02-11 09:47:10 +01:00
|
|
|
{
|
2019-04-14 05:23:37 +02:00
|
|
|
if (event.type() == WSEvent::MouseMove) {
|
2019-08-19 14:32:18 +03:00
|
|
|
ASSERT(menu_window());
|
2020-01-18 20:56:51 +13:00
|
|
|
int index = item_index_at(static_cast<const WSMouseEvent&>(event).position());
|
|
|
|
if (m_hovered_item_index == index)
|
2019-02-11 10:08:37 +01:00
|
|
|
return;
|
2020-01-18 20:56:51 +13:00
|
|
|
m_hovered_item_index = index;
|
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
|
|
|
// FIXME: Tell parent menu (if it exists) that it is currently in a 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
|
|
|
m_in_submenu = false;
|
2020-01-12 15:52:54 +13:00
|
|
|
update_for_new_hovered_item();
|
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
|
|
|
|
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);
|
|
|
|
|
|
|
|
do {
|
2020-01-18 20:56:51 +13:00
|
|
|
m_hovered_item_index--;
|
|
|
|
if (m_hovered_item_index < 0)
|
|
|
|
m_hovered_item_index = m_items.size() - 1;
|
|
|
|
} 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-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);
|
|
|
|
|
|
|
|
do {
|
2020-01-18 20:56:51 +13:00
|
|
|
m_hovered_item_index++;
|
|
|
|
if (m_hovered_item_index >= m_items.size())
|
|
|
|
m_hovered_item_index = 0;
|
|
|
|
} 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-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;
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 01:15:47 +02:00
|
|
|
CObject::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-01-18 20:56:51 +13:00
|
|
|
int WSMenu::item_index_at(const 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();
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:11:53 +02:00
|
|
|
void WSMenu::popup(const 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-11 14:38:09 +01:00
|
|
|
if (window.height() >= WSScreen::the().height()) {
|
|
|
|
adjusted_pos.set_y(0);
|
|
|
|
} else {
|
|
|
|
if (adjusted_pos.x() + window.width() >= WSScreen::the().width() - margin) {
|
|
|
|
adjusted_pos = adjusted_pos.translated(-window.width(), 0);
|
|
|
|
}
|
|
|
|
if (adjusted_pos.y() + window.height() >= WSScreen::the().height() - margin) {
|
|
|
|
adjusted_pos = adjusted_pos.translated(0, -window.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;
|
|
|
|
}
|