diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-24 13:13:30 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-24 13:52:01 +0100 |
commit | 47b63390253ba39b01a841ad29ae434d8e5c436e (patch) | |
tree | e1227de469593ee308427ad6252d68dcc9864457 | |
parent | dc47fce2d925a4fd929331354e22f502435ef4f1 (diff) | |
download | serenity-47b63390253ba39b01a841ad29ae434d8e5c436e.zip |
LibGUI: Add GUI::TabWidget::try_add_tab<T>(...)
This is a fallible variant of add_tab<T>(...) that returns ErrorOr.
-rw-r--r-- | Userland/Libraries/LibGUI/TabWidget.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TabWidget.h | 10 |
2 files changed, 17 insertions, 1 deletions
diff --git a/Userland/Libraries/LibGUI/TabWidget.cpp b/Userland/Libraries/LibGUI/TabWidget.cpp index 474b805a9d..d2c4136183 100644 --- a/Userland/Libraries/LibGUI/TabWidget.cpp +++ b/Userland/Libraries/LibGUI/TabWidget.cpp @@ -44,13 +44,19 @@ TabWidget::~TabWidget() { } -void TabWidget::add_widget(StringView title, Widget& widget) +ErrorOr<void> TabWidget::try_add_widget(StringView title, Widget& widget) { m_tabs.append({ title, nullptr, &widget }); add_child(widget); update_focus_policy(); if (on_tab_count_change) on_tab_count_change(m_tabs.size()); + return {}; +} + +void TabWidget::add_widget(StringView title, Widget& widget) +{ + MUST(try_add_widget(title, widget)); } void TabWidget::remove_widget(Widget& widget) diff --git a/Userland/Libraries/LibGUI/TabWidget.h b/Userland/Libraries/LibGUI/TabWidget.h index bd232857e1..ac0d5402d0 100644 --- a/Userland/Libraries/LibGUI/TabWidget.h +++ b/Userland/Libraries/LibGUI/TabWidget.h @@ -36,10 +36,20 @@ public: GUI::Margins const& container_margins() const { return m_container_margins; } void set_container_margins(GUI::Margins const&); + ErrorOr<void> try_add_widget(StringView, Widget&); + void add_widget(StringView, Widget&); void remove_widget(Widget&); template<class T, class... Args> + ErrorOr<NonnullRefPtr<T>> try_add_tab(StringView title, Args&&... args) + { + auto t = TRY(T::try_create(forward<Args>(args)...)); + TRY(try_add_widget(title, *t)); + return *t; + } + + template<class T, class... Args> T& add_tab(StringView title, Args&&... args) { auto t = T::construct(forward<Args>(args)...); |