2020-01-05 05:05:41 +03:00
|
|
|
#include <AK/FileSystemPath.h>
|
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <LibCore/CDirIterator.h>
|
2019-07-18 11:15:00 +03:00
|
|
|
#include <LibDraw/Font.h>
|
2020-01-05 05:05:41 +03:00
|
|
|
#include <LibDraw/PNGLoader.h>
|
2019-07-18 11:15:00 +03:00
|
|
|
#include <LibDraw/Painter.h>
|
2019-07-18 11:46:41 +03:00
|
|
|
#include <WindowServer/WSMenuManager.h>
|
2019-12-05 21:59:11 +03:00
|
|
|
#include <WindowServer/WSScreen.h>
|
2019-06-21 09:19:43 +03:00
|
|
|
#include <WindowServer/WSWindowManager.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-01-05 05:05:41 +03:00
|
|
|
//#define DEBUG_MENUS
|
|
|
|
|
2020-01-08 15:19:31 +03:00
|
|
|
static WSMenuManager* s_the;
|
|
|
|
|
|
|
|
WSMenuManager& WSMenuManager::the()
|
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2019-07-18 09:32:44 +03:00
|
|
|
WSMenuManager::WSMenuManager()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
2020-01-08 15:19:31 +03:00
|
|
|
s_the = this;
|
2019-06-21 09:19:43 +03:00
|
|
|
m_username = getlogin();
|
2019-12-15 20:07:48 +03:00
|
|
|
m_needs_window_resize = true;
|
2020-01-05 05:05:41 +03:00
|
|
|
|
|
|
|
HashTable<String> seen_app_categories;
|
|
|
|
{
|
|
|
|
CDirIterator dt("/res/apps", CDirIterator::SkipDots);
|
|
|
|
while (dt.has_next()) {
|
|
|
|
auto af_name = dt.next_path();
|
|
|
|
auto af_path = String::format("/res/apps/%s", af_name.characters());
|
|
|
|
auto af = CConfigFile::open(af_path);
|
|
|
|
if (!af->has_key("App", "Name") || !af->has_key("App", "Executable"))
|
|
|
|
continue;
|
|
|
|
auto app_name = af->read_entry("App", "Name");
|
|
|
|
auto app_executable = af->read_entry("App", "Executable");
|
|
|
|
auto app_category = af->read_entry("App", "Category");
|
|
|
|
auto app_icon_path = af->read_entry("Icons", "16x16");
|
|
|
|
m_apps.append({ app_executable, app_name, app_icon_path, app_category });
|
|
|
|
seen_app_categories.set(app_category);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> sorted_app_categories;
|
|
|
|
for (auto& category : seen_app_categories)
|
|
|
|
sorted_app_categories.append(category);
|
|
|
|
quick_sort(sorted_app_categories.begin(), sorted_app_categories.end(), [](auto& a, auto& b) { return a < b; });
|
|
|
|
|
|
|
|
u8 system_menu_name[] = { 0xc3, 0xb8, 0 };
|
|
|
|
m_system_menu = WSMenu::construct(nullptr, -1, String((const char*)system_menu_name));
|
|
|
|
|
|
|
|
// First we construct all the necessary app category submenus.
|
|
|
|
for (const auto& category : sorted_app_categories) {
|
|
|
|
|
|
|
|
if (m_app_category_menus.contains(category))
|
|
|
|
continue;
|
|
|
|
auto category_menu = WSMenu::construct(nullptr, 5000 + m_app_category_menus.size(), category);
|
|
|
|
category_menu->on_item_activation = [this](auto& item) {
|
|
|
|
if (item.identifier() >= 1 && item.identifier() <= 1u + m_apps.size() - 1) {
|
|
|
|
if (fork() == 0) {
|
|
|
|
const auto& bin = m_apps[item.identifier() - 1].executable;
|
|
|
|
execl(bin.characters(), bin.characters(), nullptr);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto item = make<WSMenuItem>(*m_system_menu, -1, category);
|
|
|
|
item->set_submenu_id(category_menu->menu_id());
|
|
|
|
m_system_menu->add_item(move(item));
|
|
|
|
m_app_category_menus.set(category, move(category_menu));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we create and insert all the app menu items into the right place.
|
|
|
|
int app_identifier = 1;
|
|
|
|
for (const auto& app : m_apps) {
|
|
|
|
auto parent_menu = m_app_category_menus.get(app.category).value_or(*m_system_menu);
|
|
|
|
parent_menu->add_item(make<WSMenuItem>(*m_system_menu, app_identifier++, app.name, String(), true, false, false, load_png(app.icon_path)));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, WSMenuItem::Separator));
|
|
|
|
|
|
|
|
m_themes_menu = WSMenu::construct(nullptr, 9000, "Themes");
|
|
|
|
|
|
|
|
auto themes_menu_item = make<WSMenuItem>(*m_system_menu, 100, "Themes");
|
|
|
|
themes_menu_item->set_submenu_id(m_themes_menu->menu_id());
|
|
|
|
m_system_menu->add_item(move(themes_menu_item));
|
|
|
|
|
|
|
|
{
|
|
|
|
CDirIterator dt("/res/themes", CDirIterator::SkipDots);
|
|
|
|
while (dt.has_next()) {
|
|
|
|
auto theme_name = dt.next_path();
|
|
|
|
auto theme_path = String::format("/res/themes/%s", theme_name.characters());
|
|
|
|
m_themes.append({ FileSystemPath(theme_name).title(), theme_path });
|
|
|
|
}
|
|
|
|
quick_sort(m_themes.begin(), m_themes.end(), [](auto& a, auto& b) { return a.name < b.name; });
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int theme_identifier = 9000;
|
|
|
|
for (auto& theme : m_themes) {
|
|
|
|
m_themes_menu->add_item(make<WSMenuItem>(*m_themes_menu, theme_identifier++, theme.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_themes_menu->on_item_activation = [this](WSMenuItem& item) {
|
|
|
|
auto& theme = m_themes[(int)item.identifier() - 9000];
|
|
|
|
WSWindowManager::the().update_theme(theme.path, theme.name);
|
|
|
|
++m_theme_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, WSMenuItem::Separator));
|
|
|
|
m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, 100, "Reload WM Config File"));
|
|
|
|
|
|
|
|
m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, WSMenuItem::Separator));
|
|
|
|
m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, 200, "About...", String(), true, false, false, load_png("/res/icons/16x16/ladybug.png")));
|
|
|
|
m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, WSMenuItem::Separator));
|
|
|
|
m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, 300, "Shutdown..."));
|
|
|
|
m_system_menu->on_item_activation = [this](WSMenuItem& item) {
|
|
|
|
if (item.identifier() >= 1 && item.identifier() <= 1u + m_apps.size() - 1) {
|
|
|
|
if (fork() == 0) {
|
|
|
|
const auto& bin = m_apps[item.identifier() - 1].executable;
|
|
|
|
execl(bin.characters(), bin.characters(), nullptr);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (item.identifier()) {
|
|
|
|
case 100:
|
|
|
|
WSWindowManager::the().reload_config(true);
|
|
|
|
break;
|
|
|
|
case 200:
|
|
|
|
if (fork() == 0) {
|
|
|
|
execl("/bin/About", "/bin/About", nullptr);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case 300:
|
|
|
|
if (fork() == 0) {
|
|
|
|
execl("/bin/SystemDialog", "/bin/SystemDialog", "--shutdown", nullptr);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_MENUS
|
|
|
|
dbg() << "WSMenu 1 item activated: " << item.text();
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE: This ensures that the system menu has the correct dimensions.
|
|
|
|
set_current_menubar(nullptr);
|
2019-06-21 09:19:43 +03:00
|
|
|
|
2020-01-08 15:19:31 +03:00
|
|
|
m_window = WSWindow::construct(*this, WSWindowType::Menubar);
|
|
|
|
m_window->set_rect(menubar_rect());
|
2019-06-21 09:19:43 +03:00
|
|
|
}
|
|
|
|
|
2020-01-08 15:19:31 +03:00
|
|
|
WSMenuManager::~WSMenuManager()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:11:53 +03:00
|
|
|
bool WSMenuManager::is_open(const WSMenu& menu) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_open_menu_stack.size(); ++i) {
|
|
|
|
if (&menu == m_open_menu_stack[i].ptr())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-18 09:32:44 +03:00
|
|
|
void WSMenuManager::draw()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
|
|
|
auto& wm = WSWindowManager::the();
|
2019-12-29 02:47:49 +03:00
|
|
|
auto palette = wm.palette();
|
2019-12-05 21:59:11 +03:00
|
|
|
auto menubar_rect = this->menubar_rect();
|
2019-06-21 09:19:43 +03:00
|
|
|
|
2019-10-30 13:06:32 +03:00
|
|
|
if (m_needs_window_resize) {
|
2019-12-15 20:07:48 +03:00
|
|
|
int username_width = Font::default_bold_font().width(m_username);
|
|
|
|
|
|
|
|
m_username_rect = {
|
|
|
|
menubar_rect.right() - menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
|
|
|
|
menubar_rect.y(),
|
|
|
|
username_width,
|
|
|
|
menubar_rect.height()
|
|
|
|
};
|
|
|
|
|
2019-12-27 21:32:52 +03:00
|
|
|
int right_edge_x = m_username_rect.left() - 4;
|
2019-12-15 20:07:48 +03:00
|
|
|
for (auto& existing_applet : m_applets) {
|
2019-12-16 17:33:42 +03:00
|
|
|
if (!existing_applet)
|
2019-12-15 20:07:48 +03:00
|
|
|
continue;
|
2019-12-16 17:33:42 +03:00
|
|
|
|
2019-12-15 20:07:48 +03:00
|
|
|
Rect new_applet_rect(right_edge_x - existing_applet->size().width(), 0, existing_applet->size().width(), existing_applet->size().height());
|
|
|
|
Rect dummy_menubar_rect(0, 0, 0, 18);
|
|
|
|
new_applet_rect.center_vertically_within(dummy_menubar_rect);
|
|
|
|
|
2019-12-16 17:33:42 +03:00
|
|
|
existing_applet->set_rect_in_menubar(new_applet_rect);
|
2019-12-15 20:07:48 +03:00
|
|
|
right_edge_x = existing_applet->rect_in_menubar().x() - 4;
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:06:32 +03:00
|
|
|
m_window->set_rect(menubar_rect);
|
|
|
|
m_needs_window_resize = false;
|
|
|
|
}
|
|
|
|
|
2019-06-21 09:19:43 +03:00
|
|
|
Painter painter(*window().backing_store());
|
|
|
|
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.fill_rect(menubar_rect, palette.window());
|
|
|
|
painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow1());
|
2019-06-21 09:19:43 +03:00
|
|
|
int index = 0;
|
2020-01-05 05:05:41 +03:00
|
|
|
for_each_active_menubar_menu([&](WSMenu& menu) {
|
2019-12-24 22:57:54 +03:00
|
|
|
Color text_color = palette.window_text();
|
2019-08-28 22:11:53 +03:00
|
|
|
if (is_open(menu)) {
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.fill_rect(menu.rect_in_menubar(), palette.menu_selection());
|
|
|
|
painter.draw_rect(menu.rect_in_menubar(), palette.menu_selection().darkened());
|
2019-06-21 09:19:43 +03:00
|
|
|
text_color = Color::White;
|
|
|
|
}
|
|
|
|
painter.draw_text(
|
|
|
|
menu.text_rect_in_menubar(),
|
|
|
|
menu.name(),
|
|
|
|
index == 1 ? wm.app_menu_font() : wm.menu_font(),
|
|
|
|
TextAlignment::CenterLeft,
|
|
|
|
text_color);
|
|
|
|
++index;
|
2019-12-16 17:33:42 +03:00
|
|
|
return IterationDecision::Continue;
|
2019-06-21 09:19:43 +03:00
|
|
|
});
|
|
|
|
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.draw_text(m_username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, palette.window_text());
|
2019-06-21 09:19:43 +03:00
|
|
|
|
2019-12-05 21:36:01 +03:00
|
|
|
for (auto& applet : m_applets) {
|
|
|
|
if (!applet)
|
|
|
|
continue;
|
|
|
|
draw_applet(*applet);
|
|
|
|
}
|
2019-06-21 09:19:43 +03:00
|
|
|
}
|
|
|
|
|
2019-07-18 09:32:44 +03:00
|
|
|
void WSMenuManager::tick_clock()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2019-07-18 09:32:44 +03:00
|
|
|
void WSMenuManager::refresh()
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
|
|
|
if (!m_window)
|
|
|
|
return;
|
|
|
|
draw();
|
|
|
|
window().invalidate();
|
|
|
|
}
|
|
|
|
|
2019-07-18 09:32:44 +03:00
|
|
|
void WSMenuManager::event(CEvent& event)
|
2019-06-21 09:19:43 +03:00
|
|
|
{
|
2019-07-21 11:23:21 +03:00
|
|
|
if (WSWindowManager::the().active_window_is_modal())
|
|
|
|
return CObject::event(event);
|
|
|
|
|
2019-06-21 16:02:11 +03:00
|
|
|
if (event.type() == WSEvent::MouseMove || event.type() == WSEvent::MouseUp || event.type() == WSEvent::MouseDown || event.type() == WSEvent::MouseWheel) {
|
2019-12-16 17:33:42 +03:00
|
|
|
|
2019-06-21 16:02:11 +03:00
|
|
|
auto& mouse_event = static_cast<WSMouseEvent&>(event);
|
2020-01-05 05:05:41 +03:00
|
|
|
for_each_active_menubar_menu([&](WSMenu& menu) {
|
2019-06-21 16:02:11 +03:00
|
|
|
if (menu.rect_in_menubar().contains(mouse_event.position())) {
|
|
|
|
handle_menu_mouse_event(menu, mouse_event);
|
2019-12-16 17:33:42 +03:00
|
|
|
return IterationDecision::Break;
|
2019-06-21 16:02:11 +03:00
|
|
|
}
|
2019-12-16 17:33:42 +03:00
|
|
|
return IterationDecision::Continue;
|
2019-06-21 16:02:11 +03:00
|
|
|
});
|
2019-11-23 00:15:39 +03:00
|
|
|
|
2019-12-16 17:33:42 +03:00
|
|
|
for (auto& applet : m_applets) {
|
|
|
|
if (!applet)
|
|
|
|
continue;
|
|
|
|
if (!applet->rect_in_menubar().contains(mouse_event.position()))
|
|
|
|
continue;
|
|
|
|
auto local_event = mouse_event.translated(-applet->rect_in_menubar().location());
|
|
|
|
applet->event(local_event);
|
2019-11-23 00:15:39 +03:00
|
|
|
}
|
2019-06-21 16:02:11 +03:00
|
|
|
}
|
WSMenu: Support menu navigation through key presses
Add event handling for key presses for navigating a menu. The currently
hovered menu item is tracked through an index which is either
incremented or decremented on up or down arrow key presses, changing the
hovered item.
Whenever there is a mouse move event, we ensure that the current index
matches the currently hovered item so that the mouse and keyboard do not
get out of sync.
If the right key is pressed, and we are on a submenu menu item, we
'enter' that submenu. While we are currently in a submenu, we forward
all keypress events to that submenu for handling. This allows us to
traverse the heirachy of a menu. While in a submenu, if the left key is
pressed, we leave that submenu and start handling the keypresses
ourselves again.
There is currently a small issue where the mouse hover and key hover can
get out of sync. The mouse can be traversing a submenu, but the parent
menu has no idea that the mouse has 'entered' a submenu, so will handle
the key presses itself, instead of forwarding them to the submenu. One
potential fix for this is for a menu to tell its menu parent that the
submenu is being traversed.
2020-01-03 04:11:49 +03:00
|
|
|
|
|
|
|
if (event.type() == WSEvent::KeyDown) {
|
|
|
|
for_each_active_menubar_menu([&](WSMenu& menu) {
|
|
|
|
if (is_open(menu))
|
|
|
|
menu.dispatch_event(event);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-21 09:19:43 +03:00
|
|
|
return CObject::event(event);
|
|
|
|
}
|
2019-06-21 16:02:11 +03:00
|
|
|
|
2019-07-18 09:32:44 +03:00
|
|
|
void WSMenuManager::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& event)
|
2019-06-21 16:02:11 +03:00
|
|
|
{
|
2019-08-28 22:11:53 +03:00
|
|
|
bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove
|
2019-10-30 13:06:32 +03:00
|
|
|
&& !m_open_menu_stack.is_empty()
|
WSMenu: Support menu navigation through key presses
Add event handling for key presses for navigating a menu. The currently
hovered menu item is tracked through an index which is either
incremented or decremented on up or down arrow key presses, changing the
hovered item.
Whenever there is a mouse move event, we ensure that the current index
matches the currently hovered item so that the mouse and keyboard do not
get out of sync.
If the right key is pressed, and we are on a submenu menu item, we
'enter' that submenu. While we are currently in a submenu, we forward
all keypress events to that submenu for handling. This allows us to
traverse the heirachy of a menu. While in a submenu, if the left key is
pressed, we leave that submenu and start handling the keypresses
ourselves again.
There is currently a small issue where the mouse hover and key hover can
get out of sync. The mouse can be traversing a submenu, but the parent
menu has no idea that the mouse has 'entered' a submenu, so will handle
the key presses itself, instead of forwarding them to the submenu. One
potential fix for this is for a menu to tell its menu parent that the
submenu is being traversed.
2020-01-03 04:11:49 +03:00
|
|
|
&& (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr());
|
2019-06-21 16:02:11 +03:00
|
|
|
bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
|
2019-11-11 14:21:57 +03:00
|
|
|
bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
|
2019-06-21 16:02:11 +03:00
|
|
|
|
2019-11-11 14:54:23 +03:00
|
|
|
if (is_mousedown_with_left_button)
|
|
|
|
m_bar_open = !m_bar_open;
|
|
|
|
|
|
|
|
if (should_open_menu && m_bar_open) {
|
WSMenu: Support menu navigation through key presses
Add event handling for key presses for navigating a menu. The currently
hovered menu item is tracked through an index which is either
incremented or decremented on up or down arrow key presses, changing the
hovered item.
Whenever there is a mouse move event, we ensure that the current index
matches the currently hovered item so that the mouse and keyboard do not
get out of sync.
If the right key is pressed, and we are on a submenu menu item, we
'enter' that submenu. While we are currently in a submenu, we forward
all keypress events to that submenu for handling. This allows us to
traverse the heirachy of a menu. While in a submenu, if the left key is
pressed, we leave that submenu and start handling the keypresses
ourselves again.
There is currently a small issue where the mouse hover and key hover can
get out of sync. The mouse can be traversing a submenu, but the parent
menu has no idea that the mouse has 'entered' a submenu, so will handle
the key presses itself, instead of forwarding them to the submenu. One
potential fix for this is for a menu to tell its menu parent that the
submenu is being traversed.
2020-01-03 04:11:49 +03:00
|
|
|
open_menu(menu);
|
2019-06-21 16:02:11 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-11-11 14:54:23 +03:00
|
|
|
|
|
|
|
if (!m_bar_open)
|
2019-11-11 14:21:57 +03:00
|
|
|
close_everyone();
|
2019-06-21 16:02:11 +03:00
|
|
|
}
|
2019-10-30 13:06:32 +03:00
|
|
|
|
|
|
|
void WSMenuManager::set_needs_window_resize()
|
|
|
|
{
|
|
|
|
m_needs_window_resize = true;
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
|
|
|
|
2020-01-05 05:05:41 +03:00
|
|
|
void WSMenuManager::close_all_menus_from_client(Badge<WSClientConnection>, WSClientConnection& client)
|
2020-01-02 22:03:32 +03:00
|
|
|
{
|
|
|
|
if (m_open_menu_stack.is_empty())
|
|
|
|
return;
|
|
|
|
if (m_open_menu_stack.first()->client() != &client)
|
|
|
|
return;
|
|
|
|
close_everyone();
|
|
|
|
}
|
|
|
|
|
2019-11-11 14:21:57 +03:00
|
|
|
void WSMenuManager::close_everyone()
|
|
|
|
{
|
|
|
|
for (auto& menu : m_open_menu_stack) {
|
|
|
|
if (menu && menu->menu_window())
|
|
|
|
menu->menu_window()->set_visible(false);
|
2020-01-03 04:09:12 +03:00
|
|
|
menu->clear_hovered_item();
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
|
|
|
m_open_menu_stack.clear();
|
2019-11-11 14:54:23 +03:00
|
|
|
m_current_menu = nullptr;
|
2019-11-11 14:21:57 +03:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenuManager::close_everyone_not_in_lineage(WSMenu& menu)
|
|
|
|
{
|
|
|
|
Vector<WSMenu*> menus_to_close;
|
|
|
|
for (auto& open_menu : m_open_menu_stack) {
|
|
|
|
if (!open_menu)
|
|
|
|
continue;
|
|
|
|
if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
|
|
|
|
continue;
|
|
|
|
menus_to_close.append(open_menu);
|
|
|
|
}
|
|
|
|
close_menus(menus_to_close);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenuManager::close_menus(const Vector<WSMenu*>& menus)
|
|
|
|
{
|
|
|
|
for (auto& menu : menus) {
|
|
|
|
if (menu == m_current_menu)
|
|
|
|
m_current_menu = nullptr;
|
|
|
|
if (menu->menu_window())
|
|
|
|
menu->menu_window()->set_visible(false);
|
2020-01-03 04:09:12 +03:00
|
|
|
menu->clear_hovered_item();
|
2019-11-11 14:21:57 +03:00
|
|
|
m_open_menu_stack.remove_first_matching([&](auto& entry) {
|
|
|
|
return entry == menu;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void collect_menu_subtree(WSMenu& menu, Vector<WSMenu*>& menus)
|
|
|
|
{
|
|
|
|
menus.append(&menu);
|
|
|
|
for (int i = 0; i < menu.item_count(); ++i) {
|
|
|
|
auto& item = menu.item(i);
|
|
|
|
if (!item.is_submenu())
|
|
|
|
continue;
|
|
|
|
collect_menu_subtree(*const_cast<WSMenuItem&>(item).submenu(), menus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenuManager::close_menu_and_descendants(WSMenu& menu)
|
|
|
|
{
|
|
|
|
Vector<WSMenu*> menus_to_close;
|
|
|
|
collect_menu_subtree(menu, menus_to_close);
|
|
|
|
close_menus(menus_to_close);
|
|
|
|
}
|
|
|
|
|
WSMenu: Support menu navigation through key presses
Add event handling for key presses for navigating a menu. The currently
hovered menu item is tracked through an index which is either
incremented or decremented on up or down arrow key presses, changing the
hovered item.
Whenever there is a mouse move event, we ensure that the current index
matches the currently hovered item so that the mouse and keyboard do not
get out of sync.
If the right key is pressed, and we are on a submenu menu item, we
'enter' that submenu. While we are currently in a submenu, we forward
all keypress events to that submenu for handling. This allows us to
traverse the heirachy of a menu. While in a submenu, if the left key is
pressed, we leave that submenu and start handling the keypresses
ourselves again.
There is currently a small issue where the mouse hover and key hover can
get out of sync. The mouse can be traversing a submenu, but the parent
menu has no idea that the mouse has 'entered' a submenu, so will handle
the key presses itself, instead of forwarding them to the submenu. One
potential fix for this is for a menu to tell its menu parent that the
submenu is being traversed.
2020-01-03 04:11:49 +03:00
|
|
|
void WSMenuManager::open_menu(WSMenu& menu)
|
|
|
|
{
|
|
|
|
if (is_open(menu))
|
|
|
|
return;
|
|
|
|
if (!menu.is_empty()) {
|
|
|
|
menu.redraw_if_theme_changed();
|
|
|
|
auto& menu_window = menu.ensure_menu_window();
|
|
|
|
menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
|
|
|
|
menu_window.set_visible(true);
|
|
|
|
}
|
|
|
|
set_current_menu(&menu);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2019-11-11 14:21:57 +03:00
|
|
|
void WSMenuManager::set_current_menu(WSMenu* menu, bool is_submenu)
|
|
|
|
{
|
2020-01-12 06:01:15 +03:00
|
|
|
if (!is_submenu) {
|
|
|
|
if (menu)
|
|
|
|
close_everyone_not_in_lineage(*menu);
|
|
|
|
else
|
|
|
|
close_everyone();
|
|
|
|
}
|
2020-01-09 09:58:55 +03:00
|
|
|
|
|
|
|
if (!menu) {
|
|
|
|
m_current_menu = nullptr;
|
|
|
|
return;
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
2020-01-09 09:58:55 +03:00
|
|
|
|
|
|
|
m_open_menu_stack.append(menu->make_weak_ptr());
|
|
|
|
m_current_menu = menu->make_weak_ptr();
|
2019-11-11 14:21:57 +03:00
|
|
|
}
|
2019-11-11 14:54:23 +03:00
|
|
|
|
|
|
|
void WSMenuManager::close_bar()
|
|
|
|
{
|
|
|
|
close_everyone();
|
|
|
|
m_bar_open = false;
|
|
|
|
}
|
2019-12-05 21:36:01 +03:00
|
|
|
|
2019-12-16 17:05:45 +03:00
|
|
|
void WSMenuManager::add_applet(WSWindow& applet)
|
2019-12-05 21:36:01 +03:00
|
|
|
{
|
2019-12-27 21:32:52 +03:00
|
|
|
int right_edge_x = m_username_rect.left() - 4;
|
2019-12-05 21:36:01 +03:00
|
|
|
for (auto& existing_applet : m_applets) {
|
|
|
|
if (existing_applet)
|
|
|
|
right_edge_x = existing_applet->rect_in_menubar().x() - 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect new_applet_rect(right_edge_x - applet.size().width(), 0, applet.size().width(), applet.size().height());
|
|
|
|
Rect dummy_menubar_rect(0, 0, 0, 18);
|
|
|
|
new_applet_rect.center_vertically_within(dummy_menubar_rect);
|
|
|
|
|
|
|
|
applet.set_rect_in_menubar(new_applet_rect);
|
|
|
|
m_applets.append(applet.make_weak_ptr());
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:05:45 +03:00
|
|
|
void WSMenuManager::remove_applet(WSWindow& applet)
|
2019-12-05 21:36:01 +03:00
|
|
|
{
|
|
|
|
m_applets.remove_first_matching([&](auto& entry) {
|
|
|
|
return &applet == entry.ptr();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:05:45 +03:00
|
|
|
void WSMenuManager::draw_applet(const WSWindow& applet)
|
2019-12-05 21:36:01 +03:00
|
|
|
{
|
2019-12-16 17:05:45 +03:00
|
|
|
if (!applet.backing_store())
|
2019-12-05 21:36:01 +03:00
|
|
|
return;
|
|
|
|
Painter painter(*window().backing_store());
|
2019-12-29 23:18:38 +03:00
|
|
|
painter.fill_rect(applet.rect_in_menubar(), WSWindowManager::the().palette().window());
|
2019-12-16 17:05:45 +03:00
|
|
|
painter.blit(applet.rect_in_menubar().location(), *applet.backing_store(), applet.backing_store()->rect());
|
2019-12-05 21:36:01 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 17:05:45 +03:00
|
|
|
void WSMenuManager::invalidate_applet(const WSWindow& applet, const Rect& rect)
|
2019-12-05 21:36:01 +03:00
|
|
|
{
|
|
|
|
draw_applet(applet);
|
2019-12-30 03:18:38 +03:00
|
|
|
window().invalidate(rect.translated(applet.rect_in_menubar().location()));
|
2019-12-05 21:36:01 +03:00
|
|
|
}
|
2019-12-05 21:59:11 +03:00
|
|
|
|
|
|
|
Rect WSMenuManager::menubar_rect() const
|
|
|
|
{
|
|
|
|
return { 0, 0, WSScreen::the().rect().width(), 18 };
|
|
|
|
}
|
2020-01-05 05:05:41 +03:00
|
|
|
|
|
|
|
WSMenu* WSMenuManager::find_internal_menu_by_id(int menu_id)
|
|
|
|
{
|
|
|
|
if (m_themes_menu->menu_id() == menu_id)
|
|
|
|
return m_themes_menu.ptr();
|
|
|
|
for (auto& it : m_app_category_menus) {
|
|
|
|
if (menu_id == it.value->menu_id())
|
|
|
|
return it.value;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenuManager::set_current_menubar(WSMenuBar* menubar)
|
|
|
|
{
|
|
|
|
if (menubar)
|
|
|
|
m_current_menubar = menubar->make_weak_ptr();
|
|
|
|
else
|
|
|
|
m_current_menubar = nullptr;
|
|
|
|
#ifdef DEBUG_MENUS
|
|
|
|
dbg() << "[WM] Current menubar is now " << menubar;
|
|
|
|
#endif
|
|
|
|
Point next_menu_location { WSMenuManager::menubar_menu_margin() / 2, 0 };
|
|
|
|
int index = 0;
|
|
|
|
for_each_active_menubar_menu([&](WSMenu& menu) {
|
|
|
|
int text_width = index == 1 ? Font::default_bold_font().width(menu.name()) : Font::default_font().width(menu.name());
|
|
|
|
menu.set_rect_in_menubar({ next_menu_location.x() - WSMenuManager::menubar_menu_margin() / 2, 0, text_width + WSMenuManager::menubar_menu_margin(), menubar_rect().height() - 1 });
|
|
|
|
menu.set_text_rect_in_menubar({ next_menu_location, { text_width, menubar_rect().height() } });
|
|
|
|
next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
|
|
|
|
++index;
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenuManager::close_menubar(WSMenuBar& menubar)
|
|
|
|
{
|
|
|
|
if (current_menubar() == &menubar)
|
|
|
|
set_current_menubar(nullptr);
|
|
|
|
}
|