diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-24 22:23:59 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-24 23:07:31 +0100 |
commit | e623e73f63e44364f4f9c90e05137d373ee2cad6 (patch) | |
tree | d859df6610308a91082538959387ee4c8ca924a7 /Userland/Libraries/LibGUI/Toolbar.cpp | |
parent | a18631c5e765d7f3148e13a66341b3499c69bf4a (diff) | |
download | serenity-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/LibGUI/Toolbar.cpp')
-rw-r--r-- | Userland/Libraries/LibGUI/Toolbar.cpp | 17 |
1 files changed, 13 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) |