summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-11-24 22:23:59 +0100
committerAndreas Kling <kling@serenityos.org>2021-11-24 23:07:31 +0100
commite623e73f63e44364f4f9c90e05137d373ee2cad6 (patch)
treed859df6610308a91082538959387ee4c8ca924a7 /Userland/Libraries
parenta18631c5e765d7f3148e13a66341b3499c69bf4a (diff)
downloadserenity-e623e73f63e44364f4f9c90e05137d373ee2cad6.zip
LibGUI: Add GUI::Toolbar::try_add_separator()
This is a fallible variant of add_separator() that returns ErrorOr.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibGUI/Toolbar.cpp17
-rw-r--r--Userland/Libraries/LibGUI/Toolbar.h2
2 files changed, 15 insertions, 4 deletions
diff --git a/Userland/Libraries/LibGUI/Toolbar.cpp b/Userland/Libraries/LibGUI/Toolbar.cpp
index 29c02ed4bb..ec2aab9741 100644
--- a/Userland/Libraries/LibGUI/Toolbar.cpp
+++ b/Userland/Libraries/LibGUI/Toolbar.cpp
@@ -111,12 +111,21 @@ GUI::Button& Toolbar::add_action(Action& action)
return *button;
}
-void Toolbar::add_separator()
+ErrorOr<void> Toolbar::try_add_separator()
{
- auto item = make<Item>();
+ // NOTE: Grow the m_items capacity before potentially adding a child widget.
+ TRY(m_items.try_ensure_capacity(m_items.size() + 1));
+
+ auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
item->type = Item::Type::Separator;
- add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal);
- m_items.append(move(item));
+ TRY(try_add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal));
+ m_items.unchecked_append(move(item));
+ return {};
+}
+
+void Toolbar::add_separator()
+{
+ MUST(try_add_separator());
}
void Toolbar::paint_event(PaintEvent& event)
diff --git a/Userland/Libraries/LibGUI/Toolbar.h b/Userland/Libraries/LibGUI/Toolbar.h
index 91fba6b029..8070867d2b 100644
--- a/Userland/Libraries/LibGUI/Toolbar.h
+++ b/Userland/Libraries/LibGUI/Toolbar.h
@@ -18,6 +18,8 @@ public:
virtual ~Toolbar() override;
ErrorOr<NonnullRefPtr<GUI::Button>> try_add_action(GUI::Action&);
+ ErrorOr<void> try_add_separator();
+
GUI::Button& add_action(GUI::Action&);
void add_separator();