1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:57:44 +00:00

LibGUI: Add GUI::Toolbar::try_add_action()

This is a fallible variant of add_action() that returns ErrorOr.
It's careful to not fail with a partially added action.
This commit is contained in:
Andreas Kling 2021-11-24 13:18:52 +01:00
parent 47b6339025
commit 9b07e13fb6
2 changed files with 17 additions and 5 deletions

View file

@ -88,19 +88,29 @@ private:
}
};
GUI::Button& Toolbar::add_action(Action& action)
ErrorOr<NonnullRefPtr<GUI::Button>> Toolbar::try_add_action(Action& action)
{
auto item = make<Item>();
auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
item->type = Item::Type::Action;
item->action = action;
auto& button = add<ToolbarButton>(action);
button.set_fixed_size(m_button_size + 8, m_button_size + 8);
// NOTE: Grow the m_items capacity before potentially adding a child widget.
// This avoids having to untangle the child widget in case of allocation failure.
TRY(m_items.try_ensure_capacity(m_items.size() + 1));
m_items.append(move(item));
auto button = TRY(try_add<ToolbarButton>(action));
button->set_fixed_size(m_button_size + 8, m_button_size + 8);
m_items.unchecked_append(move(item));
return button;
}
GUI::Button& Toolbar::add_action(Action& action)
{
auto button = MUST(try_add_action(action));
return *button;
}
void Toolbar::add_separator()
{
auto item = make<Item>();