LibGUI: Don't crash when updating menu item that's not in the window server (#83)

Don't crash when updating menu item that's not in the window server. Menu Items begin with an invalid ID of -1, and only get a valid ID if the menu they are attached to is added to the window server. They don't send updates to the server unless they have a valid ID.

Fixes #82
This commit is contained in:
Christopher Dumas 2019-05-23 19:31:00 -07:00 committed by Andreas Kling
parent ebf645d72a
commit 9823354754
Notes: sideshowbarker 2024-07-19 13:58:34 +09:00
4 changed files with 7 additions and 2 deletions

View file

@ -32,6 +32,7 @@ GMenu::~GMenu()
void GMenu::add_action(Retained<GAction> action)
{
m_items.append(make<GMenuItem>(m_menu_id, move(action)));
dbgprintf("MenuItem Menu ID: %d\n", m_menu_id);
}
void GMenu::add_separator()
@ -70,6 +71,7 @@ int GMenu::realize_menu()
auto response = GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidCreateMenu);
m_menu_id = response.menu.menu_id;
dbgprintf("GMenu: Realizing menu! New menu ID: %d", m_menu_id);
ASSERT(m_menu_id > 0);
for (int i = 0; i < m_items.size(); ++i) {
auto& item = *m_items[i];
@ -79,6 +81,7 @@ int GMenu::realize_menu()
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::AddMenuSeparator;
request.menu.menu_id = m_menu_id;
dbgprintf("MenuItem [New] Menu ID: %d\n", m_menu_id);
GEventLoop::current().sync_request(request, WSAPI_ServerMessage::Type::DidAddMenuSeparator);
continue;
}

View file

@ -33,7 +33,7 @@ private:
int realize_menu();
void unrealize_menu();
int m_menu_id { 0 };
int m_menu_id { -1 };
String m_name;
Vector<OwnPtr<GMenuItem>> m_items;
};

View file

@ -46,6 +46,8 @@ void GMenuItem::set_checked(bool checked)
void GMenuItem::update_window_server()
{
if (m_menu_id < 0)
return;
auto& action = *m_action;
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::UpdateMenuItem;

View file

@ -36,7 +36,7 @@ private:
void update_window_server();
Type m_type { Invalid };
unsigned m_menu_id { 0 };
int m_menu_id { -1 };
unsigned m_identifier { 0 };
bool m_enabled { true };
bool m_checkable { false };