mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:17: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:
parent
47b6339025
commit
9b07e13fb6
2 changed files with 17 additions and 5 deletions
|
@ -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>();
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/NonnullOwnPtrVector.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
|
||||
namespace GUI {
|
||||
|
@ -16,6 +17,7 @@ class Toolbar : public Widget {
|
|||
public:
|
||||
virtual ~Toolbar() override;
|
||||
|
||||
ErrorOr<NonnullRefPtr<GUI::Button>> try_add_action(GUI::Action&);
|
||||
GUI::Button& add_action(GUI::Action&);
|
||||
void add_separator();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue