mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:07:45 +00:00
LibGUI: Add GUI::Menu::try_add_submenu()
This is a fallible variant of add_submenu() that returns ErrorOr.
This commit is contained in:
parent
1682cd10be
commit
a18631c5e7
2 changed files with 15 additions and 5 deletions
|
@ -66,18 +66,27 @@ void Menu::add_action(NonnullRefPtr<Action> action)
|
||||||
MUST(try_add_action(move(action)));
|
MUST(try_add_action(move(action)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Menu& Menu::add_submenu(const String& name)
|
ErrorOr<NonnullRefPtr<Menu>> Menu::try_add_submenu(String name)
|
||||||
{
|
{
|
||||||
auto submenu = Menu::construct(name);
|
// NOTE: We grow the vector first, to get allocation failure handled immediately.
|
||||||
|
TRY(m_items.try_ensure_capacity(m_items.size() + 1));
|
||||||
|
|
||||||
auto item = make<MenuItem>(m_menu_id, submenu);
|
auto submenu = TRY(Menu::try_create(name));
|
||||||
|
|
||||||
|
auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) MenuItem(m_menu_id, submenu)));
|
||||||
if (m_menu_id != -1)
|
if (m_menu_id != -1)
|
||||||
realize_menu_item(*item, m_items.size());
|
realize_menu_item(*item, m_items.size());
|
||||||
m_items.append(move(item));
|
|
||||||
|
|
||||||
|
m_items.unchecked_append(move(item));
|
||||||
return submenu;
|
return submenu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Menu& Menu::add_submenu(String name)
|
||||||
|
{
|
||||||
|
auto menu = MUST(try_add_submenu(move(name)));
|
||||||
|
return menu;
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<void> Menu::try_add_separator()
|
ErrorOr<void> Menu::try_add_separator()
|
||||||
{
|
{
|
||||||
// NOTE: We grow the vector first, to get allocation failure handled immediately.
|
// NOTE: We grow the vector first, to get allocation failure handled immediately.
|
||||||
|
|
|
@ -34,10 +34,11 @@ public:
|
||||||
|
|
||||||
ErrorOr<void> try_add_action(NonnullRefPtr<Action>);
|
ErrorOr<void> try_add_action(NonnullRefPtr<Action>);
|
||||||
ErrorOr<void> try_add_separator();
|
ErrorOr<void> try_add_separator();
|
||||||
|
ErrorOr<NonnullRefPtr<Menu>> try_add_submenu(String name);
|
||||||
|
|
||||||
void add_action(NonnullRefPtr<Action>);
|
void add_action(NonnullRefPtr<Action>);
|
||||||
void add_separator();
|
void add_separator();
|
||||||
Menu& add_submenu(const String& name);
|
Menu& add_submenu(String name);
|
||||||
|
|
||||||
void popup(const Gfx::IntPoint& screen_position, const RefPtr<Action>& default_action = nullptr);
|
void popup(const Gfx::IntPoint& screen_position, const RefPtr<Action>& default_action = nullptr);
|
||||||
void dismiss();
|
void dismiss();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue